"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.uploadContactToCampaign = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const settings_1 = require("../../settings");
const lib_1 = require("../../lib");
const zod_1 = require("zod");
const lodash_1 = require("lodash");
exports.uploadContactToCampaign = {
    key: 'uploadContactToCampaign',
    title: 'Upload Contact to Campaign',
    description: 'Add a contact to a Gridspace autodialer campaign.',
    category: extensions_core_1.Category.COMMUNICATION,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: false,
    onEvent: async ({ payload, onComplete }) => {
        const { pathway, patient, activity } = payload;
        const { fields: { campaignId, data, phoneNumber }, settings: { accountId, clientSecret }, } = (0, extensions_core_1.validate)({
            schema: zod_1.z.object({ fields: config_1.FieldsSchema, settings: settings_1.SettingsSchema }),
            payload,
        });
        const client = new lib_1.GridspaceClient({ accountId, clientSecret });
        const contactData = {
            phone_number: phoneNumber,
            ...data,
            activity_id: activity.id,
            pathway_id: pathway.id,
            pathway_definition_id: pathway.definition_id,
            patient_id: patient.id,
        };
        const columnNames = (0, lodash_1.keys)(contactData);
        const contactRow = (0, lodash_1.values)(contactData);
        const requestBody = {
            column_names: columnNames,
            phone_number_column_name: 'phone_number',
            contact_rows: [contactRow],
        };
        const { num_uploaded_contacts = 0 } = await client.uploadContactsToCampaign(campaignId, requestBody);
        const event_text = num_uploaded_contacts === 0
            ? { en: `Contact was NOT uploaded to campaign ${campaignId}` }
            : { en: `Contact uploaded to campaign ${campaignId}` };
        await onComplete({
            events: [
                {
                    date: new Date().toISOString(),
                    text: event_text,
                },
            ],
            data_points: {
                num_uploaded_contacts: String(num_uploaded_contacts),
            },
        });
    },
};
//# sourceMappingURL=uploadContactToCampaign.js.map