47 lines
766 B
TypeScript
47 lines
766 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;
|
||
|
profilePicUrl?: 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("ERR_DUPLICATED_CONTACT");
|
||
|
}
|
||
|
|
||
|
const contact = await Contact.create(
|
||
|
{
|
||
|
name,
|
||
|
number,
|
||
|
email,
|
||
|
extraInfo
|
||
|
},
|
||
|
{
|
||
|
include: ["extraInfo"]
|
||
|
}
|
||
|
);
|
||
|
|
||
|
return contact;
|
||
|
};
|
||
|
|
||
|
export default CreateContactService;
|