Skip to content

Commit 6bf78ef

Browse files
committed
get groups - add user details
1 parent 3176215 commit 6bf78ef

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/api/v1/groups/services.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use strict";
22

3+
const { Users } = require("../users/schema/models");
34
const { Groups } = require("./schema/models");
45

56
module.exports = {
@@ -44,17 +45,34 @@ module.exports = {
4445

4546
getAllGroupService: async function (accountId, queryParams) {
4647
try {
48+
let includeUserDetails = false;
4749
let groupList = [];
4850
let groupQuery = { accountId: accountId };
4951

5052
if (queryParams && queryParams.userId) {
5153
groupQuery.userIds = queryParams.userId;
5254
}
5355

56+
if (queryParams && queryParams.includeUserDetails && queryParams.includeUserDetails == true) {
57+
includeUserDetails = true;
58+
}
59+
5460
let groups = await Groups.find(groupQuery);
5561

5662
for (let i = 0; i < groups.length; i++) {
57-
groupList.push({ id: groups[i].id.toString(), ...JSON.parse(JSON.stringify(groups[i])) });
63+
if (includeUserDetails) {
64+
let userList = [];
65+
for (let j = 0; j < groups[i].userIds; j++) {
66+
let userData = await Users.findById(groups[i].userIds[j]);
67+
if (userData) {
68+
userList.push(userData);
69+
}
70+
}
71+
72+
groupList.push({ id: groups[i].id.toString(), users: userList, ...JSON.parse(JSON.stringify(groups[i])) });
73+
} else {
74+
groupList.push({ id: groups[i].id.toString(), ...JSON.parse(JSON.stringify(groups[i])) });
75+
}
5876
}
5977

6078
return {

0 commit comments

Comments
 (0)