All files / src/Batch PinyaAbsencesBatch.ts

95.45% Statements 21/22
83.33% Branches 5/6
100% Functions 6/6
95.45% Lines 21/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 883x 3x     3x   3x   3x 3x       3x       2x   2x           2x 2x 2x 1x                   2x 2x   2x       2x   2x         2x       2x     2x                                                        
import ABatchNode from '@orchesty/nodejs-sdk/dist/lib/Batch/ABatchNode';
import { HttpMethods } from '@orchesty/nodejs-sdk/dist/lib/Transport/HttpMethods';
import BatchProcessDto from '@orchesty/nodejs-sdk/dist/lib/Utils/BatchProcessDto';
 
export const PINYA_ABSENCES_BATCH = 'pinya-absences-batch';
 
export default class PinyaAbsencesBatch extends ABatchNode {
 
    public constructor(resultAsBatch = false, protected batchSize = 50) {
        super(resultAsBatch);
    }
 
    public getName(): string {
        return PINYA_ABSENCES_BATCH;
    }
 
    public async processAction(dto: BatchProcessDto): Promise<BatchProcessDto> {
        const appInstall = await this.getApplicationInstallFromProcess(dto);
 
        const req = await this.getApplication().getRequestDto(
            dto,
            appInstall,
            HttpMethods.GET,
            `absences?${this.processFilter(dto)}`,
        );
        const resp = await this.getSender().send<Response>(req, { success: ['<300', '404'] });
        let response = resp.getJsonBody();
        if (!('data' in response)) { // Instead of empty data, Pinya returns an error
            response = {
                pageNumber: 0,
                pageSize: 1,
                lastPage: 0,
                totalItemsCount: 0,
                count: 0,
                data: [],
            };
        }
 
        await this.processResult(dto, response);
        this.doPagination(dto, response);
 
        return dto;
    }
 
    protected processFilter(dto: BatchProcessDto): string {
        const page = Number(dto.getBatchCursor('0'));
 
        return `PageNumber=${page}&PageSize=${this.batchSize}`;
    }
 
    // eslint-disable-next-line @typescript-eslint/require-await
    protected async processResult(dto: BatchProcessDto, response: Response): Promise<BatchProcessDto> {
        return dto.setItemList(response.data, this.resultAsBatch);
    }
 
    protected doPagination(dto: BatchProcessDto, response: Response): void {
        Iif (response.lastPage > response.pageNumber) {
            dto.setBatchCursor((response.pageNumber + 1).toString());
        } else {
            dto.removeBatchCursor();
        }
    }
 
}
 
export interface PinyaAbsencesOutput {
    id: number;
    dateFrom: string;
    dateTo: string;
    absenceState: string;
    amount: number;
    description: string;
    employeeNumber: string;
    absenceTypeCode: string;
    absenceTypeTitle: string;
    absenceTypeShortcut: string;
    isWorktime: boolean;
}
 
export interface Response {
    pageNumber: number;
    pageSize: number;
    lastPage: number;
    totalItemsCount: number;
    count: number;
    data: PinyaAbsencesOutput[];
}