fix(billing): link billing emails to the workspace subdomain (#22401)
## Problem Billing and workspace-suspension emails hardcoded a `BILLING_SETTINGS_URL` constant pointing at `https://app.twenty.com/settings/billing`. A user in `myworkspace.twenty.com` therefore received a CTA that bounced through the central `app` domain instead of landing on their own workspace. Those cross-subdomain redirects are unreliable, so it's better to link straight to the workspace. The invite, password-reset and email-verification emails already do this correctly by building a workspace-specific URL server-side with `WorkspaceDomainsService.buildWorkspaceURL(...)`; the billing/suspension senders had the `workspace` entity in scope but never used it. ## Fix Build the billing settings URL server-side and pass it into the templates as a `link` prop, mirroring the existing pattern: - **Templates** now take a `link` prop instead of the hardcoded constant: `billing-trial-ending`, `billing-trial-converting`, `billing-subscription-renewing`, `warn-suspended-workspace`. - **`BillingReminderService`** and **`CleanerWorkspaceService`** build `buildWorkspaceURL({ workspace, pathname: getSettingsPath(SettingsPath.Billing) })` and thread it through. - Wired `WorkspaceDomainsModule` into both NestJS modules; deleted the now-unused `billing-settings-url.constant.ts`; updated the reminder unit test. This also fixes **self-hosted** deployments, which previously got the same wrong hardcoded `app.twenty.com` link. ### Intentionally unchanged - `clean-suspended-workspace` keeps its central-domain "start a new workspace" CTA — that workspace is already deleted, so its subdomain no longer resolves. - `password-update-notify` (not a billing email) still uses `getBaseUrl()`; the workspace entity isn't readily loaded there. Can be a follow-up. ## Testing Extended `billing-reminder.service.spec.ts` to assert the workspace-specific `link` is threaded into the email. Note: local `typecheck`/tests could not be run because the sandbox proxy repeatedly dropped `yarn install` mid-fetch; the diff was reviewed line-by-line and import paths verified against the actual `twenty-shared` exports and module wiring. CI will provide the authoritative check. https://claude.ai/code/session_01QsgNd4SWdcRkPFyrnCgj2b --- _Generated by [Claude Code](https://claude.ai/code/session_01QsgNd4SWdcRkPFyrnCgj2b)_ <!-- This is an auto-generated description by cubic. --> <a href="https://cubic.dev/pr/twentyhq/twenty/pull/22401?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="Review in cubic" src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a> <!-- End of auto-generated description by cubic. -->
This commit is contained in:
+14
-1
@@ -8,13 +8,15 @@ import {
|
||||
CleanSuspendedWorkspaceEmail,
|
||||
WarnSuspendedWorkspaceEmail,
|
||||
} from 'twenty-emails';
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
import { SettingsPath } from 'twenty-shared/types';
|
||||
import { getSettingsPath, isDefined } from 'twenty-shared/utils';
|
||||
import { WorkspaceActivationStatus } from 'twenty-shared/workspace';
|
||||
import { In, Repository } from 'typeorm';
|
||||
|
||||
import { BillingSubscriptionEntity } from 'src/engine/core-modules/billing/entities/billing-subscription.entity';
|
||||
import { SubscriptionStatus } from 'src/engine/core-modules/billing/enums/billing-subscription-status.enum';
|
||||
import { BillingSubscriptionService } from 'src/engine/core-modules/billing/services/billing-subscription.service';
|
||||
import { WorkspaceDomainsService } from 'src/engine/core-modules/domain/workspace-domains/services/workspace-domains.service';
|
||||
import { EmailService } from 'src/engine/core-modules/email/email.service';
|
||||
import { I18nService } from 'src/engine/core-modules/i18n/i18n.service';
|
||||
import { MetricsService } from 'src/engine/core-modules/metrics/metrics.service';
|
||||
@@ -67,6 +69,7 @@ export class CleanerWorkspaceService {
|
||||
private readonly userWorkspaceRepository: Repository<UserWorkspaceEntity>,
|
||||
private readonly i18nService: I18nService,
|
||||
private readonly metricsService: MetricsService,
|
||||
private readonly workspaceDomainsService: WorkspaceDomainsService,
|
||||
) {
|
||||
this.inactiveDaysBeforeSoftDelete = this.twentyConfigService.get(
|
||||
'WORKSPACE_INACTIVE_DAYS_BEFORE_SOFT_DELETION',
|
||||
@@ -115,6 +118,7 @@ export class CleanerWorkspaceService {
|
||||
async sendWarningEmail(
|
||||
workspaceMember: WorkspaceMemberWorkspaceEntity,
|
||||
workspaceDisplayName: string | undefined,
|
||||
billingSettingsUrl: string,
|
||||
daysSinceInactive: number,
|
||||
) {
|
||||
const emailData = {
|
||||
@@ -122,6 +126,7 @@ export class CleanerWorkspaceService {
|
||||
inactiveDaysBeforeDelete: this.inactiveDaysBeforeSoftDelete,
|
||||
userName: `${workspaceMember.name.firstName} ${workspaceMember.name.lastName}`,
|
||||
workspaceDisplayName: `${workspaceDisplayName}`,
|
||||
link: billingSettingsUrl,
|
||||
locale: workspaceMember.locale,
|
||||
};
|
||||
const emailTemplate = WarnSuspendedWorkspaceEmail(emailData);
|
||||
@@ -177,11 +182,19 @@ export class CleanerWorkspaceService {
|
||||
.join(', ')}']`,
|
||||
);
|
||||
|
||||
const billingSettingsUrl = this.workspaceDomainsService
|
||||
.buildWorkspaceURL({
|
||||
workspace,
|
||||
pathname: getSettingsPath(SettingsPath.Billing),
|
||||
})
|
||||
.toString();
|
||||
|
||||
if (!dryRun) {
|
||||
for (const workspaceMember of workspaceMembers) {
|
||||
await this.sendWarningEmail(
|
||||
workspaceMember,
|
||||
workspace.displayName,
|
||||
billingSettingsUrl,
|
||||
daysSinceInactive,
|
||||
);
|
||||
|
||||
|
||||
+2
@@ -3,6 +3,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
|
||||
import { BillingModule } from 'src/engine/core-modules/billing/billing.module';
|
||||
import { BillingSubscriptionEntity } from 'src/engine/core-modules/billing/entities/billing-subscription.entity';
|
||||
import { WorkspaceDomainsModule } from 'src/engine/core-modules/domain/workspace-domains/workspace-domains.module';
|
||||
import { EmailModule } from 'src/engine/core-modules/email/email.module';
|
||||
import { MetricsModule } from 'src/engine/core-modules/metrics/metrics.module';
|
||||
import { UserWorkspaceEntity } from 'src/engine/core-modules/user-workspace/user-workspace.entity';
|
||||
@@ -31,6 +32,7 @@ import { CleanerWorkspaceService } from 'src/engine/workspace-manager/workspace-
|
||||
EmailModule,
|
||||
BillingModule,
|
||||
MetricsModule,
|
||||
WorkspaceDomainsModule,
|
||||
],
|
||||
providers: [
|
||||
DestroyWorkspaceCommand,
|
||||
|
||||
Reference in New Issue
Block a user