"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createLabOrder = void 0;
/* eslint-disable @typescript-eslint/naming-convention */
const zod_1 = require("zod");
const extensions_core_1 = require("@awell-health/extensions-core");
const client_1 = require("../client");
const zod_validation_error_1 = require("zod-validation-error");
const axios_1 = require("axios");
const labOrder_zod_1 = require("../validation/labOrder.zod");
const fields = {
    patientId: {
        id: 'patientId',
        label: 'Patient ID',
        description: 'The ID of the patient for whom the lab order is being created.',
        type: extensions_core_1.FieldType.NUMERIC,
        required: true,
    },
    practiceId: {
        id: 'practiceId',
        label: 'Practice',
        description: 'The ID of the practice for which the lab order is being created.',
        type: extensions_core_1.FieldType.NUMERIC,
        required: true,
    },
    documentDate: {
        id: 'documentDate',
        label: 'Document Date',
        description: 'The date and time of the lab order document.',
        type: extensions_core_1.FieldType.DATE,
        required: true,
    },
    orderingPhysicianId: {
        id: 'orderingPhysicianId',
        label: 'Ordering Physician ID',
        description: 'The ID of the physician who is creating the order.',
        type: extensions_core_1.FieldType.NUMERIC,
        required: true,
    },
    vendorId: {
        id: 'vendorId',
        label: 'Vendor ID',
        description: 'The ID of the vendor (or Lab) associated with the lab order.',
        type: extensions_core_1.FieldType.NUMERIC,
        required: false,
    },
    content: {
        id: 'content',
        label: 'Content',
        description: 'Represents the content of the lab order report. Should be in JSON format. See https://docs.elationhealth.com/reference/the-lab-order-content-object',
        type: extensions_core_1.FieldType.JSON,
        required: false,
    },
    siteId: {
        id: 'siteId',
        label: 'Site ID',
        description: 'The location where the patient will have the lab performed. Sites are specific to their lab vendor',
        type: extensions_core_1.FieldType.NUMERIC,
        required: false,
    },
    confidential: {
        id: 'confidential',
        label: 'Confidential',
        description: 'Defaults to false. Whether the provider has marked the order as confidential. Should not be shared with a patient.',
        type: extensions_core_1.FieldType.BOOLEAN,
        required: false,
    },
};
const dataPoints = {
    labOrderId: {
        key: 'labOrderId',
        valueType: 'number',
    },
    printableLabOrderView: {
        key: 'printableLabOrderView',
        valueType: 'string',
    },
};
exports.createLabOrder = {
    key: 'createLabOrder',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Create lab order',
    description: 'A lab order represents an order for a patient to perform some lab tests for a vendor.',
    fields,
    previewable: true,
    dataPoints,
    onActivityCreated: async (payload, onComplete, onError) => {
        var _a, _b;
        try {
            const { patientId, practiceId, orderingPhysicianId, documentDate, vendorId, content, siteId, confidential, } = payload.fields;
            const labOrder = labOrder_zod_1.labOrderSchema.parse({
                practice: practiceId,
                patient: patientId,
                document_date: documentDate,
                ordering_physician: orderingPhysicianId,
                vendor: vendorId,
                content,
                confidential,
                site: siteId,
            });
            const api = (0, client_1.makeAPIClient)(payload.settings);
            const { id, printable_view } = await api.createLabOrder(labOrder);
            await onComplete({
                data_points: {
                    labOrderId: String(id),
                    printableLabOrderView: printable_view,
                },
            });
        }
        catch (err) {
            if (err instanceof zod_1.ZodError) {
                const error = (0, zod_validation_error_1.fromZodError)(err);
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: { en: error.message },
                            error: {
                                category: 'WRONG_INPUT',
                                message: error.message,
                            },
                        },
                    ],
                });
            }
            else if (err instanceof axios_1.AxiosError) {
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: {
                                en: `${(_a = err.status) !== null && _a !== void 0 ? _a : '(no status code)'} Error: ${err.message}`,
                            },
                            error: {
                                category: 'SERVER_ERROR',
                                message: `${(_b = err.status) !== null && _b !== void 0 ? _b : '(no status code)'} Error: ${err.message}`,
                            },
                        },
                    ],
                });
            }
            else {
                const message = err.message;
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: { en: message },
                            error: {
                                category: 'SERVER_ERROR',
                                message,
                            },
                        },
                    ],
                });
            }
        }
    },
};
//# sourceMappingURL=createLabOrder.js.map