Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/modules/repositories/coupon/coupon.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ export class CouponRepository {

// subscribe to plan
const plan = await this.projectRepository.getPlanById(couponDto.plan_id, context!);

// deactivate any existing active subscription for the project
const existingSubscription = await this.projectRepository.getActiveSubscriptionsForProject(
couponDto.project_id,
context!,
);

for (const subscription of existingSubscription) {
// eslint-disable-next-line no-await-in-loop
await this.projectRepository.changeSubscriptionStatus(
subscription.id,
couponDto.project_id,
SubscriptionStatus.Inactive,
);
}

const subscriptionId = await this.projectRepository.subscribeProjectToPlan(
plan!,
couponDto.project_id,
Expand All @@ -53,7 +69,6 @@ export class CouponRepository {
validationErrorMessage: 'Failed to update coupon subscription id',
});
await context?.commitTransaction();

return code;
} catch (error) {
context?.rollbackTransaction();
Expand Down
23 changes: 23 additions & 0 deletions src/modules/repositories/projects/project.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,29 @@ import {
export class ProjectRepository {
constructor(@Inject(diConstants.logger) private readonly logger: BonadocsLogger) {}

@withDbContext
async changeSubscriptionStatus(
subscriptionId: number,
projectId: number,
status: SubscriptionStatus,
context?: DbContext,
): Promise<void> {
try {
context?.beginTransaction();
await context!.query({
text: queries.changeSubscriptionStatus,
values: [subscriptionId, projectId, status],
validateResult: (result) => !!result.rowCount,
validationErrorMessage: 'Failed to change subscription status',
});
context?.commitTransaction();
} catch (error) {
context?.rollbackTransaction();
this.logger.error('Failed to change subscription status', error);
throw error;
}
}

@withDbContext
async createProject(data: CreateProjectDto, context?: DbContext): Promise<number | undefined> {
try {
Expand Down
2 changes: 2 additions & 0 deletions src/modules/repositories/projects/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,6 @@ export const queries = {
'SELECT EXISTS ( SELECT 1 FROM "bonadocs"."projects" p WHERE p.id = $1 ) AS project_exists;',
checkIfCollectionExistById:
'SELECT EXISTS ( SELECT 1 FROM "bonadocs"."collections" c WHERE c.id = $1 AND c."project_id" = $2) AS collection_exists;',
changeSubscriptionStatus:
'UPDATE "bonadocs"."subscriptions" SET status = $3 WHERE id = $1 AND project_id = $2 RETURNING id;',
};
Loading