"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateReferralOrderResolution = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const client_1 = require("../../client");
const config_1 = require("./config");
const addEventLog_1 = require("../../../../src/lib/awell/addEventLog");
exports.updateReferralOrderResolution = {
    key: 'updateReferralOrderResolution',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Update referral order resolution',
    description: 'Update the resolution of a referral order in Elation.',
    fields: config_1.fields,
    previewable: false,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError }) => {
        const { referralOrderId, resolutionState, resolvingDocument } = config_1.FieldsValidationSchema.parse(payload.fields);
        const api = (0, client_1.makeAPIClient)(payload.settings);
        try {
            await api.updateReferralOrder(referralOrderId, {
                resolution: {
                    state: resolutionState,
                    resolving_document: resolvingDocument,
                },
            });
        }
        catch (error) {
            const err = error;
            if (err.status === 404) {
                await onError({
                    events: [
                        (0, addEventLog_1.addActivityEventLog)({
                            message: `The referral order (${referralOrderId}) does not exist.`,
                        }),
                    ],
                });
                return;
            }
            // All other errors we didn't handle explicitly we will throw
            throw error;
        }
        await onComplete();
    },
};
//# sourceMappingURL=updateReferralOrderResolution.js.map