Skip to content

Commit 8f2cfa4

Browse files
authored
Revert "feat: adds new model function for aggregated agents by department" (RocketChat#36648)
1 parent aa05adc commit 8f2cfa4

5 files changed

Lines changed: 38 additions & 90 deletions

File tree

.changeset/fluffy-cats-crash.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

apps/meteor/app/livechat/server/api/lib/departments.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,24 +168,18 @@ export async function findDepartmentsToAutocomplete({
168168
export async function findDepartmentAgents({
169169
departmentId,
170170
pagination: { offset, count, sort },
171-
}: FindDepartmentAgentsParams): Promise<
172-
PaginatedResult<{ agents: (ILivechatDepartmentAgents & { user: { _id: string; username: string; name: string } })[] }>
173-
> {
174-
const cursor = LivechatDepartmentAgents.findAgentsByDepartmentId(departmentId, {
171+
}: FindDepartmentAgentsParams): Promise<PaginatedResult<{ agents: ILivechatDepartmentAgents[] }>> {
172+
const { cursor, totalCount } = LivechatDepartmentAgents.findAgentsByDepartmentId<ILivechatDepartmentAgents>(departmentId, {
175173
sort: sort || { username: 1 },
176174
skip: offset,
177175
limit: count,
178176
});
179-
const [
180-
{
181-
result,
182-
totalCount: [{ total } = { total: 0 }],
183-
},
184-
] = await cursor.toArray();
177+
178+
const [agents, total] = await Promise.all([cursor.toArray(), totalCount]);
185179

186180
return {
187-
agents: result,
188-
count: result.length,
181+
agents,
182+
count: agents.length,
189183
offset,
190184
total,
191185
};

apps/meteor/tests/end-to-end/api/livechat/10-departments.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -841,10 +841,6 @@ import { IS_EE } from '../../../e2e/config/constants';
841841
expect(res.body.agents[0]).to.have.property('_id');
842842
expect(res.body.agents[0]).to.have.property('departmentId', dep._id);
843843
expect(res.body.agents[0]).to.have.property('departmentEnabled', true);
844-
expect(res.body.agents[0]).to.have.property('user');
845-
expect(res.body.agents[0].user).to.have.property('_id');
846-
expect(res.body.agents[0].user).to.have.property('username');
847-
expect(res.body.agents[0].user).to.have.property('name');
848844
expect(res.body.count).to.be.equal(1);
849845
await deleteDepartment(dep._id);
850846
});

packages/model-typings/src/models/ILivechatDepartmentAgentsModel.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { AvailableAgentsAggregation, ILivechatDepartmentAgents } from '@rocket.chat/core-typings';
22
import type { DeleteResult, FindCursor, FindOptions, Document, UpdateResult, Filter, AggregationCursor } from 'mongodb';
33

4-
import type { IBaseModel } from './IBaseModel';
4+
import type { FindPaginated, IBaseModel } from './IBaseModel';
55

66
export interface ILivechatDepartmentAgentsModel extends IBaseModel<ILivechatDepartmentAgents> {
77
findUsersInQueue(usersList: string[]): FindCursor<ILivechatDepartmentAgents>;
@@ -22,13 +22,22 @@ export interface ILivechatDepartmentAgentsModel extends IBaseModel<ILivechatDepa
2222
): FindCursor<ILivechatDepartmentAgents> | FindCursor<P>;
2323
findByAgentId(agentId: string, options?: FindOptions<ILivechatDepartmentAgents>): FindCursor<ILivechatDepartmentAgents>;
2424

25+
findAgentsByDepartmentId(departmentId: string): FindPaginated<FindCursor<ILivechatDepartmentAgents>>;
26+
2527
findAgentsByDepartmentId(
2628
departmentId: string,
27-
options?: FindOptions<ILivechatDepartmentAgents>,
28-
): AggregationCursor<{
29-
result: (ILivechatDepartmentAgents & { user: { _id: string; username: string; name: string } })[];
30-
totalCount: { _id: null; total: number }[];
31-
}>;
29+
options: FindOptions<ILivechatDepartmentAgents>,
30+
): FindPaginated<FindCursor<ILivechatDepartmentAgents>>;
31+
32+
findAgentsByDepartmentId<P extends Document>(
33+
departmentId: string,
34+
options: FindOptions<P extends ILivechatDepartmentAgents ? ILivechatDepartmentAgents : P>,
35+
): FindPaginated<FindCursor<P>>;
36+
37+
findAgentsByDepartmentId(
38+
departmentId: string,
39+
options?: undefined | FindOptions<ILivechatDepartmentAgents>,
40+
): FindPaginated<FindCursor<ILivechatDepartmentAgents>>;
3241

3342
findByDepartmentIds(departmentIds: string[], options?: Record<string, any>): FindCursor<ILivechatDepartmentAgents>;
3443
findAgentsByAgentIdAndBusinessHourId(_agentId: string, _businessHourId: string): Promise<ILivechatDepartmentAgents[]>;

packages/models/src/models/LivechatDepartmentAgents.ts

Lines changed: 17 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { AvailableAgentsAggregation, ILivechatDepartmentAgents, RocketChatRecordDeleted } from '@rocket.chat/core-typings';
2-
import type { ILivechatDepartmentAgentsModel } from '@rocket.chat/model-typings';
2+
import type { FindPaginated, ILivechatDepartmentAgentsModel } from '@rocket.chat/model-typings';
33
import type {
44
Collection,
55
FindCursor,
@@ -15,7 +15,6 @@ import type {
1515
} from 'mongodb';
1616

1717
import { Users } from '../index';
18-
import { readSecondaryPreferred } from '../readSecondaryPreferred';
1918
import { BaseRaw } from './BaseRaw';
2019

2120
export class LivechatDepartmentAgentsRaw extends BaseRaw<ILivechatDepartmentAgents> implements ILivechatDepartmentAgentsModel {
@@ -88,72 +87,29 @@ export class LivechatDepartmentAgentsRaw extends BaseRaw<ILivechatDepartmentAgen
8887
return this.find({ agentId }, options);
8988
}
9089

90+
findAgentsByDepartmentId(departmentId: string): FindPaginated<FindCursor<ILivechatDepartmentAgents>>;
91+
9192
findAgentsByDepartmentId(
9293
departmentId: string,
93-
options?: FindOptions<ILivechatDepartmentAgents>,
94-
): AggregationCursor<{
95-
result: (ILivechatDepartmentAgents & { user: { _id: string; username: string; name: string } })[];
96-
totalCount: { _id: null; total: number }[];
97-
}> {
98-
const lookup = {
99-
$lookup: {
100-
from: 'users',
101-
let: {
102-
agentId: '$agentId',
103-
},
104-
pipeline: [
105-
{
106-
$match: {
107-
$expr: {
108-
$eq: ['$_id', '$$agentId'],
109-
},
110-
},
111-
},
112-
{
113-
$project: {
114-
_id: 1,
115-
username: 1,
116-
name: 1,
117-
},
118-
},
119-
],
120-
as: 'user',
121-
},
122-
};
123-
const unwind = { $unwind: { path: '$user' } };
94+
options: FindOptions<ILivechatDepartmentAgents>,
95+
): FindPaginated<FindCursor<ILivechatDepartmentAgents>>;
12496

125-
const sort: Document = { $sort: options?.sort || { username: 1 } };
126-
const pagination = [sort];
97+
findAgentsByDepartmentId<P extends Document>(
98+
departmentId: string,
99+
options: FindOptions<P extends ILivechatDepartmentAgents ? ILivechatDepartmentAgents : P>,
100+
): FindPaginated<FindCursor<P>>;
127101

128-
if (options?.skip) {
129-
pagination.push({ $skip: options.skip });
130-
}
102+
findAgentsByDepartmentId(
103+
departmentId: string,
104+
options?: undefined | FindOptions<ILivechatDepartmentAgents>,
105+
): FindPaginated<FindCursor<ILivechatDepartmentAgents>> {
106+
const query = { departmentId };
131107

132-
if (options?.limit) {
133-
pagination.push({ $limit: options.limit });
108+
if (options === undefined) {
109+
return this.findPaginated(query);
134110
}
135111

136-
const facet = {
137-
$facet: {
138-
result: pagination,
139-
totalCount: [{ $group: { _id: null, total: { $sum: 1 } } }],
140-
},
141-
};
142-
143-
return this.col.aggregate<{
144-
result: (ILivechatDepartmentAgents & { user: { _id: string; username: string; name: string } })[];
145-
totalCount: { _id: null; total: number }[];
146-
}>(
147-
[
148-
{
149-
$match: { departmentId },
150-
},
151-
lookup,
152-
unwind,
153-
facet,
154-
],
155-
{ readPreference: readSecondaryPreferred(), allowDiskUse: true },
156-
);
112+
return this.findPaginated(query, options);
157113
}
158114

159115
findByDepartmentIds(departmentIds: string[], options = {}): FindCursor<ILivechatDepartmentAgents> {

0 commit comments

Comments
 (0)