-
Notifications
You must be signed in to change notification settings - Fork 17
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
N21-1285 User launches a CTL tool from a board card #4511
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0c0f8ed
- add strategy pattern to auto parameters
MarvinOehlerkingCap 014e214
fix finding of board by child
MarvinOehlerkingCap 39afe52
Merge branch 'main' into N21-1285-launch-tool-on-board
MarvinOehlerkingCap f6fa565
Merge branch 'main' into N21-1285-launch-tool-on-board
MarvinOehlerkingCap fb2f632
Merge branch 'main' into N21-1285-launch-tool-on-board
MarvinOehlerkingCap 8cee1ce
fix test
MarvinOehlerkingCap 92b839b
Merge remote-tracking branch 'origin/N21-1285-launch-tool-on-board' i…
MarvinOehlerkingCap 5a5dd87
Merge branch 'main' into N21-1285-launch-tool-on-board
arnegns File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
...modules/tool/tool-launch/service/auto-parameter-strategy/auto-context-id.strategy.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { contextExternalToolFactory, schoolExternalToolFactory } from '@shared/testing'; | ||
import { ContextExternalTool } from '../../../context-external-tool/domain'; | ||
import { SchoolExternalTool } from '../../../school-external-tool/domain'; | ||
import { AutoContextIdStrategy } from './auto-context-id.strategy'; | ||
|
||
describe(AutoContextIdStrategy.name, () => { | ||
let module: TestingModule; | ||
let strategy: AutoContextIdStrategy; | ||
|
||
beforeAll(async () => { | ||
module = await Test.createTestingModule({ | ||
providers: [AutoContextIdStrategy], | ||
}).compile(); | ||
|
||
strategy = module.get(AutoContextIdStrategy); | ||
}); | ||
|
||
afterAll(async () => { | ||
await module.close(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('getValue', () => { | ||
const setup = () => { | ||
const contextId = 'contextId'; | ||
|
||
const schoolExternalTool: SchoolExternalTool = schoolExternalToolFactory.buildWithId(); | ||
const contextExternalTool: ContextExternalTool = contextExternalToolFactory.buildWithId({ | ||
schoolToolRef: { | ||
schoolToolId: schoolExternalTool.id as string, | ||
}, | ||
contextRef: { | ||
id: contextId, | ||
}, | ||
}); | ||
|
||
return { | ||
contextId, | ||
schoolExternalTool, | ||
contextExternalTool, | ||
}; | ||
}; | ||
|
||
it('should return the context id', () => { | ||
const { contextId, schoolExternalTool, contextExternalTool } = setup(); | ||
|
||
const result: string | undefined = strategy.getValue(schoolExternalTool, contextExternalTool); | ||
|
||
expect(result).toEqual(contextId); | ||
}); | ||
}); | ||
}); |
11 changes: 11 additions & 0 deletions
11
.../src/modules/tool/tool-launch/service/auto-parameter-strategy/auto-context-id.strategy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ContextExternalTool } from '../../../context-external-tool/domain'; | ||
import { SchoolExternalTool } from '../../../school-external-tool/domain'; | ||
import { AutoParameterStrategy } from './auto-parameter.strategy'; | ||
|
||
@Injectable() | ||
export class AutoContextIdStrategy implements AutoParameterStrategy { | ||
getValue(schoolExternalTool: SchoolExternalTool, contextExternalTool: ContextExternalTool): string | undefined { | ||
return contextExternalTool.contextRef.id; | ||
} | ||
} |
194 changes: 194 additions & 0 deletions
194
...dules/tool/tool-launch/service/auto-parameter-strategy/auto-context-name.strategy.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
import { createMock, DeepMocked } from '@golevelup/ts-jest'; | ||
import { ObjectId } from '@mikro-orm/mongodb'; | ||
import { ColumnBoardService } from '@modules/board'; | ||
import { CourseService } from '@modules/learnroom'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { BoardExternalReferenceType, ColumnBoard, Course } from '@shared/domain'; | ||
import { | ||
columnBoardFactory, | ||
contextExternalToolFactory, | ||
courseFactory, | ||
schoolExternalToolFactory, | ||
setupEntities, | ||
} from '@shared/testing'; | ||
import { ToolContextType } from '../../../common/enum'; | ||
import { ContextExternalTool } from '../../../context-external-tool/domain'; | ||
import { SchoolExternalTool } from '../../../school-external-tool/domain'; | ||
import { ParameterTypeNotImplementedLoggableException } from '../../error'; | ||
import { AutoContextNameStrategy } from './auto-context-name.strategy'; | ||
|
||
describe(AutoContextNameStrategy.name, () => { | ||
let module: TestingModule; | ||
let strategy: AutoContextNameStrategy; | ||
|
||
let courseService: DeepMocked<CourseService>; | ||
let columnBoardService: DeepMocked<ColumnBoardService>; | ||
|
||
beforeAll(async () => { | ||
await setupEntities(); | ||
|
||
module = await Test.createTestingModule({ | ||
providers: [ | ||
AutoContextNameStrategy, | ||
{ | ||
provide: CourseService, | ||
useValue: createMock<CourseService>(), | ||
}, | ||
{ | ||
provide: ColumnBoardService, | ||
useValue: createMock<ColumnBoardService>(), | ||
}, | ||
], | ||
}).compile(); | ||
|
||
strategy = module.get(AutoContextNameStrategy); | ||
courseService = module.get(CourseService); | ||
columnBoardService = module.get(ColumnBoardService); | ||
}); | ||
|
||
afterAll(async () => { | ||
await module.close(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('getValue', () => { | ||
describe('when the tool context is "course"', () => { | ||
const setup = () => { | ||
const courseId = new ObjectId().toHexString(); | ||
const schoolExternalTool: SchoolExternalTool = schoolExternalToolFactory.build(); | ||
const contextExternalTool: ContextExternalTool = contextExternalToolFactory.build({ | ||
contextRef: { | ||
id: courseId, | ||
type: ToolContextType.COURSE, | ||
}, | ||
}); | ||
|
||
const course: Course = courseFactory.buildWithId( | ||
{ | ||
name: 'testName', | ||
}, | ||
courseId | ||
); | ||
|
||
courseService.findById.mockResolvedValue(course); | ||
|
||
return { | ||
schoolExternalTool, | ||
contextExternalTool, | ||
course, | ||
}; | ||
}; | ||
|
||
it('should return the course name', async () => { | ||
const { schoolExternalTool, contextExternalTool, course } = setup(); | ||
|
||
const result: string | undefined = await strategy.getValue(schoolExternalTool, contextExternalTool); | ||
|
||
expect(result).toEqual(course.name); | ||
}); | ||
}); | ||
|
||
describe('when the tool context is "board element" and the board context is "course"', () => { | ||
const setup = () => { | ||
const boardElementId = new ObjectId().toHexString(); | ||
const schoolExternalTool: SchoolExternalTool = schoolExternalToolFactory.build(); | ||
const contextExternalTool: ContextExternalTool = contextExternalToolFactory.build({ | ||
contextRef: { | ||
id: boardElementId, | ||
type: ToolContextType.BOARD_ELEMENT, | ||
}, | ||
}); | ||
|
||
const course: Course = courseFactory.buildWithId({ | ||
name: 'testName', | ||
}); | ||
|
||
const columnBoard: ColumnBoard = columnBoardFactory.build({ | ||
context: { | ||
id: course.id, | ||
type: BoardExternalReferenceType.Course, | ||
}, | ||
}); | ||
|
||
courseService.findById.mockResolvedValue(course); | ||
columnBoardService.findById.mockResolvedValue(columnBoard); | ||
|
||
return { | ||
schoolExternalTool, | ||
contextExternalTool, | ||
course, | ||
}; | ||
}; | ||
|
||
it('should return the course name', async () => { | ||
const { schoolExternalTool, contextExternalTool, course } = setup(); | ||
|
||
const result: string | undefined = await strategy.getValue(schoolExternalTool, contextExternalTool); | ||
|
||
expect(result).toEqual(course.name); | ||
}); | ||
}); | ||
|
||
describe('when the tool context is "board element" and the board context is unknown', () => { | ||
const setup = () => { | ||
const boardElementId = new ObjectId().toHexString(); | ||
const schoolExternalTool: SchoolExternalTool = schoolExternalToolFactory.build(); | ||
const contextExternalTool: ContextExternalTool = contextExternalToolFactory.build({ | ||
contextRef: { | ||
id: boardElementId, | ||
type: ToolContextType.BOARD_ELEMENT, | ||
}, | ||
}); | ||
|
||
const columnBoard: ColumnBoard = columnBoardFactory.build({ | ||
context: { | ||
id: new ObjectId().toHexString(), | ||
type: 'unknown' as unknown as BoardExternalReferenceType, | ||
}, | ||
}); | ||
|
||
columnBoardService.findById.mockResolvedValue(columnBoard); | ||
|
||
return { | ||
schoolExternalTool, | ||
contextExternalTool, | ||
}; | ||
}; | ||
|
||
it('should return undefined', async () => { | ||
const { schoolExternalTool, contextExternalTool } = setup(); | ||
|
||
const result: string | undefined = await strategy.getValue(schoolExternalTool, contextExternalTool); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('when a lookup for a context name is not implemented', () => { | ||
const setup = () => { | ||
const schoolExternalTool: SchoolExternalTool = schoolExternalToolFactory.build(); | ||
const contextExternalTool: ContextExternalTool = contextExternalToolFactory.build({ | ||
contextRef: { | ||
type: 'unknownContext' as unknown as ToolContextType, | ||
}, | ||
}); | ||
|
||
return { | ||
schoolExternalTool, | ||
contextExternalTool, | ||
}; | ||
}; | ||
|
||
it('should throw a ParameterNotImplementedLoggableException', async () => { | ||
const { schoolExternalTool, contextExternalTool } = setup(); | ||
|
||
await expect(strategy.getValue(schoolExternalTool, contextExternalTool)).rejects.toThrow( | ||
ParameterTypeNotImplementedLoggableException | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
55 changes: 55 additions & 0 deletions
55
...rc/modules/tool/tool-launch/service/auto-parameter-strategy/auto-context-name.strategy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { ColumnBoardService } from '@modules/board'; | ||
import { CourseService } from '@modules/learnroom'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { BoardExternalReferenceType, ColumnBoard, Course, EntityId } from '@shared/domain'; | ||
import { CustomParameterType, ToolContextType } from '../../../common/enum'; | ||
import { ContextExternalTool } from '../../../context-external-tool/domain'; | ||
import { SchoolExternalTool } from '../../../school-external-tool/domain'; | ||
import { ParameterTypeNotImplementedLoggableException } from '../../error'; | ||
import { AutoParameterStrategy } from './auto-parameter.strategy'; | ||
|
||
@Injectable() | ||
export class AutoContextNameStrategy implements AutoParameterStrategy { | ||
constructor(private readonly courseService: CourseService, private readonly columnBoardService: ColumnBoardService) {} | ||
|
||
async getValue( | ||
schoolExternalTool: SchoolExternalTool, | ||
contextExternalTool: ContextExternalTool | ||
): Promise<string | undefined> { | ||
switch (contextExternalTool.contextRef.type) { | ||
case ToolContextType.COURSE: { | ||
const courseValue: string = await this.getCourseValue(contextExternalTool.contextRef.id); | ||
|
||
return courseValue; | ||
} | ||
case ToolContextType.BOARD_ELEMENT: { | ||
const boardValue: string | undefined = await this.getBoardValue(contextExternalTool.contextRef.id); | ||
|
||
return boardValue; | ||
} | ||
default: { | ||
throw new ParameterTypeNotImplementedLoggableException( | ||
`${CustomParameterType.AUTO_CONTEXTNAME}/${contextExternalTool.contextRef.type as string}` | ||
); | ||
} | ||
} | ||
} | ||
|
||
private async getCourseValue(courseId: EntityId): Promise<string> { | ||
const course: Course = await this.courseService.findById(courseId); | ||
|
||
return course.name; | ||
} | ||
|
||
private async getBoardValue(boardId: EntityId): Promise<string | undefined> { | ||
const board: ColumnBoard = await this.columnBoardService.findById(boardId); | ||
|
||
if (board.context.type === BoardExternalReferenceType.Course) { | ||
const courseName: string = await this.getCourseValue(board.context.id); | ||
|
||
return courseName; | ||
} | ||
|
||
return undefined; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...r/src/modules/tool/tool-launch/service/auto-parameter-strategy/auto-parameter.strategy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ContextExternalTool } from '../../../context-external-tool/domain'; | ||
import { SchoolExternalTool } from '../../../school-external-tool/domain'; | ||
|
||
export interface AutoParameterStrategy { | ||
getValue( | ||
schoolExternalTool: SchoolExternalTool, | ||
contextExternalTool: ContextExternalTool | ||
): string | Promise<string | undefined> | undefined; | ||
} |
56 changes: 56 additions & 0 deletions
56
.../modules/tool/tool-launch/service/auto-parameter-strategy/auto-school-id.strategy.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { contextExternalToolFactory, schoolExternalToolFactory } from '@shared/testing'; | ||
import { ContextExternalTool } from '../../../context-external-tool/domain'; | ||
import { SchoolExternalTool } from '../../../school-external-tool/domain'; | ||
import { AutoSchoolIdStrategy } from './auto-school-id.strategy'; | ||
|
||
describe(AutoSchoolIdStrategy.name, () => { | ||
let module: TestingModule; | ||
let strategy: AutoSchoolIdStrategy; | ||
|
||
beforeAll(async () => { | ||
module = await Test.createTestingModule({ | ||
providers: [AutoSchoolIdStrategy], | ||
}).compile(); | ||
|
||
strategy = module.get(AutoSchoolIdStrategy); | ||
}); | ||
|
||
afterAll(async () => { | ||
await module.close(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('getValue', () => { | ||
const setup = () => { | ||
const schoolId = 'schoolId'; | ||
|
||
const schoolExternalTool: SchoolExternalTool = schoolExternalToolFactory.buildWithId({ | ||
schoolId, | ||
}); | ||
const contextExternalTool: ContextExternalTool = contextExternalToolFactory.buildWithId({ | ||
schoolToolRef: { | ||
schoolToolId: schoolExternalTool.id as string, | ||
schoolId, | ||
}, | ||
}); | ||
|
||
return { | ||
schoolId, | ||
schoolExternalTool, | ||
contextExternalTool, | ||
}; | ||
}; | ||
|
||
it('should return the context id', () => { | ||
const { schoolId, schoolExternalTool, contextExternalTool } = setup(); | ||
|
||
const result: string | undefined = strategy.getValue(schoolExternalTool, contextExternalTool); | ||
|
||
expect(result).toEqual(schoolId); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should think about introducing an interface e.g. "ExternalToolsNameProvider" which will be implemented by course and columnboard service and returns an independet object.
The interface should also provide a function to return the toolContextType.
This would decouple our strategy from the services.
The getValue function can use the nestjs reflector to get all implementations of the interface