Skip to content

Commit 7084e41

Browse files
authored
feat(ticket): Add user fetching by selected branch during filtering
1 parent b117743 commit 7084e41

File tree

3 files changed

+46
-51
lines changed
  • packages
    • core/src/data/resolvers/queries
    • plugin-tickets-api/src/graphql/resolvers/queries
    • ui-tickets/src/boards/components/editForm

3 files changed

+46
-51
lines changed

packages/core/src/data/resolvers/queries/users.ts

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IContext, IModels } from "../../../connectionResolver";
22
import {
33
checkPermission,
4-
requireLogin
4+
requireLogin,
55
} from "@erxes/api-utils/src/permissions";
66
import { escapeRegExp, getConfig, paginate } from "../../utils";
77

@@ -84,7 +84,7 @@ export class Builder {
8484
// filter by segment
8585
if (this.params.segment) {
8686
const segment = await this.models.Segments.findOne({
87-
_id: this.params.segment
87+
_id: this.params.segment,
8888
});
8989

9090
await this.segmentFilter(segment);
@@ -96,9 +96,9 @@ export class Builder {
9696
query: {
9797
bool: {
9898
must: this.positiveList,
99-
must_not: this.negativeList
100-
}
101-
}
99+
must_not: this.negativeList,
100+
},
101+
},
102102
};
103103

104104
let totalCount = 0;
@@ -108,7 +108,7 @@ export class Builder {
108108
action: "count",
109109
index: this.contentType,
110110
body: queryOptions,
111-
defaultValue: 0
111+
defaultValue: 0,
112112
});
113113

114114
totalCount = totalCountResponse.count;
@@ -117,19 +117,19 @@ export class Builder {
117117
subdomain: this.subdomain,
118118
action,
119119
index: this.contentType,
120-
body: queryOptions
120+
body: queryOptions,
121121
});
122122

123-
const list = response.hits.hits.map(hit => {
123+
const list = response.hits.hits.map((hit) => {
124124
return {
125125
_id: hit._id,
126-
...hit._source
126+
...hit._source,
127127
};
128128
});
129129

130130
return {
131131
list,
132-
totalCount
132+
totalCount,
133133
};
134134
}
135135
}
@@ -164,14 +164,14 @@ const getChildIds = async (model, ids) => {
164164
const orderQry: any[] = [];
165165
for (const item of items) {
166166
orderQry.push({
167-
order: { $regex: new RegExp(`^${escapeRegExp(item.order)}`) }
167+
order: { $regex: new RegExp(`^${escapeRegExp(item.order)}`) },
168168
});
169169
}
170170

171171
const allItems = await model.find({
172-
$or: orderQry
172+
$or: orderQry,
173173
});
174-
return allItems.map(i => i._id);
174+
return allItems.map((i) => i._id);
175175
};
176176

177177
const queryBuilder = async (
@@ -195,11 +195,11 @@ const queryBuilder = async (
195195
departmentIds,
196196
branchIds,
197197
segment,
198-
segmentData
198+
segmentData,
199199
} = params;
200200

201201
const selector: any = {
202-
isActive
202+
isActive,
203203
};
204204

205205
let andCondition: any[] = [];
@@ -210,7 +210,7 @@ const queryBuilder = async (
210210
{ employeeId: new RegExp(`.*${params.searchValue}.*`, "i") },
211211
{ username: new RegExp(`.*${params.searchValue}.*`, "i") },
212212
{ "details.fullName": new RegExp(`.*${params.searchValue}.*`, "i") },
213-
{ "details.position": new RegExp(`.*${params.searchValue}.*`, "i") }
213+
{ "details.position": new RegExp(`.*${params.searchValue}.*`, "i") },
214214
];
215215

216216
selector.$or = fields;
@@ -281,7 +281,7 @@ const queryBuilder = async (
281281
!branchUserIds.includes(user._id)
282282
) {
283283
customCond.push({
284-
branchIds: { $in: await getChildIds(models.Branches, branchIds) }
284+
branchIds: { $in: await getChildIds(models.Branches, branchIds) },
285285
});
286286
}
287287

@@ -292,27 +292,25 @@ const queryBuilder = async (
292292
) {
293293
customCond.push({
294294
departmentIds: {
295-
$in: await getChildIds(models.Departments, departmentIds)
296-
}
295+
$in: await getChildIds(models.Departments, departmentIds),
296+
},
297297
});
298298
}
299299

300300
andCondition = customCond.length ? [{ $or: customCond }] : [];
301301
}
302-
} else {
303-
if (branchIds && branchIds.length) {
304-
selector.branchIds = {
305-
$in: await getChildIds(models.Branches, branchIds)
306-
};
307-
}
308-
309-
if (departmentIds && departmentIds.length) {
310-
selector.departmentIds = {
311-
$in: await getChildIds(models.Departments, departmentIds)
312-
};
313-
}
314302
}
315303

304+
if (branchIds && branchIds.length) {
305+
selector.branchIds = {
306+
$in: await getChildIds(models.Branches, branchIds),
307+
};
308+
}
309+
if (departmentIds && departmentIds.length) {
310+
selector.departmentIds = {
311+
$in: await getChildIds(models.Departments, departmentIds),
312+
};
313+
}
316314
if (unitId) {
317315
const unit = await models.Units.getUnit({ _id: unitId });
318316

@@ -330,7 +328,7 @@ const queryBuilder = async (
330328

331329
const { list } = await qb.runQueries();
332330

333-
selector._id = { $in: list.map(l => l._id) };
331+
selector._id = { $in: list.map((l) => l._id) };
334332
}
335333

336334
if (andCondition.length) {
@@ -352,7 +350,7 @@ const userQueries = {
352350
const selector = {
353351
...userBrandIdsSelector,
354352
...(await queryBuilder(models, args, subdomain, user)),
355-
...NORMAL_USER_SELECTOR
353+
...NORMAL_USER_SELECTOR,
356354
};
357355

358356
const { sortField, sortDirection } = args;
@@ -373,7 +371,7 @@ const userQueries = {
373371
{
374372
isActive,
375373
ids,
376-
assignedToMe
374+
assignedToMe,
377375
}: { isActive: boolean; ids: string[]; assignedToMe: string },
378376
{ userBrandIdsSelector, user, models }: IContext
379377
) {
@@ -390,7 +388,7 @@ const userQueries = {
390388
}
391389

392390
return models.Users.find({ ...selector, ...NORMAL_USER_SELECTOR }).sort({
393-
username: 1
391+
username: 1,
394392
});
395393
},
396394

@@ -412,7 +410,7 @@ const userQueries = {
412410
const selector = {
413411
...userBrandIdsSelector,
414412
...(await queryBuilder(models, args, subdomain, user)),
415-
...NORMAL_USER_SELECTOR
413+
...NORMAL_USER_SELECTOR,
416414
};
417415

418416
return models.Users.find(selector).countDocuments();
@@ -438,7 +436,7 @@ const userQueries = {
438436
*/
439437
async userMovements(_root, args, { models }: IContext) {
440438
return await models.UserMovements.find(args).sort({ createdAt: -1 });
441-
}
439+
},
442440
};
443441

444442
requireLogin(userQueries, "usersTotalCount");

packages/plugin-tickets-api/src/graphql/resolvers/queries/utils.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -671,16 +671,6 @@ export const generateCommonFilters = async (
671671
],
672672
};
673673
}
674-
filter.$or = [
675-
{
676-
isCheckUserTicket: { $ne: true },
677-
assignedUserIds: { $eq: [] },
678-
},
679-
{
680-
isCheckUserTicket: true,
681-
$or: [{ assignedUserIds: currentUserId }, { userId: currentUserId }],
682-
},
683-
];
684674

685675
return filter;
686676
};
@@ -1128,6 +1118,8 @@ export const getItemList = async (
11281118
labels: "$labels_doc",
11291119
stage: { $arrayElemAt: ["$stages_doc", 0] },
11301120
name: 1,
1121+
isCheckUserTicket: 1,
1122+
assignedUserIds: 1,
11311123
isComplete: 1,
11321124
startDate: 1,
11331125
closeDate: 1,
@@ -1385,6 +1377,13 @@ export const getItemList = async (
13851377
let order = 0;
13861378

13871379
for (const item of list) {
1380+
if (item.isCheckUserTicket === true) {
1381+
if (
1382+
!(item.userId === user._id || item.assignedUserIds?.includes(user._id))
1383+
) {
1384+
continue;
1385+
}
1386+
}
13881387
if (
13891388
item.customFieldsData &&
13901389
item.customFieldsData.length > 0 &&

packages/ui-tickets/src/boards/components/editForm/Sidebar.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,11 @@ class Sidebar extends React.Component<Props> {
3535
render() {
3636
const { item, saveItem, sidebar, childrenSection, currentUser } =
3737
this.props;
38-
3938
const userOnChange = (usrs) => saveItem({ assignedUserIds: usrs });
4039
const onChangeStructure = (values, name) => saveItem({ [name]: values });
4140
const assignedUserIds = (item.assignedUsers || []).map((user) => user._id);
42-
const branchIds = currentUser.branchIds;
43-
const departmentIds = currentUser.departmentIds;
44-
41+
const branchIds = item?.branchIds;
42+
const departmentIds = item?.departmentIds;
4543
return (
4644
<RightContent>
4745
<FormGroup>

0 commit comments

Comments
 (0)