mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-21 05:09:18 +00:00
changed contacts routes to typescript
This commit is contained in:
39
backend/src/services/ContactServices/UpdateContactService.ts
Normal file
39
backend/src/services/ContactServices/UpdateContactService.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import AppError from "../../errors/AppError";
|
||||
import Contact from "../../models/Contact";
|
||||
|
||||
interface ContactData {
|
||||
email?: string;
|
||||
number?: string;
|
||||
name?: string;
|
||||
}
|
||||
|
||||
interface Request {
|
||||
contactData: ContactData;
|
||||
contactId: string;
|
||||
}
|
||||
|
||||
const UpdateContactService = async ({
|
||||
contactData,
|
||||
contactId
|
||||
}: Request): Promise<Contact> => {
|
||||
const { email, name, number } = contactData;
|
||||
|
||||
const contact = await Contact.findOne({
|
||||
where: { id: contactId },
|
||||
attributes: ["id", "name", "number", "email"]
|
||||
});
|
||||
|
||||
if (!contact) {
|
||||
throw new AppError("No contact found with this ID.", 404);
|
||||
}
|
||||
|
||||
await contact.update({
|
||||
name,
|
||||
number,
|
||||
email
|
||||
});
|
||||
|
||||
return contact;
|
||||
};
|
||||
|
||||
export default UpdateContactService;
|
||||
Reference in New Issue
Block a user