mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-20 12:49:32 +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> => {
|
export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||||
const { name, status, isDefault }: WhatsappData = req.body;
|
const { name, status, isDefault }: WhatsappData = req.body;
|
||||||
|
|
||||||
const whatsapp = await CreateWhatsAppService({ name, status, isDefault });
|
const { whatsapp, oldDefaultWhatsapp } = await CreateWhatsAppService({
|
||||||
|
name,
|
||||||
|
status,
|
||||||
|
isDefault
|
||||||
|
});
|
||||||
|
|
||||||
initWbot(whatsapp)
|
initWbot(whatsapp)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
@@ -40,6 +44,13 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
|
|||||||
whatsapp
|
whatsapp
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (oldDefaultWhatsapp) {
|
||||||
|
io.emit("whatsapp", {
|
||||||
|
action: "update",
|
||||||
|
whatsapp: oldDefaultWhatsapp
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return res.status(200).json(whatsapp);
|
return res.status(200).json(whatsapp);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -58,7 +69,10 @@ export const update = async (
|
|||||||
const { whatsappId } = req.params;
|
const { whatsappId } = req.params;
|
||||||
const whatsappData = req.body;
|
const whatsappData = req.body;
|
||||||
|
|
||||||
const whatsapp = await UpdateWhatsAppService({ whatsappData, whatsappId });
|
const { whatsapp, oldDefaultWhatsapp } = await UpdateWhatsAppService({
|
||||||
|
whatsappData,
|
||||||
|
whatsappId
|
||||||
|
});
|
||||||
|
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
io.emit("whatsapp", {
|
io.emit("whatsapp", {
|
||||||
@@ -66,6 +80,13 @@ export const update = async (
|
|||||||
whatsapp
|
whatsapp
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (oldDefaultWhatsapp) {
|
||||||
|
io.emit("whatsapp", {
|
||||||
|
action: "update",
|
||||||
|
whatsapp: oldDefaultWhatsapp
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return res.status(200).json(whatsapp);
|
return res.status(200).json(whatsapp);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -9,11 +9,16 @@ interface Request {
|
|||||||
isDefault?: boolean;
|
isDefault?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Response {
|
||||||
|
whatsapp: Whatsapp;
|
||||||
|
oldDefaultWhatsapp: Whatsapp | null;
|
||||||
|
}
|
||||||
|
|
||||||
const CreateWhatsAppService = async ({
|
const CreateWhatsAppService = async ({
|
||||||
name,
|
name,
|
||||||
status = "INITIALIZING",
|
status = "INITIALIZING",
|
||||||
isDefault = false
|
isDefault = false
|
||||||
}: Request): Promise<Whatsapp> => {
|
}: Request): Promise<Response> => {
|
||||||
const schema = Yup.object().shape({
|
const schema = Yup.object().shape({
|
||||||
name: Yup.string()
|
name: Yup.string()
|
||||||
.required()
|
.required()
|
||||||
@@ -31,21 +36,7 @@ const CreateWhatsAppService = async ({
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
isDefault: Yup.boolean()
|
isDefault: Yup.boolean().required()
|
||||||
.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;
|
|
||||||
}
|
|
||||||
)
|
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -54,13 +45,30 @@ const CreateWhatsAppService = async ({
|
|||||||
throw new AppError(err.message);
|
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({
|
const whatsapp = await Whatsapp.create({
|
||||||
name,
|
name,
|
||||||
status,
|
status,
|
||||||
isDefault
|
isDefault
|
||||||
});
|
});
|
||||||
|
|
||||||
return whatsapp;
|
return { whatsapp, oldDefaultWhatsapp };
|
||||||
};
|
};
|
||||||
|
|
||||||
export default CreateWhatsAppService;
|
export default CreateWhatsAppService;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import * as Yup from "yup";
|
import * as Yup from "yup";
|
||||||
|
import { Op } from "sequelize";
|
||||||
|
|
||||||
import AppError from "../../errors/AppError";
|
import AppError from "../../errors/AppError";
|
||||||
import Whatsapp from "../../models/Whatsapp";
|
import Whatsapp from "../../models/Whatsapp";
|
||||||
@@ -14,28 +15,18 @@ interface Request {
|
|||||||
whatsappId: string;
|
whatsappId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Response {
|
||||||
|
whatsapp: Whatsapp;
|
||||||
|
oldDefaultWhatsapp: Whatsapp | null;
|
||||||
|
}
|
||||||
|
|
||||||
const UpdateWhatsAppService = async ({
|
const UpdateWhatsAppService = async ({
|
||||||
whatsappData,
|
whatsappData,
|
||||||
whatsappId
|
whatsappId
|
||||||
}: Request): Promise<Whatsapp> => {
|
}: Request): Promise<Response> => {
|
||||||
const schema = Yup.object().shape({
|
const schema = Yup.object().shape({
|
||||||
name: Yup.string().min(2),
|
name: Yup.string().min(2),
|
||||||
default: Yup.boolean().test(
|
isDefault: Yup.boolean()
|
||||||
"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;
|
|
||||||
}
|
|
||||||
)
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const { name, status, isDefault } = whatsappData;
|
const { name, status, isDefault } = whatsappData;
|
||||||
@@ -46,6 +37,17 @@ const UpdateWhatsAppService = async ({
|
|||||||
throw new AppError(err.message);
|
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({
|
const whatsapp = await Whatsapp.findOne({
|
||||||
where: { id: whatsappId }
|
where: { id: whatsappId }
|
||||||
});
|
});
|
||||||
@@ -53,14 +55,13 @@ const UpdateWhatsAppService = async ({
|
|||||||
if (!whatsapp) {
|
if (!whatsapp) {
|
||||||
throw new AppError("ERR_NO_WAPP_FOUND", 404);
|
throw new AppError("ERR_NO_WAPP_FOUND", 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
await whatsapp.update({
|
await whatsapp.update({
|
||||||
name,
|
name,
|
||||||
status,
|
status,
|
||||||
isDefault
|
isDefault
|
||||||
});
|
});
|
||||||
|
|
||||||
return whatsapp;
|
return { whatsapp, oldDefaultWhatsapp };
|
||||||
};
|
};
|
||||||
|
|
||||||
export default UpdateWhatsAppService;
|
export default UpdateWhatsAppService;
|
||||||
|
|||||||
Reference in New Issue
Block a user