All files / src/Batch AMailstepListBatch.ts

65.9% Statements 29/44
28.57% Branches 4/14
80% Functions 12/15
67.44% Lines 29/43

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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 18722x 22x 22x   22x 22x   22x   22x                     11x                             11x 11x   11x         11x     11x     11x     11x         11x       8x                         8x         11x         33x       11x         11x       3x   3x                   22x                           11x 11x                               11x       11x                                                                                    
import CoreFormsEnum from '@orchesty/nodejs-sdk/dist/lib/Application/Base/CoreFormsEnum';
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';
import { StatusCodes } from 'http-status-codes';
import { ESHOP_ID } from '../MailstepApplication';
 
export const LAST_RUN = 'lastRun';
 
export default abstract class AMailstepListBatch<
    IInput,
    IOutput,
    Select extends string,
    Filter extends string,
    Sorter extends string,
> extends ABatchNode {
 
    protected abstract getUrl(): string;
 
    public async processAction(dto: BatchProcessDto<IInput>): Promise<BatchProcessDto<IOutput>> {
        const requestDto = await this.getApplication().getRequestDto(
            dto,
            await this.getApplicationInstallFromProcess(dto),
            HttpMethods.POST,
            this.getUrl(),
            {
                select: await this.getSelects(dto),
                criteria: await this.getFilters(dto),
                sort: await this.getRawSorters(dto),
                limit: await this.getLimit(dto),
                offset: await this.getOffset(dto),
                nested: await this.getNested(dto),
            },
        );
 
        const responseDto = await this.getSender().send<IResponse<IOutput>>(requestDto, [StatusCodes.OK]);
        const response = responseDto.getJsonBody();
 
        Iif (await this.useAsBatch(dto)) {
            if (response.results.length) {
                dto.addItem(response.results);
            }
        } else {
            dto.setItemList(response.results);
        }
 
        Iif (response.paging.returned === await this.getLimit(dto)) {
            dto.setBatchCursor((Number(dto.getBatchCursor('1')) + 1).toString());
        } else {
            await this.setLastRun(dto);
        }
 
        return dto as unknown as Promise<BatchProcessDto<IOutput>>;
    }
 
    // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/require-await
    protected async getSelects(dto: BatchProcessDto<IInput>): Promise<Select[]> {
        return [];
    }
 
    protected async getFilters(dto: BatchProcessDto<IInput>): Promise<IFilter<Filter>> {
        Iif (await this.useLastRun(dto)) {
            const lastRun = await this.getLastRun(dto);
 
            if (lastRun) {
                return {
                    // @ts-expect-error Intentionally
                    changedAt: {
                        gte: lastRun,
                    },
                };
            }
        }
 
        return {};
    }
 
    // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/require-await
    protected async getSorters(dto: BatchProcessDto<IInput>): Promise<ISorter<Sorter>> {
        return {};
    }
 
    // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/require-await
    protected async getLimit(dto: BatchProcessDto<IInput>): Promise<number> {
        return 1_000;
    }
 
    protected async getOffset(dto: BatchProcessDto<IInput>): Promise<number> {
        return (Number(dto.getBatchCursor('1')) - 1) * await this.getLimit(dto);
    }
 
    // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/require-await
    protected async getNested(dto: BatchProcessDto<IInput>): Promise<boolean> {
        return false;
    }
 
    protected async getEshopId(dto: BatchProcessDto<IInput>): Promise<string> {
        const applicationInstall = await this.getApplicationInstallFromProcess(dto);
 
        return applicationInstall.getSettings()[CoreFormsEnum.AUTHORIZATION_FORM][ESHOP_ID];
    }
 
    // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/require-await
    protected async getLastRunKey(dto: BatchProcessDto<IInput>): Promise<string> {
        return this.getUrl();
    }
 
    // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/require-await
    protected async useLastRun(dto: BatchProcessDto<IInput>): Promise<boolean> {
        return false;
    }
 
    protected async getLastRun(dto: BatchProcessDto<IInput>): Promise<string | undefined> {
        if (!await this.useLastRun(dto)) {
            return undefined;
        }
 
        const applicationInstall = await this.getApplicationInstallFromProcess(dto);
 
        return applicationInstall.getNonEncryptedSettings()[LAST_RUN]?.[await this.getLastRunKey(dto)];
    }
 
    protected async setLastRun(dto: BatchProcessDto<IInput>): Promise<void> {
        Eif (!await this.useLastRun(dto)) {
            return;
        }
 
        const applicationInstall = await this.getApplicationInstallFromProcess(dto);
 
        applicationInstall.addNonEncryptedSettings({
            [LAST_RUN]: {
                [await this.getLastRunKey(dto)]: new Date().toISOString(),
            },
        });
 
        await this.getDbClient().getApplicationRepository().update(applicationInstall);
    }
 
    // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/require-await
    protected async useAsBatch(dto: BatchProcessDto<IInput>): Promise<boolean> {
        return false;
    }
 
    private async getRawSorters(dto: BatchProcessDto<IInput>): Promise<IRawSorter[]> {
        return Object.entries(await this.getSorters(dto)).map(([field, order]) => ({
            field,
            order: order as unknown as IDirection, // eslint-disable-line @typescript-eslint/no-unnecessary-type-assertion
        }));
    }
 
}
 
export type IFilter<Filter extends string> = Partial<Record<Filter, ICondition>>;
 
export type ISorter<Sorter extends string> = Partial<Record<Sorter, IDirection>>;
 
interface ICondition {
    eq?: string | number;
    neq?: string | number;
    in?: string[] | number[];
    nin?: string[] | number[];
    like?: string | number;
    notLike?: string | number;
    gt?: number;
    gte?: number;
    lt?: number;
    lte?: number;
    null?: boolean;
}
 
interface IResponse<IOutput> {
    results: IOutput[];
    paging: {
        returned: number;
        from: number;
        to: number;
        total: number;
    };
}
 
interface IRawSorter {
    field: string;
    order: IDirection;
}
 
type IDirection = 'ASC' | 'DESC';