Skip to content

Commit

Permalink
remove unneeded mongo operations
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-lehnen-rc committed Nov 13, 2024
1 parent ea08d74 commit 98246f9
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@ import { LivechatContacts } from '@rocket.chat/models';
* @returns the updated contact
*/
export async function addContactEmail(contactId: ILivechatContact['_id'], email: string): Promise<ILivechatContact> {
const contact = await LivechatContacts.findOneById(contactId);
const contact = await LivechatContacts.addEmail(contactId, email);

if (!contact) {
throw new Error('error-contact-not-found');
}

const emails = contact.emails?.map(({ address }) => address) || [];
if (!emails.includes(email)) {
return LivechatContacts.addEmail(contactId, email) as Promise<ILivechatContact>;
}

return contact;
}
2 changes: 1 addition & 1 deletion apps/meteor/server/models/raw/LivechatContacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ export class LivechatContactsRaw extends BaseRaw<ILivechatContact> implements IL
}

async addEmail(contactId: string, email: string): Promise<ILivechatContact | null> {
const updatedContact = await this.findOneAndUpdate({ _id: contactId }, { $push: { emails: { address: email } } });
const updatedContact = await this.findOneAndUpdate({ _id: contactId }, { $addToSet: { emails: { address: email } } });

return updatedContact.value;
}
Expand Down

0 comments on commit 98246f9

Please sign in to comment.