Skip to content

Commit f5b405f

Browse files
committed
refactor(billing): simplify promo code retrieval and improve index initialization logic
1 parent be4f3b2 commit f5b405f

3 files changed

Lines changed: 22 additions & 20 deletions

File tree

src/models/promoCodeUsagesFactory.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,21 @@ export default class PromoCodeUsagesFactory extends AbstractModelFactory<PromoCo
118118
* with the same keys/options and does not throw if the index is already present.
119119
*/
120120
private async ensureIndexesOnce(): Promise<void> {
121-
this.indexesPromise ??= Promise.all([
122-
this.collection.createIndex({ promoCodeId: 1 }),
123-
this.collection.createIndex({
124-
promoCodeId: 1,
125-
userId: 1,
126-
}, { unique: true }),
127-
this.collection.createIndex({
128-
promoCodeId: 1,
129-
workspaceId: 1,
130-
}, { unique: true }),
131-
this.collection.createIndex({ workspaceId: 1 }),
132-
this.collection.createIndex({ userId: 1 }),
133-
]).then(() => undefined);
121+
if (!this.indexesPromise) {
122+
this.indexesPromise = Promise.all([
123+
this.collection.createIndex({ promoCodeId: 1 }),
124+
this.collection.createIndex({
125+
promoCodeId: 1,
126+
userId: 1,
127+
}, { unique: true }),
128+
this.collection.createIndex({
129+
promoCodeId: 1,
130+
workspaceId: 1,
131+
}, { unique: true }),
132+
this.collection.createIndex({ workspaceId: 1 }),
133+
this.collection.createIndex({ userId: 1 }),
134+
]).then(() => undefined);
135+
}
134136

135137
await this.indexesPromise;
136138
}

src/models/promoCodesFactory.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,7 @@ export default class PromoCodesFactory extends AbstractModelFactory<PromoCodeDBS
3535
public async findByValue(value: string): Promise<PromoCodeModel | null> {
3636
await this.ensureIndexesOnce();
3737

38-
const promoCode = await this.collection.findOne({ value });
39-
40-
if (!promoCode) {
41-
return null;
42-
}
43-
44-
return new PromoCodeModel(promoCode);
38+
return this.findOne({ value });
4539
}
4640

4741
/**

src/services/promoCodeService.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ export type PromoCodeUtm = Utm;
143143
* Normalizes promo code value before DB lookup.
144144
*
145145
* @param value - raw promo code value
146+
* @returns normalized promo code value
146147
*/
147148
export function normalizePromoCodeValue(value: string): string {
148149
return value.trim().toUpperCase();
@@ -152,6 +153,7 @@ export function normalizePromoCodeValue(value: string): string {
152153
* Checks if promo value format is allowed.
153154
*
154155
* @param value - normalized promo code value
156+
* @returns whether value has allowed promo code format
155157
*/
156158
function isAllowedPromoValue(value: string): boolean {
157159
return Boolean(value) && PROMO_CODE_REGEXP.test(value);
@@ -161,6 +163,7 @@ function isAllowedPromoValue(value: string): boolean {
161163
* Returns whether plan is available for purchase (not hidden).
162164
*
163165
* @param plan - tariff plan
166+
* @returns whether plan can be selected for paid purchase or grant_plan promo
164167
*/
165168
function isPlanAvailableForPurchase(plan: PlanModel): boolean {
166169
return plan.isHidden !== true;
@@ -171,6 +174,7 @@ function isPlanAvailableForPurchase(plan: PlanModel): boolean {
171174
*
172175
* @param benefit - promo benefit
173176
* @param plan - selected plan
177+
* @returns whether benefit can be applied to the selected plan
174178
*/
175179
function isPlanApplicable(benefit: PromoCodeBenefit, plan: PlanModel): boolean {
176180
if (benefit.type === 'grant_plan') {
@@ -188,6 +192,7 @@ function isPlanApplicable(benefit: PromoCodeBenefit, plan: PlanModel): boolean {
188192
* Returns whether discount promo can affect plan price.
189193
*
190194
* @param plan - tariff plan
195+
* @returns whether plan is paid and available for purchase
191196
*/
192197
function isDiscountablePlan(plan: PlanModel): boolean {
193198
return plan.monthlyCharge > 0 && isPlanAvailableForPurchase(plan);
@@ -198,6 +203,7 @@ function isDiscountablePlan(plan: PlanModel): boolean {
198203
*
199204
* @param benefit - promo benefit
200205
* @param plan - selected plan
206+
* @returns calculated promo price for selected plan
201207
*/
202208
export function calculatePromoCodePlanPrice(benefit: PromoCodeBenefit, plan: PlanModel): PromoCodePlanPrice {
203209
const originalAmount = plan.monthlyCharge;

0 commit comments

Comments
 (0)