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

9.6 KB
Raw Blame
import { ActivepiecesError, assertNotNullOrUndefined, ErrorCode, isNil, Permission, SeekPage } from '@activepieces/core-utils'
import { CreatePlatformProjectRequest, ListProjectRequestForPlatformQueryParams, PlatformRole, Principal, PrincipalType, ProjectType, ProjectWithLimits, SERVICE_KEY_SECURITY_OPENAPI, UpdateProjectPlatformRequest } from '@activepieces/shared'
import { FastifyBaseLogger } from 'fastify'
import { FastifyPluginAsyncZod } from 'fastify-type-provider-zod'
import { StatusCodes } from 'http-status-codes'
import { z } from 'zod'
import { ProjectResourceType } from '../../core/security/authorization/common'
import { securityAccess } from '../../core/security/authorization/fastify-security'
import { platformService } from '../../platform/platform.service'
import { projectService } from '../../project/project-service'
import { userService } from '../../user/user-service'
import { platformPlanService } from '../platform/platform-plan/platform-plan.service'
import { platformProjectService } from './platform-project-service'

const DEFAULT_LIMIT_SIZE = 50

export const platformProjectController: FastifyPluginAsyncZod = async (app) => {

    app.get('/:id', GetProjectRequest, async (request) => {
        return platformProjectService(request.log).getWithPlanAndUsageOrThrow(request.projectId)
    })


    app.post('/', CreateProjectRequest, async (request, reply) => {
        const platformId = request.principal.platform.id
        assertNotNullOrUndefined(platformId, 'platformId')
        await assertMaximumNumberOfProjectsReachedByEdition(platformId, request.log)
        const projectWithUsage = await platformProjectService(request.log).create({
            platformId,
            displayName: request.body.displayName,
            externalId: request.body.externalId ?? undefined,
            metadata: request.body.metadata ?? undefined,
            maxConcurrentJobs: request.body.maxConcurrentJobs ?? undefined,
            globalConnectionExternalIds: request.body.globalConnectionExternalIds ?? undefined,
            alertReceiverEmail: request.body.alertReceiverEmail ?? undefined,
            sensitive: request.body.sensitive ?? undefined,
        })
        await reply.status(StatusCodes.CREATED).send(projectWithUsage)
    })

    app.get('/', ListProjectRequestForPlatform, async (request, _reply) => {
        const userId = await getUserId(request.principal, request.log)
        const user = await userService(request.log).getOneOrFail({ id: userId })
        return platformProjectService(request.log).getForPlatform({
            platformId: request.principal.platform.id,
            externalId: request.query.externalId,
            externalUserId: request.query.externalUserId,
            cursorRequest: request.query.cursor ?? null,
            displayName: request.query.displayName,
            types: request.query.types,
            limit: request.query.limit ?? DEFAULT_LIMIT_SIZE,
            userId,
            isPrivileged: userService(request.log).isUserPrivileged(user),
            principalType: request.principal.type,
        })
    })

    app.post('/:id', UpdateProjectRequest, async (request) => {
        const project = await projectService(request.log).getOneOrThrow(request.params.id)
        const haveTokenForTheProject = request.projectId === project.id
        const ownThePlatform = await isPlatformAdmin({
            platformId: request.principal.platform.id,
            type: request.principal.type,
            id: request.principal.id,
        }, project.platformId, request.log)
        if (!haveTokenForTheProject && !ownThePlatform) {
            throw new ActivepiecesError({
                code: ErrorCode.AUTHORIZATION,
                params: {},
            })
        }
        const isDefault = ownThePlatform ? request.body.isDefault : undefined
        if (isDefault === true && !await isProjectRolesEnabled({ platformId: project.platformId, log: request.log })) {
            throw new ActivepiecesError({
                code: ErrorCode.FEATURE_DISABLED,
                params: {
                    message: 'Default projects are not enabled for this platform',
                },
            })
        }
        return platformProjectService(request.log).update({
            platformId: request.principal.platform.id,
            projectId: request.params.id,
            request: {
                ...request.body,
                externalId: ownThePlatform ? request.body.externalId : undefined,
                executionDataRetentionDays: ownThePlatform ? request.body.executionDataRetentionDays : undefined,
                isDefault,
            },
        })
    })

    app.delete('/:id', DeleteProjectRequest, async (req, res) => {
        await assertProjectIsSafeToDelete(req.params.id, req.principal.platform.id, req.log)
        await platformProjectService(req.log).markForDeletion({
            id: req.params.id,
            platformId: req.principal.platform.id,
            enforceDefaultGuard: req.principal.type === PrincipalType.USER && await isProjectRolesEnabled({ platformId: req.principal.platform.id, log: req.log }),
        })

        return res.status(StatusCodes.NO_CONTENT).send()
    })
}

async function getUserId(principal: Principal, log: FastifyBaseLogger): Promise<string> {
    if (principal.type === PrincipalType.SERVICE) {
        const platform = await platformService(log).getOneOrThrow(principal.platform.id)
        return platform.ownerId
    }
    return principal.id
}

async function isPlatformAdmin(principal: {
    platformId: string
    type: PrincipalType
    id: string
}, projectPlatformId: string, log: FastifyBaseLogger): Promise<boolean> {
    if (principal.platformId !== projectPlatformId) {
        return false
    }
    if (principal.type === PrincipalType.SERVICE) {
        return true
    }
    const user = await userService(log).getOneOrFail({
        id: principal.id,
    })
    return user.platformRole === PlatformRole.ADMIN
}




async function assertProjectIsSafeToDelete(projectId: string, callerPlatformId: string, log: FastifyBaseLogger): Promise<void> {
    const project = await projectService(log).getOneOrThrow(projectId)
    if (project.platformId !== callerPlatformId) {
        throw new ActivepiecesError({
            code: ErrorCode.ENTITY_NOT_FOUND,
            params: {
                entityType: 'project',
                entityId: projectId,
            },
        })
    }
}

async function isProjectRolesEnabled({ platformId, log }: IsProjectRolesEnabledParams): Promise<boolean> {
    const platformPlan = await platformPlanService(log).getOrCreateForPlatform(platformId)
    return platformPlan.projectRolesEnabled
}

async function assertMaximumNumberOfProjectsReachedByEdition(platformId: string, log: FastifyBaseLogger): Promise<void> {
    const platform = await platformService(log).getOneWithPlanOrThrow(platformId)
    const billedTeamProjectsLimit = platform.plan.billedTeamProjectsLimit

    if (isNil(billedTeamProjectsLimit)) {
        return
    }
    if (billedTeamProjectsLimit <= 0) {
        throw new ActivepiecesError({
            code: ErrorCode.VALIDATION,
            params: {
                message: 'Team projects are not available on your current plan',
            },
        })
    }
    const projectsCount = await projectService(log).countByPlatformIdAndType(platformId, ProjectType.TEAM)
    if (projectsCount >= billedTeamProjectsLimit) {
        throw new ActivepiecesError({
            code: ErrorCode.FEATURE_DISABLED,
            params: {
                message: `Maximum limit of ${billedTeamProjectsLimit} team project(s) reached for this plan. Upgrade your plan to add more team projects.`,
            },
        })
    }
}

const GetProjectRequest = {
    config: {
        security: securityAccess.project(
            [PrincipalType.USER, PrincipalType.SERVICE],
            undefined, {
                type: ProjectResourceType.PARAM,
                paramKey: 'id',
            }),
    },
}

const UpdateProjectRequest = {
    config: {
        security: securityAccess.project([PrincipalType.USER, PrincipalType.SERVICE], Permission.WRITE_PROJECT, {
            type: ProjectResourceType.PARAM,
            paramKey: 'id',
        }),
    },
    schema: {
        tags: ['projects'],
        security: [SERVICE_KEY_SECURITY_OPENAPI],
        params: z.object({
            id: z.string(),
        }),
        response: {
            [StatusCodes.OK]: ProjectWithLimits,
        },
        body: UpdateProjectPlatformRequest,
    },
}

const CreateProjectRequest = {
    config: {
        security: securityAccess.publicPlatform([PrincipalType.USER, PrincipalType.SERVICE]),
    },
    schema: {
        tags: ['projects'],
        response: {
            [StatusCodes.CREATED]: ProjectWithLimits,
        },
        security: [SERVICE_KEY_SECURITY_OPENAPI],
        body: CreatePlatformProjectRequest,
    },
}

const ListProjectRequestForPlatform = {
    config: {
        security: securityAccess.publicPlatform([PrincipalType.USER, PrincipalType.SERVICE]),
    },
    schema: {
        response: {
            [StatusCodes.OK]: SeekPage(ProjectWithLimits),
        },
        querystring: ListProjectRequestForPlatformQueryParams,
        tags: ['projects'],
        security: [SERVICE_KEY_SECURITY_OPENAPI],
    },
}

const DeleteProjectRequest = {
    config: {
        security: securityAccess.platformAdminOnly([PrincipalType.USER, PrincipalType.SERVICE]),
    },
    schema: {
        params: z.object({
            id: z.string(),
        }),
        tags: ['projects'],
        security: [SERVICE_KEY_SECURITY_OPENAPI],
    },
}

type IsProjectRolesEnabledParams = {
    platformId: string
    log: FastifyBaseLogger
}