"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendFormCompletionRequest = void 0;
const errors_1 = require("../../lib/sdk/graphql-codegen/errors");
const extensions_core_1 = require("@awell-health/extensions-core");
const sdk_1 = require("../../lib/sdk/graphql-codegen/generated/sdk");
const graphqlClient_1 = require("../../lib/sdk/graphql-codegen/graphqlClient");
const sendFormCompletionRequest_zod_1 = require("../../lib/validation/sendFormCompletionRequest.zod");
const lodash_1 = require("lodash");
const config_1 = require("./config");
exports.sendFormCompletionRequest = {
    key: 'sendFormCompletionRequest',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Send form completion request',
    description: 'Send a form completion request to a patient in Healthie.',
    fields: config_1.fields,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        const { fields, settings } = payload;
        try {
            const { healthie_patient_id, form_id, is_recurring, frequency, monthday, weekday, hour, minute, period, ends_on, } = sendFormCompletionRequest_zod_1.FieldsSchema.parse(fields);
            const client = (0, graphqlClient_1.initialiseClient)(settings);
            if (client !== undefined) {
                const sdk = (0, sdk_1.getSdk)(client);
                await sdk.createFormCompletionRequest({
                    input: {
                        /**
                         * Although the Healthie API call allows sending form completion requests to multiple users per API call,
                         * we decided that every action only sends one form completion request.
                         * This heavily simplifies the logic and better fits our domain model.
                         * If a user would like to send multiple form completion requests,
                         * they you can just add multiple actions.
                         */
                        recipient_ids: healthie_patient_id,
                        form: form_id,
                        is_recurring: is_recurring !== null && is_recurring !== void 0 ? is_recurring : false,
                        frequency,
                        monthday,
                        weekday,
                        hour,
                        minute,
                        period,
                        recurrence_ends: !(0, lodash_1.isNil)(ends_on),
                        ends_on,
                    },
                });
                await onComplete();
            }
            else {
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: { en: 'API client requires an API url and API key' },
                            error: {
                                category: 'MISSING_SETTINGS',
                                message: 'Missing api url or api key',
                            },
                        },
                    ],
                });
            }
        }
        catch (err) {
            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=sendFormCompletionRequest.js.map