@@ -47,7 +47,10 @@ export default class PromoCodeUsagesFactory extends AbstractModelFactory<PromoCo
4747 public async findByPromoCodeAndUser ( promoCodeId : ObjectId , userId : string ) : Promise < PromoCodeUsageModel | null > {
4848 await this . ensureIndexesOnce ( ) ;
4949
50- const usage = await this . collection . findOne ( { promoCodeId, userId } ) ;
50+ const usage = await this . collection . findOne ( {
51+ promoCodeId,
52+ userId,
53+ } ) ;
5154
5255 if ( ! usage ) {
5356 return null ;
@@ -65,7 +68,10 @@ export default class PromoCodeUsagesFactory extends AbstractModelFactory<PromoCo
6568 public async findByPromoCodeAndWorkspace ( promoCodeId : ObjectId , workspaceId : ObjectId ) : Promise < PromoCodeUsageModel | null > {
6669 await this . ensureIndexesOnce ( ) ;
6770
68- const usage = await this . collection . findOne ( { promoCodeId, workspaceId } ) ;
71+ const usage = await this . collection . findOne ( {
72+ promoCodeId,
73+ workspaceId,
74+ } ) ;
6975
7076 if ( ! usage ) {
7177 return null ;
@@ -96,13 +102,21 @@ export default class PromoCodeUsagesFactory extends AbstractModelFactory<PromoCo
96102 * Creates indexes required by promo usage limits.
97103 */
98104 private async ensureIndexesOnce ( ) : Promise < void > {
99- this . indexesPromise ??= Promise . all ( [
100- this . collection . createIndex ( { promoCodeId : 1 } ) ,
101- this . collection . createIndex ( { promoCodeId : 1 , userId : 1 } , { unique : true } ) ,
102- this . collection . createIndex ( { promoCodeId : 1 , workspaceId : 1 } , { unique : true } ) ,
103- this . collection . createIndex ( { workspaceId : 1 } ) ,
104- this . collection . createIndex ( { userId : 1 } ) ,
105- ] ) . then ( ( ) => undefined ) ;
105+ if ( ! this . indexesPromise ) {
106+ this . indexesPromise = Promise . all ( [
107+ this . collection . createIndex ( { promoCodeId : 1 } ) ,
108+ this . collection . createIndex ( {
109+ promoCodeId : 1 ,
110+ userId : 1 ,
111+ } , { unique : true } ) ,
112+ this . collection . createIndex ( {
113+ promoCodeId : 1 ,
114+ workspaceId : 1 ,
115+ } , { unique : true } ) ,
116+ this . collection . createIndex ( { workspaceId : 1 } ) ,
117+ this . collection . createIndex ( { userId : 1 } ) ,
118+ ] ) . then ( ( ) => undefined ) ;
119+ }
106120
107121 await this . indexesPromise ;
108122 }
0 commit comments