File

src/module/pdf/subservices/PDFGenerator.withStudents.ts

Extends

PDFGenerator

Index

Properties
Methods

Properties

Private Readonly logger
Default value : new Logger(PDFGenerator.name)
Inherited from PDFGenerator
Defined in PDFGenerator:13

Methods

Private Static getFirstDifferentPosition
getFirstDifferentPosition(first: string, second: string)

Searches for the position of the first character which both strings DO NOT have in common. This position is then returned.

Parameters :
Name Type Optional Description
first string No

First string

second string No

Second string

Returns : number

The first position in which both string differ. If they are completely equal the length of the first string is returned.

Protected getShortenedMatriculationNumbers
getShortenedMatriculationNumbers(students: (Student | IStudent)[])

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 considers the given students. If one extends that array without re-running this function the identifying feature may get lost.

Parameters :
Name Type Optional Description
students (Student | IStudent)[] No

All students to get the shortened number from.

The shortened but still identifying matriculation numbers of all given students.

Private Static reverseString
reverseString(string: string)
Parameters :
Name Type Optional Description
string string No

String to reverse

Returns : string

The reversed string.

Public Abstract generatePDF
generatePDF(options: T)
Inherited from PDFGenerator
Defined in PDFGenerator:22

Generates a PDF from the given options.

Parameters :
Name Type Optional Description
options T No

Options from which the PDF gets generated.

Returns : Promise<Buffer>

Generated PDF as Buffer.

Protected Async generatePDFFromBodyContent
generatePDFFromBodyContent(body: string)
Inherited from PDFGenerator
Defined in PDFGenerator:31

Generates a PDF from the given body. The body gets put in an HTML wrapper first.

Parameters :
Name Type Optional Description
body string No

Body content to be put in the PDF as HTML body.

Returns : Promise<Buffer>

Buffer containing the generated PDF.

Private Static getCustomCSS
getCustomCSS()
Inherited from PDFGenerator
Defined in PDFGenerator:114
Returns : string

Some small customizations to the GitHub markdown CSS.

Private Async getGithubMarkdownCSS
getGithubMarkdownCSS()
Inherited from PDFGenerator
Defined in PDFGenerator:88
Returns : Promise<string>

The GitHub markdown CSS.

Private Async getHighlightCSS
getHighlightCSS()
Inherited from PDFGenerator
Defined in PDFGenerator:95
Returns : Promise<string>

The HighlightJS CSS.

Private Async loadCSSFile
loadCSSFile(moduleName: string)
Inherited from PDFGenerator
Defined in PDFGenerator:99
Parameters :
Name Type Optional
moduleName string No
Returns : Promise<string>
Private Async putBodyInHTML
putBodyInHTML(body: string)
Inherited from PDFGenerator
Defined in PDFGenerator:69

Puts the given body in corresponding a <body> element. The returned string is a complete HTML "file" with slightly customized GitHub Markdown CSS.

Parameters :
Name Type Optional Description
body string No

Body to embed in HTML.

Returns : Promise<string>

Complete HTML "file" which contains the given body as body.

import { PDFGenerator } from './PDFGenerator.core';
import { IStudent } from 'shared/model/Student';
import { Student } from '../../../database/entities/student.entity';

interface ShortenedMatriculationInfo {
    studentId: string;
    shortenedNo: string;
}

export abstract class PDFWithStudentsGenerator<T> extends PDFGenerator<T> {
    /**
     * 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 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: (Student | IStudent)[]
    ): ShortenedMatriculationInfo[] {
        const result: ShortenedMatriculationInfo[] = [];
        const matriculationNos: { id: string; reversedNumber: string }[] = [];

        for (const student of students) {
            if (student.matriculationNo) {
                matriculationNos.push({
                    id: student.id,
                    reversedNumber: PDFWithStudentsGenerator.reverseString(student.matriculationNo),
                });
            }
        }

        matriculationNos.sort((a, b) => a.reversedNumber.localeCompare(b.reversedNumber));

        matriculationNos.forEach((current, idx) => {
            let positionPrev: number = 0;
            let positionNext: number = 0;

            if (idx !== 0) {
                const prev = matriculationNos[idx - 1];
                positionPrev = PDFWithStudentsGenerator.getFirstDifferentPosition(
                    current.reversedNumber,
                    prev.reversedNumber
                );
            }

            if (idx !== matriculationNos.length - 1) {
                const next = matriculationNos[idx + 1];
                positionNext = PDFWithStudentsGenerator.getFirstDifferentPosition(
                    current.reversedNumber,
                    next.reversedNumber
                );
            }

            const position: number = Math.max(positionPrev, positionNext);
            const substring = PDFWithStudentsGenerator.reverseString(
                current.reversedNumber.substr(0, position + 1)
            );

            result.push({
                studentId: current.id,
                shortenedNo: substring.padStart(7, '*'),
            });
        });

        return result.sort((a, b) => a.shortenedNo.localeCompare(b.shortenedNo));
    }

    /**
     * Searches for the position of the first character which both strings __DO NOT__ have in common. This position is then returned.
     *
     * @param first First string
     * @param second Second string
     * @returns The first position in which both string differ. If they are completely equal the length of the first string is returned.
     */
    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;
            }
        }

        return first.length;
    }

    /**
     * @param string String to reverse
     * @returns The reversed string.
     */
    private static reverseString(string: string): string {
        return string.split('').reverse().join('');
    }
}

results matching ""

    No results matching ""