From a6a63a04085d782457db06360daf6908cbd082c0 Mon Sep 17 00:00:00 2001 From: Ricardo Paes Date: Wed, 23 Feb 2022 11:13:50 -0300 Subject: [PATCH] :sparkles: create functions to check if the user has a link with whatsapp --- backend/src/helpers/GetDefaultWhatsApp.ts | 12 +++++++++++- .../src/helpers/GetDefaultWhatsAppByUser.ts | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 backend/src/helpers/GetDefaultWhatsAppByUser.ts diff --git a/backend/src/helpers/GetDefaultWhatsApp.ts b/backend/src/helpers/GetDefaultWhatsApp.ts index afcb362..ef74cda 100644 --- a/backend/src/helpers/GetDefaultWhatsApp.ts +++ b/backend/src/helpers/GetDefaultWhatsApp.ts @@ -1,7 +1,17 @@ import AppError from "../errors/AppError"; import Whatsapp from "../models/Whatsapp"; +import GetDefaultWhatsAppByUser from "./GetDefaultWhatsAppByUser"; + +const GetDefaultWhatsApp = async ( + userId?: number +): Promise => { + if(userId) { + const whatsappByUser = await GetDefaultWhatsAppByUser(userId); + if(whatsappByUser !== null) { + return whatsappByUser; + } + } -const GetDefaultWhatsApp = async (): Promise => { const defaultWhatsapp = await Whatsapp.findOne({ where: { isDefault: true } }); diff --git a/backend/src/helpers/GetDefaultWhatsAppByUser.ts b/backend/src/helpers/GetDefaultWhatsAppByUser.ts new file mode 100644 index 0000000..f9d6aac --- /dev/null +++ b/backend/src/helpers/GetDefaultWhatsAppByUser.ts @@ -0,0 +1,18 @@ +import User from "../models/User"; +import Whatsapp from "../models/Whatsapp"; +import { logger } from "../utils/logger"; + +const GetDefaultWhatsAppByUser = async ( + userId: number +): Promise => { + const user = await User.findByPk(userId, {include: ["whatsapp"]}); + if( user === null ) { + return null; + } + + logger.info(`Found whatsapp linked to user '${user.name}' is '${user.whatsapp.name}'.`); + + return user.whatsapp; +}; + +export default GetDefaultWhatsAppByUser;