Skip to content

Commit

Permalink
filter empty mediumId
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinOehlerkingCap committed Jun 14, 2024
1 parent 33ea7a4 commit 48083ae
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 49 deletions.
2 changes: 1 addition & 1 deletion apps/server/src/infra/schulconnex-client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ export {
SchulconnexPersonenkontextResponse,
SchulconnexSonstigeGruppenzugehoerigeResponse,
} from './response';
export { schulconnexResponseFactory } from './testing/schulconnex-response-factory';
export { schulconnexResponseFactory, schulconnexLizenzInfoResponseFactory } from './testing';
export { SchulconnexClientConfig } from './schulconnex-client-config';
1 change: 1 addition & 0 deletions apps/server/src/infra/schulconnex-client/testing/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { schulconnexResponseFactory } from './schulconnex-response-factory';
export { schulconnexLizenzInfoResponseFactory } from './schulconnex-lizenz-info-response-factory';
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Factory } from 'fishery';
import { SchulconnexLizenzInfoActionType, SchulconnexLizenzInfoResponse } from '../response';

export const schulconnexLizenzInfoResponseFactory = Factory.define<SchulconnexLizenzInfoResponse[]>(() => [
{
export const schulconnexLizenzInfoResponseFactory = Factory.define<SchulconnexLizenzInfoResponse>(() => {
return {
target: {
uid: 'bildungscloud',
partOf: '',
Expand All @@ -12,5 +12,5 @@ export const schulconnexLizenzInfoResponseFactory = Factory.define<SchulconnexLi
action: [SchulconnexLizenzInfoActionType.EXECUTE],
},
],
},
]);
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import {
SchulconnexGroupRole,
SchulconnexGroupType,
SchulconnexGruppenResponse,
schulconnexLizenzInfoResponseFactory,
SchulconnexPersonenkontextResponse,
SchulconnexResponse,
schulconnexResponseFactory,
SchulconnexSonstigeGruppenzugehoerigeResponse,
} from '@infra/schulconnex-client';
import { SchulconnexLizenzInfoResponse } from '@infra/schulconnex-client/response';
import { schulconnexLizenzInfoResponseFactory } from '@infra/schulconnex-client/testing/schulconnex-lizenz-info-response-factory';
import { GroupTypes } from '@modules/group';
import { Test, TestingModule } from '@nestjs/testing';
import { RoleName } from '@shared/domain/interface';
Expand Down Expand Up @@ -43,25 +43,21 @@ describe(SchulconnexResponseMapper.name, () => {
provisioningFeatures = module.get(ProvisioningFeatures);
});

const setupSchulconnexResponse = () => {
const externalUserId = 'aef1f4fd-c323-466e-962b-a84354c0e713';
const externalSchoolId = 'df66c8e6-cfac-40f7-b35b-0da5d8ee680e';
describe('mapToExternalSchoolDto', () => {
describe('when a schulconnex response is provided', () => {
const setup = () => {
const externalSchoolId = 'df66c8e6-cfac-40f7-b35b-0da5d8ee680e';

const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build();
const licenseResponse: SchulconnexLizenzInfoResponse[] = schulconnexLizenzInfoResponseFactory.build();
const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build();

return {
externalUserId,
externalSchoolId,
schulconnexResponse,
licenseResponse,
};
};
return {
externalSchoolId,
schulconnexResponse,
};
};

describe('mapToExternalSchoolDto', () => {
describe('when a schulconnex response is provided', () => {
it('should map the response to an ExternalSchoolDto', () => {
const { schulconnexResponse, externalSchoolId } = setupSchulconnexResponse();
const { schulconnexResponse, externalSchoolId } = setup();

const result: ExternalSchoolDto = mapper.mapToExternalSchoolDto(schulconnexResponse);

Expand All @@ -77,8 +73,19 @@ describe(SchulconnexResponseMapper.name, () => {

describe('mapToExternalUserDto', () => {
describe('when a schulconnex response is provided', () => {
const setup = () => {
const externalUserId = 'aef1f4fd-c323-466e-962b-a84354c0e713';

const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build();

return {
externalUserId,
schulconnexResponse,
};
};

it('should map the response to an ExternalUserDto', () => {
const { schulconnexResponse, externalUserId } = setupSchulconnexResponse();
const { schulconnexResponse, externalUserId } = setup();

const result: ExternalUserDto = mapper.mapToExternalUserDto(schulconnexResponse);

Expand All @@ -97,7 +104,7 @@ describe(SchulconnexResponseMapper.name, () => {
describe('mapToExternalGroupDtos', () => {
describe('when no group is given', () => {
const setup = () => {
const { schulconnexResponse } = setupSchulconnexResponse();
const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build();
schulconnexResponse.personenkontexte[0].gruppen = undefined;

return {
Expand All @@ -116,10 +123,8 @@ describe(SchulconnexResponseMapper.name, () => {

describe('when unknown group type is given', () => {
const setup = () => {
const { schulconnexResponse } = setupSchulconnexResponse();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
schulconnexResponse.personenkontexte[0].gruppen?.[0].gruppe.typ = 'unknown';
const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build();
schulconnexResponse.personenkontexte[0].gruppen![0].gruppe.typ = 'unknown' as SchulconnexGroupType;

return {
schulconnexResponse,
Expand All @@ -141,7 +146,7 @@ describe(SchulconnexResponseMapper.name, () => {
schulconnexOtherGroupusersEnabled: true,
});

const { schulconnexResponse } = setupSchulconnexResponse();
const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build();
const personenkontext: SchulconnexPersonenkontextResponse = schulconnexResponse.personenkontexte[0];
const group: SchulconnexGruppenResponse = personenkontext.gruppen![0];
const otherParticipant: SchulconnexSonstigeGruppenzugehoerigeResponse = group.sonstige_gruppenzugehoerige![0];
Expand Down Expand Up @@ -179,7 +184,7 @@ describe(SchulconnexResponseMapper.name, () => {

describe('when group type other is provided', () => {
const setup = () => {
const { schulconnexResponse } = setupSchulconnexResponse();
const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build();
schulconnexResponse.personenkontexte[0].gruppen![0]!.gruppe.typ = SchulconnexGroupType.OTHER;

return {
Expand All @@ -202,7 +207,7 @@ describe(SchulconnexResponseMapper.name, () => {

describe('when group type course is provided', () => {
const setup = () => {
const { schulconnexResponse } = setupSchulconnexResponse();
const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build();
schulconnexResponse.personenkontexte[0].gruppen![0]!.gruppe.typ = SchulconnexGroupType.COURSE;

return {
Expand All @@ -225,7 +230,7 @@ describe(SchulconnexResponseMapper.name, () => {

describe('when the group role mapping for the user is missing', () => {
const setup = () => {
const { schulconnexResponse } = setupSchulconnexResponse();
const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build();
schulconnexResponse.personenkontexte[0].gruppen![0]!.gruppenzugehoerigkeit.rollen = [
SchulconnexGroupRole.SCHOOL_SUPPORT,
];
Expand All @@ -246,7 +251,7 @@ describe(SchulconnexResponseMapper.name, () => {

describe('when the user has no role in the group', () => {
const setup = () => {
const { schulconnexResponse } = setupSchulconnexResponse();
const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build();
schulconnexResponse.personenkontexte[0].gruppen![0]!.gruppenzugehoerigkeit.rollen = [];

return {
Expand All @@ -268,7 +273,7 @@ describe(SchulconnexResponseMapper.name, () => {
Object.assign<IProvisioningFeatures, Partial<IProvisioningFeatures>>(provisioningFeatures, {
schulconnexOtherGroupusersEnabled: false,
});
const { schulconnexResponse } = setupSchulconnexResponse();
const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build();
schulconnexResponse.personenkontexte[0].gruppen![0].sonstige_gruppenzugehoerige = undefined;

return {
Expand All @@ -291,7 +296,7 @@ describe(SchulconnexResponseMapper.name, () => {
schulconnexOtherGroupusersEnabled: true,
});

const { schulconnexResponse } = setupSchulconnexResponse();
const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build();
schulconnexResponse.personenkontexte[0].gruppen![0].sonstige_gruppenzugehoerige = undefined;

return {
Expand All @@ -310,7 +315,7 @@ describe(SchulconnexResponseMapper.name, () => {

describe('when other participants have unknown roles', () => {
const setup = () => {
const { schulconnexResponse } = setupSchulconnexResponse();
const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build();
schulconnexResponse.personenkontexte[0].gruppen![0]!.sonstige_gruppenzugehoerige = [
{
ktid: 'ktid',
Expand All @@ -334,9 +339,19 @@ describe(SchulconnexResponseMapper.name, () => {
});

describe('mapToExternalLicenses', () => {
describe('when a sanis response with license is provided', () => {
describe('when a license response has a medium id and no media source', () => {
const setup = () => {
const licenseResponse: SchulconnexLizenzInfoResponse[] = schulconnexLizenzInfoResponseFactory.buildList(1, {
target: { uid: 'bildungscloud', partOf: '' },
});

return {
licenseResponse,
};
};

it('should map the response to an ExternalLicenseDto', () => {
const { licenseResponse } = setupSchulconnexResponse();
const { licenseResponse } = setup();

const result: ExternalLicenseDto[] = SchulconnexResponseMapper.mapToExternalLicenses(licenseResponse);

Expand All @@ -348,5 +363,50 @@ describe(SchulconnexResponseMapper.name, () => {
]);
});
});

describe('when a license response has a medium id and a media source', () => {
const setup = () => {
const licenseResponse: SchulconnexLizenzInfoResponse[] = schulconnexLizenzInfoResponseFactory.buildList(1, {
target: { uid: 'bildungscloud', partOf: 'bildungscloud-source' },
});

return {
licenseResponse,
};
};

it('should map the response to an ExternalLicenseDto', () => {
const { licenseResponse } = setup();

const result: ExternalLicenseDto[] = SchulconnexResponseMapper.mapToExternalLicenses(licenseResponse);

expect(result).toEqual<ExternalLicenseDto[]>([
{
mediumId: 'bildungscloud',
mediaSourceId: 'bildungscloud-source',
},
]);
});
});

describe('when a license response has no medium id', () => {
const setup = () => {
const licenseResponse: SchulconnexLizenzInfoResponse[] = schulconnexLizenzInfoResponseFactory.buildList(1, {
target: { uid: '', partOf: 'bildungscloud-source' },
});

return {
licenseResponse,
};
};

it('should should be filtered out', () => {
const { licenseResponse } = setup();

const result: ExternalLicenseDto[] = SchulconnexResponseMapper.mapToExternalLicenses(licenseResponse);

expect(result).toEqual<ExternalLicenseDto[]>([]);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,20 @@ export class SchulconnexResponseMapper {
}

public static mapToExternalLicenses(licenseInfos: SchulconnexLizenzInfoResponse[]): ExternalLicenseDto[] {
const externalLicenseDtos: ExternalLicenseDto[] = licenseInfos.map((license: SchulconnexLizenzInfoResponse) => {
if (license.target.partOf === '') {
license.target.partOf = undefined;
}

const externalLicenseDto: ExternalLicenseDto = new ExternalLicenseDto({
mediumId: license.target.uid,
mediaSourceId: license.target.partOf,
});

return externalLicenseDto;
});
const externalLicenseDtos: ExternalLicenseDto[] = licenseInfos
.map((license: SchulconnexLizenzInfoResponse) => {
if (license.target.partOf === '') {
license.target.partOf = undefined;
}

const externalLicenseDto: ExternalLicenseDto = new ExternalLicenseDto({
mediumId: license.target.uid,
mediaSourceId: license.target.partOf,
});

return externalLicenseDto;
})
.filter((license: ExternalLicenseDto) => license.mediumId !== '');

return externalLicenseDtos;
}
Expand Down

0 comments on commit 48083ae

Please sign in to comment.