Skip to content

Commit

Permalink
feat: add ContactService
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-anderson committed Jun 24, 2024
1 parent 115f51b commit 3d0e228
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/services/ContactService/createContact.ts
Original file line number Diff line number Diff line change
@@ -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<ContactItem & GqlContactObject> => {
// 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,
};
};
22 changes: 22 additions & 0 deletions src/services/ContactService/findContactByID.ts
Original file line number Diff line number Diff line change
@@ -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;
};
13 changes: 13 additions & 0 deletions src/services/ContactService/index.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 3d0e228

Please sign in to comment.