Skip to content

Commit

Permalink
N21-1319 schoolexternaltool metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
mrikallab committed Nov 10, 2023
1 parent f6de63f commit f1e1044
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ApiProperty } from '@nestjs/swagger';
import { ToolContextType } from '../../../common/enum';

export class SchoolExternalToolMetadataResponse {
@ApiProperty()
contextExternalToolCountPerContext: Map<ToolContextType, number>;

constructor(schoolExternalToolMetadataResponse: SchoolExternalToolMetadataResponse) {
this.contextExternalToolCountPerContext = schoolExternalToolMetadataResponse.contextExternalToolCountPerContext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import { Body, Controller, Delete, Get, Param, Post, Query, Put, HttpCode, HttpS
import { ValidationError } from '@shared/common';
import { LegacyLogger } from '@src/core/logger';
import { Authenticate, CurrentUser, ICurrentUser } from '@modules/authentication';
import { SchoolExternalToolMetadata } from '../domain/school-external-tool-metadata';
import { SchoolExternalToolRequestMapper, SchoolExternalToolResponseMapper } from '../mapper';
import { ExternalToolSearchListResponse } from '../../external-tool/controller/dto';
import { SchoolExternalToolMetadataMapper } from '../mapper/school-external-tool-metadata.mapper';
import {
SchoolExternalToolIdParams,
SchoolExternalToolPostParams,
Expand All @@ -26,6 +28,7 @@ import {
import { SchoolExternalToolDto } from '../uc/dto/school-external-tool.types';
import { SchoolExternalToolUc } from '../uc';
import { SchoolExternalTool } from '../domain';
import { SchoolExternalToolMetadataResponse } from './dto/school-external-tool-metadata.response';

@ApiTags('Tool')
@Authenticate('jwt')
Expand Down Expand Up @@ -136,4 +139,23 @@ export class ToolSchoolController {

return response;
}

@Get('/:schoolExternalToolId/metadata')
@ApiOperation({ summary: 'Gets the metadata of an school external tool.' })
@ApiOkResponse({
description: 'Metadata of school external tool fetched successfully.',
})
@ApiUnauthorizedResponse({ description: 'User is not logged in.' })
async getMetaDataForExternalTool(
@CurrentUser() currentUser: ICurrentUser,
@Param() params: SchoolExternalToolIdParams
): Promise<SchoolExternalToolMetadataResponse> {
const schoolExternalToolMetadata: SchoolExternalToolMetadata =
await this.schoolExternalToolUc.getMetadataForSchoolExternalTool(currentUser.userId, params.schoolExternalToolId);

const mapped: SchoolExternalToolMetadataResponse =
SchoolExternalToolMetadataMapper.mapToSchoolExternalToolMetadataResponse(schoolExternalToolMetadata);

return mapped;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ToolContextType } from '../../common/enum';

export class SchoolExternalToolMetadata {
contextExternalToolCountPerContext: Map<ToolContextType, number>;

constructor(schoolExternalToolMetadata: SchoolExternalToolMetadata) {
this.contextExternalToolCountPerContext = schoolExternalToolMetadata.contextExternalToolCountPerContext;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { SchoolExternalToolMetadataResponse } from '../controller/dto/school-external-tool-metadata.response';
import { SchoolExternalToolMetadata } from '../domain/school-external-tool-metadata';

export class SchoolExternalToolMetadataMapper {
static mapToSchoolExternalToolMetadataResponse(
schoolExternalToolMetadata: SchoolExternalToolMetadata
): SchoolExternalToolMetadataResponse {
const externalToolMetadataResponse: SchoolExternalToolMetadataResponse = new SchoolExternalToolMetadataResponse({
contextExternalToolCountPerContext: schoolExternalToolMetadata.contextExternalToolCountPerContext,
});

return externalToolMetadataResponse;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Injectable } from '@nestjs/common';
import { EntityId } from '@shared/domain';
import { ContextExternalToolRepo, ExternalToolRepo, SchoolExternalToolRepo } from '@shared/repo';
import { Logger } from '../../../../core/logger';
import { ToolContextType } from '../../common/enum';
import { SchoolExternalToolMetadata } from '../domain/school-external-tool-metadata';

@Injectable()
export class SchoolExternalToolMetadataService {
constructor(
private readonly logger: Logger,
private readonly externalToolRepo: ExternalToolRepo,
private readonly schoolToolRepo: SchoolExternalToolRepo,
private readonly contextToolRepo: ContextExternalToolRepo
) {}

async getMetadata(schoolExternalToolId: EntityId) {
const toolCountPerContext: { ToolContextType; number }[] = await Promise.all(
// Context Course
Object.values(ToolContextType).map(
async (
contextType: ToolContextType
): Promise<{
ToolContextType;
number;
}> => {
const countPerContext: number =
await this.contextToolRepo.countContextExternalToolsBySchoolToolIdAndContextType(
contextType,
schoolExternalToolId
);
return { ToolContextType: contextType, number: countPerContext };
}
)
);

const schoolExternaltoolMetadata = this.createSchoolExternalToolMetadata(toolCountPerContext);

/* const contextExternalToolMetadata: Map<ToolContextType, number> = new Map(
toolCountPerContext.map((contextExternalToolCountPerSchool: { ToolContextType; number }) => [
contextExternalToolCountPerSchool.ToolContextType,
contextExternalToolCountPerSchool.number,
])
);
const externaltoolMetadata: ExternalToolMetadata = new ExternalToolMetadata({
schoolExternalToolCount,
contextExternalToolCountPerContext: contextExternalToolMetadata,
});
*/

return schoolExternaltoolMetadata;
}

private createContextExternalToolMetaData(
toolCountPerContext: { ToolContextType; number }[]
): Map<ToolContextType, number> {
const contextExternalToolMetadata: Map<ToolContextType, number> = new Map(

Check failure on line 59 in apps/server/src/modules/tool/school-external-tool/service/school-external-tool-metadata.service.ts

View workflow job for this annotation

GitHub Actions / nest_lint

Unsafe assignment of type Map<any, any> to a variable of type Map<ToolContextType, number>
toolCountPerContext.map((contextExternalToolCountPerSchool: { ToolContextType; number }) => [
contextExternalToolCountPerSchool.ToolContextType,
contextExternalToolCountPerSchool.number,
])
);

return contextExternalToolMetadata;
}

private createSchoolExternalToolMetadata(
contextExternalToolCountPerContext: { ToolContextType; number }[]
): SchoolExternalToolMetadata {
const schoolExternaltoolMetadata: SchoolExternalToolMetadata = new SchoolExternalToolMetadata({
contextExternalToolCountPerContext: this.createContextExternalToolMetaData(contextExternalToolCountPerContext),
});

return schoolExternaltoolMetadata;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { AuthorizationContext, AuthorizationContextBuilder } from '@modules/auth
import { ToolPermissionHelper } from '../../common/uc/tool-permission-helper';
import { ContextExternalToolService } from '../../context-external-tool/service';
import { SchoolExternalTool } from '../domain';
import { SchoolExternalToolMetadata } from '../domain/school-external-tool-metadata';
import { SchoolExternalToolService, SchoolExternalToolValidationService } from '../service';
import { SchoolExternalToolMetadataService } from '../service/school-external-tool-metadata.service';
import { SchoolExternalToolDto, SchoolExternalToolQueryInput } from './dto/school-external-tool.types';

@Injectable()
Expand All @@ -13,6 +15,7 @@ export class SchoolExternalToolUc {
private readonly schoolExternalToolService: SchoolExternalToolService,
private readonly contextExternalToolService: ContextExternalToolService,
private readonly schoolExternalToolValidationService: SchoolExternalToolValidationService,
private readonly schoolexternaltoolMetadataService: SchoolExternalToolMetadataService,
private readonly toolPermissionHelper: ToolPermissionHelper
) {}

Expand Down Expand Up @@ -95,4 +98,20 @@ export class SchoolExternalToolUc {
const saved = await this.schoolExternalToolService.saveSchoolExternalTool(updated);
return saved;
}

async getMetadataForSchoolExternalTool(
userId: EntityId,
schoolExternalToolId: EntityId
): Promise<SchoolExternalToolMetadata> {
const schoolExternalTool: SchoolExternalTool = await this.schoolExternalToolService.findById(schoolExternalToolId);

const context: AuthorizationContext = AuthorizationContextBuilder.read([Permission.SCHOOL_TOOL_ADMIN]);
await this.toolPermissionHelper.ensureSchoolPermissions(userId, schoolExternalTool, context);

const metadata: SchoolExternalToolMetadata = await this.schoolexternaltoolMetadataService.getMetadata(
schoolExternalToolId
);

return metadata;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export class SchoolExternalToolRepo extends BaseDORepo<
return domainObjects;
}

async countSchoolExternalToolsByExternalToolId(toolId: string): Promise<number> {
const schoolExternalToolCount: number = await this._em.count(this.entityName, { tool: toolId });
return schoolExternalToolCount;
}

async findBySchoolId(schoolId: string): Promise<SchoolExternalTool[]> {
const entities: SchoolExternalToolEntity[] = await this._em.find(this.entityName, { school: schoolId });
const domainObjects: SchoolExternalTool[] = entities.map((entity: SchoolExternalToolEntity): SchoolExternalTool => {
Expand Down

0 comments on commit f1e1044

Please sign in to comment.