Skip to content

Commit 0aa6738

Browse files
committed
fix
1 parent e85c91e commit 0aa6738

3 files changed

Lines changed: 27 additions & 11 deletions

File tree

src/models/promoCodeUsagesFactory.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/models/promoCodesFactory.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ export default class PromoCodesFactory extends AbstractModelFactory<PromoCodeDBS
4848
* Creates indexes required by promo codes lookups.
4949
*/
5050
private async ensureIndexesOnce(): Promise<void> {
51-
this.indexesPromise ??= this.collection.createIndex({ value: 1 }, { unique: true }).then(() => undefined);
51+
if (!this.indexesPromise) {
52+
this.indexesPromise = this.collection.createIndex({ value: 1 }, { unique: true }).then(() => undefined);
53+
}
5254

5355
await this.indexesPromise;
5456
}

src/resolvers/user.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import isE2E from '../utils/isE2E';
1010
import { dateFromObjectId } from '../utils/dates';
1111
import * as telegram from '../utils/telegram';
1212
import { MongoError } from 'mongodb';
13-
import type { Utm } from '@hawk.so/types';
13+
import type { Utm, UserDBScheme } from '@hawk.so/types';
1414
import { validateUtmParams } from '../utils/utm/utm';
1515

1616
/**

0 commit comments

Comments
 (0)