"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.signNonVisitNote = 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.signNonVisitNote = {
    key: 'signNonVisitNote',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Sign non-visit note',
    description: 'Sign a non-visit note in Elation.',
    fields: config_1.fields,
    previewable: true,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError }) => {
        var _a;
        const { nonVisitNoteId, signedBy } = config_1.FieldsValidationSchema.parse(payload.fields);
        const api = (0, client_1.makeAPIClient)(payload.settings);
        try {
            await api.updateNonVisitNote(nonVisitNoteId, {
                signed_by: signedBy,
            });
        }
        catch (error) {
            const err = error;
            if (err.status === 404) {
                await onError({
                    events: [
                        (0, addEventLog_1.addActivityEventLog)({
                            message: `The non-visit note (${nonVisitNoteId}) does not exist.`,
                        }),
                    ],
                });
                return;
            }
            if (err.status === 400) {
                const reason = JSON.stringify((_a = err.response) === null || _a === void 0 ? void 0 : _a.data);
                if (reason.includes('signed non visit notes are not editable')) {
                    await onError({
                        events: [
                            (0, addEventLog_1.addActivityEventLog)({
                                message: `Non-visit note was already signed and cannot be signed again.`,
                            }),
                        ],
                    });
                    return;
                }
                /**
                 * A non-visit note can be signed by a Physician or an Office Staff.
                 * You cannot sign a non-visit note using a User ID.
                 * If you pass an invalid ID, you will get a 400 error with a message like:
                 * "Invalid pk \"1425910919331837\" - object does not exist."
                 */
                if (reason.includes('Invalid pk')) {
                    await onError({
                        events: [
                            (0, addEventLog_1.addActivityEventLog)({
                                message: `The signedBy field is not a valid physician or office staff.`,
                            }),
                        ],
                    });
                    return;
                }
            }
            // All other errors we didn't handle explicitly we will throw
            throw error;
        }
        await onComplete();
    },
};
//# sourceMappingURL=signNonVisitNote.js.map