Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

better code

Blue Moused4b6ecd9a32b
diff --git a/backend/apps/cloud/src/experiment/experiment.controller.ts b/backend/apps/cloud/src/experiment/experiment.controller.ts
index f663b628d..dd0171c4f 100644
--- a/backend/apps/cloud/src/experiment/experiment.controller.ts
+++ b/backend/apps/cloud/src/experiment/experiment.controller.ts
@@ -426,18 +426,10 @@ export class ExperimentController {
)
}
- await this.experimentService.deleteVariantsByExperiment(id)
-
- for (const v of experimentDto.variants) {
- const variant = new ExperimentVariant()
- variant.name = v.name
- variant.key = v.key
- variant.description = v.description || null
- variant.rolloutPercentage = v.rolloutPercentage
- variant.isControl = v.isControl
- variant.experiment = experiment
- await this.experimentService.createVariant(variant)
- }
+ await this.experimentService.recreateVariants(
+ experiment,
+ experimentDto.variants,
+ )
}
const exposureTrigger =
diff --git a/backend/apps/cloud/src/experiment/experiment.service.ts b/backend/apps/cloud/src/experiment/experiment.service.ts
index d4f49bbdc..4ba395d15 100644
--- a/backend/apps/cloud/src/experiment/experiment.service.ts
+++ b/backend/apps/cloud/src/experiment/experiment.service.ts
@@ -112,4 +112,30 @@ export class ExperimentService {
where: { experiment: { id: experimentId } },
})
}
+
+ async recreateVariants(
+ experiment: Experiment,
+ variantsData: Partial<ExperimentVariant>[],
+ ): Promise<void> {
+ await this.experimentRepository.manager.transaction(
+ async transactionalEntityManager => {
+ await transactionalEntityManager.delete(ExperimentVariant, {
+ experiment: { id: experiment.id },
+ })
+
+ const variants = variantsData.map(v => {
+ const variant = new ExperimentVariant()
+ variant.name = v.name
+ variant.key = v.key
+ variant.description = v.description || null
+ variant.rolloutPercentage = v.rolloutPercentage
+ variant.isControl = v.isControl
+ variant.experiment = experiment
+ return variant
+ })
+
+ await transactionalEntityManager.save(ExperimentVariant, variants)
+ },
+ )
+ }
}
diff --git a/backend/apps/cloud/src/revenue/revenue.controller.ts b/backend/apps/cloud/src/revenue/revenue.controller.ts
index 28ae70067..2eea07fbe 100644
--- a/backend/apps/cloud/src/revenue/revenue.controller.ts
+++ b/backend/apps/cloud/src/revenue/revenue.controller.ts
@@ -353,6 +353,12 @@ export class RevenueAnalyticsController {
this.projectService.allowedToView(project, userId)
+ if (!project.paddleApiKeyEnc && !project.stripeApiKeyEnc) {
+ throw new BadRequestException(
+ 'Revenue tracking is not configured for this project',
+ )
+ }
+
const safeTimezone = this.analyticsService.getSafeTimezone(dto.timezone)
const timeBucket = getLowestPossibleTimeBucket(dto.period, dto.from, dto.to)
@@ -385,24 +391,20 @@ export class RevenueAnalyticsController {
): Promise<RevenueBreakdownDto> {
this.logger.log({ userId, ...dto }, 'GET /log/revenue/breakdown')
- const project = await this.projectService.getFullProject(
- dto.pid,
- undefined,
- ['stripeApiKeyEnc', 'paddleApiKeyEnc'],
- )
+ const project = await this.projectService.getFullProject(dto.pid)
if (_isEmpty(project)) {
throw new NotFoundException('Project not found')
}
+ this.projectService.allowedToView(project, userId)
+
if (!project.paddleApiKeyEnc && !project.stripeApiKeyEnc) {
throw new BadRequestException(
'Revenue tracking is not configured for this project',
)
}
- this.projectService.allowedToView(project, userId)
-
const safeTimezone = this.analyticsService.getSafeTimezone(dto.timezone)
const timeBucket = getLowestPossibleTimeBucket(dto.period, dto.from, dto.to)