create functions to check if the user has a link with whatsapp

This commit is contained in:
Ricardo Paes
2022-02-23 11:13:50 -03:00
parent 67ad73a8bd
commit a6a63a0408
2 changed files with 29 additions and 1 deletions

View File

@@ -1,7 +1,17 @@
import AppError from "../errors/AppError";
import Whatsapp from "../models/Whatsapp";
import GetDefaultWhatsAppByUser from "./GetDefaultWhatsAppByUser";
const GetDefaultWhatsApp = async (
userId?: number
): Promise<Whatsapp> => {
if(userId) {
const whatsappByUser = await GetDefaultWhatsAppByUser(userId);
if(whatsappByUser !== null) {
return whatsappByUser;
}
}
const GetDefaultWhatsApp = async (): Promise<Whatsapp> => {
const defaultWhatsapp = await Whatsapp.findOne({
where: { isDefault: true }
});

View File

@@ -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<Whatsapp | null> => {
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;