feat: v2 managed organizations (#19341)

* refactor: allow non unique PlatformBilling customerId

* feat: add PlatformBilling managed organizations fields

* feat: ManagedOrganization table

* refactor: bill overdue based on teamId

* refactor: restructure organizations module

* wip: organizations endpoints

* Revert "wip: organizations endpoints"

This reverts commit 0e9e66fc74f31da436f12930c1b6597c650ed621.

* refactor: unique index for managed organizations

* wip: organizations endpoints

* wip: create managed organization

* remove unecessary membership check because we have guard

* feat: create managed org

* feat: get managed org and orgs

* feat: update and delete managed orgs

* wip: api key logic

* feat: allow variable api key length

* feat: refresh managed org api key

* feat: create managed org OAuth clients using api key

* finish merge main

* chore: bump platform libraries

* tests: fix and add more tests to api-auth.strategy.e2e

* refactor: dont request managed org slug and handle metadata as object

* revert: billing service and repository update overdue based on sub and customer ids

* refactor: v2 OAuth client permissions (#19501)

* refactor: v2 permissions as string array

* refactor: frontend work with permissions as string

* tests: test '*' permissions

* fix:managed org creator have profile & test can fetch oauth client

* fix: tests

* fix: OAuthClientCard on frontend

* fix: tests
This commit is contained in:
Lauris Skraucis authored and GitHub committed 2025-02-25 18:10:46 -07:00
1 parent e4ab8d87c4
commit a44bcafcf2
192 files changed
+9103 -619

No files matched your search

@@ -25,4 +25,12 @@ export class ApiKeysRepositoryFixture {
return { apiKey, keyString };
}
async getTeamApiKeys(teamId: number) {
return await this.prismaReadClient.apiKey.findMany({
where: {
teamId,
},
});
}
}
@@ -11,14 +11,14 @@ export class PlatformBillingRepositoryFixture {
this.prismaWriteClient = module.get(PrismaWriteService).prisma;
}
async create(orgId: number) {
async create(orgId: number, plan?: string) {
const randomString = Date.now().toString(36);
return this.prismaWriteClient.platformBilling.create({
data: {
id: orgId,
customerId: `cus_123_${randomString}`,
subscriptionId: `sub_123_${randomString}`,
plan: "STARTER",
plan: plan || "STARTER",
},
});
}
@@ -28,6 +28,19 @@ export class PlatformBillingRepositoryFixture {
where: {
id: orgId,
},
include: {
managerBilling: true,
managedBillings: true,
},
});
}
async getByCustomerSubscriptionIds(customerId: string, subscriptionId: string) {
return this.prismaWriteClient.platformBilling.findMany({
where: {
customerId,
subscriptionId,
},
});
}
@@ -0,0 +1,37 @@
import { PrismaReadService } from "@/modules/prisma/prisma-read.service";
import { PrismaWriteService } from "@/modules/prisma/prisma-write.service";
import { TestingModule } from "@nestjs/testing";
export class ManagedOrganizationsRepositoryFixture {
private prismaReadClient: PrismaReadService["prisma"];
private prismaWriteClient: PrismaWriteService["prisma"];
constructor(private readonly module: TestingModule) {
this.prismaReadClient = module.get(PrismaReadService).prisma;
this.prismaWriteClient = module.get(PrismaWriteService).prisma;
}
async getOrganizationWithManagedOrganizations(organizationId: number) {
return this.prismaReadClient.team.findUnique({
where: {
id: organizationId,
isOrganization: true,
},
include: {
managedOrganization: true,
managedOrganizations: true,
},
});
}
async getManagedOrganization(managerOrganizationId: number, managedOrganizationId: number) {
return this.prismaReadClient.managedOrganization.findUnique({
where: {
managerOrganizationId_managedOrganizationId: {
managerOrganizationId,
managedOrganizationId,
},
},
});
}
}
@@ -1,7 +1,7 @@
import { PrismaReadService } from "@/modules/prisma/prisma-read.service";
import { PrismaWriteService } from "@/modules/prisma/prisma-write.service";
import { TestingModule } from "@nestjs/testing";
import { PlatformOAuthClient } from "@prisma/client";
import { PlatformOAuthClient, Prisma } from "@prisma/client";
import { CreateOAuthClientInput } from "@calcom/platform-types";
@@ -18,6 +18,10 @@ export class OAuthClientRepositoryFixture {
return this.prismaReadClient.platformOAuthClient.findFirst({ where: { id: clientId } });
}
async getByOrgId(orgId: PlatformOAuthClient["organizationId"]) {
return this.prismaReadClient.platformOAuthClient.findMany({ where: { organizationId: orgId } });
}
async getUsers(clientId: PlatformOAuthClient["id"]) {
const response = await this.prismaReadClient.platformOAuthClient.findFirst({
where: { id: clientId },
@@ -29,7 +33,11 @@ export class OAuthClientRepositoryFixture {
return response?.users;
}
async create(organizationId: number, data: CreateOAuthClientInput, secret: string) {
async create(
organizationId: number,
data: Omit<Prisma.PlatformOAuthClientCreateInput, "organization" | "secret">,
secret: string
) {
return this.prismaWriteClient.platformOAuthClient.create({
data: {
...data,
@@ -16,6 +16,10 @@ export class ProfileRepositoryFixture {
return this.prismaReadClient.profile.findFirst({ where: { id: profileId } });
}
async findByOrgIdUserId(orgId: number, userId: number) {
return this.prismaReadClient.profile.findFirst({ where: { userId, organizationId: orgId } });
}
async create(data: Prisma.ProfileCreateInput) {
return this.prismaWriteClient.profile.create({ data });
}
@@ -1,21 +1,30 @@
import { BaseStrategy } from "@/lib/passport/strategies/types";
import { ApiAuthGuardRequest } from "@/modules/auth/strategies/api-auth/api-auth.strategy";
import { UsersService } from "@/modules/users/services/users.service";
import { UsersRepository } from "@/modules/users/users.repository";
import { Injectable } from "@nestjs/common";
import { PassportStrategy } from "@nestjs/passport";
@Injectable()
export class ApiAuthMockStrategy extends PassportStrategy(BaseStrategy, "api-auth") {
constructor(private readonly email: string, private readonly usersRepository: UsersRepository) {
constructor(
private readonly email: string,
private readonly usersRepository: UsersRepository,
private readonly usersService: UsersService
) {
super();
}
async authenticate() {
async authenticate(request: ApiAuthGuardRequest) {
try {
const user = await this.usersRepository.findByEmailWithProfile(this.email);
if (!user) {
throw new Error("User with the provided ID not found");
}
const organizationId = this.usersService.getUserMainOrgId(user) as number;
request.organizationId = organizationId;
return this.success(user);
} catch (error) {
console.error(error);
+4 -2
View File
@@ -1,10 +1,12 @@
import { ApiAuthStrategy } from "@/modules/auth/strategies/api-auth/api-auth.strategy";
import { UsersService } from "@/modules/users/services/users.service";
import { UsersRepository } from "@/modules/users/users.repository";
import { TestingModuleBuilder } from "@nestjs/testing";
import { ApiAuthMockStrategy } from "test/mocks/api-auth-mock.strategy";
export const withApiAuth = (email: string, module: TestingModuleBuilder) =>
module.overrideProvider(ApiAuthStrategy).useFactory({
factory: (usersRepository: UsersRepository) => new ApiAuthMockStrategy(email, usersRepository),
inject: [UsersRepository],
factory: (usersRepository: UsersRepository, usersService: UsersService) =>
new ApiAuthMockStrategy(email, usersRepository, usersService),
inject: [UsersRepository, UsersService],
});