Skip to content

Commit

Permalink
fix specs
Browse files Browse the repository at this point in the history
  • Loading branch information
uidp committed Nov 17, 2023
1 parent a4ffcb0 commit 46f1b1e
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,7 @@ describe('[ImportUserModule]', () => {
const officialSchoolNumber = school ? school.officialSchoolNumber : undefined;
const inMaintenanceSince = school ? school.inMaintenanceSince : undefined;
const inUserMigration = school ? school.inUserMigration : undefined;
const systems =
school && school.systems.isInitialized()
? school.systems.getItems().map((system: SystemEntity) => system.id)
: [];
const systems = school ? school.systems : [];
const federalState = school ? school.federalState : federalStateFactory.build();

return new LegacySchoolDo({
Expand Down
35 changes: 19 additions & 16 deletions apps/server/src/shared/domain/entity/school.entity.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import {
Collection,
Embeddable,
Embedded,
Entity,
Index,
ManyToMany,
ManyToOne,
OneToOne,
Property,
} from '@mikro-orm/core';
import { Embeddable, Embedded, Entity, Index, ManyToOne, OneToOne, Property } from '@mikro-orm/core';
import { UserLoginMigrationEntity } from '@shared/domain/entity/user-login-migration.entity';
import { ObjectId } from 'bson';
import { EntityId } from '../types';
import { BaseEntity } from './base.entity';
import { FederalStateEntity } from './federal-state.entity';
import { SchoolYearEntity } from './schoolyear.entity';
import { SystemEntity } from './system.entity';
import { FederalStateEntity } from './federal-state.entity';

export enum SchoolFeatures {
ROCKET_CHAT = 'rocketChat',
Expand Down Expand Up @@ -60,7 +52,7 @@ export class SchoolRoles {
}

@Entity({ tableName: 'schools' })
@Index({ properties: ['externalId', 'systems'] })
@Index({ properties: ['externalId', '_systems'] })
export class SchoolEntity extends BaseEntity {
@Property({ nullable: true })
features?: SchoolFeatures[];
Expand All @@ -83,8 +75,19 @@ export class SchoolEntity extends BaseEntity {
@Property({ nullable: true })
officialSchoolNumber?: string;

@ManyToMany(() => SystemEntity, undefined, { fieldName: 'systems' })
systems = new Collection<SystemEntity>(this);
@Property({ fieldName: 'systems' })
_systems: ObjectId[] = [];

get systems() {
return this._systems.map((s) => s.toHexString());
}

set systems(systemIds: EntityId[]) {
this._systems = systemIds.map((sid) => new ObjectId(sid));
}

// @ManyToMany(() => SystemEntity, undefined, { fieldName: 'systems' })
// systems = new Collection<SystemEntity>(this);

@Embedded(() => SchoolRoles, { object: true, nullable: true, prefix: false })
permissions?: SchoolRoles;
Expand Down Expand Up @@ -123,7 +126,7 @@ export class SchoolEntity extends BaseEntity {
this.officialSchoolNumber = props.officialSchoolNumber;
}
if (props.systems) {
this.systems.set(props.systems);
this.systems = props.systems.map((s) => s.id);
}
if (props.features) {
this.features = props.features;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ISchoolProperties,
LegacySchoolDo,
SchoolEntity,
SchoolFeatures,
SchoolRolePermission,
SchoolRoles,
SchoolYearEntity,
Expand Down Expand Up @@ -78,6 +79,21 @@ describe('LegacySchoolRepo', () => {
expect(result).toMatchObject(expected);
expect(result.id).toBeDefined();
});

it('should successfully save school after adding a system', async () => {
const system: SystemEntity = systemFactory.buildWithId();
const schoolEntity: SchoolEntity = schoolFactory.build({ externalId: 'externalId' });
schoolEntity.systems = [system.id];

await em.persistAndFlush(schoolEntity);
em.clear();

const school = await repo.findById(schoolEntity.id);

school.features = [SchoolFeatures.NEXTCLOUD];

await expect(repo.save(school)).resolves.not.toThrow();
});
});
});

Expand Down Expand Up @@ -116,13 +132,13 @@ describe('LegacySchoolRepo', () => {
it('should find school by external ID', async () => {
const system: SystemEntity = systemFactory.buildWithId();
const schoolEntity: SchoolEntity = schoolFactory.build({ externalId: 'externalId' });
schoolEntity.systems.add(system);
schoolEntity.systems = [system.id];

await em.persistAndFlush(schoolEntity);
await em.persistAndFlush([system, schoolEntity]);

const result: LegacySchoolDo | null = await repo.findByExternalId(
schoolEntity.externalId as string,
schoolEntity.systems[0].id
schoolEntity.systems[0]
);

expect(result?.externalId).toEqual(schoolEntity.externalId);
Expand Down
9 changes: 6 additions & 3 deletions apps/server/src/shared/repo/school/legacy-school.repo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EntityName } from '@mikro-orm/core';
import { EntityManager } from '@mikro-orm/mongodb';
import { EntityManager, ObjectId } from '@mikro-orm/mongodb';
import { Injectable, InternalServerErrorException } from '@nestjs/common';
import {
EntityId,
Expand All @@ -26,7 +26,10 @@ export class LegacySchoolRepo extends BaseDORepo<LegacySchoolDo, SchoolEntity, I
}

async findByExternalId(externalId: string, systemId: string): Promise<LegacySchoolDo | null> {
const school: SchoolEntity | null = await this._em.findOne(SchoolEntity, { externalId, systems: systemId });
const school: SchoolEntity | null = await this._em.findOne(SchoolEntity, {
externalId,
_systems: { $in: [new ObjectId(systemId)] },
});

const schoolDo: LegacySchoolDo | null = school ? this.mapEntityToDO(school) : null;
return schoolDo;
Expand Down Expand Up @@ -57,7 +60,7 @@ export class LegacySchoolRepo extends BaseDORepo<LegacySchoolDo, SchoolEntity, I
previousExternalId: entity.previousExternalId,
officialSchoolNumber: entity.officialSchoolNumber,
schoolYear: entity.schoolYear,
systems: entity.systems.isInitialized() ? entity.systems.getItems().map((system: SystemEntity) => system.id) : [],
systems: entity.systems,
userLoginMigrationId: entity.userLoginMigration?.id,
federalState: entity.federalState,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ describe('UserRepo', () => {
beforeEach(async () => {
system = systemFactory.buildWithId();
school = schoolFactory.buildWithId();
school.systems.add(system);
school.systems = [system.id];
user = userFactory.buildWithId({ externalId, school });

await em.persistAndFlush([user, system, school]);
Expand All @@ -175,7 +175,7 @@ describe('UserRepo', () => {
});

it('should return null if school has no corresponding system', async () => {
school.systems.removeAll();
school.systems = [];

const result: UserDO | null = await repo.findByExternalId(user.externalId as string, system.id);

Expand All @@ -192,7 +192,7 @@ describe('UserRepo', () => {
beforeEach(async () => {
system = systemFactory.buildWithId();
school = schoolFactory.buildWithId();
school.systems.add(system);
school.systems = [system.id];
user = userFactory.buildWithId({ externalId, school });

await em.persistAndFlush([user, system, school]);
Expand Down
9 changes: 4 additions & 5 deletions apps/server/src/shared/repo/user/user-do.repo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EntityName, FilterQuery, QueryOrderMap } from '@mikro-orm/core';
import { UserQuery } from '@modules/user/service/user-query.type';
import { Injectable } from '@nestjs/common';
import { EntityNotFoundError } from '@shared/common';
import {
Expand All @@ -10,14 +11,12 @@ import {
SchoolEntity,
SortOrder,
SortOrderMap,
SystemEntity,
User,
} from '@shared/domain';
import { RoleReference } from '@shared/domain/domainobject';
import { Page } from '@shared/domain/domainobject/page';
import { UserDO } from '@shared/domain/domainobject/user.do';
import { BaseDORepo, Scope } from '@shared/repo';
import { UserQuery } from '@modules/user/service/user-query.type';
import { UserScope } from './user.scope';

@Injectable()
Expand Down Expand Up @@ -61,7 +60,7 @@ export class UserDORepo extends BaseDORepo<UserDO, User, IUserProperties> {
const userEntity: User = await this._em.findOneOrFail(this.entityName, id as FilterQuery<User>);

if (populate) {
await this._em.populate(userEntity, ['roles', 'school.systems', 'school.schoolYear']);
await this._em.populate(userEntity, ['roles', 'school._systems', 'school.schoolYear']);
await this.populateRoles(userEntity.roles.getItems());
}

Expand All @@ -77,10 +76,10 @@ export class UserDORepo extends BaseDORepo<UserDO, User, IUserProperties> {
}

async findByExternalId(externalId: string, systemId: string): Promise<UserDO | null> {
const userEntitys: User[] = await this._em.find(User, { externalId }, { populate: ['school.systems'] });
const userEntitys: User[] = await this._em.find(User, { externalId }, { populate: ['school._systems'] });
const userEntity: User | undefined = userEntitys.find((user: User): boolean => {
const { systems } = user.school;
return systems && !!systems.getItems().find((system: SystemEntity): boolean => system.id === systemId);
return systems && !!systems.find((system) => system === systemId);
});

const userDo: UserDO | null = userEntity ? this.mapEntityToDO(userEntity) : null;
Expand Down
6 changes: 3 additions & 3 deletions apps/server/src/shared/repo/user/user.repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ export class UserRepo extends BaseRepo<User> {
const user = await super.findById(id);

if (populate) {
await this._em.populate(user, ['roles', 'school.systems', 'school.schoolYear']);
await this._em.populate(user, ['roles', 'school._systems', 'school.schoolYear']);
await this.populateRoles(user.roles.getItems());
}

return user;
}

async findByExternalIdOrFail(externalId: string, systemId: string): Promise<User> {
const [users] = await this._em.findAndCount(User, { externalId }, { populate: ['school.systems'] });
const [users] = await this._em.findAndCount(User, { externalId }, { populate: ['school._systems'] });
const resultUser = users.find((user) => {
const { systems } = user.school;
return systems && systems.getItems().find((system) => system.id === systemId);
return systems && systems.find((system) => system === systemId);
});
return resultUser ?? Promise.reject();
}
Expand Down

0 comments on commit 46f1b1e

Please sign in to comment.