From 6af96c0b72ac495ecf640014a9fe6e9a5280e418 Mon Sep 17 00:00:00 2001 From: Phillip Wirth Date: Tue, 22 Oct 2024 15:01:40 +0200 Subject: [PATCH 1/4] BC-8293 context-external-tools.contextId from string to ObjectId --- .../mikro-orm/Migration20241022205656.ts | 17 ++++++++++++++++ .../api-test/tool-context.api.spec.ts | 20 ++++++++++--------- .../entity/context-external-tool.entity.ts | 7 ++++--- .../context-external-tool.repo.spec.ts | 10 +++++----- .../context-external-tool.repo.ts | 2 +- .../context-external-tool.scope.ts | 3 ++- backup/setup/context-external-tools.json | 4 ++-- backup/setup/migrations.json | 9 +++++++++ 8 files changed, 51 insertions(+), 21 deletions(-) create mode 100644 apps/server/src/migrations/mikro-orm/Migration20241022205656.ts diff --git a/apps/server/src/migrations/mikro-orm/Migration20241022205656.ts b/apps/server/src/migrations/mikro-orm/Migration20241022205656.ts new file mode 100644 index 00000000000..912ae18523c --- /dev/null +++ b/apps/server/src/migrations/mikro-orm/Migration20241022205656.ts @@ -0,0 +1,17 @@ +import { Migration } from '@mikro-orm/migrations-mongodb'; + +export class Migration20241022205656 extends Migration { + async up(): Promise { + await this.driver.nativeUpdate('context-external-tools', { contextId: { $type: 'string' } }, [ + { $set: { contextId: { $toObjectId: '$contextId' } } }, + ]); + } + + async down(): Promise { + await this.driver.nativeUpdate('context-external-tools', { contextId: { $type: 'objectId' } }, [ + { + $set: { contextId: { $toString: '$contextId' } }, + }, + ]); + } +} diff --git a/apps/server/src/modules/tool/context-external-tool/controller/api-test/tool-context.api.spec.ts b/apps/server/src/modules/tool/context-external-tool/controller/api-test/tool-context.api.spec.ts index 54a366a5f25..0af81812244 100644 --- a/apps/server/src/modules/tool/context-external-tool/controller/api-test/tool-context.api.spec.ts +++ b/apps/server/src/modules/tool/context-external-tool/controller/api-test/tool-context.api.spec.ts @@ -310,7 +310,7 @@ describe('ToolContextController (API)', () => { expect(result.statusCode).toEqual(HttpStatus.NO_CONTENT); const deleted: ContextExternalToolEntity | null = await em.findOne(ContextExternalToolEntity, { - contextId: contextExternalToolEntity.id, + contextId: new ObjectId(contextExternalToolEntity.id), }); expect(deleted).toBeNull(); @@ -433,7 +433,7 @@ describe('ToolContextController (API)', () => { await setup(); const response = await loggedInClient.get( - `${contextExternalTool1.contextType}/${contextExternalTool1.contextId}` + `${contextExternalTool1.contextType}/${contextExternalTool1.contextId.toHexString()}` ); expect(response.status).toEqual(HttpStatus.OK); @@ -448,7 +448,7 @@ describe('ToolContextController (API)', () => { ], id: contextExternalTool1.id, schoolToolId: contextExternalTool1.schoolTool.id, - contextId: contextExternalTool1.contextId, + contextId: contextExternalTool1.contextId.toHexString(), contextType: ToolContextType.COURSE, displayName: contextExternalTool1.displayName, }, @@ -461,7 +461,7 @@ describe('ToolContextController (API)', () => { ], id: contextExternalTool2.id, schoolToolId: contextExternalTool2.schoolTool.id, - contextId: contextExternalTool2.contextId, + contextId: contextExternalTool2.contextId.toHexString(), contextType: ToolContextType.COURSE, displayName: contextExternalTool2.displayName, }, @@ -478,7 +478,7 @@ describe('ToolContextController (API)', () => { ], id: contextExternalToolFromOtherSchool.id, schoolToolId: contextExternalToolFromOtherSchool.schoolTool.id, - contextId: contextExternalToolFromOtherSchool.contextId, + contextId: contextExternalToolFromOtherSchool.contextId.toHexString(), contextType: ToolContextType.COURSE, displayName: contextExternalToolFromOtherSchool.displayName, }, @@ -491,7 +491,7 @@ describe('ToolContextController (API)', () => { const { contextExternalTool1 } = await setup(); const response = await testApiClient.get( - `${contextExternalTool1.contextType}/${contextExternalTool1.contextId}` + `${contextExternalTool1.contextType}/${contextExternalTool1.contextId.toHexString()}` ); expect(response.statusCode).toEqual(HttpStatus.UNAUTHORIZED); @@ -503,7 +503,7 @@ describe('ToolContextController (API)', () => { const { contextExternalTool1, otherLoggedInClient } = await setup(); const response = await otherLoggedInClient.get( - `${contextExternalTool1.contextType}/${contextExternalTool1.contextId}` + `${contextExternalTool1.contextType}/${contextExternalTool1.contextId.toHexString()}` ); expect(response.status).toEqual(HttpStatus.OK); @@ -568,7 +568,7 @@ describe('ToolContextController (API)', () => { expect(response.status).toEqual(HttpStatus.OK); expect(response.body).toEqual({ schoolToolId: contextExternalTool.schoolTool.id, - contextId: contextExternalTool.contextId, + contextId: contextExternalTool.contextId.toHexString(), contextType: ToolContextType.COURSE, id: contextExternalTool.id, displayName: contextExternalTool.displayName, @@ -638,7 +638,9 @@ describe('ToolContextController (API)', () => { it('should return unauthorized', async () => { const { contextExternalTool } = await setup(); - const response = await testApiClient.get(`${contextExternalTool.contextType}/${contextExternalTool.contextId}`); + const response = await testApiClient.get( + `${contextExternalTool.contextType}/${contextExternalTool.contextId.toHexString()}` + ); expect(response.status).toEqual(HttpStatus.UNAUTHORIZED); }); diff --git a/apps/server/src/modules/tool/context-external-tool/entity/context-external-tool.entity.ts b/apps/server/src/modules/tool/context-external-tool/entity/context-external-tool.entity.ts index 0077295ce8c..b43b30b4cbe 100644 --- a/apps/server/src/modules/tool/context-external-tool/entity/context-external-tool.entity.ts +++ b/apps/server/src/modules/tool/context-external-tool/entity/context-external-tool.entity.ts @@ -1,6 +1,7 @@ import { Embedded, Entity, ManyToOne, Property } from '@mikro-orm/core'; import { BaseEntityWithTimestamps } from '@shared/domain/entity/base.entity'; import { EntityId } from '@shared/domain/types'; +import { ObjectId } from '@mikro-orm/mongodb'; import { CustomParameterEntryEntity } from '../../common/entity'; import { SchoolExternalToolEntity } from '../../school-external-tool/entity'; import { ContextExternalToolType } from './context-external-tool-type.enum'; @@ -10,7 +11,7 @@ export interface ContextExternalToolEntityProps { schoolTool: SchoolExternalToolEntity; - contextId: string; + contextId: EntityId | ObjectId; contextType: ContextExternalToolType; @@ -25,7 +26,7 @@ export class ContextExternalToolEntity extends BaseEntityWithTimestamps { schoolTool: SchoolExternalToolEntity; @Property() - contextId: string; + contextId: ObjectId; @Property() contextType: ContextExternalToolType; @@ -42,7 +43,7 @@ export class ContextExternalToolEntity extends BaseEntityWithTimestamps { this.id = props.id; } this.schoolTool = props.schoolTool; - this.contextId = props.contextId; + this.contextId = new ObjectId(props.contextId); this.contextType = props.contextType; this.displayName = props.displayName; this.parameters = props.parameters ?? []; diff --git a/apps/server/src/shared/repo/contextexternaltool/context-external-tool.repo.spec.ts b/apps/server/src/shared/repo/contextexternaltool/context-external-tool.repo.spec.ts index 1334823da8a..a17efc3df59 100644 --- a/apps/server/src/shared/repo/contextexternaltool/context-external-tool.repo.spec.ts +++ b/apps/server/src/shared/repo/contextexternaltool/context-external-tool.repo.spec.ts @@ -258,13 +258,13 @@ describe(ContextExternalToolRepo.name, () => { const query: ContextExternalToolQuery = { context: { - id: contextExternalTool1.contextId, + id: contextExternalTool1.contextId.toHexString(), }, }; const result: ContextExternalTool[] = await repo.find(query); - expect(result[0].contextRef.id).toEqual(contextExternalTool1.contextId); + expect(result[0].contextRef.id).toEqual(contextExternalTool1.contextId.toHexString()); }); }); @@ -303,7 +303,7 @@ describe(ContextExternalToolRepo.name, () => { const query: ContextExternalToolQuery = { context: { - id: contextExternalTool1.contextId, + id: contextExternalTool1.contextId.toHexString(), }, }; @@ -373,7 +373,7 @@ describe(ContextExternalToolRepo.name, () => { expect(result.getProps()).toEqual({ id: contextExternalTool.id, contextRef: { - id: contextExternalTool.contextId, + id: contextExternalTool.contextId.toHexString(), type: ToolContextType.COURSE, }, displayName: contextExternalTool.displayName, @@ -417,7 +417,7 @@ describe(ContextExternalToolRepo.name, () => { expect(result?.getProps()).toEqual({ id: contextExternalTool.id, contextRef: { - id: contextExternalTool.contextId, + id: contextExternalTool.contextId.toHexString(), type: ToolContextType.COURSE, }, displayName: contextExternalTool.displayName, diff --git a/apps/server/src/shared/repo/contextexternaltool/context-external-tool.repo.ts b/apps/server/src/shared/repo/contextexternaltool/context-external-tool.repo.ts index 2b3b0a7a551..a5bd792ca85 100644 --- a/apps/server/src/shared/repo/contextexternaltool/context-external-tool.repo.ts +++ b/apps/server/src/shared/repo/contextexternaltool/context-external-tool.repo.ts @@ -136,7 +136,7 @@ export class ContextExternalToolRepo { }); const contextRef: ContextRef = new ContextRef({ - id: entity.contextId, + id: entity.contextId.toHexString(), type: this.mapContextTypeToDomainObjectType(entity.contextType), }); diff --git a/apps/server/src/shared/repo/contextexternaltool/context-external-tool.scope.ts b/apps/server/src/shared/repo/contextexternaltool/context-external-tool.scope.ts index b481f31b458..6b3eaa09037 100644 --- a/apps/server/src/shared/repo/contextexternaltool/context-external-tool.scope.ts +++ b/apps/server/src/shared/repo/contextexternaltool/context-external-tool.scope.ts @@ -2,6 +2,7 @@ import { ToolContextType } from '@modules/tool/common/enum'; import { ContextExternalToolEntity } from '@modules/tool/context-external-tool/entity'; import { EntityId } from '@shared/domain/types'; import { Scope } from '@shared/repo'; +import { ObjectId } from '@mikro-orm/mongodb'; export class ContextExternalToolScope extends Scope { byId(id: EntityId | undefined): ContextExternalToolScope { @@ -21,7 +22,7 @@ export class ContextExternalToolScope extends Scope { byContextId(contextId: EntityId | undefined): ContextExternalToolScope { if (contextId !== undefined) { - this.addQuery({ contextId }); + this.addQuery({ contextId: new ObjectId(contextId) }); } return this; diff --git a/backup/setup/context-external-tools.json b/backup/setup/context-external-tools.json index 1c1c03b942f..2b6ed119fbe 100644 --- a/backup/setup/context-external-tools.json +++ b/backup/setup/context-external-tools.json @@ -12,7 +12,7 @@ "schoolTool": { "$oid": "644a46e5d0a8301e6cf25d86" }, - "contextId": "0000dcfbfb5c7a3f00bf21ab", + "contextId": { "$oid" : "0000dcfbfb5c7a3f00bf21ab" }, "contextType": "course", "displayName": "Google-Suche", "parameters": [ @@ -26,7 +26,7 @@ "_id": { "$oid": "647de3cfab79fd5bd57e68f4" }, - "contextId": "0000dcfbfb5c7a3f00bf21ab", + "contextId": { "$oid" : "0000dcfbfb5c7a3f00bf21ab" }, "contextType": "course", "parameters": [], "schoolTool": { diff --git a/backup/setup/migrations.json b/backup/setup/migrations.json index 225324cd518..a5e10452d6a 100644 --- a/backup/setup/migrations.json +++ b/backup/setup/migrations.json @@ -232,5 +232,14 @@ "created_at": { "$date": "2024-10-02T20:12:54.209Z" } + }, + { + "_id": { + "$oid": "66fda9462a63b5749b3a64c9" + }, + "name": "Migration20240115103302", + "created_at": { + "$date": "2024-10-22T01:12:54.209Z" + } } ] From 82811c06857dc206d6b4f1bcc5a23c2db25117b5 Mon Sep 17 00:00:00 2001 From: Phillip Wirth Date: Tue, 22 Oct 2024 16:51:21 +0200 Subject: [PATCH 2/4] added migration --- .../mikro-orm/Migration20241022205656.ts | 32 +++++++++++++++---- backup/setup/migrations.json | 9 ------ 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/apps/server/src/migrations/mikro-orm/Migration20241022205656.ts b/apps/server/src/migrations/mikro-orm/Migration20241022205656.ts index 912ae18523c..072c8f0711c 100644 --- a/apps/server/src/migrations/mikro-orm/Migration20241022205656.ts +++ b/apps/server/src/migrations/mikro-orm/Migration20241022205656.ts @@ -2,16 +2,36 @@ import { Migration } from '@mikro-orm/migrations-mongodb'; export class Migration20241022205656 extends Migration { async up(): Promise { - await this.driver.nativeUpdate('context-external-tools', { contextId: { $type: 'string' } }, [ - { $set: { contextId: { $toObjectId: '$contextId' } } }, - ]); + await this.getCollection('context-external-tools').updateMany( + { + contextId: { $type: 'string' }, + }, + [ + { + $set: { + contextId: { + $toObjectId: '$contextId', + }, + }, + }, + ] + ); } async down(): Promise { - await this.driver.nativeUpdate('context-external-tools', { contextId: { $type: 'objectId' } }, [ + await this.getCollection('context-external-tools').updateMany( { - $set: { contextId: { $toString: '$contextId' } }, + contextId: { $type: 'objectId' }, }, - ]); + [ + { + $set: { + contextId: { + $toString: '$contextId', + }, + }, + }, + ] + ); } } diff --git a/backup/setup/migrations.json b/backup/setup/migrations.json index a5e10452d6a..225324cd518 100644 --- a/backup/setup/migrations.json +++ b/backup/setup/migrations.json @@ -232,14 +232,5 @@ "created_at": { "$date": "2024-10-02T20:12:54.209Z" } - }, - { - "_id": { - "$oid": "66fda9462a63b5749b3a64c9" - }, - "name": "Migration20240115103302", - "created_at": { - "$date": "2024-10-22T01:12:54.209Z" - } } ] From ab1d7e70a851b821e499956caa7723ec02f08b25 Mon Sep 17 00:00:00 2001 From: Phillip Wirth Date: Tue, 22 Oct 2024 17:23:05 +0200 Subject: [PATCH 3/4] might be happy now --- backup/setup/migrations.json | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/backup/setup/migrations.json b/backup/setup/migrations.json index 225324cd518..d8778bc0d98 100644 --- a/backup/setup/migrations.json +++ b/backup/setup/migrations.json @@ -232,5 +232,14 @@ "created_at": { "$date": "2024-10-02T20:12:54.209Z" } + }, + { + "_id": { + "$oid": "6717bba2b08d6ccb1dd5db60" + }, + "name": "Migration20241022205656", + "created_at": { + "$date": "2024-10-22T14:50:10.445Z" + } } ] From d2444f5f1a787af0b8dac3608aef49c11ee56836 Mon Sep 17 00:00:00 2001 From: Phillip Wirth Date: Tue, 22 Oct 2024 17:38:48 +0200 Subject: [PATCH 4/4] fixed test --- .../entity/context-external-tool.entity.spec.ts | 2 +- .../contextexternaltool/context-external-tool.scope.spec.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/apps/server/src/modules/tool/context-external-tool/entity/context-external-tool.entity.spec.ts b/apps/server/src/modules/tool/context-external-tool/entity/context-external-tool.entity.spec.ts index b04b7c1ee10..7ffec395d88 100644 --- a/apps/server/src/modules/tool/context-external-tool/entity/context-external-tool.entity.spec.ts +++ b/apps/server/src/modules/tool/context-external-tool/entity/context-external-tool.entity.spec.ts @@ -33,7 +33,7 @@ describe(ExternalToolEntity.name, () => { const contextExternalToolEntity: ContextExternalToolEntity = new ContextExternalToolEntity({ id: new ObjectId().toHexString(), schoolTool: schoolExternalToolEntityFactory.buildWithId(), - contextId: 'mockContextId', + contextId: new ObjectId(), contextType: ContextExternalToolType.MEDIA_BOARD, parameters: [], }); diff --git a/apps/server/src/shared/repo/contextexternaltool/context-external-tool.scope.spec.ts b/apps/server/src/shared/repo/contextexternaltool/context-external-tool.scope.spec.ts index 1083e3a85df..4143a6da892 100644 --- a/apps/server/src/shared/repo/contextexternaltool/context-external-tool.scope.spec.ts +++ b/apps/server/src/shared/repo/contextexternaltool/context-external-tool.scope.spec.ts @@ -1,6 +1,7 @@ import { ToolContextType } from '@modules/tool/common/enum'; import { SchoolExternalToolEntity } from '@modules/tool/school-external-tool/entity'; import { schoolExternalToolEntityFactory } from '@modules/tool/school-external-tool/testing'; +import { ObjectId } from '@mikro-orm/mongodb'; import { ContextExternalToolScope } from './context-external-tool.scope'; describe('CourseExternalToolScope', () => { @@ -63,7 +64,7 @@ describe('CourseExternalToolScope', () => { scope.byContextId(schoolExternalToolEntity.id); - expect(scope.query).toEqual({ contextId: schoolExternalToolEntity.id }); + expect(scope.query).toEqual({ contextId: new ObjectId(schoolExternalToolEntity.id) }); }); }); });