"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLastTaskPerformer = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const validatePayloadAndCreateSdk_1 = require("../../lib/validatePayloadAndCreateSdk");
const config_1 = require("./config");
const lodash_1 = require("lodash");
exports.getLastTaskPerformer = {
    key: 'getLastTaskPerformer',
    category: extensions_core_1.Category.COMMUNICATION,
    title: 'Get last task performer',
    description: 'Retrieves the performer of the last task.',
    fields: config_1.fields,
    previewable: true,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError }) => {
        var _a, _b;
        const { taskSdk, pathway } = await (0, validatePayloadAndCreateSdk_1.validatePayloadAndCreateSdk)({
            fieldsSchema: config_1.FieldsValidationSchema,
            payload,
        });
        /**
         * Get the last task performed in the current care flow.
         */
        const { data } = await taskSdk.getTasks({
            status: 'completed',
            careflow_id: pathway.id, // We only want to get the last task performed in the current care flow.
            sort_by: 'completed_at',
            direction: 'desc',
            limit: 1, // Given we sort by completed_at, we only need to get the last task so a limit of 1 is enough.
            offset: 0,
            activity_object_type: 'form', // only form tasks are completed in the worklist
        });
        const lastTask = (0, lodash_1.first)(data.tasks);
        if ((0, lodash_1.isNil)(lastTask)) {
            await onError({
                events: [
                    {
                        date: new Date().toISOString(),
                        text: {
                            en: `No completed task found in this care flow`,
                        },
                        error: {
                            category: 'SERVER_ERROR',
                            message: `No completed task found in this care flow`,
                        },
                    },
                ],
            });
            return;
        }
        const performer = (0, lodash_1.get)(lastTask, 'performer');
        const events = (0, lodash_1.isNil)(performer)
            ? [
                {
                    date: new Date().toISOString(),
                    text: {
                        en: `The last completed task in this care flow (${lastTask.title} - ${lastTask.id}) has no performer.`,
                    },
                },
            ]
            : [];
        await onComplete({
            data_points: {
                stytchUserId: (_a = (0, lodash_1.get)(performer, 'stytch_user_id')) !== null && _a !== void 0 ? _a : undefined,
                email: (_b = (0, lodash_1.get)(performer, 'email')) !== null && _b !== void 0 ? _b : undefined,
            },
            events,
        });
    },
};
//# sourceMappingURL=getLastTaskPerformer.js.map