Skip to content

Commit

Permalink
should be good
Browse files Browse the repository at this point in the history
  • Loading branch information
arunas-smala committed Nov 5, 2024
1 parent 9e11ee3 commit 9cef823
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions services/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,29 @@ export interface User extends BaseModelInterface {
items: { type: 'object' },
virtual: true,
default: [],
populate(ctx: any, _values: any, items: any[]) {
return Promise.all(
items.map(async (item: any) => {
const userGroups: Array<UserGroup> = await ctx.call('users.getVisibleUserGroups', {
id: item.id,
populate: {
keyField: 'id',
async handler(ctx: any, values: any[], items: any[]) {
const userGroups: Record<number, UserGroup[]> = await ctx.call(
'users.getVisibleUserGroups',
{
id: values,
mapping: true,
populate: 'group',
});
if (!userGroups || !userGroups.length) return [];
return userGroups.map((i) => ({
...(i.group as Group),
role: i.role,
}));
}),
);
},
);

return values.reduce(
(acc: any, item) => ({
...acc,
[item]: (userGroups[item] || []).map((i) => ({
...(i.group as Group),
role: i.role,
})),
}),
{},
);
},
},
},

Expand Down Expand Up @@ -533,10 +542,13 @@ export default class UsersService extends moleculer.Service {
});

if (mapping) {
return response.reduce((acc: any, item: any) => ({
...acc,
[item.user]: item,
}));
return response.reduce(
(acc: any, item: any) => ({
...acc,
[item.user]: item,
}),
{},
);
}

return response;
Expand Down

0 comments on commit 9cef823

Please sign in to comment.