mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-20 04:39:20 +00:00
finished contact migration to typescript
This commit is contained in:
@@ -1,16 +1,23 @@
|
||||
import AppError from "../../errors/AppError";
|
||||
import Contact from "../../models/Contact";
|
||||
|
||||
interface ExtraInfo {
|
||||
name: string;
|
||||
value: string;
|
||||
}
|
||||
|
||||
interface Request {
|
||||
name: string;
|
||||
number: string;
|
||||
email?: string;
|
||||
extraInfo?: ExtraInfo[];
|
||||
}
|
||||
|
||||
const CreateContactService = async ({
|
||||
name,
|
||||
number,
|
||||
email
|
||||
email,
|
||||
extraInfo
|
||||
}: Request): Promise<Contact> => {
|
||||
const numberExists = await Contact.findOne({
|
||||
where: { number }
|
||||
@@ -20,11 +27,17 @@ const CreateContactService = async ({
|
||||
throw new AppError("A contact with this number already exists.");
|
||||
}
|
||||
|
||||
const contact = await Contact.create({
|
||||
name,
|
||||
number,
|
||||
email
|
||||
});
|
||||
const contact = await Contact.create(
|
||||
{
|
||||
name,
|
||||
number,
|
||||
email,
|
||||
extraInfo
|
||||
},
|
||||
{
|
||||
include: ["extraInfo"]
|
||||
}
|
||||
);
|
||||
|
||||
return contact;
|
||||
};
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
import AppError from "../../errors/AppError";
|
||||
import Contact from "../../models/Contact";
|
||||
import ContactCustomField from "../../models/ContactCustomField";
|
||||
|
||||
interface ExtraInfo {
|
||||
id?: number;
|
||||
name: string;
|
||||
value: string;
|
||||
}
|
||||
interface ContactData {
|
||||
email?: string;
|
||||
number?: string;
|
||||
name?: string;
|
||||
extraInfo?: ExtraInfo[];
|
||||
}
|
||||
|
||||
interface Request {
|
||||
@@ -16,21 +23,41 @@ const UpdateContactService = async ({
|
||||
contactData,
|
||||
contactId
|
||||
}: Request): Promise<Contact> => {
|
||||
const { email, name, number } = contactData;
|
||||
const { email, name, number, extraInfo } = contactData;
|
||||
|
||||
const contact = await Contact.findOne({
|
||||
where: { id: contactId },
|
||||
attributes: ["id", "name", "number", "email"]
|
||||
attributes: ["id", "name", "number", "email"],
|
||||
include: ["extraInfo"]
|
||||
});
|
||||
|
||||
if (!contact) {
|
||||
throw new AppError("No contact found with this ID.", 404);
|
||||
}
|
||||
|
||||
if (extraInfo) {
|
||||
await Promise.all(
|
||||
extraInfo.map(async info => {
|
||||
await ContactCustomField.upsert({ ...info, contactId: contact.id });
|
||||
})
|
||||
);
|
||||
|
||||
await Promise.all(
|
||||
contact.extraInfo.map(async oldInfo => {
|
||||
const stillExists = extraInfo.findIndex(info => info.id === oldInfo.id);
|
||||
|
||||
if (stillExists === -1) {
|
||||
await ContactCustomField.destroy({ where: { id: oldInfo.id } });
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
await contact.update({
|
||||
name,
|
||||
number,
|
||||
email
|
||||
email,
|
||||
extraInfo
|
||||
});
|
||||
|
||||
return contact;
|
||||
|
||||
Reference in New Issue
Block a user