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

N21-2020 Fixes mediaboard error #5058

Merged
merged 1 commit into from
Jun 10, 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { ObjectId } from '@mikro-orm/mongodb';
import { ExternalToolService } from '@modules/tool';
import { CustomParameterScope, ToolContextType } from '@modules/tool/common/enum';
import { ContextExternalToolService } from '@modules/tool/context-external-tool';
Expand Down Expand Up @@ -66,32 +67,35 @@ describe(MediaAvailableLineService.name, () => {
});

describe('getUnusedAvailableSchoolExternalTools', () => {
const setup = () => {
const user: User = userFactory.build();
describe('when there are unused tools', () => {
const setup = () => {
const user: User = userFactory.build();

const schoolExternalTool: SchoolExternalTool = schoolExternalToolFactory
.withSchoolId(user.school.id)
.buildWithId();
const usedSchoolExternalTool: SchoolExternalTool = schoolExternalToolFactory
.withSchoolId(user.school.id)
.buildWithId();
const schoolExternalTool: SchoolExternalTool = schoolExternalToolFactory
.withSchoolId(user.school.id)
.buildWithId();
const usedSchoolExternalTool: SchoolExternalTool = schoolExternalToolFactory
.withSchoolId(user.school.id)
.buildWithId();

const usedContextExternalTool: ContextExternalTool = contextExternalToolFactory
.withSchoolExternalToolRef(usedSchoolExternalTool.id, user.school.id)
.buildWithId();
const usedContextExternalTool: ContextExternalTool = contextExternalToolFactory
.withSchoolExternalToolRef(usedSchoolExternalTool.id, user.school.id)
.buildWithId();

const mediaExternalToolElement: MediaExternalToolElement = mediaExternalToolElementFactory.build({
contextExternalToolId: usedContextExternalTool.id,
});
const board: MediaBoard = mediaBoardFactory.addChild(mediaExternalToolElement).build();
const mediaExternalToolElement: MediaExternalToolElement = mediaExternalToolElementFactory.build({
contextExternalToolId: usedContextExternalTool.id,
});
const board: MediaBoard = mediaBoardFactory.addChild(mediaExternalToolElement).build();

schoolExternalToolService.findSchoolExternalTools.mockResolvedValue([schoolExternalTool, usedSchoolExternalTool]);
contextExternalToolService.findByIdOrFail.mockResolvedValueOnce(usedContextExternalTool);
schoolExternalToolService.findSchoolExternalTools.mockResolvedValue([
schoolExternalTool,
usedSchoolExternalTool,
]);
contextExternalToolService.findById.mockResolvedValueOnce(usedContextExternalTool);

return { user, board, mediaExternalToolElement, schoolExternalTool };
};
return { user, board, mediaExternalToolElement, schoolExternalTool };
};

describe('when there are unused tools', () => {
it('should call the service to get school external tools for users school', async () => {
const { user, board } = setup();

Expand All @@ -108,7 +112,7 @@ describe(MediaAvailableLineService.name, () => {

await service.getUnusedAvailableSchoolExternalTools(user, board);

expect(contextExternalToolService.findByIdOrFail).toHaveBeenCalledWith(
expect(contextExternalToolService.findById).toHaveBeenCalledWith(
mediaExternalToolElement.contextExternalToolId
);
});
Expand All @@ -124,6 +128,56 @@ describe(MediaAvailableLineService.name, () => {
expect(schoolExternalTools).toEqual([schoolExternalTool]);
});
});

describe('when there are elements on board which has deleted context external tool', () => {
const setup = () => {
const user: User = userFactory.build();

const schoolExternalTool: SchoolExternalTool = schoolExternalToolFactory
.withSchoolId(user.school.id)
.buildWithId();
const usedSchoolExternalTool: SchoolExternalTool = schoolExternalToolFactory
.withSchoolId(user.school.id)
.buildWithId();

const usedContextExternalTool: ContextExternalTool = contextExternalToolFactory
.withSchoolExternalToolRef(usedSchoolExternalTool.id, user.school.id)
.buildWithId();

const mediaExternalToolElement: MediaExternalToolElement = mediaExternalToolElementFactory.build({
contextExternalToolId: usedContextExternalTool.id,
});
const mediaExternalToolElementWithDeletedTool: MediaExternalToolElement = mediaExternalToolElementFactory.build(
{
contextExternalToolId: new ObjectId().toHexString(),
}
);
const board: MediaBoard = mediaBoardFactory
.addChild(mediaExternalToolElement)
.addChild(mediaExternalToolElementWithDeletedTool)
.build();

schoolExternalToolService.findSchoolExternalTools.mockResolvedValue([
schoolExternalTool,
usedSchoolExternalTool,
]);
contextExternalToolService.findById.mockResolvedValueOnce(usedContextExternalTool);
contextExternalToolService.findById.mockResolvedValueOnce(null);

return { user, board, mediaExternalToolElement, schoolExternalTool };
};

it('should return the available tools', async () => {
const { user, board, schoolExternalTool } = setup();

const schoolExternalTools: SchoolExternalTool[] = await service.getUnusedAvailableSchoolExternalTools(
user,
board
);

expect(schoolExternalTools).toEqual([schoolExternalTool]);
});
});
});

describe('getAvailableExternalToolsForSchool', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,17 @@ export class MediaAvailableLineService {
}

private async getContextExternalToolsByBoard(board: MediaBoard): Promise<ContextExternalTool[]> {
const contextExternalTools: Promise<ContextExternalTool>[] = board
const contextExternalTools: Promise<ContextExternalTool | null>[] = board
.getChildrenOfType(MediaExternalToolElement)
.map((element: MediaExternalToolElement) =>
this.contextExternalToolService.findByIdOrFail(element.contextExternalToolId)
this.contextExternalToolService.findById(element.contextExternalToolId)
);

const allContextExternalTools: ContextExternalTool[] = await Promise.all(contextExternalTools);
const notNullContextExternalTools: ContextExternalTool[] = (await Promise.all(contextExternalTools)).filter(
(tool): tool is ContextExternalTool => tool !== null
);

return allContextExternalTools;
return notNullContextExternalTools;
}

public matchTools(
Expand Down
Loading