mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 19:59:20 +00:00
48 lines
796 B
TypeScript
48 lines
796 B
TypeScript
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 = "",
|
|
extraInfo = []
|
|
}: Request): Promise<Contact> => {
|
|
const numberExists = await Contact.findOne({
|
|
where: { number }
|
|
});
|
|
|
|
if (numberExists) {
|
|
throw new AppError("A contact with this number already exists.");
|
|
}
|
|
|
|
console.log("extra", extraInfo);
|
|
|
|
const contact = await Contact.create(
|
|
{
|
|
name,
|
|
number,
|
|
email,
|
|
extraInfo
|
|
},
|
|
{
|
|
include: ["extraInfo"]
|
|
}
|
|
);
|
|
|
|
return contact;
|
|
};
|
|
|
|
export default CreateContactService;
|