"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.assignPatientToGroup = void 0;
const lodash_1 = require("lodash");
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 errors_1 = require("../../lib/sdk/graphql-codegen/errors");
const config_1 = require("./config");
exports.assignPatientToGroup = {
    key: 'assignPatientToGroup',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Assign patient to group',
    description: 'Assign or remove a patient from a group in Healthie.',
    fields: config_1.fields,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        const { fields, settings } = payload;
        const { id, groupId } = fields;
        try {
            if ((0, lodash_1.isNil)(id)) {
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: { en: 'Fields are missing' },
                            error: {
                                category: 'MISSING_FIELDS',
                                message: '`id` is missing',
                            },
                        },
                    ],
                });
                return;
            }
            const client = (0, graphqlClient_1.initialiseClient)(settings);
            if (client !== undefined) {
                const sdk = (0, sdk_1.getSdk)(client);
                await sdk.updatePatient({
                    input: {
                        id,
                        // "" group will remove from current group according to docs
                        user_group_id: groupId !== null && groupId !== void 0 ? groupId : '',
                    },
                });
                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=assignPatientToGroup.js.map