diff --git a/apps/server/src/modules/user/legacy/controller/dto/user.response.ts b/apps/server/src/modules/user/legacy/controller/dto/user.response.ts index 5521b20f3f9..95e5bea2401 100644 --- a/apps/server/src/modules/user/legacy/controller/dto/user.response.ts +++ b/apps/server/src/modules/user/legacy/controller/dto/user.response.ts @@ -1,6 +1,6 @@ import { ApiProperty } from '@nestjs/swagger'; -import { ConsentsResponse } from './consents.response'; import { ClassResponse } from './class.response'; +import { ConsentsResponse } from './consents.response'; export class UserResponse { constructor({ @@ -17,6 +17,7 @@ export class UserResponse { importHash, lastLoginSystemChange, outdatedSince, + isEditable, }: UserResponse) { this._id = _id.toString(); this.firstName = firstName; @@ -31,6 +32,7 @@ export class UserResponse { this.importHash = importHash; this.lastLoginSystemChange = lastLoginSystemChange; this.outdatedSince = outdatedSince; + this.isEditable = isEditable; } @ApiProperty() @@ -71,4 +73,7 @@ export class UserResponse { @ApiProperty() outdatedSince?: Date; + + @ApiProperty() + isEditable?: boolean; } diff --git a/apps/server/src/modules/user/legacy/repo/helper/aggregation-helper.ts b/apps/server/src/modules/user/legacy/repo/helper/aggregation-helper.ts index 229cd34e8d0..9aa07181143 100644 --- a/apps/server/src/modules/user/legacy/repo/helper/aggregation-helper.ts +++ b/apps/server/src/modules/user/legacy/repo/helper/aggregation-helper.ts @@ -374,11 +374,47 @@ export const createMultiDocumentAggregation = ({ const selectSortDiff = Object.getOwnPropertyNames(sort || {}).filter((s) => !select.includes(s)); const aggregation = []; + aggregation.push( + // @ts-ignore + { + $lookup: { + from: 'accounts', + let: { user_id: '$_id' }, + pipeline: [{ $match: { $expr: { $eq: ['$userId', '$$user_id'] } } }, { $project: { systemId: 1, _id: 0 } }], + as: 'account', + }, + }, + { + $unwind: { + path: '$account', + preserveNullAndEmptyArrays: true, + }, + }, + { + $addFields: { + systemId: '$account.systemId', + }, + }, + { + $project: { + account: 0, + }, + } + ); + + // @ts-ignore + aggregation.push({ + $unwind: { + path: '$account', + preserveNullAndEmptyArrays: true, + }, + }); + if (searchQuery) { // to sort by this value, add 'searchQuery' to sort value // @ts-ignore match.$text = { - $search: searchQuery + $search: searchQuery, }; } diff --git a/apps/server/src/modules/user/legacy/repo/users-admin.repo.ts b/apps/server/src/modules/user/legacy/repo/users-admin.repo.ts index 3ea72b8b399..7cd25548f24 100644 --- a/apps/server/src/modules/user/legacy/repo/users-admin.repo.ts +++ b/apps/server/src/modules/user/legacy/repo/users-admin.repo.ts @@ -3,9 +3,9 @@ import { User } from '@shared/domain/entity'; import { EntityId } from '@shared/domain/types'; import { BaseRepo } from '@shared/repo/base.repo'; import { ObjectId } from 'bson'; -import { createMultiDocumentAggregation, SearchQueryHelper } from './helper'; import { UsersSearchQueryParams } from '../controller/dto'; import { UserSearchQuery } from '../interfaces'; +import { createMultiDocumentAggregation, SearchQueryHelper } from './helper'; @Injectable() export class UsersAdminRepo extends BaseRepo { @@ -71,6 +71,7 @@ export class UsersAdminRepo extends BaseRepo { 'preferences.registrationMailSend', 'lastLoginSystemChange', 'outdatedSince', + 'systemId', ], skip: params?.$skip ?? params?.skip, limit: params?.$limit ?? params?.limit, diff --git a/apps/server/src/modules/user/legacy/service/users-admin.service.ts b/apps/server/src/modules/user/legacy/service/users-admin.service.ts index ca8eecc1989..2c2ad0bc19d 100644 --- a/apps/server/src/modules/user/legacy/service/users-admin.service.ts +++ b/apps/server/src/modules/user/legacy/service/users-admin.service.ts @@ -1,10 +1,10 @@ import { Injectable } from '@nestjs/common'; +import { EntityNotFoundError } from '@shared/common'; +import { User } from '@shared/domain/entity'; import { EntityId } from '@shared/domain/types'; import { Logger } from '@src/core/logger'; -import { User } from '@shared/domain/entity'; -import { EntityNotFoundError } from '@shared/common'; -import { UsersAdminRepo } from '../repo'; import { UserListResponse, UserResponse, UsersSearchQueryParams } from '../controller/dto'; +import { UsersAdminRepo } from '../repo'; @Injectable() export class UsersAdminService { @@ -18,13 +18,17 @@ export class UsersAdminService { schoolYearId: EntityId | undefined, params: UsersSearchQueryParams ): Promise { - const usersResponse = (await this.usersAdminRepo.getUsersWithNestedData( - roleId, - schoolId, - schoolYearId, - params - )) as UserListResponse[]; - return new UserListResponse(usersResponse[0]); + const response = await this.usersAdminRepo.getUsersWithNestedData(roleId, schoolId, schoolYearId, params); + + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment + response[0].data = response[0].data.map((user) => { + // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access + return { ...user, isEditable: !!user.systemId }; + }); + + const userResponse = response[0] as UserListResponse; + + return new UserListResponse(userResponse); } async getUserWithNestedData(