refactor(workspace, users, billing): remove default workspace + rename (#9360)

Replaced user-based parameterization with workspace-focused logic across
seed scripts, mocks, and billing services. Removed redundant `user`
references and standardized to `workspace` to align with updated
business rules. Adjusted mock data and tests to reflect these changes.

Fix https://github.com/twentyhq/twenty/issues/9295
This commit is contained in:
Antoine Moreaux
2025-01-06 12:33:57 +01:00
committed by GitHub
parent 25083e5405
commit 3eb7ec909e
12 changed files with 35 additions and 51 deletions

View File

@ -21,7 +21,7 @@ export const seedCoreSchema = async (
const schemaName = 'core';
await seedWorkspaces(workspaceDataSource, schemaName, workspaceId);
await seedUsers(workspaceDataSource, schemaName, workspaceId);
await seedUsers(workspaceDataSource, schemaName);
await seedUserWorkspaces(workspaceDataSource, schemaName, workspaceId);
};

View File

@ -13,7 +13,6 @@ export const DEMO_SEED_USER_IDS = {
export const seedUsers = async (
workspaceDataSource: DataSource,
schemaName: string,
workspaceId: string,
) => {
await workspaceDataSource
.createQueryBuilder()
@ -56,16 +55,22 @@ export const seedUsers = async (
};
export const deleteUsersByWorkspace = async (
workspaceDataSource: DataSource,
dataSource: DataSource,
schemaName: string,
workspaceId: string,
) => {
await workspaceDataSource
const user = await dataSource
.createQueryBuilder(`${schemaName}.${tableName}`, 'user')
.leftJoinAndSelect('user.workspaces', 'userWorkspace')
.where(`userWorkspace."workspaceId" = :workspaceId`, {
workspaceId,
})
.getMany();
await dataSource
.createQueryBuilder()
.delete()
.from(`${schemaName}.${tableName}`)
.where(`"${tableName}"."defaultWorkspaceId" = :workspaceId`, {
workspaceId,
})
.where(`"${tableName}"."id" IN (:...ids)`, { ids: user.map((u) => u.id) })
.execute();
};

View File

@ -12,7 +12,7 @@ export const seedCoreSchema = async (
const schemaName = 'core';
await seedWorkspaces(workspaceDataSource, schemaName, workspaceId);
await seedUsers(workspaceDataSource, schemaName, workspaceId);
await seedUsers(workspaceDataSource, schemaName);
await seedUserWorkspaces(workspaceDataSource, schemaName, workspaceId);
await seedFeatureFlags(workspaceDataSource, schemaName, workspaceId);
};

View File

@ -1,7 +1,5 @@
import { DataSource } from 'typeorm';
// import { SeedWorkspaceId } from 'src/database/typeorm-seeds/core/workspaces';
const tableName = 'user';
export const DEV_SEED_USER_IDS = {
@ -13,7 +11,6 @@ export const DEV_SEED_USER_IDS = {
export const seedUsers = async (
workspaceDataSource: DataSource,
schemaName: string,
workspaceId: string,
) => {
await workspaceDataSource
.createQueryBuilder()
@ -54,18 +51,3 @@ export const seedUsers = async (
])
.execute();
};
export const deleteUsersByWorkspace = async (
workspaceDataSource: DataSource,
schemaName: string,
workspaceId: string,
) => {
await workspaceDataSource
.createQueryBuilder()
.delete()
.from(`${schemaName}.${tableName}`)
.where(`"${tableName}"."defaultWorkspaceId" = :workspaceId`, {
workspaceId,
})
.execute();
};

View File

@ -37,9 +37,8 @@ export class BillingResolver {
}
@Query(() => SessionEntity)
@UseGuards(WorkspaceAuthGuard, UserAuthGuard)
@UseGuards(WorkspaceAuthGuard)
async billingPortalSession(
@AuthUser() user: User,
@AuthWorkspace() workspace: Workspace,
@Args() { returnUrlPath }: BillingSessionInput,
) {
@ -89,8 +88,8 @@ export class BillingResolver {
@Mutation(() => UpdateBillingEntity)
@UseGuards(WorkspaceAuthGuard)
async updateBillingSubscription(@AuthUser() user: User) {
await this.billingSubscriptionService.applyBillingSubscription(user);
async updateBillingSubscription(@AuthWorkspace() workspace: Workspace) {
await this.billingSubscriptionService.applyBillingSubscription(workspace);
return { success: true };
}

View File

@ -3,7 +3,6 @@ import { InjectRepository } from '@nestjs/typeorm';
import assert from 'assert';
import { User } from '@sentry/node';
import Stripe from 'stripe';
import { Not, Repository } from 'typeorm';
@ -15,6 +14,7 @@ import { SubscriptionInterval } from 'src/engine/core-modules/billing/enums/bill
import { SubscriptionStatus } from 'src/engine/core-modules/billing/enums/billing-subscription-status.enum';
import { StripeService } from 'src/engine/core-modules/billing/stripe/stripe.service';
import { EnvironmentService } from 'src/engine/core-modules/environment/environment.service';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
@Injectable()
export class BillingSubscriptionService {
@ -114,9 +114,9 @@ export class BillingSubscriptionService {
return entitlement.value;
}
async applyBillingSubscription(user: User) {
async applyBillingSubscription(workspace: Workspace) {
const billingSubscription = await this.getCurrentBillingSubscriptionOrThrow(
{ workspaceId: user.defaultWorkspaceId },
{ workspaceId: workspace.id },
);
const newInterval =
@ -125,9 +125,7 @@ export class BillingSubscriptionService {
: SubscriptionInterval.Year;
const billingSubscriptionItem =
await this.getCurrentBillingSubscriptionItemOrThrow(
user.defaultWorkspaceId,
);
await this.getCurrentBillingSubscriptionItemOrThrow(workspace.id);
const productPrice = await this.stripeService.getStripePrice(
AvailableProduct.BasePlan,