Skip to content

Commit

Permalink
Add is editable to response
Browse files Browse the repository at this point in the history
  • Loading branch information
bischofmax committed Apr 30, 2024
1 parent 30fbd28 commit 7cc9bb3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -17,6 +17,7 @@ export class UserResponse {
importHash,
lastLoginSystemChange,
outdatedSince,
isEditable,
}: UserResponse) {
this._id = _id.toString();
this.firstName = firstName;
Expand All @@ -31,6 +32,7 @@ export class UserResponse {
this.importHash = importHash;
this.lastLoginSystemChange = lastLoginSystemChange;
this.outdatedSince = outdatedSince;
this.isEditable = isEditable;
}

@ApiProperty()
Expand Down Expand Up @@ -71,4 +73,7 @@ export class UserResponse {

@ApiProperty()
outdatedSince?: Date;

@ApiProperty()
isEditable?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}

Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/modules/user/legacy/repo/users-admin.repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<User> {
Expand Down Expand Up @@ -71,6 +71,7 @@ export class UsersAdminRepo extends BaseRepo<User> {
'preferences.registrationMailSend',
'lastLoginSystemChange',
'outdatedSince',
'systemId',
],
skip: params?.$skip ?? params?.skip,
limit: params?.$limit ?? params?.limit,
Expand Down
24 changes: 14 additions & 10 deletions apps/server/src/modules/user/legacy/service/users-admin.service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -18,13 +18,17 @@ export class UsersAdminService {
schoolYearId: EntityId | undefined,
params: UsersSearchQueryParams
): Promise<UserListResponse> {
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(
Expand Down

0 comments on commit 7cc9bb3

Please sign in to comment.