mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 20:29:17 +00:00
changed contacts routes to typescript
This commit is contained in:
32
backend/src/services/ContactServices/CreateContactService.ts
Normal file
32
backend/src/services/ContactServices/CreateContactService.ts
Normal 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;
|
||||
Reference in New Issue
Block a user