From 3d0e2287408a0baf7e0b3b06e71efaa9fbfa8802 Mon Sep 17 00:00:00 2001 From: trevor-anderson <43518091+trevor-anderson@users.noreply.github.com> Date: Mon, 24 Jun 2024 08:02:29 -0400 Subject: [PATCH] feat: add ContactService --- src/services/ContactService/createContact.ts | 37 +++++++++++++++++++ .../ContactService/findContactByID.ts | 22 +++++++++++ src/services/ContactService/index.ts | 13 +++++++ 3 files changed, 72 insertions(+) create mode 100644 src/services/ContactService/createContact.ts create mode 100644 src/services/ContactService/findContactByID.ts create mode 100644 src/services/ContactService/index.ts diff --git a/src/services/ContactService/createContact.ts b/src/services/ContactService/createContact.ts new file mode 100644 index 0000000..1469ccf --- /dev/null +++ b/src/services/ContactService/createContact.ts @@ -0,0 +1,37 @@ +import { Contact, type ContactItem } from "@/models/Contact"; +import { User } from "@/models/User"; +import { UserInputError } from "@/utils/httpErrors.js"; +import type { Contact as GqlContactObject } from "@/types/graphql.js"; + +/** + * ### ContactService: createContact + */ +export const createContact = async ({ + authenticatedUserID, + contactUserID, +}: { + authenticatedUserID: string; + contactUserID: string; +}): Promise => { + // First, ensure the user hasn't somehow sent their own ID + if (contactUserID.toUpperCase() === authenticatedUserID.toUpperCase()) + throw new UserInputError("Can not add yourself as a contact"); + + const requestedContactUser = await User.getItem({ id: contactUserID }); + + if (!requestedContactUser) throw new UserInputError("Requested user not found."); + + // Note: createItem won't overwrite existing if Contact already exists. + const newContact = await Contact.createItem({ + userID: authenticatedUserID, + contactUserID: requestedContactUser.id, + handle: requestedContactUser.handle, + }); + + return { + ...newContact, + email: requestedContactUser.email, + phone: requestedContactUser.phone, + profile: requestedContactUser.profile, + }; +}; diff --git a/src/services/ContactService/findContactByID.ts b/src/services/ContactService/findContactByID.ts new file mode 100644 index 0000000..ca3c38a --- /dev/null +++ b/src/services/ContactService/findContactByID.ts @@ -0,0 +1,22 @@ +import { Contact } from "@/models/Contact"; +import { UserInputError } from "@/utils/httpErrors.js"; + +/** + * ### ContactService: findContactByID + */ +export const findContactByID = async ({ + authenticatedUserID, + contactID, +}: { + authenticatedUserID: string; + contactID: string; +}) => { + const contact = await Contact.getItem({ + userID: authenticatedUserID, + id: contactID, + }); + + if (!contact) throw new UserInputError("A contact with the provided ID could not be found."); + + return contact; +}; diff --git a/src/services/ContactService/index.ts b/src/services/ContactService/index.ts new file mode 100644 index 0000000..efd3c6a --- /dev/null +++ b/src/services/ContactService/index.ts @@ -0,0 +1,13 @@ +import { createContact } from "./createContact.js"; +import { findContactByID } from "./findContactByID.js"; + +/** + * #### ContactService + * + * This object contains methods which implement business logic for operations + * related to users' Contacts. + */ +export const ContactService = { + createContact, + findContactByID, +} as const;