"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkPatientTag = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const errors_1 = require("../../lib/sdk/graphql-codegen/errors");
const sdk_1 = require("../../lib/sdk/graphql-codegen/generated/sdk");
const graphqlClient_1 = require("../../lib/sdk/graphql-codegen/graphqlClient");
const config_1 = require("./config");
exports.checkPatientTag = {
    key: 'checkPatientTag',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Check patient tag',
    description: 'Check if a patient has the given tag in their active tags in Healthie.',
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        var _a, _b;
        helpers.log({ fields: payload.fields }, 'Processing checkPatientTag');
        const { fields, settings } = payload;
        const { id, patientId } = fields;
        try {
            const client = (0, graphqlClient_1.initialiseClient)(settings);
            if (client !== undefined) {
                const sdk = (0, sdk_1.getSdk)(client);
                const { data } = await sdk.getUser({ id: patientId });
                const activeTags = (_b = (_a = data.user) === null || _a === void 0 ? void 0 : _a.active_tags) !== null && _b !== void 0 ? _b : [];
                const hasTag = activeTags.some((tag) => tag.id === id);
                await onComplete({
                    data_points: {
                        hasTag: String(hasTag),
                    },
                });
            }
            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) {
            helpers.log({ err }, 'error', 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=checkPatientTag.js.map