changed whatsapp controllers to typescript

This commit is contained in:
canove
2020-09-19 10:18:31 -03:00
parent 872e237b48
commit 5dda3aabaf
5 changed files with 150 additions and 82 deletions

View File

@@ -0,0 +1,16 @@
import Whatsapp from "../../models/Whatsapp";
import AppError from "../../errors/AppError";
const DeleteWhatsApprService = async (id: string): Promise<void> => {
const whatsapp = await Whatsapp.findOne({
where: { id }
});
if (!whatsapp) {
throw new AppError("No whatsapp found with this ID.", 404);
}
await whatsapp.destroy();
};
export default DeleteWhatsApprService;

View File

@@ -0,0 +1,18 @@
import Whatsapp from "../../models/Whatsapp";
import AppError from "../../errors/AppError";
const ShowWhatsAppService = async (
id: string
): Promise<Whatsapp | undefined> => {
const whatsapp = await Whatsapp.findOne({
where: { id }
});
if (!whatsapp) {
throw new AppError("No whatsapp found with this ID.", 404);
}
return whatsapp;
};
export default ShowWhatsAppService;

View File

@@ -0,0 +1,66 @@
import * as Yup from "yup";
import AppError from "../../errors/AppError";
import Whatsapp from "../../models/Whatsapp";
interface WhatsappData {
name?: string;
status?: string;
isDefault?: boolean;
}
interface Request {
whatsappData: WhatsappData;
whatsappId: string;
}
const UpdateWhatsAppService = async ({
whatsappData,
whatsappId
}: Request): Promise<Whatsapp> => {
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;
}
)
});
const { name, status, isDefault } = whatsappData;
try {
await schema.validate({ name, status, isDefault });
} catch (err) {
throw new AppError(err.message);
}
const whatsapp = await Whatsapp.findOne({
where: { id: whatsappId }
});
if (!whatsapp) {
throw new AppError("No whatsapp found with this ID.", 404);
}
await whatsapp.update({
name,
status,
isDefault
});
return whatsapp;
};
export default UpdateWhatsAppService;