Skip to content

Commit

Permalink
Merge pull request #27 from AplinkosMinisterija/populate-invited-data…
Browse files Browse the repository at this point in the history
…-rewrite

Rewrite invited logic - one query instead of N
  • Loading branch information
ambrazasp authored Nov 5, 2024
2 parents c911879 + 6f699f4 commit 256364c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 20 deletions.
36 changes: 28 additions & 8 deletions services/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,32 @@ export interface User extends BaseModelInterface {
invited: {
virtual: true,
type: 'boolean',
get: async ({ ctx, entity }: FieldHookCallback) => {
const isUserEvartaiInvited: Boolean = await ctx.call('usersEvartai.isUserInvited', {
id: entity.id,
});
const isUserLocalInvited: Boolean = await ctx.call('usersLocal.isUserInvited', {
id: entity.id,
});
return isUserEvartaiInvited || isUserLocalInvited;
populate: {
keyField: 'id',
async handler(ctx: any, values: any[], items: any[]) {
const invitedEvartaiUsersById: { [key: string]: Boolean } = await ctx.call(
'usersEvartai.isUserInvited',
{
id: values,
mapping: true,
},
);
const invitedLocalUsersById: { [key: string]: Boolean } = await ctx.call(
'usersLocal.isUserInvited',
{
id: values,
mapping: true,
},
);

return values.reduce(
(acc: any, item) => ({
...acc,
[item]: !!invitedEvartaiUsersById[item] || !!invitedLocalUsersById[item],
}),
{},
);
},
},
},

Expand Down Expand Up @@ -188,6 +206,8 @@ export interface User extends BaseModelInterface {
...COMMON_FIELDS,
},

defaultPopulates: ['invited'],

scopes: {
...COMMON_SCOPES,
},
Expand Down
34 changes: 28 additions & 6 deletions services/usersEvartai.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,14 +509,36 @@ export default class UsersEvartaiService extends moleculer.Service {

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

if (mapping) {
const users: User[] = await ctx.call('users.find', {
query: {
id: { $in: Array.isArray(id) ? id : [id] },
},
fields: ['id', 'firstName', 'lastName'],
});

return users.reduce(
(acc: any, item) => ({
...acc,
[item.id]: !item.firstName && !item.lastName,
}),
{},
);
}

const user: User = await ctx.call('users.resolve', {
id,
fields: ['firstName', 'lastName'],
Expand Down
33 changes: 27 additions & 6 deletions services/usersLocal.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,14 +449,35 @@ export default class UsersLocalService extends moleculer.Service {

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

if (mapping) {
const users: UserLocal[] = await ctx.call('usersLocal.find', {
query: {
id: { $in: Array.isArray(id) ? id : [id] },
},
fields: ['id', 'password'],
});

return users.reduce(
(acc: any, item) => ({
...acc,
[item.id]: !item.password,
}),
{},
);
}

const adapter = await this.getAdapter(ctx);

Expand Down

0 comments on commit 256364c

Please sign in to comment.