From dbfed5b76e20e278b62873364b560a4b3be6814b Mon Sep 17 00:00:00 2001 From: Dudrie Date: Sat, 19 Feb 2022 09:07:17 +0100 Subject: [PATCH 1/3] Fix unecessary fetch of students in ScheinResultsPDFGenerator. --- .../module/pdf/subservices/PDFGenerator.schein.ts | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/server/src/module/pdf/subservices/PDFGenerator.schein.ts b/server/src/module/pdf/subservices/PDFGenerator.schein.ts index 6de0ed29c..dc5a60396 100644 --- a/server/src/module/pdf/subservices/PDFGenerator.schein.ts +++ b/server/src/module/pdf/subservices/PDFGenerator.schein.ts @@ -27,15 +27,12 @@ export class ScheinResultsPDFGenerator extends PDFWithStudentsGenerator { - const [allStudents, summaries] = await Promise.all([ - this.studentService.findAll(), - this.scheincriteriaService.getResultsOfAllStudents(), - ]); + public async generatePDF({ enableShortMatriculationNo }: GeneratorOptions): Promise { + const summaries = await this.scheincriteriaService.getResultsOfAllStudents(); - const students = allStudents.filter((student) => !!student.matriculationNo); + const students = Object.values(summaries) + .map((s) => s.student) + .filter((student) => !!student.matriculationNo); const shortenedMatriculationNumbers = this.getShortenedMatriculationNumbers(students); const statuses: Scheinstatus[] = []; const template = this.templateService.getScheinstatusTemplate(); @@ -50,7 +47,7 @@ export class ScheinResultsPDFGenerator extends PDFWithStudentsGenerator s.id === studentId)?.matriculationNo; From 7fa22ad41f8bdc77f186673fa079f16641408a28 Mon Sep 17 00:00:00 2001 From: Dudrie Date: Sat, 19 Feb 2022 09:17:18 +0100 Subject: [PATCH 2/3] Fix typing issue. --- .../subservices/PDFGenerator.withStudents.ts | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/server/src/module/pdf/subservices/PDFGenerator.withStudents.ts b/server/src/module/pdf/subservices/PDFGenerator.withStudents.ts index af4c0535a..025be9213 100644 --- a/server/src/module/pdf/subservices/PDFGenerator.withStudents.ts +++ b/server/src/module/pdf/subservices/PDFGenerator.withStudents.ts @@ -1,5 +1,5 @@ import { PDFGenerator } from './PDFGenerator.core'; -import { StudentDocument } from '../../../database/models/student.model'; +import { IStudent } from 'shared/model/Student'; interface ShortendMatriculationInfo { studentId: string; @@ -16,9 +16,7 @@ export abstract class PDFWithStudentsGenerator extends PDFGenerator { * * @returns The shortened but still identifying matriculation numbers of all given students. */ - protected getShortenedMatriculationNumbers( - students: StudentDocument[] - ): ShortendMatriculationInfo[] { + protected getShortenedMatriculationNumbers(students: IStudent[]): ShortendMatriculationInfo[] { const result: ShortendMatriculationInfo[] = []; const matriculationNos: { id: string; reversedNumber: string }[] = []; @@ -26,7 +24,7 @@ export abstract class PDFWithStudentsGenerator extends PDFGenerator { if (student.matriculationNo) { matriculationNos.push({ id: student.id, - reversedNumber: this.reverseString(student.matriculationNo), + reversedNumber: PDFWithStudentsGenerator.reverseString(student.matriculationNo), }); } } @@ -39,7 +37,7 @@ export abstract class PDFWithStudentsGenerator extends PDFGenerator { if (idx !== 0) { const prev = matriculationNos[idx - 1]; - positionPrev = this.getFirstDifferentPosition( + positionPrev = PDFWithStudentsGenerator.getFirstDifferentPosition( current.reversedNumber, prev.reversedNumber ); @@ -47,14 +45,16 @@ export abstract class PDFWithStudentsGenerator extends PDFGenerator { if (idx !== matriculationNos.length - 1) { const next = matriculationNos[idx + 1]; - positionNext = this.getFirstDifferentPosition( + positionNext = PDFWithStudentsGenerator.getFirstDifferentPosition( current.reversedNumber, next.reversedNumber ); } const position: number = Math.max(positionPrev, positionNext); - const substring = this.reverseString(current.reversedNumber.substr(0, position + 1)); + const substring = PDFWithStudentsGenerator.reverseString( + current.reversedNumber.substr(0, position + 1) + ); result.push({ studentId: current.id, @@ -72,7 +72,7 @@ export abstract class PDFWithStudentsGenerator extends PDFGenerator { * @param second Second string * @returns The first position in which both string differ. If they are completly equal the length of the first string is returned. */ - private getFirstDifferentPosition(first: string, second: string): number { + private static getFirstDifferentPosition(first: string, second: string): number { for (let i = 0; i < first.length; i++) { if (first.charAt(i) !== second.charAt(i)) { return i; @@ -86,7 +86,7 @@ export abstract class PDFWithStudentsGenerator extends PDFGenerator { * @param string String to reverse * @returns The reversed string. */ - private reverseString(string: string): string { + private static reverseString(string: string): string { return string.split('').reverse().join(''); } } From 87195c9a5298c82a9aa634eec76a58c2e18fb322 Mon Sep 17 00:00:00 2001 From: Dudrie Date: Sat, 19 Feb 2022 09:23:34 +0100 Subject: [PATCH 3/3] Fix typing. --- .../module/pdf/subservices/PDFGenerator.withStudents.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/server/src/module/pdf/subservices/PDFGenerator.withStudents.ts b/server/src/module/pdf/subservices/PDFGenerator.withStudents.ts index 025be9213..a8ca5e6bc 100644 --- a/server/src/module/pdf/subservices/PDFGenerator.withStudents.ts +++ b/server/src/module/pdf/subservices/PDFGenerator.withStudents.ts @@ -1,5 +1,6 @@ import { PDFGenerator } from './PDFGenerator.core'; import { IStudent } from 'shared/model/Student'; +import { StudentDocument } from '../../../database/models/student.model'; interface ShortendMatriculationInfo { studentId: string; @@ -10,13 +11,15 @@ export abstract class PDFWithStudentsGenerator extends PDFGenerator { /** * Returns the shortened number for all students together with the ID of the student to which the shortened matriculation number belongs to. * - * Those shortened numbers are still enough to identify a student. However, this is only true if one only consideres the given students. If one extends that array without re-running this function the identifying feature may get lost. + * Those shortened numbers are still enough to identify a student. However, this is only true if one only considers the given students. If one extends that array without re-running this function the identifying feature may get lost. * * @param students All students to get the shortened number from. * * @returns The shortened but still identifying matriculation numbers of all given students. */ - protected getShortenedMatriculationNumbers(students: IStudent[]): ShortendMatriculationInfo[] { + protected getShortenedMatriculationNumbers( + students: (StudentDocument | IStudent)[] + ): ShortendMatriculationInfo[] { const result: ShortendMatriculationInfo[] = []; const matriculationNos: { id: string; reversedNumber: string }[] = [];