Skip to content

Commit

Permalink
BC - 6386 Upgrade mikro-orm to 5.6.16 (#4727)
Browse files Browse the repository at this point in the history
* updated microORM

* added date type in base.entity and submission-container-element

* declared boolean fields in file-entity

* declared preferences as object

* added date-declaration to User Entity

* defined Date and boolean Fields

* type removed to decorator

* declared boolean in entities

---------

Co-authored-by: virgilchiriac <virgil.chiriac@dataport.de>
  • Loading branch information
wolfganggreschus and virgilchiriac authored Feb 15, 2024
1 parent e26897b commit 1623572
Show file tree
Hide file tree
Showing 17 changed files with 121 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { Test, TestingModule } from '@nestjs/testing';
import { BoardExternalReferenceType } from '@shared/domain/domainobject';
import { CardNode } from '@shared/domain/entity';
import {
TestApiClient,
UserAndAccountTestFactory,
cardNodeFactory,
cleanupCollections,
columnBoardNodeFactory,
columnNodeFactory,
courseFactory,
TestApiClient,
UserAndAccountTestFactory,
} from '@shared/testing';

const baseRouteName = '/cards';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { BoardExternalReferenceType, ContentElementType } from '@shared/domain/d
import { FileElementNode, RichTextElementNode, SubmissionContainerElementNode } from '@shared/domain/entity';
import { InputFormat } from '@shared/domain/types';
import {
TestApiClient,
UserAndAccountTestFactory,
cardNodeFactory,
cleanupCollections,
columnBoardNodeFactory,
Expand All @@ -17,6 +15,8 @@ import {
fileElementNodeFactory,
richTextElementNodeFactory,
submissionContainerElementNodeFactory,
TestApiClient,
UserAndAccountTestFactory,
} from '@shared/testing';

describe(`content element update content (api)`, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
CardNode,
ColumnBoardNode,
ColumnNode,
DrawingElementNode,
ExternalToolElementNodeEntity,
FileElementNode,
LinkElementNode,
RichTextElementNode,
DrawingElementNode,
SubmissionContainerElementNode,
SubmissionItemNode,
} from '@shared/domain/entity';
Expand All @@ -19,12 +19,12 @@ import {
columnBoardNodeFactory,
columnFactory,
contextExternalToolEntityFactory,
drawingElementFactory,
externalToolElementFactory,
fileElementFactory,
linkElementFactory,
richTextElementFactory,
setupEntities,
drawingElementFactory,
submissionContainerElementFactory,
submissionItemFactory,
} from '@shared/testing';
Expand Down Expand Up @@ -265,6 +265,7 @@ describe(RecursiveSaveVisitor.name, () => {
it('should persist the board node', () => {
const board = columnBoardFactory.build();
const boardNode = columnBoardNodeFactory.build();

em.getUnitOfWork().getById.mockReturnValue(boardNode);

visitor.visitColumnBoard(board);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export class FileSecurityCheckEntity {
@Property()
requestToken?: string = uuid();

@Property()
@Property({ type: Date })
createdAt = new Date();

@Property()
@Property({ type: Date, onUpdate: () => new Date() })
updatedAt = new Date();

constructor(props: FileSecurityCheckEntityProps) {
Expand Down
6 changes: 4 additions & 2 deletions apps/server/src/modules/files/entity/file.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ export class FileEntity extends BaseEntityWithTimestamps {
@Property({ nullable: true })
deletedAt?: Date;

@Property()
// you have to set the type explicitly to boolean, otherwise metadata will be wrong
@Property({ type: 'boolean' })
deleted = false;

@Property()
// you have to set the type explicitly to boolean, otherwise metadata will be wrong
@Property({ type: 'boolean' })
isDirectory = false;

@Property()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SanisResponse, schulconnexResponseFactory } from '@infra/schulconnex-client';
import { EntityManager, ObjectId } from '@mikro-orm/mongodb';
import { OauthTokenResponse } from '@modules/oauth/service/dto';
import { ServerTestModule } from '@modules/server/server.module';
import {
FilterImportUserParams,
Expand Down Expand Up @@ -36,7 +37,6 @@ import {
} from '@shared/testing';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import { OauthTokenResponse } from '@modules/oauth/service/dto';
import { IUserImportFeatures, UserImportFeatures } from '../../config';

describe('ImportUser Controller (API)', () => {
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/shared/domain/entity/base.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ export abstract class BaseEntityWithTimestamps<Optional = never> implements Auth
@SerializedPrimaryKey()
id!: string;

@Property()
@Property({ type: Date })
createdAt = new Date();

@Property({ onUpdate: () => new Date() })
@Property({ type: Date, onUpdate: () => new Date() })
updatedAt = new Date();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BoardDoBuilder, BoardNodeType } from './types';

@Entity({ discriminatorValue: BoardNodeType.SUBMISSION_CONTAINER_ELEMENT })
export class SubmissionContainerElementNode extends BoardNode {
@Property({ nullable: true })
@Property({ type: Date, nullable: true })
dueDate: Date | null;

constructor(props: SubmissionContainerNodeProps) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class ColumnBoardTarget extends BaseEntityWithTimestamps implements Learn
this.published = false;
}

@Property()
@Property({ type: 'boolean' })
published = false;

@Property({ fieldName: 'columnBoard' })
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/shared/domain/entity/lesson.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class LessonEntity extends BaseEntityWithTimestamps implements LearnroomE
name: string;

@Index()
@Property()
@Property({ type: 'boolean' })
hidden = false;

@Index()
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/shared/domain/entity/task.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class Task extends BaseEntityWithTimestamps implements LearnroomElement,
@Index()
dueDate?: Date;

@Property()
@Property({ type: 'boolean' })
private = true;

@Property({ nullable: true })
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/shared/domain/entity/user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Collection, Embedded, Entity, Index, ManyToMany, ManyToOne, Property } from '@mikro-orm/core';
import { EntityWithSchool } from '../interface';
import { EntityId } from '../types';
import { BaseEntityWithTimestamps } from './base.entity';
import { Role } from './role.entity';
import { SchoolEntity } from './school.entity';
import { UserParentsEntity } from './user-parents.entity';
import { EntityId } from '../types';

export enum LanguageType {
DE = 'de',
Expand Down Expand Up @@ -96,7 +96,7 @@ export class User extends BaseEntityWithTimestamps implements EntityWithSchool {
@Property({ nullable: true })
forcePasswordChange?: boolean;

@Property({ nullable: true })
@Property({ type: 'object', nullable: true })
preferences?: Record<string, unknown>;

@Property({ nullable: true })
Expand Down
53 changes: 24 additions & 29 deletions apps/server/src/shared/repo/user/user.repo.integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ describe('user repo', () => {
};
};

it('should return empty array', async () => {
it('should return zero', async () => {
const { user } = setup();

const result = await repo.deleteUser(user.id);
Expand All @@ -476,47 +476,42 @@ describe('user repo', () => {
const user2: User = userFactory.buildWithId();
const user3: User = userFactory.buildWithId();
await em.persistAndFlush([user1, user2, user3]);

const expectedUser2 = {
firstName: user2.firstName,
lastName: user2.lastName,
email: user2.email,
roles: user2.roles,
school: user2.school,
};

const expectedUser3 = {
firstName: user3.firstName,
lastName: user3.lastName,
email: user3.email,
roles: user3.roles,
school: user3.school,
};

const expectedResult = 1;
em.clear();

return {
expectedResult,
expectedUser2,
expectedUser3,
user1,
user2,
user3,
};
};
it('should delete user', async () => {
const { expectedResult, expectedUser2, expectedUser3, user1, user2, user3 } = await setup();
const deleteResult = await repo.deleteUser(user1.id);
expect(deleteResult).toEqual(expectedResult);
const { user1 } = await setup();
await repo.deleteUser(user1.id);

const result1 = await em.find(User, { id: user1.id });
expect(result1).toHaveLength(0);
});

it('should return one deleted user', async () => {
const { user1 } = await setup();
const result = await repo.deleteUser(user1.id);
expect(result).toEqual(1);
});

it('should not affect other users', async () => {
const { user1, user2, user3 } = await setup();
await repo.deleteUser(user1.id);

const emUser2 = await em.find(User, { id: user2.id });
const emUser3 = await em.find(User, { id: user3.id });
expect(emUser2).toHaveLength(1);
expect(emUser3).toHaveLength(1);

const result2 = await repo.findById(user2.id);
expect(result2).toMatchObject(expectedUser2);
const resultUser2 = await repo.findById(user2.id);
const resultUser3 = await repo.findById(user3.id);

const result3 = await repo.findById(user3.id);
expect(result3).toMatchObject(expectedUser3);
expect(resultUser2.id).toEqual(user2.id);
expect(resultUser3.id).toEqual(user3.id);
});
});
});
Expand Down
8 changes: 7 additions & 1 deletion apps/server/src/shared/repo/user/user.repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,13 @@ export class UserRepo extends BaseRepo<User> {

const userDocuments = await this._em.aggregate(User, pipeline);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const users = userDocuments.map((userDocument) => this._em.map(User, userDocument));
const users = userDocuments.map((userDocument) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const { createdAt, updatedAt, ...newUserDocument } = userDocument;

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return this._em.map(User, newUserDocument);
});
await this._em.populate(users, ['roles']);
return [users, count];
}
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/shared/testing/factory/base.factory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ObjectId } from '@mikro-orm/mongodb';
import { BuildOptions, DeepPartial, Factory, GeneratorFn, HookFn } from 'fishery';
import { ObjectId } from 'mongodb';

/**
* Entity factory based on thoughtbot/fishery
Expand Down
Loading

0 comments on commit 1623572

Please sign in to comment.