"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPatient = 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 helpers_1 = require("../../lib/helpers");
const getPatient_zod_1 = require("../../lib/validation/getPatient.zod");
const config_1 = require("./config");
exports.getPatient = {
    key: 'getPatient',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Get patient',
    description: 'Retrieve the details of a patient in Healthie.',
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        var _a, _b, _c, _d, _e, _f, _g, _h;
        const { fields, settings } = payload;
        const { 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 { data: { dob, phoneNumber }, events, } = (0, getPatient_zod_1.validateGetPatient)(data.user);
                await onComplete({
                    data_points: {
                        firstName: (_a = data.user) === null || _a === void 0 ? void 0 : _a.first_name,
                        lastName: (_b = data.user) === null || _b === void 0 ? void 0 : _b.last_name,
                        dob,
                        email: (_c = data.user) === null || _c === void 0 ? void 0 : _c.email,
                        gender: (_d = data.user) === null || _d === void 0 ? void 0 : _d.gender,
                        isoSex: String((0, helpers_1.mapHealthieGenderToIsoSex)((_e = data.user) === null || _e === void 0 ? void 0 : _e.gender)),
                        phoneNumber,
                        groupName: (_g = (_f = data.user) === null || _f === void 0 ? void 0 : _f.user_group) === null || _g === void 0 ? void 0 : _g.name,
                        primaryProviderId: (_h = data.user) === null || _h === void 0 ? void 0 : _h.dietitian_id,
                    },
                    events,
                });
            }
            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=getPatient.js.map