"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAppointment = 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 dateValidation_1 = require("../../lib/validation/dateValidation");
const config_1 = require("./config");
exports.getAppointment = {
    key: 'getAppointment',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Get appointment',
    description: 'Retrieve the details of an appointment 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, _j;
        const { fields, settings } = payload;
        const { appointmentId } = fields;
        try {
            const client = (0, graphqlClient_1.initialiseClient)(settings);
            if (client !== undefined) {
                const sdk = (0, sdk_1.getSdk)(client);
                const { data } = await sdk.getAppointment({
                    id: appointmentId,
                });
                const date = (0, dateValidation_1.convertDate)((_a = data.appointment) === null || _a === void 0 ? void 0 : _a.date);
                await onComplete({
                    data_points: {
                        appointmentTypeId: (_c = (_b = data.appointment) === null || _b === void 0 ? void 0 : _b.appointment_type) === null || _c === void 0 ? void 0 : _c.id,
                        appointmentTypeName: (_e = (_d = data.appointment) === null || _d === void 0 ? void 0 : _d.appointment_type) === null || _e === void 0 ? void 0 : _e.name,
                        contactType: (_f = data.appointment) === null || _f === void 0 ? void 0 : _f.contact_type,
                        date,
                        patientId: (_h = (_g = data === null || data === void 0 ? void 0 : data.appointment) === null || _g === void 0 ? void 0 : _g.user) === null || _h === void 0 ? void 0 : _h.id,
                        appointmentStatus: (_j = data.appointment) === null || _j === void 0 ? void 0 : _j.pm_status,
                    },
                });
            }
            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=getAppointment.js.map