projeto-hit/backend/src/services/ContactServices/CreateContactService.ts

59 lines
1.1 KiB
TypeScript
Raw Normal View History

import AppError from "../../errors/AppError";
import Contact from "../../models/Contact";
import { createOrUpdateContactCache } from '../../helpers/ContactsCache'
import GetProfilePicUrl from "../WbotServices/GetProfilePicUrl";
interface ExtraInfo {
name: string;
value: string;
}
interface Request {
name: string;
number: string;
email?: string;
profilePicUrl?: string;
extraInfo?: ExtraInfo[];
}
const CreateContactService = async ({
name,
number,
email = "",
profilePicUrl='',
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,
profilePicUrl,
extraInfo
},
{
include: ["extraInfo"]
}
);
// TEST DEL
await createOrUpdateContactCache(`contact:${contact.id}`, {id: contact.id, name, number, profilePicUrl, isGroup:'false', extraInfo, email })
//
return contact;
};
export default CreateContactService;