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

4.0 KB
Raw Blame
import { SeekPage } from '@activepieces/core-utils'
import { CreatePlatformEventDestinationRequestBody, EventDestination, ListPlatformEventDestinationsRequestBody, PrincipalType, TestPlatformEventDestinationRequestBody, TestPlatformEventDestinationResponse, UpdatePlatformEventDestinationRequestBody } from '@activepieces/shared'
import { FastifyPluginAsyncZod } from 'fastify-type-provider-zod'
import { StatusCodes } from 'http-status-codes'
import { z } from 'zod'
import { securityAccess } from '../../core/security/authorization/fastify-security'
import { eventDestinationTestRateLimit } from '../../core/security/rate-limit'
import { eventDestinationService } from '../../event-destinations/event-destinations.service'

export const platformWebhooksController: FastifyPluginAsyncZod = async (app) => {
    app.post('/', CreateEventDestinationRequest, async (req) => {
        return eventDestinationService(req.log).create({
            request: req.body,
            platformId: req.principal.platform.id,
        })
    })

    app.post('/:id', UpdateEventDestinationRequest, async (req) => {
        return eventDestinationService(req.log).update({
            id: req.params.id,
            platformId: req.principal.platform.id,
            request: req.body,
        })
    })

    app.patch('/:id', UpdateEventDestinationRequest, async (req) => {
        return eventDestinationService(req.log).update({
            id: req.params.id,
            platformId: req.principal.platform.id,
            request: req.body,
        })
    })
    app.get('/', ListEventDestinationsRequest, async (req) => {
        return eventDestinationService(req.log).list({
            platformId: req.principal.platform.id,
            cursorRequest: req.query.cursor ?? null,
            limit: req.query.limit ?? 10,
        })
    })
    app.delete('/:id', DeleteEventDestinationRequest, async (req) => {
        return eventDestinationService(req.log).delete({
            id: req.params.id,
            platformId: req.principal.platform.id,
        })
    })

    app.post('/test', TestPlatformEventDestinationRequest, async (req) => {
        return eventDestinationService(req.log).test({
            platformId: req.principal.platform.id,
            projectId: undefined,
            url: req.body.url,
            event: req.body.event,
            format: req.body.format,
            headers: req.body.headers,
        })
    })
}

export const CreateEventDestinationRequest = {
    schema: {
        body: CreatePlatformEventDestinationRequestBody,
    },
    config: {
        security: securityAccess.platformAdminOnly([PrincipalType.USER, PrincipalType.SERVICE]),
    },
}

export const UpdateEventDestinationRequest = {
    schema: {
        body: UpdatePlatformEventDestinationRequestBody,
        params: z.object({
            id: z.string(),
        }),
    },
    config: {
        security: securityAccess.platformAdminOnly([PrincipalType.USER, PrincipalType.SERVICE]),
    },
}

export const ListEventDestinationsRequest = {
    schema: {
        querystring: ListPlatformEventDestinationsRequestBody,
        response: {
            [StatusCodes.OK]: SeekPage(EventDestination),
        },
        tags: ['event-destinations'],
        description: 'List event destinations',
    },
    response: {
        [StatusCodes.OK]: SeekPage(EventDestination),
    },
    config: {
        security: securityAccess.platformAdminOnly([PrincipalType.USER, PrincipalType.SERVICE]),
    },
}

export const DeleteEventDestinationRequest = {
    schema: {
        params: z.object({
            id: z.string(),
        }),
    },
    config: {
        security: securityAccess.platformAdminOnly([PrincipalType.USER, PrincipalType.SERVICE]),
    },
}

export const TestPlatformEventDestinationRequest = {
    schema: {
        body: TestPlatformEventDestinationRequestBody,
        response: {
            [StatusCodes.OK]: TestPlatformEventDestinationResponse,
        },
    },
    config: {
        security: securityAccess.platformAdminOnly([PrincipalType.USER]),
        rateLimit: eventDestinationTestRateLimit,
    },
}