Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BC-8293 context-external-tools.contextId from string to ObjectId #5308

Merged
merged 6 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions apps/server/src/migrations/mikro-orm/Migration20241022205656.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Migration } from '@mikro-orm/migrations-mongodb';

export class Migration20241022205656 extends Migration {
async up(): Promise<void> {
await this.getCollection('context-external-tools').updateMany(
{
contextId: { $type: 'string' },
},
[
{
$set: {
contextId: {
$toObjectId: '$contextId',
},
},
},
]
);
}

async down(): Promise<void> {
await this.getCollection('context-external-tools').updateMany(
{
contextId: { $type: 'objectId' },
},
[
{
$set: {
contextId: {
$toString: '$contextId',
},
},
},
]
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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,
},
Expand All @@ -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,
},
Expand All @@ -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,
},
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -10,7 +11,7 @@ export interface ContextExternalToolEntityProps {

schoolTool: SchoolExternalToolEntity;

contextId: string;
contextId: EntityId | ObjectId;

contextType: ContextExternalToolType;

Expand All @@ -25,7 +26,7 @@ export class ContextExternalToolEntity extends BaseEntityWithTimestamps {
schoolTool: SchoolExternalToolEntity;

@Property()
contextId: string;
contextId: ObjectId;

@Property()
contextType: ContextExternalToolType;
Expand All @@ -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 ?? [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
});

Expand Down Expand Up @@ -303,7 +303,7 @@ describe(ContextExternalToolRepo.name, () => {

const query: ContextExternalToolQuery = {
context: {
id: contextExternalTool1.contextId,
id: contextExternalTool1.contextId.toHexString(),
},
};

Expand Down Expand Up @@ -373,7 +373,7 @@ describe(ContextExternalToolRepo.name, () => {
expect(result.getProps()).toEqual<ContextExternalToolProps>({
id: contextExternalTool.id,
contextRef: {
id: contextExternalTool.contextId,
id: contextExternalTool.contextId.toHexString(),
type: ToolContextType.COURSE,
},
displayName: contextExternalTool.displayName,
Expand Down Expand Up @@ -417,7 +417,7 @@ describe(ContextExternalToolRepo.name, () => {
expect(result?.getProps()).toEqual<ContextExternalToolProps>({
id: contextExternalTool.id,
contextRef: {
id: contextExternalTool.contextId,
id: contextExternalTool.contextId.toHexString(),
type: ToolContextType.COURSE,
},
displayName: contextExternalTool.displayName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export class ContextExternalToolRepo {
});

const contextRef: ContextRef = new ContextRef({
id: entity.contextId,
id: entity.contextId.toHexString(),
type: this.mapContextTypeToDomainObjectType(entity.contextType),
});

Expand Down
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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) });
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContextExternalToolEntity> {
byId(id: EntityId | undefined): ContextExternalToolScope {
Expand All @@ -21,7 +22,7 @@ export class ContextExternalToolScope extends Scope<ContextExternalToolEntity> {

byContextId(contextId: EntityId | undefined): ContextExternalToolScope {
if (contextId !== undefined) {
this.addQuery({ contextId });
this.addQuery({ contextId: new ObjectId(contextId) });
}

return this;
Expand Down
4 changes: 2 additions & 2 deletions backup/setup/context-external-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"schoolTool": {
"$oid": "644a46e5d0a8301e6cf25d86"
},
"contextId": "0000dcfbfb5c7a3f00bf21ab",
"contextId": { "$oid" : "0000dcfbfb5c7a3f00bf21ab" },
"contextType": "course",
"displayName": "Google-Suche",
"parameters": [
Expand All @@ -26,7 +26,7 @@
"_id": {
"$oid": "647de3cfab79fd5bd57e68f4"
},
"contextId": "0000dcfbfb5c7a3f00bf21ab",
"contextId": { "$oid" : "0000dcfbfb5c7a3f00bf21ab" },
"contextType": "course",
"parameters": [],
"schoolTool": {
Expand Down
9 changes: 9 additions & 0 deletions backup/setup/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]
Loading