"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addOrUpdateContact = void 0;
const config_1 = require("./config");
const extensions_core_1 = require("@awell-health/extensions-core");
const settings_1 = require("../../../settings");
const fields_1 = require("./config/fields");
const zod_validation_error_1 = require("zod-validation-error");
const zod_1 = require("zod");
const classes_1 = require("@sendgrid/helpers/classes");
const client_1 = require("../../../client");
const dataPoints = {
    jobId: {
        key: 'jobId',
        valueType: 'string',
    },
};
exports.addOrUpdateContact = {
    key: 'addOrUpdateContact',
    title: 'Add or update contact',
    description: 'Add or update contact',
    category: extensions_core_1.Category.COMMUNICATION,
    fields: config_1.fields,
    dataPoints,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        try {
            const { fields: { email, firstName, lastName, listIds, customFields }, settings: { apiKey }, } = (0, extensions_core_1.validate)({
                schema: zod_1.z.object({
                    fields: fields_1.FieldsValidationSchema,
                    settings: settings_1.SettingsValidationSchema,
                }),
                payload,
            });
            const sendgridClient = new client_1.SendgridClient({ apiKey });
            const sgResponse = await sendgridClient.marketing.contacts.addOrUpdate({
                contacts: [
                    {
                        email,
                        first_name: firstName,
                        last_name: lastName,
                        custom_fields: customFields,
                    },
                ],
                listIds,
            });
            await onComplete({
                data_points: {
                    jobId: sgResponse[0].body.job_id,
                },
            });
        }
        catch (err) {
            if (err instanceof zod_1.ZodError) {
                const error = (0, zod_validation_error_1.fromZodError)(err);
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: { en: error.name },
                            error: {
                                category: 'WRONG_INPUT',
                                message: `${error.message}`,
                            },
                        },
                    ],
                });
                return;
            }
            else if (err instanceof classes_1.ResponseError) {
                const events = (0, client_1.mapSendgridErrorsToActivityErrors)(err);
                await onError({
                    events,
                });
                return;
            }
            const error = err;
            await onError({
                events: [
                    {
                        date: new Date().toISOString(),
                        text: { en: 'Something went wrong while orchestration the action' },
                        error: {
                            category: 'SERVER_ERROR',
                            message: error.message,
                        },
                    },
                ],
            });
        }
    },
};
//# sourceMappingURL=addOrUpdateContact.js.map