diff --git a/apps/api/v2/package.json b/apps/api/v2/package.json index 2ab92e5169..c9e1133890 100644 --- a/apps/api/v2/package.json +++ b/apps/api/v2/package.json @@ -28,7 +28,7 @@ "dependencies": { "@calcom/platform-constants": "*", "@calcom/platform-enums": "*", - "@calcom/platform-libraries": "npm:@calcom/platform-libraries@0.0.47", + "@calcom/platform-libraries": "npm:@calcom/platform-libraries@0.0.48", "@calcom/platform-libraries-0.0.2": "npm:@calcom/platform-libraries@0.0.2", "@calcom/platform-types": "*", "@calcom/platform-utils": "*", diff --git a/apps/api/v2/src/modules/atoms/atoms.module.ts b/apps/api/v2/src/modules/atoms/atoms.module.ts index 26c97f16c4..ab7f33f7ed 100644 --- a/apps/api/v2/src/modules/atoms/atoms.module.ts +++ b/apps/api/v2/src/modules/atoms/atoms.module.ts @@ -2,6 +2,7 @@ import { EventTypesModule_2024_06_14 } from "@/ee/event-types/event-types_2024_0 import { SchedulesRepository_2024_06_11 } from "@/ee/schedules/schedules_2024_06_11/schedules.repository"; import { AtomsController } from "@/modules/atoms/controllers/atoms.controller"; import { EventTypesAtomService } from "@/modules/atoms/services/event-types-atom.service"; +import { CredentialsRepository } from "@/modules/credentials/credentials.repository"; import { MembershipsRepository } from "@/modules/memberships/memberships.repository"; import { OrganizationsModule } from "@/modules/organizations/organizations.module"; import { PrismaModule } from "@/modules/prisma/prisma.module"; @@ -14,6 +15,7 @@ import { Module } from "@nestjs/common"; providers: [ EventTypesAtomService, MembershipsRepository, + CredentialsRepository, UsersRepository, UsersService, SchedulesRepository_2024_06_11, diff --git a/apps/api/v2/src/modules/atoms/controllers/atoms.controller.ts b/apps/api/v2/src/modules/atoms/controllers/atoms.controller.ts index 6dc0a29306..72f7ef43ff 100644 --- a/apps/api/v2/src/modules/atoms/controllers/atoms.controller.ts +++ b/apps/api/v2/src/modules/atoms/controllers/atoms.controller.ts @@ -1,4 +1,5 @@ import { API_VERSIONS_VALUES } from "@/lib/api-versions"; +import { EventTypesAppInput } from "@/modules/atoms/inputs/event-types-app.input"; import { EventTypesAtomService } from "@/modules/atoms/services/event-types-atom.service"; import { GetUser } from "@/modules/auth/decorators/get-user/get-user.decorator"; import { ApiAuthGuard } from "@/modules/auth/guards/api-auth/api-auth.guard"; @@ -13,6 +14,7 @@ import { VERSION_NEUTRAL, Patch, Body, + Query, } from "@nestjs/common"; import { ApiTags as DocsTags, ApiExcludeController as DocsExcludeController } from "@nestjs/swagger"; @@ -50,6 +52,26 @@ export class AtomsController { }; } + @Get("event-types-app/:appSlug") + @Version(VERSION_NEUTRAL) + @UseGuards(ApiAuthGuard) + async getAtomEventTypeApp( + @GetUser() user: UserWithProfile, + @Param("appSlug") appSlug: string, + @Query() queryParams: EventTypesAppInput + ): Promise> { + const { teamId } = queryParams; + + const app = await this.eventTypesService.getEventTypesAppIntegration(appSlug, user.id, user.name, teamId); + + return { + status: SUCCESS_STATUS, + data: { + app, + }, + }; + } + @Patch("event-types/:eventTypeId") @Version(VERSION_NEUTRAL) @UseGuards(ApiAuthGuard) diff --git a/apps/api/v2/src/modules/atoms/inputs/event-types-app.input.ts b/apps/api/v2/src/modules/atoms/inputs/event-types-app.input.ts new file mode 100644 index 0000000000..5bb99c4b50 --- /dev/null +++ b/apps/api/v2/src/modules/atoms/inputs/event-types-app.input.ts @@ -0,0 +1,9 @@ +import { Transform } from "class-transformer"; +import { IsNumber, IsOptional } from "class-validator"; + +export class EventTypesAppInput { + @Transform(({ value }: { value: string }) => value && parseInt(value)) + @IsNumber() + @IsOptional() + teamId?: number; +} diff --git a/apps/api/v2/src/modules/atoms/services/event-types-atom.service.ts b/apps/api/v2/src/modules/atoms/services/event-types-atom.service.ts index 1e93278406..f8fceb2401 100644 --- a/apps/api/v2/src/modules/atoms/services/event-types-atom.service.ts +++ b/apps/api/v2/src/modules/atoms/services/event-types-atom.service.ts @@ -1,5 +1,7 @@ import { EventTypesService_2024_06_14 } from "@/ee/event-types/event-types_2024_06_14/services/event-types.service"; +import { CredentialsRepository } from "@/modules/credentials/credentials.repository"; import { MembershipsRepository } from "@/modules/memberships/memberships.repository"; +import { OrganizationsTeamsRepository } from "@/modules/organizations/repositories/organizations-teams.repository"; import { OrganizationsEventTypesService } from "@/modules/organizations/services/event-types/organizations-event-types.service"; import { PrismaReadService } from "@/modules/prisma/prisma-read.service"; import { PrismaWriteService } from "@/modules/prisma/prisma-write.service"; @@ -12,13 +14,33 @@ import { TUpdateEventTypeInputSchema, systemBeforeFieldEmail, getEventTypeById, + getEnabledAppsFromCredentials, + getAppFromSlug, + MembershipRole, +} from "@calcom/platform-libraries"; +import type { + App, + CredentialDataWithTeamName, + LocationOption, + TeamQuery, + CredentialOwner, + TDependencyData, + CredentialPayload, } from "@calcom/platform-libraries"; import { PrismaClient } from "@calcom/prisma/client"; +type EnabledAppType = App & { + credential: CredentialDataWithTeamName; + credentials: CredentialDataWithTeamName[]; + locationOption: LocationOption | null; +}; + @Injectable() export class EventTypesAtomService { constructor( private readonly membershipsRepository: MembershipsRepository, + private readonly credentialsRepository: CredentialsRepository, + private readonly organizationsTeamsRepository: OrganizationsTeamsRepository, private readonly usersService: UsersService, private readonly dbWrite: PrismaWriteService, private readonly dbRead: PrismaReadService, @@ -146,4 +168,110 @@ export class EventTypesAtomService { ); } } + + async getEventTypesAppIntegration(slug: string, userId: number, userName: string | null, teamId?: number) { + let credentials = await this.credentialsRepository.getAllUserCredentialsById(userId); + let userTeams: TeamQuery[] = []; + if (teamId) { + const teamsQuery = await this.organizationsTeamsRepository.getUserTeamsById(userId); + // If a team is a part of an org then include those apps + // Don't want to iterate over these parent teams + const filteredTeams: TeamQuery[] = []; + const parentTeams: TeamQuery[] = []; + // Only loop and grab parent teams if a teamId was given. If not then all teams will be queried + if (teamId) { + teamsQuery.forEach((team) => { + if (team?.parent) { + const { parent, ...filteredTeam } = team; + filteredTeams.push(filteredTeam); + // Only add parent team if it's not already in teamsQuery + if (!teamsQuery.some((t) => t.id === parent.id)) { + parentTeams.push(parent); + } + } + }); + } + userTeams = [...teamsQuery, ...parentTeams]; + const teamAppCredentials: CredentialPayload[] = userTeams.flatMap((teamApp) => { + return teamApp.credentials ? teamApp.credentials.flat() : []; + }); + if (teamId) { + credentials = teamAppCredentials; + } else { + credentials = credentials.concat(teamAppCredentials); + } + } + const enabledApps = await getEnabledAppsFromCredentials(credentials, { + where: { slug }, + }); + const apps = await Promise.all( + enabledApps + .filter(({ ...app }) => app.slug === slug) + .map( + async ({ + credentials: _, + credential, + key: _2 /* don't leak to frontend */, + ...app + }: EnabledAppType) => { + const userCredentialIds = credentials + .filter((c) => c.appId === app.slug && !c.teamId) + .map((c) => c.id); + const invalidCredentialIds = credentials + .filter((c) => c.appId === app.slug && c.invalid) + .map((c) => c.id); + const teams = await Promise.all( + credentials + .filter((c) => c.appId === app.slug && c.teamId) + .map(async (c) => { + const team = userTeams.find((team) => team.id === c.teamId); + if (!team) { + return null; + } + return { + teamId: team.id, + name: team.name, + logoUrl: team.logoUrl, + credentialId: c.id, + isAdmin: + team.members[0].role === MembershipRole.ADMIN || + team.members[0].role === MembershipRole.OWNER, + }; + }) + ); + const isSetupAlready = credential && app.categories.includes("payment") ? true : undefined; + let dependencyData: TDependencyData = []; + if (app.dependencies?.length) { + dependencyData = app.dependencies.map((dependency) => { + const dependencyInstalled = enabledApps.some( + (dbAppIterator: EnabledAppType) => + dbAppIterator.credentials.length && dbAppIterator.slug === dependency + ); + // If the app marked as dependency is simply deleted from the codebase, + // we can have the situation where App is marked installed in DB but we couldn't get the app. + const dependencyName = getAppFromSlug(dependency)?.name; + return { name: dependencyName, installed: dependencyInstalled }; + }); + } + const credentialOwner: CredentialOwner = { + name: userName, + teamId, + }; + return { + ...app, + ...(teams.length && { + credentialOwner, + }), + userCredentialIds, + invalidCredentialIds, + teams, + isInstalled: !!userCredentialIds.length || !!teams.length || app.isGlobal, + isSetupAlready, + ...(app.dependencies && { dependencyData }), + }; + } + ) + ); + return apps[0]; + } } diff --git a/apps/api/v2/src/modules/credentials/credentials.repository.ts b/apps/api/v2/src/modules/credentials/credentials.repository.ts index ea830c2d64..9ece085a5f 100644 --- a/apps/api/v2/src/modules/credentials/credentials.repository.ts +++ b/apps/api/v2/src/modules/credentials/credentials.repository.ts @@ -4,6 +4,7 @@ import { Injectable } from "@nestjs/common"; import { Prisma } from "@prisma/client"; import { APPS_TYPE_ID_MAPPING } from "@calcom/platform-constants"; +import { credentialForCalendarServiceSelect } from "@calcom/platform-libraries"; @Injectable() export class CredentialsRepository { @@ -65,6 +66,18 @@ export class CredentialsRepository { }); } + async getAllUserCredentialsById(userId: number) { + return await this.dbRead.prisma.credential.findMany({ + where: { + userId, + }, + select: credentialForCalendarServiceSelect, + orderBy: { + id: "asc", + }, + }); + } + async getUserCredentialById(userId: number, credentialId: number, type: string) { return await this.dbRead.prisma.credential.findUnique({ where: { diff --git a/apps/api/v2/src/modules/organizations/repositories/organizations-teams.repository.ts b/apps/api/v2/src/modules/organizations/repositories/organizations-teams.repository.ts index 91b4d0d94c..844ce31750 100644 --- a/apps/api/v2/src/modules/organizations/repositories/organizations-teams.repository.ts +++ b/apps/api/v2/src/modules/organizations/repositories/organizations-teams.repository.ts @@ -4,6 +4,8 @@ import { PrismaReadService } from "@/modules/prisma/prisma-read.service"; import { PrismaWriteService } from "@/modules/prisma/prisma-write.service"; import { Injectable } from "@nestjs/common"; +import { credentialForCalendarServiceSelect } from "@calcom/platform-libraries"; + @Injectable() export class OrganizationsTeamsRepository { constructor(private readonly dbRead: PrismaReadService, private readonly dbWrite: PrismaWriteService) {} @@ -119,4 +121,53 @@ export class OrganizationsTeamsRepository { return team.members.map((member) => member.userId); } + + async getUserTeamsById(userId: number) { + const userTeams = await this.dbRead.prisma.team.findMany({ + where: { + members: { + some: { + userId, + accepted: true, + }, + }, + }, + select: { + id: true, + credentials: { + select: credentialForCalendarServiceSelect, + }, + name: true, + logoUrl: true, + members: { + where: { + userId, + }, + select: { + role: true, + }, + }, + parent: { + select: { + id: true, + credentials: { + select: credentialForCalendarServiceSelect, + }, + name: true, + logoUrl: true, + members: { + where: { + userId, + }, + select: { + role: true, + }, + }, + }, + }, + }, + }); + + return userTeams; + } } diff --git a/apps/web/public/static/locales/en/common.json b/apps/web/public/static/locales/en/common.json index 8ffa5487bb..1dc90beb69 100644 --- a/apps/web/public/static/locales/en/common.json +++ b/apps/web/public/static/locales/en/common.json @@ -1387,6 +1387,8 @@ "event_limit_tab_description": "How often you can be booked", "event_advanced_tab_description": "Calendar settings & more...", "event_advanced_tab_title": "Advanced", + "event_payments_tab_title": "Payments", + "event_payments_tab_description": "Set up payments for event", "event_setup_multiple_duration_error": "Event Setup: Multiple durations requires at least 1 option.", "event_setup_multiple_duration_default_error": "Event Setup: Please select a valid default duration.", "event_setup_booking_limits_error": "Booking limits must be in ascending order. [day, week, month, year]", diff --git a/packages/app-store/_components/AppCard.tsx b/packages/app-store/_components/AppCard.tsx index 376e2d25ae..094fb73eed 100644 --- a/packages/app-store/_components/AppCard.tsx +++ b/packages/app-store/_components/AppCard.tsx @@ -3,6 +3,7 @@ import Link from "next/link"; import { useTranslation } from "react-i18next"; import { useAppContextWithSchema } from "@calcom/app-store/EventTypeAppContext"; +import { useIsPlatform } from "@calcom/atoms/monorepo"; import { classNames } from "@calcom/lib"; import type { RouterOutputs } from "@calcom/trpc/react"; import { Switch, Badge, Avatar, Button, Icon } from "@calcom/ui"; @@ -39,6 +40,7 @@ export default function AppCard({ const { t } = useTranslation(); const [animationRef] = useAutoAnimate(); const { setAppData, LockedIcon, disabled: managedDisabled } = useAppContextWithSchema(); + const isPlatform = useIsPlatform(); return (
- {!hideSettingsIcon && ( + {!hideSettingsIcon && !isPlatform && (