Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

3.9 KB
Raw Blame
import { trelloAuth } from '../..';
import { TriggerStrategy, createTrigger, PiecePropValueSchema, Property, AppConnectionValueForAuthProperty } from '@activepieces/pieces-framework';
import { DedupeStrategy, Polling, pollingHelper } from '@activepieces/pieces-common';
import dayjs from 'dayjs';
import { trelloCommon, getCardsInBoard, getCardsInList } from '../common';
import { deadlineTriggerOutputSchema } from '../output-schemas';

interface Props {
    board_id: string;
    list_id_opt?: string;
    time_before_due: number;
    time_unit: string;
}

const polling: Polling<AppConnectionValueForAuthProperty<typeof trelloAuth>, Props> = {
    strategy: DedupeStrategy.TIMEBASED,
    async items({ auth, propsValue, lastFetchEpochMS }) {
        const { board_id, list_id_opt, time_before_due, time_unit } = propsValue;
        const getCards = list_id_opt ? getCardsInList : getCardsInBoard;
        const cards: any[] = await getCards(auth.username, auth.password, list_id_opt || board_id);

        if(lastFetchEpochMS ===0)
        {
            // If lastFetchEpochMS is 0, we assume this is the test run and return all cards with due dates
            return cards
                .filter(card => card.due && !card.dueComplete)
                .map(card => ({
                    epochMilliSeconds: dayjs(card.due).valueOf(),
                    data: card,
                }));
        }
        const now = dayjs();
        const upcoming = now.add(time_before_due, time_unit as dayjs.ManipulateType);

        return cards
            .filter(card => card.due && !card.dueComplete && dayjs(card.due).isAfter(now) && dayjs(card.due).isBefore(upcoming) && dayjs(card.due).valueOf() > lastFetchEpochMS)
            .map(card => ({
                epochMilliSeconds: dayjs(card.due).valueOf(),
                data: card,
            }));
    },
};

export const deadlineTrigger = createTrigger({
    auth: trelloAuth,
    name: 'deadline',
    classification: 'READ',
    displayName: 'Card Deadline',
    description: "Fires a set time before each card's due date.",
    outputSchema: deadlineTriggerOutputSchema,
    aiMetadata: {
        description: 'Fires for each card whose due date falls within a configured lead time (e.g. 24 hours) ahead of now, scoped to a board or an optional list. Represents an approaching, not-yet-completed card deadline; polls periodically and skips cards already marked due-complete.',
    },
    type: TriggerStrategy.POLLING,
    propertyGroups: [
        { key: 'watch', display: 'section', label: 'Where to Watch', icon: 'inbox', props: ['board_id', 'list_id_opt'] },
        { key: 'timing', display: 'section', label: 'Timing', icon: 'calendar', props: ['time_before_due', 'time_unit'] },
    ],
    props: {
        board_id: trelloCommon.board_id,
        list_id_opt: trelloCommon.list_id_opt,
        time_before_due: Property.Number({
            displayName: 'Time Before Due',
            description: 'How long before the due date to fire.',
            required: true,
            display: 'stepper',
            min: 1,
            max: 999,
            step: 1,
            defaultValue: 24,
        }),
        time_unit: Property.StaticDropdown({
            displayName: 'Time Unit',
            required: true,
            display: 'cards',
            options: {
            options: [
                { label: 'Minutes', value: 'minutes', icon: 'calendar' },
                { label: 'Hours', value: 'hours', icon: 'calendar' },
            ],
            },
            defaultValue: 'hours',
        }),
    },
    async onEnable(context) {
        await pollingHelper.onEnable(polling, context);
    },
    async onDisable(context) {
        await pollingHelper.onDisable(polling, context);
    },
    async run(context) {
        return await pollingHelper.poll(polling, context);
    },
    async test(context) {
        return await pollingHelper.test(polling, context);
    },
    sampleData: undefined,
});