Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update contact endpoint #32729

Merged
merged 90 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 87 commits
Commits
Show all changes
90 commits
Select commit Hold shift + click to select a range
40312b9
feat: create contact entity
tapiarafael Jun 27, 2024
668d934
feat: add create contact endpoint
tapiarafael Jun 27, 2024
de25810
add deprecation message to old endpoint
tapiarafael Jun 27, 2024
74c9ed8
remove unused file
tapiarafael Jun 27, 2024
ae70c00
fix: typo in prop
tapiarafael Jun 27, 2024
20c3892
test: implement contact tests
tapiarafael Jun 27, 2024
7e8dfa3
Merge branch 'develop' into feat/create-contact-endpoint
tapiarafael Jun 27, 2024
da25a9c
Create sixty-spoons-own.md
tapiarafael Jun 27, 2024
b7e0bf7
Update sixty-spoons-own.md
tapiarafael Jun 28, 2024
675bb12
Update .changeset/sixty-spoons-own.md
tapiarafael Jul 2, 2024
c59c4c2
test: include custom fields validations
tapiarafael Jul 2, 2024
e13f736
test: ensure body is being validated
tapiarafael Jul 2, 2024
e329740
test: ensure error message is equal the expected
tapiarafael Jul 2, 2024
4b74679
test: ensure that it is possible to create contact with contact manag…
tapiarafael Jul 2, 2024
8b1a303
feat: create new permission
tapiarafael Jul 2, 2024
3d26650
fix: replace MeteorError to Error
tapiarafael Jul 3, 2024
36b5021
Merge branch 'develop' into feat/create-contact-endpoint
tapiarafael Jul 3, 2024
9fe155c
refactor: deprecate old endpoint
tapiarafael Jul 3, 2024
8e7da69
Merge branch 'develop' into feat/create-contact-endpoint
tapiarafael Jul 3, 2024
35bc941
fix: remove migration
tapiarafael Jul 4, 2024
9813b26
feat: implement update contact endpoint
tapiarafael Jul 5, 2024
1549a4e
Create heavy-snails-help.md
tapiarafael Jul 5, 2024
e7b6048
Merge branch 'develop' into feat/create-contact-endpoint
tapiarafael Jul 5, 2024
fc24f66
Merge branch 'feat/create-contact-endpoint' into feat/update-contact-…
tapiarafael Jul 9, 2024
6c908af
Merge branch 'develop' into feat/create-contact-endpoint
tapiarafael Jul 9, 2024
a5c7ec5
Merge branch 'feat/create-contact-endpoint' into feat/update-contact-…
tapiarafael Jul 9, 2024
b4c2686
test: additional fields
tapiarafael Jul 9, 2024
af05b31
test: create agent to test
tapiarafael Jul 9, 2024
06a656e
test: restore setting to the original value
tapiarafael Jul 9, 2024
d61e15e
fix: set `phones` as required property
tapiarafael Jul 9, 2024
167da1a
refactor: breaks `validateCustomFields` into two functions
tapiarafael Jul 9, 2024
3a08e05
test: ensure that custom fields are properly validated
tapiarafael Jul 9, 2024
716162c
Merge branch 'develop' into feat/create-contact-endpoint
tapiarafael Jul 9, 2024
d1927ed
Update .changeset/heavy-snails-help.md
tapiarafael Jul 9, 2024
63a4c63
Update apps/meteor/tests/end-to-end/api/livechat/contacts.ts
tapiarafael Jul 9, 2024
bdf60f3
Merge branch 'feat/create-contact-endpoint' into feat/update-contact-…
tapiarafael Jul 9, 2024
56b36c4
test: create agent to test
tapiarafael Jul 9, 2024
f602e92
refactor: pass the user to check if is valid manager
tapiarafael Jul 9, 2024
c7cf819
test: ensure that contact manager is properly validated
tapiarafael Jul 9, 2024
422943d
refactor: extract update to a proper method
tapiarafael Jul 9, 2024
37bdda6
Merge branch 'develop' into feat/create-contact-endpoint
tapiarafael Jul 9, 2024
7022d3b
Merge branch 'feat/create-contact-endpoint' into feat/update-contact-…
tapiarafael Jul 9, 2024
438c697
Merge branch 'develop' into feat/create-contact-endpoint
tapiarafael Jul 9, 2024
cb9a598
Merge branch 'feat/create-contact-endpoint' into feat/update-contact-…
tapiarafael Jul 9, 2024
8c3a68f
Update .changeset/sixty-spoons-own.md
tapiarafael Jul 10, 2024
80172f9
Update apps/meteor/app/livechat/server/lib/Contacts.ts
tapiarafael Jul 10, 2024
0d0d016
fix: use correct method to deprecation
tapiarafael Jul 10, 2024
3bba59f
Merge branch 'develop' into feat/create-contact-endpoint
tapiarafael Jul 10, 2024
87316ca
Merge branch 'feat/create-contact-endpoint' into feat/update-contact-…
tapiarafael Jul 10, 2024
cf6da0c
fix: type of IUser
tapiarafael Jul 10, 2024
2cb7fcd
Merge branch 'develop' into feat/create-contact-endpoint
tapiarafael Jul 12, 2024
92c220b
Merge branch 'feat/create-contact-endpoint' into feat/update-contact-…
tapiarafael Jul 12, 2024
8ebf18a
Merge branch 'develop' into feat/create-contact-endpoint
tapiarafael Jul 15, 2024
a8a4071
Merge branch 'feat/create-contact-endpoint' into feat/update-contact-…
tapiarafael Jul 15, 2024
ef56b06
fix: find only agents
tapiarafael Jul 16, 2024
307661a
Update apps/meteor/app/livechat/server/api/v1/contact.ts
tapiarafael Jul 16, 2024
51b9d0b
Merge branch 'develop' into feat/create-contact-endpoint
tapiarafael Jul 16, 2024
ba6a59e
Merge branch 'feat/create-contact-endpoint' into feat/update-contact-…
tapiarafael Jul 16, 2024
6cbc007
test: remove test since it never happen
tapiarafael Jul 16, 2024
98b50bc
Merge branch 'develop' into feat/create-contact-endpoint
tapiarafael Jul 16, 2024
d08fe10
Merge branch 'feat/create-contact-endpoint' into feat/update-contact-…
tapiarafael Jul 16, 2024
e07090a
Merge branch 'develop' into feat/create-contact-endpoint
tapiarafael Jul 16, 2024
c1633e6
Merge branch 'develop' into feat/create-contact-endpoint
tapiarafael Jul 17, 2024
c4a50c1
Merge branch 'feat/create-contact-endpoint' into feat/update-contact-…
tapiarafael Jul 18, 2024
39fffda
Merge branch 'develop' into feat/create-contact-endpoint
tapiarafael Jul 29, 2024
46145de
Merge branch 'feat/create-contact-endpoint' into feat/update-contact-…
tapiarafael Jul 29, 2024
a5483d5
Merge branch 'develop' into feat/create-contact-endpoint
tapiarafael Aug 8, 2024
b285d39
Merge branch 'feat/create-contact-endpoint' into feat/update-contact-…
tapiarafael Aug 8, 2024
ff1c391
Merge branch 'develop' into feat/update-contact-endpoint
tapiarafael Aug 23, 2024
33dace9
Merge branch 'develop' into feat/update-contact-endpoint
tapiarafael Aug 26, 2024
3c87d58
feat: add translation for new permissions
tapiarafael Aug 28, 2024
a4d84fe
refactor: extract contact manager validation to a function
tapiarafael Aug 28, 2024
504c4ec
refactor: use findOneAndUpdate instead
tapiarafael Aug 28, 2024
0babebc
fix: set unknown to false when contact is updated
tapiarafael Aug 28, 2024
cb5d963
refactor: get only the id to check if user exists
tapiarafael Aug 28, 2024
1fea4e2
fix: throw an error if additional custom fields are provided
tapiarafael Aug 28, 2024
0cc0e83
test: mock user repository
tapiarafael Aug 28, 2024
32cb0a6
test: add updateContact unit tests
tapiarafael Aug 28, 2024
c3bfd27
Merge branch 'develop' into feat/update-contact-endpoint
tapiarafael Aug 28, 2024
c903de9
fix: make name not nullable
tapiarafael Aug 29, 2024
f54dc84
test: admin already has the permission by default
tapiarafael Aug 29, 2024
a23a9e0
test: use after/before to set permissions
tapiarafael Aug 29, 2024
67f25b3
chore: improve description
tapiarafael Aug 29, 2024
559340c
Merge branch 'develop' into feat/update-contact-endpoint
tapiarafael Aug 29, 2024
d1858ad
Merge branch 'develop' into feat/update-contact-endpoint
tapiarafael Sep 2, 2024
43187b5
Merge branch 'develop' into feat/update-contact-endpoint
tapiarafael Sep 2, 2024
726414e
Update .changeset/heavy-snails-help.md
tapiarafael Sep 2, 2024
b922e3d
fix: set the correct translation
tapiarafael Sep 2, 2024
39c5a02
Merge branch 'develop' into feat/update-contact-endpoint
tapiarafael Sep 2, 2024
9ec995d
Merge branch 'develop' into feat/update-contact-endpoint
casalsgh Sep 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/heavy-snails-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rocket.chat/meteor": minor
"@rocket.chat/rest-typings": minor
---

Implemented "omnichannel/contacts.update" endpoint to update contacts
4 changes: 4 additions & 0 deletions apps/meteor/app/authorization/server/constant/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ export const permissions = [
_id: 'create-livechat-contact',
roles: ['livechat-manager', 'livechat-monitor', 'livechat-agent', 'admin'],
},
{
_id: 'update-livechat-contact',
tapiarafael marked this conversation as resolved.
Show resolved Hide resolved
roles: ['livechat-manager', 'livechat-monitor', 'livechat-agent', 'admin'],
},
{ _id: 'view-livechat-manager', roles: ['livechat-manager', 'livechat-monitor', 'admin'] },
{
_id: 'view-omnichannel-contact-center',
Expand Down
19 changes: 17 additions & 2 deletions apps/meteor/app/livechat/server/api/v1/contact.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { LivechatCustomField, LivechatVisitors } from '@rocket.chat/models';
import { isPOSTOmnichannelContactsProps } from '@rocket.chat/rest-typings';
import { isPOSTOmnichannelContactsProps, isPOSTUpdateOmnichannelContactsProps } from '@rocket.chat/rest-typings';
import { escapeRegExp } from '@rocket.chat/string-helpers';
import { Match, check } from 'meteor/check';
import { Meteor } from 'meteor/meteor';

import { API } from '../../../../api/server';
import { Contacts, createContact } from '../../lib/Contacts';
import { Contacts, createContact, updateContact } from '../../lib/Contacts';

API.v1.addRoute(
'omnichannel/contact',
Expand Down Expand Up @@ -101,3 +101,18 @@ API.v1.addRoute(
},
},
);
API.v1.addRoute(
'omnichannel/contacts.update',
{ authRequired: true, permissionsRequired: ['update-livechat-contact'], validateParams: isPOSTUpdateOmnichannelContactsProps },
{
async post() {
if (!process.env.TEST_MODE) {
throw new Meteor.Error('error-not-allowed', 'This endpoint is only allowed in test mode');
}

const contact = await updateContact({ ...this.bodyParams });

return API.v1.success({ contact });
},
},
);
61 changes: 56 additions & 5 deletions apps/meteor/app/livechat/server/lib/Contacts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import type { ILivechatContactChannel, ILivechatCustomField, ILivechatVisitor, IOmnichannelRoom, IUser } from '@rocket.chat/core-typings';
import type {
ILivechatContact,
ILivechatContactChannel,
ILivechatCustomField,
ILivechatVisitor,
IOmnichannelRoom,
IUser,
} from '@rocket.chat/core-typings';
import {
LivechatVisitors,
Users,
Expand Down Expand Up @@ -45,6 +52,16 @@ type CreateContactParams = {
channels?: ILivechatContactChannel[];
};

type UpdateContactParams = {
contactId: string;
name?: string;
emails?: string[];
phones?: string[];
customFields?: Record<string, unknown>;
contactManager?: string;
channels?: ILivechatContactChannel[];
};

export const Contacts = {
async registerContact({
token,
Expand Down Expand Up @@ -189,10 +206,7 @@ export async function createContact(params: CreateContactParams): Promise<string
const { name, emails, phones, customFields = {}, contactManager, channels, unknown } = params;

if (contactManager) {
const contactManagerUser = await Users.findOneAgentById<Pick<IUser, 'roles'>>(contactManager, { projection: { roles: 1 } });
if (!contactManagerUser) {
throw new Error('error-contact-manager-not-found');
}
await validateContactManager(contactManager);
}

const allowedCustomFields = await getAllowedCustomFields();
Expand All @@ -211,6 +225,29 @@ export async function createContact(params: CreateContactParams): Promise<string
return insertedId;
}

export async function updateContact(params: UpdateContactParams): Promise<ILivechatContact> {
const { contactId, name, emails, phones, customFields, contactManager, channels } = params;

const contact = await LivechatContacts.findOneById<Pick<ILivechatContact, '_id'>>(contactId, { projection: { _id: 1 } });

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

if (contactManager) {
await validateContactManager(contactManager);
}

if (customFields) {
const allowedCustomFields = await getAllowedCustomFields();
validateCustomFields(allowedCustomFields, customFields);
}

const updatedContact = await LivechatContacts.updateContact(contactId, { name, emails, phones, contactManager, channels, customFields });

return updatedContact;
}

async function getAllowedCustomFields(): Promise<ILivechatCustomField[]> {
return LivechatCustomField.findByScope(
'visitor',
Expand Down Expand Up @@ -245,4 +282,18 @@ export function validateCustomFields(allowedCustomFields: ILivechatCustomField[]
}
}
}

const allowedCustomFieldIds = new Set(allowedCustomFields.map((cf) => cf._id));
for (const key in customFields) {
if (!allowedCustomFieldIds.has(key)) {
throw new Error(i18n.t('error-custom-field-not-allowed', { key }));
}
}
}

export async function validateContactManager(contactManagerUserId: string) {
const contactManagerUser = await Users.findOneAgentById<Pick<IUser, '_id'>>(contactManagerUserId, { projection: { _id: 1 } });
if (!contactManagerUser) {
throw new Error('error-contact-manager-not-found');
}
}
9 changes: 9 additions & 0 deletions apps/meteor/server/models/raw/LivechatContacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,13 @@ export class LivechatContactsRaw extends BaseRaw<ILivechatContact> implements IL
constructor(db: Db, trash?: Collection<RocketChatRecordDeleted<ILivechatContact>>) {
super(db, 'livechat_contact', trash);
}

async updateContact(contactId: string, data: Partial<ILivechatContact>): Promise<ILivechatContact> {
const updatedValue = await this.findOneAndUpdate(
{ _id: contactId },
{ $set: { ...data, unknown: false } },
{ returnDocument: 'after' },
);
return updatedValue.value as ILivechatContact;
}
}
Loading
Loading