All files / src/Connector ShoptetJobFinishedWebhook.ts

66.66% Statements 16/24
50% Branches 4/8
100% Functions 2/2
66.66% Lines 16/24

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  12x 12x   12x   12x     12x       1x 1x           1x 1x 1x         1x 1x   1x 1x     1x         1x                                                          
import ProcessDto from '@orchesty/nodejs-sdk/dist/lib/Utils/ProcessDto';
import ResultCode from '@orchesty/nodejs-sdk/dist/lib/Utils/ResultCode';
import AShoptetConnector from './AShoptetConnector';
 
export const NAME = 'shoptet-job-finished-webhook';
 
export default class ShoptetJobFinishedWebhook extends AShoptetConnector {
 
    public getName(): string {
        return NAME;
    }
 
    public async processAction(dto: ProcessDto<IInput>): Promise<ProcessDto<IInput> | ProcessDto<IOutput>> {
        const data = dto.getJsonData();
        Iif (data.event !== 'job:finished') {
            dto.setStopProcess(ResultCode.STOP_AND_FAILED, `Event [event=${data.event}] is not supported`);
 
            return dto;
        }
 
        const repo = this.getDbClient().getApplicationRepository();
        const appInstall = await repo.findOne({ nonEncrypted: { eshopId: data.eshopId.toString() }, enabled: null });
        Iif (!appInstall) {
            dto.setStopProcess(ResultCode.DO_NOT_CONTINUE, `Shoptet with eshopId [${data.eshopId}] is not installed.`);
 
            return dto;
        }
        dto.setUser(appInstall.getUser());
        const response = await this.doRequest(`api/system/jobs/${data.eventInstance}`, dto, undefined, appInstall) as IResponse;
 
        const { job } = response.data;
        Iif (job.endpoint === '/api/products/snapshot') {
            appInstall.addNonEncryptedSettings({ lastRunListProductChanges: job.completionTime });
            await repo.update(appInstall);
        } else Iif (job.endpoint === '/api/orders/snapshot') {
            appInstall.addNonEncryptedSettings({ lastRunListOrderChanges: job.completionTime });
            await repo.update(appInstall);
        }
 
        return dto.setNewJsonData(response.data.job);
    }
 
}
 
interface IInput {
    eshopId: number;
    event: string;
    eventCreated: string;
    eventInstance: string;
}
 
interface IResponse {
    data: {
        job: IOutput;
    };
}
 
export interface IOutput {
    jobId: string;
    endpoint: string;
    creationTime: string;
    duration: string;
    completionTime: string;
    status: string;
    resultUrl: string;
    validUntil: string;
    log: string;
}