mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-21 05:09:18 +00:00
changed whatsapp model
This commit is contained in:
@@ -18,6 +18,7 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
|
|||||||
interface WhatsappData {
|
interface WhatsappData {
|
||||||
name: string;
|
name: string;
|
||||||
status: string;
|
status: string;
|
||||||
|
isDefault?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const store = async (req: Request, res: Response): Promise<Response> => {
|
export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||||
@@ -25,9 +26,9 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
|
|||||||
|
|
||||||
console.log("aqui");
|
console.log("aqui");
|
||||||
|
|
||||||
const { name, status }: WhatsappData = req.body;
|
const { name, status, isDefault }: WhatsappData = req.body;
|
||||||
|
|
||||||
const whatsapp = await CreateWhatsAppService({ name, status });
|
const whatsapp = await CreateWhatsAppService({ name, status, isDefault });
|
||||||
|
|
||||||
// if (!whatsapp) {
|
// if (!whatsapp) {
|
||||||
// return res.status(400).json({ error: "Cannot create whatsapp session." });
|
// return res.status(400).json({ error: "Cannot create whatsapp session." });
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ class Whatsapp extends Model<Whatsapp> {
|
|||||||
@Default(false)
|
@Default(false)
|
||||||
@AllowNull
|
@AllowNull
|
||||||
@Column
|
@Column
|
||||||
default: boolean;
|
isDefault: boolean;
|
||||||
|
|
||||||
@CreatedAt
|
@CreatedAt
|
||||||
createdAt: Date;
|
createdAt: Date;
|
||||||
|
|||||||
@@ -6,39 +6,43 @@ import Whatsapp from "../../models/Whatsapp";
|
|||||||
interface Request {
|
interface Request {
|
||||||
name: string;
|
name: string;
|
||||||
status?: string;
|
status?: string;
|
||||||
|
isDefault?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const CreateWhatsAppService = async ({
|
const CreateWhatsAppService = async ({
|
||||||
name,
|
name,
|
||||||
status = "INITIALIZING"
|
status = "INITIALIZING",
|
||||||
|
isDefault = false
|
||||||
}: Request): Promise<Whatsapp> => {
|
}: Request): Promise<Whatsapp> => {
|
||||||
// const schema = Yup.object().shape({
|
const schema = Yup.object().shape({
|
||||||
// name: Yup.string().required().min(2),
|
name: Yup.string().required().min(2),
|
||||||
// default: Yup.boolean()
|
isDefault: Yup.boolean()
|
||||||
// .required()
|
.required()
|
||||||
// .test(
|
.test(
|
||||||
// "Check-default",
|
"Check-default",
|
||||||
// "Only one default whatsapp is permited",
|
"Only one default whatsapp is permited",
|
||||||
// async value => {
|
async value => {
|
||||||
// if (value === true) {
|
if (value === true) {
|
||||||
// const whatsappFound = await Whatsapp.findOne({
|
const whatsappFound = await Whatsapp.findOne({
|
||||||
// where: { default: true }
|
where: { isDefault: true }
|
||||||
// });
|
});
|
||||||
// return !Boolean(whatsappFound);
|
return !whatsappFound;
|
||||||
// } else return true;
|
}
|
||||||
// }
|
return true;
|
||||||
// )
|
}
|
||||||
// });
|
)
|
||||||
|
});
|
||||||
|
|
||||||
// try {
|
try {
|
||||||
// await schema.validate({ name, status });
|
await schema.validate({ name, status, isDefault });
|
||||||
// } catch (err) {
|
} catch (err) {
|
||||||
// throw new AppError(err.message);
|
throw new AppError(err.message);
|
||||||
// }
|
}
|
||||||
|
|
||||||
const whatsapp = await Whatsapp.create({
|
const whatsapp = await Whatsapp.create({
|
||||||
name,
|
name,
|
||||||
status
|
status,
|
||||||
|
isDefault
|
||||||
});
|
});
|
||||||
|
|
||||||
return whatsapp;
|
return whatsapp;
|
||||||
|
|||||||
Reference in New Issue
Block a user