"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.updatePatientQuickNote = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const extensions_core_2 = require("@awell-health/extensions-core");
const graphqlClient_1 = require("../../lib/sdk/graphql-codegen/graphqlClient");
const settings_1 = require("../../settings");
const errors_1 = require("../../lib/sdk/graphql-codegen/errors");
const zod_1 = require("zod");
const zod_validation_error_1 = require("zod-validation-error");
const config_1 = require("./config");
const sdk_1 = require("../../lib/sdk/graphql-codegen/generated/sdk");
exports.updatePatientQuickNote = {
    key: 'updatePatientQuickNote',
    category: extensions_core_2.Category.EHR_INTEGRATIONS,
    title: 'Update patient quick note',
    description: 'Update a patient quick note in Healthie.',
    fields: config_1.fields,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        var _a, _b;
        try {
            const { settings, fields: { patientId, overwrite, quickNote }, } = (0, extensions_core_1.validate)({
                schema: zod_1.z.object({
                    settings: settings_1.SettingsValidationSchema,
                    fields: config_1.FieldsValidationSchema,
                }),
                payload,
            });
            const client = (0, graphqlClient_1.initialiseClient)(settings);
            if (client != null) {
                const sdk = (0, sdk_1.getSdk)(client);
                if (overwrite) {
                    await sdk.updatePatient({
                        input: { id: patientId, quick_notes: quickNote },
                    });
                }
                else {
                    const { data } = await sdk.getUser({ id: patientId });
                    const currentNotes = (_b = (_a = data === null || data === void 0 ? void 0 : data.user) === null || _a === void 0 ? void 0 : _a.quick_notes) !== null && _b !== void 0 ? _b : '';
                    await sdk.updatePatient({
                        input: {
                            id: patientId,
                            quick_notes: currentNotes.concat(quickNote),
                        },
                    });
                }
                await onComplete({});
            }
        }
        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 errors_1.HealthieError) {
                const errors = (0, errors_1.mapHealthieToActivityError)(err.errors);
                await onError({
                    events: errors,
                });
            }
            else {
                const error = err;
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: { en: 'Healthie API reported an error' },
                            error: {
                                category: 'SERVER_ERROR',
                                message: error.message,
                            },
                        },
                    ],
                });
            }
        }
    },
};
//# sourceMappingURL=updatePatientQuickNote.js.map