Skip to content

Commit

Permalink
Move uc to api folder
Browse files Browse the repository at this point in the history
  • Loading branch information
dyedwiper committed Nov 16, 2023
1 parent 622fdf5 commit 991590a
Show file tree
Hide file tree
Showing 24 changed files with 141 additions and 508 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { County, FederalStateDto } from '../../domain';
import { County, FederalState } from '../../domain';
import { CountyResponse, FederalStateResponse } from '../dto/response';

export class FederalStateResponseMapper {
public static mapToResponse(federalState: FederalStateDto): FederalStateResponse {
const counties = federalState.counties && this.mapToCountyResponses(federalState.counties);
public static mapToResponse(federalState: FederalState): FederalStateResponse {
const federalStateProps = federalState.getProps();
const counties = federalStateProps.counties && this.mapToCountyResponses(federalStateProps.counties);

const res = new FederalStateResponse({
...federalState,
id: federalState.id,
name: federalStateProps.name,
abbreviation: federalStateProps.abbreviation,
logoUrl: federalStateProps.logoUrl,
counties,
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { SchoolYearDto } from '../../domain';
import { SchoolYear } from '../../domain';
import { SchoolYearResponse } from '../dto/response';

export class SchoolYearResponseMapper {
public static mapToResponse(schoolYear: SchoolYearDto): SchoolYearResponse {
const res = new SchoolYearResponse(schoolYear);
public static mapToResponse(schoolYear: SchoolYear): SchoolYearResponse {
const schoolYearProps = schoolYear.getProps();

const res = new SchoolYearResponse({
id: schoolYear.id,
name: schoolYearProps.name,
startDate: schoolYearProps.startDate,
endDate: schoolYearProps.endDate,
});

return res;
}

public static mapToResponses(schoolYears: SchoolYear[]): SchoolYearResponse[] {
const dtos = schoolYears.map((schoolYear) => this.mapToResponse(schoolYear));

return dtos;
}
}
46 changes: 26 additions & 20 deletions apps/server/src/modules/school/api/mapper/school.response.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,47 @@
import { SchoolDto, SchoolForExternalInviteDto } from '../../domain';
import { SchoolForExternalInviteResponse, SchoolResponse } from '../dto/response';
import { School } from '../../domain';
import { SchoolForExternalInviteResponse, SchoolResponse, YearsResponse } from '../dto/response';
import { FederalStateResponseMapper } from './federal-state.response.mapper';
import { SchoolYearResponseMapper } from './school-year.response.mapper';
import { SystemResponseMapper } from './system.response.mapper';
import { YearsResponseMapper } from './years.response.mapper';

export class SchoolResponseMapper {
public static mapToResponse(school: SchoolDto): SchoolResponse {
const federalState = FederalStateResponseMapper.mapToResponse(school.federalState);
const currentYear = school.currentYear && SchoolYearResponseMapper.mapToResponse(school.currentYear);
const features = school.features && Array.from(school.features);
const systems = school.systems?.map((system) => SystemResponseMapper.mapToResponse(system));
const years = YearsResponseMapper.mapToResponse(school.years);

const res = new SchoolResponse({
...school,
federalState,
public static mapToResponse(school: School, years: YearsResponse): SchoolResponse {
const schoolProps = school.getProps();

const federalState = FederalStateResponseMapper.mapToResponse(schoolProps.federalState);
const currentYear = schoolProps.currentYear && SchoolYearResponseMapper.mapToResponse(schoolProps.currentYear);
const features = schoolProps.features && Array.from(schoolProps.features);
const systems = schoolProps.systems?.map((system) => SystemResponseMapper.mapToResponse(system));

const dto = new SchoolResponse({
...schoolProps,
currentYear,
federalState,
features,
systems,
inMaintenance: school.isInMaintenance(),
isExternal: school.isExternal(),
years,
});

return res;
return dto;
}

public static mapToListForExternalInviteResponse(
schools: SchoolForExternalInviteDto[]
): SchoolForExternalInviteResponse[] {
public static mapToListForExternalInviteResponses(schools: School[]): SchoolForExternalInviteResponse[] {
const dtos = schools.map((school) => this.mapToExternalInviteResponse(school));

return dtos;
}

private static mapToExternalInviteResponse(school: SchoolForExternalInviteDto): SchoolForExternalInviteResponse {
const res = new SchoolForExternalInviteResponse(school);
private static mapToExternalInviteResponse(school: School): SchoolForExternalInviteResponse {
const schoolProps = school.getProps();

const dto = new SchoolForExternalInviteResponse({
id: school.id,
name: schoolProps.name,
purpose: schoolProps.purpose,
});

return res;
return dto;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import { SystemDto } from '../../domain';
import { System } from '../../domain';
import { SystemResponse } from '../dto/response';

export class SystemResponseMapper {
public static mapToResponse(system: SystemDto): SystemResponse {
const res = new SystemResponse(system);
public static mapToResponse(system: System): SystemResponse {
const systemProps = system.getProps();

const res = new SystemResponse({
id: system.id,
type: systemProps.type,
url: systemProps.url,
alias: systemProps.alias,
displayName: systemProps.displayName,
oauthConfig: systemProps.oauthConfig,
oidcConfig: systemProps.oidcConfig,
ldapConfig: systemProps.ldapConfig,
provisioningStrategy: systemProps.provisioningStrategy,
provisioningUrl: systemProps.provisioningUrl,
});

return res;
}
Expand Down
65 changes: 60 additions & 5 deletions apps/server/src/modules/school/api/mapper/years.response.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,65 @@
import { YearsDto } from '../../domain';
import { YearsResponse } from '../dto/response';
import { School, SchoolYear } from '../../domain';
import { SchoolYearResponse, YearsResponse } from '../dto/response';
import { SchoolYearResponseMapper } from './school-year.response.mapper';

export class YearsResponseMapper {
public static mapToResponse(years: YearsDto) {
const res = new YearsResponse(years);
public static mapToResponse(school: School, schoolYears: SchoolYear[]): YearsResponse {
const schoolYearResponses = SchoolYearResponseMapper.mapToResponses(schoolYears);

return res;
const activeYear = this.computeActiveYear(school, schoolYears);
const nextYear = this.computeNextYear(schoolYears, activeYear);
const lastYear = this.computeLastYear(schoolYears, activeYear);
const defaultYear = activeYear || nextYear;

const years = {
schoolYears: schoolYearResponses,
activeYear,
nextYear,
lastYear,
defaultYear,
};

return years;
}

private static computeActiveYear(school: School, schoolYears: SchoolYear[]): SchoolYearResponse | undefined {
let activeYear = school.getProps().currentYear;

if (!activeYear) {
const now = new Date();
activeYear = schoolYears.find(
(schoolYear) => schoolYear.getProps().startDate <= now && schoolYear.getProps().endDate >= now
);
}

const dto = activeYear && SchoolYearResponseMapper.mapToResponse(activeYear);

return dto;
}

private static computeNextYear(
schoolYears: SchoolYear[],
activeYear?: SchoolYearResponse
): SchoolYearResponse | undefined {
const indexOfActiveYear = schoolYears.findIndex((schoolYear) => schoolYear.id === activeYear?.id);

const nextYear = schoolYears[indexOfActiveYear + 1];

const dto = nextYear && SchoolYearResponseMapper.mapToResponse(nextYear);

return dto;
}

private static computeLastYear(
schoolYears: SchoolYear[],
activeYear?: SchoolYearResponse
): SchoolYearResponse | undefined {
const indexOfActiveYear = schoolYears.findIndex((schoolYear) => schoolYear.id === activeYear?.id);

const lastYear = schoolYears[indexOfActiveYear - 1];

const dto = lastYear && SchoolYearResponseMapper.mapToResponse(lastYear);

return dto;
}
}
10 changes: 3 additions & 7 deletions apps/server/src/modules/school/api/school.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Authenticate, CurrentUser } from '@modules/authentication/decorator/aut
import { ICurrentUser } from '@modules/authentication/interface/user';
import { Controller, Get, Param, Query } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { SchoolUc } from '../domain';
import { SchoolQueryParams, SchoolUrlParams } from './dto/param';
import { SchoolForExternalInviteResponse, SchoolResponse } from './dto/response';
import { SchoolResponseMapper } from './mapper';
import { SchoolUc } from './school.uc';

@ApiTags('School')
@Authenticate('jwt')
Expand All @@ -21,9 +21,7 @@ export class SchoolController {
@Param() urlParams: SchoolUrlParams,
@CurrentUser() user: ICurrentUser
): Promise<SchoolResponse> {
const school = await this.schoolUc.getSchool(urlParams.schoolId, user.userId);

const res = SchoolResponseMapper.mapToResponse(school);
const res = await this.schoolUc.getSchool(urlParams.schoolId, user.userId);

return res;
}
Expand All @@ -34,9 +32,7 @@ export class SchoolController {
@Query() query: SchoolQueryParams,
@CurrentUser() user: ICurrentUser
): Promise<SchoolForExternalInviteResponse[]> {
const schools = await this.schoolUc.getSchoolListForExternalInvite(query, user.schoolId);

const res = SchoolResponseMapper.mapToListForExternalInviteResponse(schools);
const res = await this.schoolUc.getSchoolListForExternalInvite(query, user.schoolId);

return res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { AuthorizationService } from '@modules/authorization/domain/service/auth
import { Injectable } from '@nestjs/common';
import { SortOrder } from '@shared/domain';
import { EntityId } from '@shared/domain/types/entity-id';
import { SchoolDto, SchoolForExternalInviteDto } from '../dto';
import { SchoolDtoMapper } from '../mapper';
import { YearsDtoMapper } from '../mapper/years.dto.mapper';
import { SchoolQuery } from '../query';
import { SchoolService, SchoolYearService } from '../service';
import { SchoolQuery } from '../domain/query';
import { SchoolService, SchoolYearService } from '../domain/service';
import { SchoolForExternalInviteResponse, SchoolResponse } from './dto/response';
import { SchoolResponseMapper } from './mapper';
import { YearsResponseMapper } from './mapper/years.response.mapper';

@Injectable()
export class SchoolUc {
Expand All @@ -17,25 +17,25 @@ export class SchoolUc {
private readonly schoolYearService: SchoolYearService
) {}

public async getSchool(schoolId: EntityId, userId: EntityId): Promise<SchoolDto> {
public async getSchool(schoolId: EntityId, userId: EntityId): Promise<SchoolResponse> {
const school = await this.schoolService.getSchool(schoolId);

const user = await this.authorizationService.getUserWithPermissions(userId);
const authContext = AuthorizationContextBuilder.read([]);
this.authorizationService.checkPermission(user, school, authContext);

const schoolYears = await this.schoolYearService.getAllSchoolYears();
const yearsDto = YearsDtoMapper.mapToDto(school, schoolYears);
const yearsResponse = YearsResponseMapper.mapToResponse(school, schoolYears);

const dto = SchoolDtoMapper.mapToDto(school, yearsDto);
const dto = SchoolResponseMapper.mapToResponse(school, yearsResponse);

return dto;
}

public async getSchoolListForExternalInvite(
query: SchoolQuery,
ownSchoolId: EntityId
): Promise<SchoolForExternalInviteDto[]> {
): Promise<SchoolForExternalInviteResponse[]> {
const findOptions = {
order: {
name: SortOrder.asc,
Expand All @@ -46,7 +46,7 @@ export class SchoolUc {

// TODO: Do we want authorization here? At the moment there is no fitting permission.

const dtos = SchoolDtoMapper.mapToListForExternalInviteDtos(schools);
const dtos = SchoolResponseMapper.mapToListForExternalInviteResponses(schools);

return dtos;
}
Expand Down
21 changes: 0 additions & 21 deletions apps/server/src/modules/school/domain/dto/federal-state.dto.ts

This file was deleted.

6 changes: 0 additions & 6 deletions apps/server/src/modules/school/domain/dto/index.ts

This file was deleted.

This file was deleted.

16 changes: 0 additions & 16 deletions apps/server/src/modules/school/domain/dto/school-year.dto.ts

This file was deleted.

Loading

0 comments on commit 991590a

Please sign in to comment.