From 8eaa084170a12ca30399bb55a7ad22d0a90b9048 Mon Sep 17 00:00:00 2001 From: phegalante Date: Wed, 21 Oct 2020 20:08:50 -0300 Subject: [PATCH] improvement: change default whatsapp logic --- backend/src/controllers/WhatsAppController.ts | 25 ++++++++++- .../WhatsappService/CreateWhatsAppService.ts | 42 +++++++++++-------- .../WhatsappService/UpdateWhatsAppService.ts | 39 ++++++++--------- 3 files changed, 68 insertions(+), 38 deletions(-) diff --git a/backend/src/controllers/WhatsAppController.ts b/backend/src/controllers/WhatsAppController.ts index 51ec7b9..de4684c 100644 --- a/backend/src/controllers/WhatsAppController.ts +++ b/backend/src/controllers/WhatsAppController.ts @@ -25,7 +25,11 @@ export const index = async (req: Request, res: Response): Promise => { export const store = async (req: Request, res: Response): Promise => { const { name, status, isDefault }: WhatsappData = req.body; - const whatsapp = await CreateWhatsAppService({ name, status, isDefault }); + const { whatsapp, oldDefaultWhatsapp } = await CreateWhatsAppService({ + name, + status, + isDefault + }); initWbot(whatsapp) .then(() => { @@ -40,6 +44,13 @@ export const store = async (req: Request, res: Response): Promise => { whatsapp }); + if (oldDefaultWhatsapp) { + io.emit("whatsapp", { + action: "update", + whatsapp: oldDefaultWhatsapp + }); + } + return res.status(200).json(whatsapp); }; @@ -58,7 +69,10 @@ export const update = async ( const { whatsappId } = req.params; const whatsappData = req.body; - const whatsapp = await UpdateWhatsAppService({ whatsappData, whatsappId }); + const { whatsapp, oldDefaultWhatsapp } = await UpdateWhatsAppService({ + whatsappData, + whatsappId + }); const io = getIO(); io.emit("whatsapp", { @@ -66,6 +80,13 @@ export const update = async ( whatsapp }); + if (oldDefaultWhatsapp) { + io.emit("whatsapp", { + action: "update", + whatsapp: oldDefaultWhatsapp + }); + } + return res.status(200).json(whatsapp); }; diff --git a/backend/src/services/WhatsappService/CreateWhatsAppService.ts b/backend/src/services/WhatsappService/CreateWhatsAppService.ts index fbc3fa5..20f14d8 100644 --- a/backend/src/services/WhatsappService/CreateWhatsAppService.ts +++ b/backend/src/services/WhatsappService/CreateWhatsAppService.ts @@ -9,11 +9,16 @@ interface Request { isDefault?: boolean; } +interface Response { + whatsapp: Whatsapp; + oldDefaultWhatsapp: Whatsapp | null; +} + const CreateWhatsAppService = async ({ name, status = "INITIALIZING", isDefault = false -}: Request): Promise => { +}: Request): Promise => { const schema = Yup.object().shape({ name: Yup.string() .required() @@ -31,21 +36,7 @@ const CreateWhatsAppService = async ({ return true; } ), - isDefault: Yup.boolean() - .required() - .test( - "Check-default", - "Only one default whatsapp is permitted", - async value => { - if (value === true) { - const whatsappFound = await Whatsapp.findOne({ - where: { isDefault: true } - }); - return !whatsappFound; - } - return true; - } - ) + isDefault: Yup.boolean().required() }); try { @@ -54,13 +45,30 @@ const CreateWhatsAppService = async ({ throw new AppError(err.message); } + const whatsappFound = await Whatsapp.findOne(); + + if (!whatsappFound) { + isDefault = !whatsappFound; + } + + let oldDefaultWhatsapp: Whatsapp | null = null; + + if (isDefault) { + oldDefaultWhatsapp = await Whatsapp.findOne({ + where: { isDefault: true } + }); + if (oldDefaultWhatsapp) { + await oldDefaultWhatsapp.update({ isDefault: false }); + } + } + const whatsapp = await Whatsapp.create({ name, status, isDefault }); - return whatsapp; + return { whatsapp, oldDefaultWhatsapp }; }; export default CreateWhatsAppService; diff --git a/backend/src/services/WhatsappService/UpdateWhatsAppService.ts b/backend/src/services/WhatsappService/UpdateWhatsAppService.ts index aceb500..f98818d 100644 --- a/backend/src/services/WhatsappService/UpdateWhatsAppService.ts +++ b/backend/src/services/WhatsappService/UpdateWhatsAppService.ts @@ -1,4 +1,5 @@ import * as Yup from "yup"; +import { Op } from "sequelize"; import AppError from "../../errors/AppError"; import Whatsapp from "../../models/Whatsapp"; @@ -14,28 +15,18 @@ interface Request { whatsappId: string; } +interface Response { + whatsapp: Whatsapp; + oldDefaultWhatsapp: Whatsapp | null; +} + const UpdateWhatsAppService = async ({ whatsappData, whatsappId -}: Request): Promise => { +}: Request): Promise => { const schema = Yup.object().shape({ name: Yup.string().min(2), - default: Yup.boolean().test( - "Check-default", - "Only one default whatsapp is permited", - async value => { - if (value === true) { - const whatsappFound = await Whatsapp.findOne({ - where: { default: true } - }); - if (whatsappFound) { - return !(whatsappFound.id !== +whatsappId); - } - return true; - } - return true; - } - ) + isDefault: Yup.boolean() }); const { name, status, isDefault } = whatsappData; @@ -46,6 +37,17 @@ const UpdateWhatsAppService = async ({ throw new AppError(err.message); } + let oldDefaultWhatsapp: Whatsapp | null = null; + + if (isDefault) { + oldDefaultWhatsapp = await Whatsapp.findOne({ + where: { isDefault: true, id: { [Op.not]: whatsappId } } + }); + if (oldDefaultWhatsapp) { + await oldDefaultWhatsapp.update({ isDefault: false }); + } + } + const whatsapp = await Whatsapp.findOne({ where: { id: whatsappId } }); @@ -53,14 +55,13 @@ const UpdateWhatsAppService = async ({ if (!whatsapp) { throw new AppError("ERR_NO_WAPP_FOUND", 404); } - await whatsapp.update({ name, status, isDefault }); - return whatsapp; + return { whatsapp, oldDefaultWhatsapp }; }; export default UpdateWhatsAppService;