"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.updatePatient = void 0;
const lodash_1 = require("lodash");
const extensions_core_1 = require("@awell-health/extensions-core");
const validatePayloadAndCreateSdk_1 = require("../../lib/sdk/validatePayloadAndCreateSdk");
const errors_1 = require("../../lib/sdk/graphql-codegen/errors");
const config_1 = require("./config");
const zod_1 = require("zod");
exports.updatePatient = {
    key: 'updatePatient',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Update a patient',
    description: 'Update a patient in Healthie.',
    fields: config_1.fields,
    previewable: true,
    onEvent: async ({ payload, onComplete, onError }) => {
        try {
            const { fields, healthieSdk } = await (0, validatePayloadAndCreateSdk_1.validatePayloadAndCreateSdk)({
                fieldsSchema: config_1.FieldsValidationSchema,
                payload,
            });
            const dietitian_id = fields.provider_id === '' || (0, lodash_1.isNil)(fields.provider_id)
                ? undefined
                : fields.provider_id;
            await healthieSdk.client.mutation({
                updateClient: {
                    __args: {
                        input: {
                            id: fields.id,
                            first_name: fields.first_name,
                            last_name: fields.last_name,
                            legal_name: fields.legal_name,
                            email: fields.email,
                            phone_number: fields.phone_number,
                            dietitian_id,
                            gender: fields.gender,
                            gender_identity: fields.gender_identity,
                            height: fields.height,
                            sex: fields.sex,
                            user_group_id: fields.user_group_id,
                            active: fields.active,
                            dob: fields.dob,
                            resend_welcome: fields.resend_welcome_email,
                            record_identifier: fields.record_identifier,
                            additional_record_identifier: fields.additional_record_identifier,
                        },
                    },
                    user: {
                        id: true,
                    },
                    messages: {
                        __scalar: true,
                    },
                },
            });
            await onComplete();
        }
        catch (err) {
            if (err instanceof errors_1.HealthieError) {
                const errors = (0, errors_1.mapHealthieToActivityError)(err.errors);
                await onError({
                    events: errors,
                });
            }
            else if (err instanceof zod_1.ZodError) {
                throw err;
            }
            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=updatePatient.js.map