Skip to content

Commit 56d6546

Browse files
Егор КоноваловЕгор Коновалов
authored andcommitted
fix(): eslint and types
1 parent 0053c2c commit 56d6546

4 files changed

Lines changed: 81 additions & 65 deletions

File tree

workers/limiter/src/dbHelper.ts

Lines changed: 72 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Collection, Db, ObjectId } from 'mongodb';
22
import { ProjectDBScheme, WorkspaceDBScheme } from '@hawk.so/types';
33
import { WorkspaceWithTariffPlan } from '../types';
44
import HawkCatcher from '@hawk.so/nodejs';
5-
import { CriticalError } from '../../../lib/workerErrors';
5+
import { CriticalError, NonCriticalError } from '../../../lib/workerErrors';
66

77
/**
88
* Class that implements methods used for interaction between limiter and db
@@ -44,6 +44,9 @@ export class DbHelper {
4444
* @param id - id of the workspace to fetch
4545
*/
4646
public getWorkspacesWithTariffPlans(id: string): Promise<WorkspaceWithTariffPlan>;
47+
/**
48+
* @param id - id of the workspace to fetch
49+
*/
4750
public getWorkspacesWithTariffPlans(id?: string): AsyncGenerator<WorkspaceWithTariffPlan> | Promise<WorkspaceWithTariffPlan> {
4851
if (id !== undefined) {
4952
return this.getOneWorkspaceWithTariffPlan(id);
@@ -52,66 +55,6 @@ export class DbHelper {
5255
return this.yieldWorkspacesWithTariffPlans();
5356
}
5457

55-
/**
56-
* Returns a single workspace with its tariff plan by id
57-
*
58-
* @param id - workspace id
59-
*/
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-
85-
/* eslint-disable-next-line */
86-
private tariffPlanLookupPipeline(): any[] {
87-
return [
88-
{
89-
$lookup: {
90-
from: 'plans',
91-
localField: 'tariffPlanId',
92-
foreignField: '_id',
93-
as: 'tariffPlan',
94-
},
95-
},
96-
{
97-
$unwind: {
98-
path: '$tariffPlan',
99-
},
100-
},
101-
{
102-
$project: {
103-
_id: 1,
104-
name: 1,
105-
isBlocked: 1,
106-
blockedDate: 1,
107-
lastChargeDate: 1,
108-
billingPeriodEventsCount: 1,
109-
tariffPlan: 1,
110-
},
111-
},
112-
];
113-
}
114-
11558
/**
11659
* Updates workspaces data in Database
11760
*
@@ -204,4 +147,72 @@ export class DbHelper {
204147

205148
return this.projectsCollection.find(query).toArray();
206149
}
150+
151+
/**
152+
* Returns a single workspace with its tariff plan by id
153+
*
154+
* @param id - workspace id
155+
*/
156+
private async getOneWorkspaceWithTariffPlan(id: string): Promise<WorkspaceWithTariffPlan> {
157+
const pipeline = [
158+
{
159+
$match: {
160+
_id: new ObjectId(id),
161+
},
162+
},
163+
...this.tariffPlanLookupPipeline(),
164+
];
165+
166+
const workspace = this.workspacesCollection.aggregate<WorkspaceWithTariffPlan>(pipeline).next();
167+
168+
if (workspace === null) {
169+
throw new NonCriticalError(`Workspace ${id} not found`, {
170+
workspaceId: id,
171+
});
172+
}
173+
174+
return workspace;
175+
}
176+
177+
/**
178+
* Yields all workspaces with their tariff plans one by one
179+
*/
180+
private async * yieldWorkspacesWithTariffPlans(): AsyncGenerator<WorkspaceWithTariffPlan> {
181+
const pipeline = this.tariffPlanLookupPipeline();
182+
const cursor = this.workspacesCollection.aggregate<WorkspaceWithTariffPlan>(pipeline);
183+
184+
for await (const workspace of cursor) {
185+
yield workspace;
186+
}
187+
}
188+
189+
/* eslint-disable-next-line */
190+
private tariffPlanLookupPipeline(): any[] {
191+
return [
192+
{
193+
$lookup: {
194+
from: 'plans',
195+
localField: 'tariffPlanId',
196+
foreignField: '_id',
197+
as: 'tariffPlan',
198+
},
199+
},
200+
{
201+
$unwind: {
202+
path: '$tariffPlan',
203+
},
204+
},
205+
{
206+
$project: {
207+
_id: 1,
208+
name: 1,
209+
isBlocked: 1,
210+
blockedDate: 1,
211+
lastChargeDate: 1,
212+
billingPeriodEventsCount: 1,
213+
tariffPlan: 1,
214+
},
215+
},
216+
];
217+
}
207218
}

workers/limiter/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ export default class LimiterWorker extends Worker {
223223
updatedWorkspace.isBlocked = true;
224224
updatedWorkspace.blockedDate = new Date();
225225

226-
this.redis.appendBannedProjects(projectIds);
226+
await this.redis.appendBannedProjects(projectIds);
227227
message += this.formSingleWorkspaceMessage(updatedWorkspace, projectsToUpdate, 'blocked');
228228
}
229229
}

workers/limiter/tests/dbHelper.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ describe('DbHelper', () => {
161161
*/
162162
const cursor = dbHelper.getWorkspacesWithTariffPlans();
163163

164-
const workspaces = []
164+
const workspaces = [];
165165

166166
for await (const workspace of cursor) {
167-
workspaces.push(workspace)
167+
workspaces.push(workspace);
168168
}
169169

170170
/**

workers/limiter/types/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,9 @@ import { PlanDBScheme, WorkspaceDBScheme } from '@hawk.so/types';
33
/**
44
* Workspace with its tariff plan
55
*/
6-
export type WorkspaceWithTariffPlan = WorkspaceDBScheme & {tariffPlan: PlanDBScheme};
6+
export type WorkspaceWithTariffPlan = Pick<
7+
WorkspaceDBScheme,
8+
'_id' | 'name' | 'isBlocked' | 'blockedDate' | 'lastChargeDate' | 'billingPeriodEventsCount'
9+
> & {
10+
tariffPlan: PlanDBScheme;
11+
};

0 commit comments

Comments
 (0)