-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
115f51b
commit 3d0e228
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |