-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add sync customer command and drop subscription customer constraint (#…
…9131) **TLDR:** Solves (twentyhq/private-issues#212) Add command to sync customer data from stripe to BillingCustomerTable for all active workspaces. Drop foreign key contraint on billingCustomer in BillingSubscription (in order to not break the DB). **In order to test:** - Billing should be enabled - Have some workspaces that are active and whose id's are not mentioned in BillingCustomer (but the customer are present in stripe). Run the command: `npx nx run twenty-server:command billing:sync-customer-data` Take into consideration Due that all the previous subscriptions in Stripe have the workspaceId in their metadata, we use that information as source of true for the data sync **Things to do:** - Add tests for Billing utils - Separate StripeService into multipleServices (stripeSubscriptionService, stripePriceService etc) perhaps add them in (twentyhq/private-issues#201)?
- Loading branch information
Showing
5 changed files
with
112 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...nty-server/src/engine/core-modules/billing/commands/billing-sync-customer-data.command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
|
||
import chalk from 'chalk'; | ||
import { Command } from 'nest-commander'; | ||
import { Repository } from 'typeorm'; | ||
|
||
import { | ||
ActiveWorkspacesCommandOptions, | ||
ActiveWorkspacesCommandRunner, | ||
} from 'src/database/commands/active-workspaces.command'; | ||
import { BillingCustomer } from 'src/engine/core-modules/billing/entities/billing-customer.entity'; | ||
import { StripeService } from 'src/engine/core-modules/billing/stripe/stripe.service'; | ||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; | ||
|
||
interface SyncCustomerDataCommandOptions | ||
extends ActiveWorkspacesCommandOptions {} | ||
|
||
@Command({ | ||
name: 'billing:sync-customer-data', | ||
description: 'Sync customer data from Stripe for all active workspaces', | ||
}) | ||
export class BillingSyncCustomerDataCommand extends ActiveWorkspacesCommandRunner { | ||
constructor( | ||
@InjectRepository(Workspace, 'core') | ||
protected readonly workspaceRepository: Repository<Workspace>, | ||
private readonly stripeService: StripeService, | ||
@InjectRepository(BillingCustomer, 'core') | ||
protected readonly billingCustomerRepository: Repository<BillingCustomer>, | ||
) { | ||
super(workspaceRepository); | ||
} | ||
|
||
async executeActiveWorkspacesCommand( | ||
_passedParam: string[], | ||
options: SyncCustomerDataCommandOptions, | ||
workspaceIds: string[], | ||
): Promise<void> { | ||
this.logger.log('Running command to sync customer data'); | ||
|
||
for (const workspaceId of workspaceIds) { | ||
this.logger.log(`Running command for workspace ${workspaceId}`); | ||
|
||
try { | ||
await this.syncCustomerDataForWorkspace(workspaceId, options); | ||
} catch (error) { | ||
this.logger.log( | ||
chalk.red( | ||
`Running command on workspace ${workspaceId} failed with error: ${error}, ${error.stack}`, | ||
), | ||
); | ||
continue; | ||
} finally { | ||
this.logger.log( | ||
chalk.green(`Finished running command for workspace ${workspaceId}.`), | ||
); | ||
} | ||
} | ||
|
||
this.logger.log(chalk.green(`Command completed!`)); | ||
} | ||
|
||
private async syncCustomerDataForWorkspace( | ||
workspaceId: string, | ||
options: SyncCustomerDataCommandOptions, | ||
): Promise<void> { | ||
const billingCustomer = await this.billingCustomerRepository.findOne({ | ||
where: { | ||
workspaceId, | ||
}, | ||
}); | ||
|
||
if (!options.dryRun && !billingCustomer) { | ||
const stripeCustomerId = | ||
await this.stripeService.getStripeCustomerIdFromWorkspaceId( | ||
workspaceId, | ||
); | ||
|
||
if (stripeCustomerId) { | ||
await this.billingCustomerRepository.upsert( | ||
{ | ||
stripeCustomerId, | ||
workspaceId, | ||
}, | ||
{ | ||
conflictPaths: ['workspaceId'], | ||
}, | ||
); | ||
} | ||
} | ||
|
||
if (options.verbose) { | ||
this.logger.log( | ||
chalk.yellow(`Added ${workspaceId} to billingCustomer table`), | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters