"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSubscription = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const lib_1 = require("../../lib");
exports.createSubscription = {
    key: 'createSubscription',
    category: extensions_core_1.Category.BILLING,
    title: 'Create subscription',
    description: 'Create a subscription in Stripe',
    fields: config_1.fields,
    previewable: false,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        helpers.log({ fields: payload.fields }, 'Processing createSubscription');
        try {
            const { fields: input, stripe, patient, pathway, activity, } = await (0, lib_1.validateAndCreateStripeSdk)({
                fieldsSchema: config_1.FieldsValidationSchema,
                payload,
            });
            const res = await stripe.subscriptions.create({
                customer: input.customer,
                items: [
                    {
                        price: input.item,
                    },
                ],
                payment_settings: {
                    payment_method_types: ['card'],
                },
                metadata: {
                    awellPatientId: patient.id,
                    awellCareflowId: pathway.id,
                    awellActivityId: activity.id,
                },
            });
            await onComplete({
                data_points: {
                    subscriptionId: res.id,
                },
            });
        }
        catch (err) {
            helpers.log({ err }, 'error', err);
            const error = err;
            await onError({
                events: [
                    {
                        date: new Date().toISOString(),
                        text: { en: error.message },
                        error: {
                            category: 'SERVER_ERROR',
                            message: error.message,
                        },
                    },
                ],
            });
        }
    },
};
//# sourceMappingURL=createSubscription.js.map