From 6d9bef094fa63d84ef2df017f5e988d3f87b3740 Mon Sep 17 00:00:00 2001 From: adriano Date: Tue, 4 Apr 2023 09:30:28 -0300 Subject: [PATCH] =?UTF-8?q?Commit=20de=20corre=C3=A7=C3=A3o=20de=20valida?= =?UTF-8?q?=C3=A7=C3=A3o.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/controllers/ContactController.ts | 6 ++++ .../src/controllers/QuickAnswerController.ts | 4 +-- .../ContactServices/CreateContactService.ts | 3 +- .../ContactServices/UpdateContactService.ts | 30 +++++++++++++++---- .../UserServices/UpdateUserService.ts | 2 +- 5 files changed, 35 insertions(+), 10 deletions(-) diff --git a/backend/src/controllers/ContactController.ts b/backend/src/controllers/ContactController.ts index 29e4469..1c05596 100644 --- a/backend/src/controllers/ContactController.ts +++ b/backend/src/controllers/ContactController.ts @@ -108,6 +108,12 @@ export const store = async (req: Request, res: Response): Promise => { console.log('xxxxxxxxxxx profilePicUrl: ',profilePicUrl) + + // console.log(`newContact.name: ${newContact.name}\n + // newContact.number: ${newContact.number}\n + // newContact.email: ${newContact.email}\n + // newContact.extraInfo: ${newContact.extraInfo}`) + let name = newContact.name let number = validNumber let email = newContact.email diff --git a/backend/src/controllers/QuickAnswerController.ts b/backend/src/controllers/QuickAnswerController.ts index 3828e91..60e2702 100644 --- a/backend/src/controllers/QuickAnswerController.ts +++ b/backend/src/controllers/QuickAnswerController.ts @@ -41,7 +41,7 @@ export const store = async (req: Request, res: Response): Promise => { try { await QuickAnswerSchema.validate(newQuickAnswer); - } catch (err) { + } catch (err:any) { throw new AppError(err.message); } @@ -79,7 +79,7 @@ export const update = async ( try { await schema.validate(quickAnswerData); - } catch (err) { + } catch (err: any) { throw new AppError(err.message); } diff --git a/backend/src/services/ContactServices/CreateContactService.ts b/backend/src/services/ContactServices/CreateContactService.ts index 87d9e7c..c09bc33 100644 --- a/backend/src/services/ContactServices/CreateContactService.ts +++ b/backend/src/services/ContactServices/CreateContactService.ts @@ -23,7 +23,8 @@ const CreateContactService = async ({ email = "", profilePicUrl='', extraInfo = [] -}: Request): Promise => { +}: Request): Promise => { + const numberExists = await Contact.findOne({ where: { number } }); diff --git a/backend/src/services/ContactServices/UpdateContactService.ts b/backend/src/services/ContactServices/UpdateContactService.ts index 4548577..a4963e5 100644 --- a/backend/src/services/ContactServices/UpdateContactService.ts +++ b/backend/src/services/ContactServices/UpdateContactService.ts @@ -21,7 +21,7 @@ interface Request { contactData: ContactData; contactId: string; } - + const UpdateContactService = async ({ contactData, @@ -29,6 +29,8 @@ const UpdateContactService = async ({ }: Request): Promise => { const { email, name, number, extraInfo } = contactData; + // console.log('email, name, number, extraInfo: ', email, name, number, extraInfo) + const contact = await Contact.findOne({ where: { id: contactId }, attributes: ["id", "name", "number", "email", "profilePicUrl"], @@ -55,18 +57,34 @@ const UpdateContactService = async ({ } }) ); - } + } - const oldNumber = contact.number + const oldNumber = contact.number + + + //Solução para o erro tcp_wrap.cc + // console.log('----------> oldNumber: ', oldNumber) + + if (number) { + const numberExists = await Contact.findOne({ + where: { number } + }); + + if (numberExists) { + throw new AppError("ERR_DUPLICATED_CONTACT"); + } + } + + // await contact.update({ name, number, email - }); + }); //TEST DEL - await updateTicketsByContactsCache(oldNumber, contact.name, contact.number) + await updateTicketsByContactsCache(oldNumber, contact.name, contact.number) // @@ -75,7 +93,7 @@ const UpdateContactService = async ({ include: ["extraInfo"] }); - + // console.log('contactcontactcontactcontact: ',flatten(JSON.parse(JSON.stringify(contact)))) await updateContactCacheById(contact.id, JSON.parse(JSON.stringify(contact))) diff --git a/backend/src/services/UserServices/UpdateUserService.ts b/backend/src/services/UserServices/UpdateUserService.ts index 6d0638c..5292e23 100644 --- a/backend/src/services/UserServices/UpdateUserService.ts +++ b/backend/src/services/UserServices/UpdateUserService.ts @@ -44,7 +44,7 @@ const UpdateUserService = async ({ try { await schema.validate({ email, password, profile, name }); - } catch (err) { + } catch (err: any) { throw new AppError(err.message); }