All files / src/Batch OutlookGetEvents.ts

92.3% Statements 24/26
50% Branches 4/8
100% Functions 2/2
92.3% Lines 24/26

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 677x 7x         7x 7x 7x   7x     7x       2x 2x 2x 2x   2x   2x 1x     2x       2x 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';
import { IInput as IEvent } from '../Connector/OutlookCreateEvent';
import OutlookApplication from '../OutlookApplication';
 
export const NAME = 'outlook-get-events';
export const LAST_RUN_KEY = 'lastRunListEvents';
const LIMIT = 1000;
 
export default class OutlookGetEvents extends ABatchNode {
 
    public getName(): string {
        return NAME;
    }
 
    public async processAction(dto: BatchProcessDto<IInput>): Promise<BatchProcessDto> {
        const { from } = dto.getJsonData();
        const appInstall = await this.getApplicationInstallFromProcess(dto);
        const lastRun = appInstall.getNonEncryptedSettings()[LAST_RUN_KEY];
        let page = Number(dto.getBatchCursor('0'));
 
        let url = `/me/events?$count=true&top=${LIMIT}&$skip=${page * LIMIT}`;
 
        if (from) {
            url = `${url}&$filter=start/dateTime%20gt%20${from}`;
        }
 
        Iif (lastRun) {
            url = `${url}${from ? 'and ' : '&$filter='}lastModifiedDateTime gt ${lastRun}`;
        }
 
        const app = this.getApplication<OutlookApplication>();
        const requestDto = app.getRequestDto(
            dto,
            appInstall,
            HttpMethods.GET,
            url,
        );
 
        const res = await this.getSender().send<IResponseJson>(requestDto);
        const { '@odata.count': count, value } = res.getJsonBody();
 
        Iif (count > ++page * LIMIT) {
            dto.setBatchCursor(page.toString());
        } else {
            appInstall.addNonEncryptedSettings({ [LAST_RUN_KEY]: new Date() });
            await this.getDbClient().getApplicationRepository().update(appInstall);
        }
 
        dto.setItemList(value);
 
        return dto;
    }
 
}
 
export interface IInput {
    from?: string;
}
 
export interface IResponseJson {
    // eslint-disable-next-line @typescript-eslint/naming-convention
    '@odata.count': number;
    value: IEvent[];
}