fix: check whatsapp default when editing

This commit is contained in:
canove
2020-09-06 12:51:16 -03:00
parent 60e9181ece
commit 371477aa98

View File

@@ -22,10 +22,10 @@ exports.store = async (req, res) => {
async value => { async value => {
// console.log("cai no if", value); // console.log("cai no if", value);
if (value === true) { if (value === true) {
const defaultFound = await Whatsapp.findOne({ const whatsappFound = await Whatsapp.findOne({
where: { default: true }, where: { default: true },
}); });
return !Boolean(defaultFound); return !Boolean(whatsappFound);
} else return true; } else return true;
} }
), ),
@@ -72,6 +72,8 @@ exports.show = async (req, res) => {
}; };
exports.update = async (req, res) => { exports.update = async (req, res) => {
const { whatsappId } = req.params;
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() default: Yup.boolean()
@@ -80,12 +82,13 @@ exports.update = async (req, res) => {
"Check-default", "Check-default",
"Only one default whatsapp is permited", "Only one default whatsapp is permited",
async value => { async value => {
// console.log("cai no if", value);
if (value === true) { if (value === true) {
const defaultFound = await Whatsapp.findOne({ const whatsappFound = await Whatsapp.findOne({
where: { default: true }, where: { default: true },
}); });
return !Boolean(defaultFound); if (whatsappFound) {
return !(whatsappFound.id !== +whatsappId);
}
} else return true; } else return true;
} }
), ),
@@ -98,7 +101,6 @@ exports.update = async (req, res) => {
} }
const io = getIO(); const io = getIO();
const { whatsappId } = req.params;
const whatsapp = await Whatsapp.findByPk(whatsappId); const whatsapp = await Whatsapp.findByPk(whatsappId);