changed contacts routes to typescript

This commit is contained in:
canove
2020-09-15 19:28:06 -03:00
parent 14d90a2dd4
commit b04c0b878e
21 changed files with 536 additions and 133 deletions

View File

@@ -0,0 +1,32 @@
import AppError from "../../errors/AppError";
import Contact from "../../models/Contact";
interface Request {
name: string;
number: string;
email?: string;
}
const CreateContactService = async ({
name,
number,
email
}: Request): Promise<Contact> => {
const numberExists = await Contact.findOne({
where: { number }
});
if (numberExists) {
throw new AppError("A contact with this number already exists.");
}
const contact = await Contact.create({
name,
number,
email
});
return contact;
};
export default CreateContactService;