Skip to content

Commit 2f68edc

Browse files
Егор КоноваловЕгор Коновалов
authored andcommitted
imp(): fix function overload
1 parent bbf1f66 commit 2f68edc

2 files changed

Lines changed: 47 additions & 29 deletions

File tree

workers/limiter/src/dbHelper.ts

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,48 @@ export class DbHelper {
4343
*
4444
* @param id - id of the workspace to fetch
4545
*/
46-
public async getWorkspacesWithTariffPlans(id: string): Promise<WorkspaceWithTariffPlan>;
46+
public getWorkspacesWithTariffPlans(id: string): Promise<WorkspaceWithTariffPlan>;
47+
public getWorkspacesWithTariffPlans(id?: string): AsyncGenerator<WorkspaceWithTariffPlan> | Promise<WorkspaceWithTariffPlan> {
48+
if (id !== undefined) {
49+
return this.getOneWorkspaceWithTariffPlan(id);
50+
}
51+
52+
return this.yieldWorkspacesWithTariffPlans();
53+
}
54+
4755
/**
48-
* Returns workspace with its tariff plan by its id
56+
* Returns a single workspace with its tariff plan by id
4957
*
5058
* @param id - workspace id
5159
*/
52-
public async *getWorkspacesWithTariffPlans(id?: string): AsyncGenerator<WorkspaceWithTariffPlan> | Promise<WorkspaceWithTariffPlan> {
60+
private async getOneWorkspaceWithTariffPlan(id: string): Promise<WorkspaceWithTariffPlan> {
61+
const pipeline = [
62+
{
63+
$match: {
64+
_id: new ObjectId(id),
65+
},
66+
},
67+
...this.tariffPlanLookupPipeline(),
68+
];
69+
70+
return this.workspacesCollection.aggregate<WorkspaceWithTariffPlan>(pipeline).next();
71+
}
72+
73+
/**
74+
* Yields all workspaces with their tariff plans one by one
75+
*/
76+
private async *yieldWorkspacesWithTariffPlans(): AsyncGenerator<WorkspaceWithTariffPlan> {
77+
const pipeline = this.tariffPlanLookupPipeline();
78+
const cursor = this.workspacesCollection.aggregate<WorkspaceWithTariffPlan>(pipeline);
79+
80+
for await (const workspace of cursor) {
81+
yield workspace;
82+
}
83+
}
84+
5385
/* eslint-disable-next-line */
54-
const queue: any[] = [
86+
private tariffPlanLookupPipeline(): any[] {
87+
return [
5588
{
5689
$lookup: {
5790
from: 'plans',
@@ -66,32 +99,17 @@ export class DbHelper {
6699
},
67100
},
68101
{
69-
projection: {
102+
$project: {
70103
_id: 1,
104+
name: 1,
71105
isBlocked: 1,
106+
blockedDate: 1,
72107
lastChargeDate: 1,
108+
billingPeriodEventsCount: 1,
73109
tariffPlan: 1,
74-
}
75-
}
76-
];
77-
78-
if (id !== undefined) {
79-
queue.unshift({
80-
$match: {
81-
_id: new ObjectId(id),
82110
},
83-
});
84-
}
85-
86-
const workspaces = this.workspacesCollection.aggregate<WorkspaceWithTariffPlan>(queue);
87-
88-
if (id !== undefined) {
89-
return await workspaces.next();
90-
}
91-
92-
for await (const workspace of workspaces) {
93-
yield workspace;
94-
}
111+
},
112+
];
95113
}
96114

97115
/**

workers/limiter/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export default class LimiterWorker extends Worker {
198198
* If workspace is already blocked - do nothing
199199
*/
200200
if (workspace.isBlocked) {
201-
return;
201+
continue;
202202
}
203203

204204
const workspaceProjects = await this.dbHelper.getProjects(workspace._id.toString());
@@ -211,7 +211,7 @@ export default class LimiterWorker extends Worker {
211211
* If there are no projects to update - move on to next workspace
212212
*/
213213
if (projectsToUpdate.length === 0) {
214-
return;
214+
continue;
215215
}
216216

217217
/**
@@ -226,9 +226,9 @@ export default class LimiterWorker extends Worker {
226226
this.redis.appendBannedProjects(projectIds);
227227
message += this.formSingleWorkspaceMessage(updatedWorkspace, projectsToUpdate, 'blocked');
228228
}
229-
};
229+
}
230230

231-
this.dbHelper.updateWorkspacesEventsCountAndIsBlocked(updatedWorkspaces);
231+
await this.dbHelper.updateWorkspacesEventsCountAndIsBlocked(updatedWorkspaces);
232232

233233
this.sendRegularReport(message);
234234
}

0 commit comments

Comments
 (0)