Skip to content

Commit

Permalink
Implementando gerador de CodeSnippet
Browse files Browse the repository at this point in the history
  • Loading branch information
rtenorioh committed Sep 24, 2024
1 parent d24f5b6 commit 8be45a3
Show file tree
Hide file tree
Showing 8 changed files with 368 additions and 16 deletions.
12 changes: 9 additions & 3 deletions backend/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@
"ecmaVersion": 12,
"sourceType": "module"
},
"plugins": ["@typescript-eslint", "prettier"],
"plugins": [
"@typescript-eslint",
"prettier"
],
"rules": {
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{ "argsIgnorePattern": "_" }
{
"argsIgnorePattern": "_"
}
],
"prefer-const": "off",
"import/prefer-default-export": "off",
"no-console": "off",
"no-param-reassign": "off",
Expand All @@ -46,4 +52,4 @@
"typescript": {}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { QueryInterface } from "sequelize";

module.exports = {
up: async (queryInterface: QueryInterface) => {
await queryInterface.removeConstraint("tickets", "tickets_ibfk_2");

await queryInterface.addConstraint("tickets", ["userId"], {
type: "foreign key",
name: "tickets_ibfk_2",
references: {
table: "users",
field: "id"
},
onDelete: "CASCADE",
onUpdate: "CASCADE"
});
},

down: async (queryInterface: QueryInterface) => {
await queryInterface.removeConstraint("tickets", "tickets_ibfk_2");

await queryInterface.addConstraint("tickets", ["userId"], {
type: "foreign key",
name: "tickets_ibfk_2",
references: {
table: "users",
field: "id"
},
onDelete: "SET NULL",
onUpdate: "CASCADE"
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { QueryInterface } from "sequelize";

export default {
up: async (queryInterface: QueryInterface) => {
// Remover a constraint antiga
await queryInterface.removeConstraint(
"tickets",
"Tickets_queueId_foreign_idx"
);

// Adicionar a nova constraint com SET NULL e CASCADE
await queryInterface.addConstraint("tickets", ["queueId"], {
type: "foreign key",
name: "Tickets_queueId_foreign_idx",
references: {
table: "queues",
field: "id"
},
onDelete: "SET NULL", // Ao deletar, coloca NULL no campo queueId
onUpdate: "CASCADE" // Ao atualizar o id na tabela queues, atualiza o queueId em tickets
});
},

down: async (queryInterface: QueryInterface) => {
// Remover a constraint recém adicionada
await queryInterface.removeConstraint(
"tickets",
"Tickets_queueId_foreign_idx"
);

// Restaurar a constraint antiga
await queryInterface.addConstraint("tickets", ["queueId"], {
type: "foreign key",
name: "Tickets_queueId_foreign_idx",
references: {
table: "queues",
field: "id"
},
onDelete: "RESTRICT",
onUpdate: "CASCADE"
});
}
};
24 changes: 15 additions & 9 deletions backend/src/models/Ticket.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {
Table,
AutoIncrement,
BelongsTo,
Column,
CreatedAt,
UpdatedAt,
Model,
PrimaryKey,
Default,
ForeignKey,
BelongsTo,
HasMany,
AutoIncrement,
Default
Model,
PrimaryKey,
Table,
UpdatedAt
} from "sequelize-typescript";

import Contact from "./Contact";
Expand Down Expand Up @@ -48,7 +48,10 @@ class Ticket extends Model<Ticket> {
@Column
userId: number;

@BelongsTo(() => User)
@BelongsTo(() => User, {
onDelete: "CASCADE",
onUpdate: "CASCADE"
})
user: User;

@ForeignKey(() => Contact)
Expand All @@ -69,7 +72,10 @@ class Ticket extends Model<Ticket> {
@Column
queueId: number;

@BelongsTo(() => Queue)
@BelongsTo(() => Queue, {
onDelete: "SET NULL",
onUpdate: "CASCADE"
})
queue: Queue;

@HasMany(() => Message)
Expand Down
4 changes: 2 additions & 2 deletions backend/src/services/WbotServices/wbotMessageListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,8 @@ const handleMessage = async (
try {
let msgContact: WbotContact;
let groupContact: Contact | undefined;
const userId = 0;
const queueId = 0;
let userId;
let queueId;

if (msg.fromMe) {
// messages sent automatically by wbot have a special character in front of it
Expand Down
118 changes: 118 additions & 0 deletions frontend/src/components/CodeSnippetGenerator/codeSnippets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
const codeSnippets = {
HTTP: (number, body, userId, queueId, whatsappId, token) => `POST ${process.env.REACT_APP_BACKEND_URL}/api/messages/send HTTP/1.1
User-Agent: vscode-restclient
Authorization: Bearer ${token}
Content-Type: application/json
Host: localhost:4000
Content-Length: ${85 + body.length}
{
"number": "${number}",
"body": "${body}",
"userId": "${userId}",
"queueId": "${queueId}",
"whatsappId": "${whatsappId}"
}`,
JavaScript_JQuery: (number, body, userId, queueId, whatsappId, token) => `const settings = {
"async": true,
"crossDomain": true,
"url": "${process.env.REACT_APP_BACKEND_URL}/api/messages/send}",
"method": "POST",
"headers": {
"user-agent": "vscode-restclient",
"authorization": "Bearer ${token}",
"content-type": "application/json"
},
"processData": false,
"data": "{\"number\": \"${number}\",\"body\": \"${body}\",\"userId\": \"${userId}\",\"queueId\": \"${queueId}\",\"whatsappId\": \"${whatsappId}\"}"
};
$.ajax(settings).done(function (response) {
console.log(response);
});
`,
JavaScript_fetch: (number, body, userId, queueId, whatsappId, token) => `
fetch("${process.env.REACT_APP_BACKEND_URL}/api/messages/send", {
"method": "POST",
"headers": {
"user-agent": "vscode-restclient",
"Content-Type": "application/json",
"Authorization": "Bearer ${token}",
},
"body": {
number: "${number}",
body: '${body}',
userId: '${userId}',
queueId: '${queueId}',
whatsAppId: '${whatsappId}'
},
})
.then(response => {
console.log(response);
})
.catch(err => {
console.error(err);
});
`,
NODEjs_Request: (number, body, userId, queueId, whatsappId, token) => `
const request = require('request');
const options = {
method: 'POST',
url: '${process.env.REACT_APP_BACKEND_URL}/api/messages/send',
headers: {
'user-agent': 'vscode-restclient',
'Authorization': 'Bearer ${token}',
'Content-Type': 'application/json',
},
body: {
number: '${number}',
body: '${body}',
userId: '${userId}',
queueId: '${queueId}',
whatsAppId: '${whatsappId}',
},
json: true
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log('Response:', body);
});`,
PHP_cURL: (number, body, userId, queueId, whatsappId, token) => `
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_PORT => "4000",
CURLOPT_URL => "${process.env.REACT_APP_BACKEND_URL}/api/messages/send",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"number\": \"${number}\",\"body\": \"${body}\",\"userId\": \"${userId}\",\"queueId\": \"${queueId}\",\"whatsappId\": \"${whatsappId}\"}",
CURLOPT_HTTPHEADER => [
'Authorization': 'Bearer ${token}',
"content-type: application/json",
"user-agent: vscode-restclient"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
`,
};

export default codeSnippets;
Loading

0 comments on commit 8be45a3

Please sign in to comment.