"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPatient = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const validatePayloadAndCreateSdks_1 = require("../../lib/validatePayloadAndCreateSdks");
const axios_1 = require("axios");
const awell_1 = require("../../../../src/lib/awell");
exports.getPatient = {
    key: 'getPatient',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Get patient',
    description: 'Retrieve patient details from Cerner',
    fields: config_1.fields,
    previewable: true,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError }) => {
        const { cernerFhirR4Sdk, fields: { resourceId }, } = await (0, validatePayloadAndCreateSdks_1.validatePayloadAndCreateSdks)({
            fieldsSchema: config_1.FieldsValidationSchema,
            payload,
        });
        try {
            const { data } = await cernerFhirR4Sdk.getPatient(resourceId);
            await onComplete({
                data_points: {
                    patient: JSON.stringify(data),
                },
            });
        }
        catch (error) {
            if (error instanceof axios_1.AxiosError) {
                const err = error;
                if (err.status === 404)
                    await onError({
                        events: [
                            (0, awell_1.addActivityEventLog)({
                                message: 'Patient not found',
                            }),
                        ],
                    });
                return;
            }
            // Throw all other errors
            throw error;
        }
    },
};
//# sourceMappingURL=getPatient.js.map