mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-17 19:37:02 +00:00
improvement: change default whatsapp logic
This commit is contained in:
@@ -25,7 +25,11 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
|
||||
export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||
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<Response> => {
|
||||
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);
|
||||
};
|
||||
|
||||
|
||||
@@ -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<Whatsapp> => {
|
||||
}: Request): Promise<Response> => {
|
||||
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;
|
||||
|
||||
@@ -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<Whatsapp> => {
|
||||
}: Request): Promise<Response> => {
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user