File

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

Extends

MarkdownPDFGenerator

Index

Properties
Methods

Constructor

constructor(sheetService: SheetService, fileService: FileService, markdownService: MarkdownService)
Parameters :
Name Type Optional
sheetService SheetService No
fileService FileService No
markdownService MarkdownService No

Methods

Private Async convertToZip
convertToZip(undefined: ConvertZipParams)

Generates a ZIP file containing the PDFs generated from the given teamData.

Parameters :
Name Type Optional
ConvertZipParams No
Returns : Promise<NodeJS.ReadableStream>

Zip file content and metadata.

Async generateTeamGradingFiles
generateTeamGradingFiles(params: GenerateTeamGradingParams)

Generates a PDF from the grading of the given team for the given sheet.

Parameters :
Name Type Optional Description
params GenerateTeamGradingParams No

Options passed to the markdown generation function.

Returns : Promise<Buffer | NodeJS.ReadableStream>

Buffer or NodeJS.ReadableStream containing the generated PDF.

Async generateTutorialGradingZIP
generateTutorialGradingZIP(params: GenerateAllTeamsGradingParams)

Generates a ZIP containing a PDF per team of the given tutorial with it's grading for the given sheet.

Parameters :
Name Type Optional Description
params GenerateAllTeamsGradingParams No

Options passed to the markdown generation function.

Returns : Promise<NodeJS.ReadableStream>

ReadableStream containing the ZIP-file containing the PDFs of the gradings.

Private generateZIP
generateZIP(data: ZipData[])

Generates a ZIP file containing the given data.

Parameters :
Name Type Optional Description
data ZipData[] No

Data to put into the ZIP file.

Returns : NodeJS.ReadableStream

NodeJS.ReadableStream of the generated ZIP file.

Public generatePDF
generatePDF(options: GeneratorOptions)
Inherited from PDFGenerator
Defined in PDFGenerator:15
Parameters :
Name Type Optional
options GeneratorOptions No
Returns : Promise<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.

Properties

Private Readonly logger
Default value : new Logger(PDFGenerator.name)
Inherited from PDFGenerator
Defined in PDFGenerator:13
import { Injectable } from '@nestjs/common';
import JSZip from 'jszip';
import { Sheet } from '../../../database/entities/sheet.entity';
import { MarkdownService } from '../../markdown/markdown.service';
import {
    GenerateAllTeamsGradingParams,
    GenerateTeamGradingParams,
    SingleTeamGradings,
} from '../../markdown/markdown.types';
import { SheetService } from '../../sheet/sheet.service';
import { FileService } from '../file.service';
import { MarkdownPDFGenerator } from './PDFGenerator.markdown';

interface ZipData {
    filename: string;
    payload: Buffer;
}

interface ConvertZipParams {
    sheet: Sheet;
    teamData: SingleTeamGradings;
}

@Injectable()
export class GradingPDFGenerator extends MarkdownPDFGenerator {
    constructor(
        private readonly sheetService: SheetService,
        private readonly fileService: FileService,
        markdownService: MarkdownService
    ) {
        super(markdownService);
    }

    /**
     * Generates a PDF from the grading of the given team for the given sheet.
     *
     * @param params Options passed to the markdown generation function.
     *
     * @returns Buffer or NodeJS.ReadableStream containing the generated PDF.
     *
     * @throws `NotFoundException` - If either no team with the given ID or no sheet with the given ID could be found.
     * @throws `BadRequestException` - If the given team does not have any students or the students do not hold a grading for the given sheet.
     */
    async generateTeamGradingFiles(
        params: GenerateTeamGradingParams
    ): Promise<Buffer | NodeJS.ReadableStream> {
        const sheet = await this.sheetService.findById(params.sheetId);
        const teamData = await this.markdownService.getTeamGrading(params);
        const dataCount = teamData.markdownData.length;

        if (dataCount <= 1) {
            return await this.generatePDF({
                markdown: teamData.markdownData[0]?.markdown ?? '',
            });
        } else {
            return this.convertToZip({ sheet, teamData });
        }
    }

    /**
     * Generates a ZIP containing a PDF per team of the given tutorial with it's grading for the given sheet.
     *
     * @param params Options passed to the markdown generation function.
     *
     * @returns ReadableStream containing the ZIP-file containing the PDFs of the gradings.
     *
     * @throws `NotFoundException` - If either no tutorial with the given ID or no sheet with the given ID could be found.
     */
    async generateTutorialGradingZIP(
        params: GenerateAllTeamsGradingParams
    ): Promise<NodeJS.ReadableStream> {
        const sheet = await this.sheetService.findById(params.sheetId);
        const { markdownData: markdownForGradings } =
            await this.markdownService.getAllTeamsGradings(params);
        const files: ZipData[] = [];

        for (const gradingMD of markdownForGradings) {
            files.push({
                filename: await this.fileService.getGradingFilename({
                    sheet,
                    teamName: gradingMD.teamName,
                    extension: 'pdf',
                }),
                payload: await this.generatePDF({
                    markdown: gradingMD.markdown,
                }),
            });
        }

        return this.generateZIP(files);
    }

    /**
     * Generates a ZIP file containing the PDFs generated from the given teamData.
     *
     * @param params Params to generate the ZIP file from.
     *
     * @returns Zip file content and metadata.
     */
    private async convertToZip({
        sheet,
        teamData,
    }: ConvertZipParams): Promise<NodeJS.ReadableStream> {
        const data: ZipData[] = [];

        for (const { markdown, teamName } of teamData.markdownData) {
            const pdf = await this.generatePDF({ markdown });
            data.push({
                filename: await this.fileService.getGradingFilename({
                    sheet,
                    teamName,
                    extension: 'pdf',
                }),
                payload: pdf,
            });
        }

        return this.generateZIP(data);
    }

    /**
     * Generates a ZIP file containing the given data.
     *
     * @param data Data to put into the ZIP file.
     *
     * @returns NodeJS.ReadableStream of the generated ZIP file.
     */
    private generateZIP(data: ZipData[]): NodeJS.ReadableStream {
        const zip = new JSZip();

        data.forEach(({ filename, payload }) => {
            zip.file(filename, payload, { binary: true });
        });

        return zip.generateNodeStream({ type: 'nodebuffer' });
    }
}

results matching ""

    No results matching ""