"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateLead = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const lib_1 = require("../../lib");
const errors_1 = require("../../lib/errors");
const addEventLog_1 = require("../../../../src/lib/awell/addEventLog");
exports.updateLead = {
    key: 'updateLead',
    category: extensions_core_1.Category.CUSTOMER_SUPPORT,
    title: 'Update lead',
    description: 'Updates an existing lead object in Salesforce',
    fields: config_1.fields,
    previewable: false,
    dataPoints: config_1.dataPoints,
    onActivityCreated: async (payload, onComplete, onError) => {
        const { fields, salesforceClient } = await (0, lib_1.validatePayloadAndCreateClient)({
            fieldsSchema: config_1.FieldsValidationSchema,
            payload,
        });
        try {
            await salesforceClient.updateRecord({
                sObject: 'Lead',
                sObjectId: fields.sObjectId,
                data: fields.data,
            });
            await onComplete({
                events: [
                    (0, addEventLog_1.addActivityEventLog)({
                        message: `Lead updated. Data passed:\n\n ${JSON.stringify(fields.data, null, 2)}`,
                    }),
                ],
            });
        }
        catch (error) {
            if ((0, errors_1.isSalesforceError)(error)) {
                await onError({
                    events: [(0, errors_1.parseSalesforceError)(error)],
                });
            }
            else {
                // we handle ZodError and unknown errors in extension-server directly
                throw error;
            }
        }
    },
};
//# sourceMappingURL=updateLead.js.map