"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendSms = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const validatePayloadAndCreateSdk_1 = require("../../lib/validatePayloadAndCreateSdk");
const config_1 = require("./config");
const addEventLog_1 = require("../../../../src/lib/awell/addEventLog");
exports.sendSms = {
    key: 'sendSms',
    category: extensions_core_1.Category.COMMUNICATION,
    title: 'Send SMS',
    description: 'Send an SMS',
    fields: config_1.fields,
    previewable: false,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError }) => {
        var _a;
        const { fields, zoomSdk } = await (0, validatePayloadAndCreateSdk_1.validatePayloadAndCreateSdk)({
            fieldsSchema: config_1.FieldsValidationSchema,
            payload,
        });
        const { data } = await zoomSdk.sendSms({
            contact_center_number: fields.contactCenterNumber,
            consumer_numbers: [fields.to],
            body: fields.body,
        });
        /**
         * Custom error handling for Zoom API
         * For some errors, Zoom returns a 200 with success: false
         *
         * All other errors are handled with AxiosError and caught in Extensions Core
         */
        if (!data[0].success) {
            await onError({
                events: [
                    (0, addEventLog_1.addActivityEventLog)({
                        message: (_a = data[0].description) !== null && _a !== void 0 ? _a : 'Unknown error',
                    }),
                ],
            });
            return;
        }
        await onComplete({
            data_points: {
                messageId: data[0].message_id,
                success: String(data[0].success),
            },
            events: [
                (0, addEventLog_1.addActivityEventLog)({
                    message: `Success: ${String(data[0].success)}\nMessage ID: ${String(data[0].message_id)}\nDescription: ${String(data[0].description)}`,
                }),
            ],
        });
    },
};
//# sourceMappingURL=sendSms.js.map