Skip to content

Commit

Permalink
Merge pull request #28 from AplinkosMinisterija/optimize-groups
Browse files Browse the repository at this point in the history
wip: optimize groups populate
  • Loading branch information
ambrazasp authored Nov 5, 2024
2 parents 256364c + 00cf385 commit fb66100
Showing 1 changed file with 50 additions and 21 deletions.
71 changes: 50 additions & 21 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 @@ -497,18 +506,26 @@ export default class UsersService extends moleculer.Service {

@Action({
params: {
id: {
type: 'number',
convert: true,
},
id: [
{
type: 'array',
items: 'number|convert',
},
'number|convert',
],
mapping: 'boolean|default:false',
},
})
async getVisibleUserGroups(
ctx: Context<{ id: number; populate?: string }, AppAuthMeta & UserAuthMeta>,
ctx: Context<
{ id: number | number[]; populate?: string; mapping?: boolean },
AppAuthMeta & UserAuthMeta
>,
) {
const { id, populate } = ctx.params;
const { id, populate, mapping } = ctx.params;

const query: any = {
user: id,
user: { $in: Array.isArray(id) ? id : [id] },
};
if (ctx.meta?.app?.id) {
const groupsIds = await ctx.call('permissions.getVisibleGroupsIds', {
Expand All @@ -519,10 +536,22 @@ export default class UsersService extends moleculer.Service {
};
}

return ctx.call('userGroups.find', {
const response: UserGroup[] = await ctx.call('userGroups.find', {
populate,
query,
});

if (mapping) {
return response.reduce<Record<number, UserGroup[]>>(
(acc, item) => ({
...acc,
[item.user as number]: [...(acc[item.user as number] || []), item],
}),
{},
);
}

return response;
}

@Method
Expand Down

0 comments on commit fb66100

Please sign in to comment.