mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-20 20:59:16 +00:00
✨ create functions to check if the user has a link with whatsapp
This commit is contained in:
@@ -1,7 +1,17 @@
|
|||||||
import AppError from "../errors/AppError";
|
import AppError from "../errors/AppError";
|
||||||
import Whatsapp from "../models/Whatsapp";
|
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({
|
const defaultWhatsapp = await Whatsapp.findOne({
|
||||||
where: { isDefault: true }
|
where: { isDefault: true }
|
||||||
});
|
});
|
||||||
|
|||||||
18
backend/src/helpers/GetDefaultWhatsAppByUser.ts
Normal file
18
backend/src/helpers/GetDefaultWhatsAppByUser.ts
Normal 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;
|
||||||
Reference in New Issue
Block a user