Skip to content

Commit 89b75c1

Browse files
authored
Merge pull request #806 from Meshmulla/main
Refactor: Add Repository Methods and Remove Direct Access to SubscriptionIndexRepository.repo
2 parents 141a435 + 1d3a77c commit 89b75c1

4 files changed

Lines changed: 67 additions & 23 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,4 +280,4 @@
280280

281281
### Performance Improvements
282282

283-
* Optimize creator profile page performance ([877e30f](https://github.com/MyFanss/MyFans/commit/877e30f6f87fcef42d889e4a79afba5a7aceba82)), closes [#415](https://github.com/MyFanss/MyFans/issues/415)
283+
* Optimize creator profile page performance ([877e30f](https://github.com/Meshmulla/MyFans/commit/877e30f6f87fcef42d889e4a79afba5a7aceba82)), closes [#415](https://github.com/Meshmulla/MyFans/issues/415)

backend/src/subscriptions/repositories/subscription-index.repository.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,29 @@ export class SubscriptionIndexRepository {
143143
order: { indexedAt: 'DESC' },
144144
});
145145
}
146+
147+
async findWithCursor(
148+
fan: string,
149+
status: SubscriptionStatus | undefined,
150+
sort: string | undefined,
151+
cursorId: string | undefined,
152+
limit: number,
153+
): Promise<SubscriptionIndexEntity[]> {
154+
const qb = this.repo
155+
.createQueryBuilder('sub')
156+
.where('sub.fan = :fan', { fan })
157+
.orderBy(sort === 'created' ? 'sub.createdAt' : 'sub.expiryUnix', 'DESC')
158+
.take(limit + 1);
159+
160+
if (status) {
161+
qb.andWhere('sub.status = :status', { status });
162+
}
163+
164+
if (cursorId) {
165+
qb.andWhere('sub.id > :cursorId', { cursorId });
166+
}
167+
168+
return qb.getMany();
169+
}
146170
}
147171

backend/src/subscriptions/subscriptions.service.spec.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ describe('SubscriptionsService', () => {
106106
}
107107
}),
108108
findAllForReconciler: jest.fn(async () => currentSubs),
109+
findWithCursor: jest.fn(async (fan, status, sort, cursorId, limit) => {
110+
let filtered = currentSubs.filter(
111+
(sub) => sub.fan === fan && (status ? sub.status === status : true),
112+
);
113+
if (cursorId) {
114+
filtered = filtered.filter((sub) => sub.id > cursorId);
115+
}
116+
return filtered.slice(0, limit + 1);
117+
}),
109118
} as unknown as jest.Mocked<SubscriptionIndexRepository>;
110119

111120
const module: TestingModule = await Test.createTestingModule({
@@ -154,8 +163,38 @@ describe('SubscriptionsService', () => {
154163
);
155164

156165
expect(result.data).toHaveLength(1);
157-
expect(result.total).toBe(1);
158-
expect(repo.findAndCountForFan).toHaveBeenCalled();
166+
expect(repo.findWithCursor).toHaveBeenCalledWith(
167+
'GFANADDRESS333333333333333333333333333333333333333333333333',
168+
undefined,
169+
undefined,
170+
undefined,
171+
20,
172+
);
173+
});
174+
175+
it('passes status and cursor to findWithCursor', async () => {
176+
await service.addSubscription(
177+
'GFANADDRESS666666666666666666666666666666666666666666666666',
178+
'GAAAAAAAAAAAAAAA',
179+
1,
180+
Math.floor(Date.now() / 1000) + 60,
181+
);
182+
183+
await service.listSubscriptions(
184+
'GFANADDRESS666666666666666666666666666666666666666666666666',
185+
SubscriptionStatus.ACTIVE,
186+
'created',
187+
'some-cursor',
188+
10,
189+
);
190+
191+
expect(repo.findWithCursor).toHaveBeenCalledWith(
192+
'GFANADDRESS666666666666666666666666666666666666666666666666',
193+
SubscriptionStatus.ACTIVE,
194+
'created',
195+
'some-cursor',
196+
10,
197+
);
159198
});
160199

161200
it('publishes a renewal event when confirming an existing subscription', async () => {

backend/src/subscriptions/subscriptions.service.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -398,26 +398,7 @@ export class SubscriptionsService {
398398
cursor?: string,
399399
limit: number = 20,
400400
) {
401-
const where: any = { fan };
402-
403-
if (status) {
404-
where.status = status;
405-
}
406-
407-
const queryBuilder = this.indexRepo.repo
408-
.createQueryBuilder('sub')
409-
.where('sub.fan = :fan', { fan })
410-
.orderBy(sort === 'created' ? 'sub.createdAt' : 'sub.expiryUnix', 'DESC')
411-
.take(limit + 1);
412-
413-
if (cursor) {
414-
const cursorId = parseInt(cursor, 10);
415-
if (!isNaN(cursorId)) {
416-
queryBuilder.andWhere('sub.id > :cursorId', { cursorId });
417-
}
418-
}
419-
420-
const results = await queryBuilder.getMany();
401+
const results = await this.indexRepo.findWithCursor(fan, status, sort, cursor, limit);
421402
const hasMore = results.length > limit;
422403
if (hasMore) {
423404
results.pop();

0 commit comments

Comments
 (0)