@@ -12,11 +12,6 @@ export default class PromoCodeUsagesFactory extends AbstractModelFactory<PromoCo
1212 */
1313 protected collection : Collection < PromoCodeUsageDBScheme > ;
1414
15- /**
16- * Index creation promise.
17- */
18- private indexesPromise ?: Promise < void > ;
19-
2015 /**
2116 * Creates promo code usages factory instance.
2217 *
@@ -33,8 +28,6 @@ export default class PromoCodeUsagesFactory extends AbstractModelFactory<PromoCo
3328 * @param promoCodeId - promo code id
3429 */
3530 public async countByPromoCodeId ( promoCodeId : ObjectId ) : Promise < number > {
36- await this . ensureIndexesOnce ( ) ;
37-
3831 return this . collection . countDocuments ( { promoCodeId } ) ;
3932 }
4033
@@ -45,8 +38,6 @@ export default class PromoCodeUsagesFactory extends AbstractModelFactory<PromoCo
4538 * @param userId - user id
4639 */
4740 public async findByPromoCodeAndUser ( promoCodeId : ObjectId , userId : string ) : Promise < PromoCodeUsageModel | null > {
48- await this . ensureIndexesOnce ( ) ;
49-
5041 const usage = await this . collection . findOne ( {
5142 promoCodeId,
5243 userId,
@@ -66,8 +57,6 @@ export default class PromoCodeUsagesFactory extends AbstractModelFactory<PromoCo
6657 * @param workspaceId - workspace id
6758 */
6859 public async findByPromoCodeAndWorkspace ( promoCodeId : ObjectId , workspaceId : ObjectId ) : Promise < PromoCodeUsageModel | null > {
69- await this . ensureIndexesOnce ( ) ;
70-
7160 const usage = await this . collection . findOne ( {
7261 promoCodeId,
7362 workspaceId,
@@ -86,8 +75,6 @@ export default class PromoCodeUsagesFactory extends AbstractModelFactory<PromoCo
8675 * @param usageData - promo code usage data
8776 */
8877 public async create ( usageData : Omit < PromoCodeUsageDBScheme , '_id' > ) : Promise < PromoCodeUsageModel > {
89- await this . ensureIndexesOnce ( ) ;
90-
9178 const usage = {
9279 _id : new ObjectId ( ) ,
9380 ...usageData ,
@@ -110,30 +97,4 @@ export default class PromoCodeUsagesFactory extends AbstractModelFactory<PromoCo
11097 _id : usageId ,
11198 } ) ;
11299 }
113-
114- /**
115- * Ensures promo usage indexes exist before queries.
116- *
117- * MongoDB createIndex is idempotent: after API restart it reuses an existing index
118- * with the same keys/options and does not throw if the index is already present.
119- */
120- private async ensureIndexesOnce ( ) : Promise < void > {
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- }
136-
137- await this . indexesPromise ;
138- }
139100}
0 commit comments