Skip to content

Commit ec7c0b7

Browse files
committed
imp(limiter): messages
1 parent 221bc41 commit ec7c0b7

2 files changed

Lines changed: 28 additions & 19 deletions

File tree

workers/limiter/src/index.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,20 @@ export default class LimiterWorker extends Worker {
115115
return;
116116
}
117117

118+
/**
119+
* If workspace is already blocked - do nothing
120+
*/
121+
if (workspace.isBlocked) {
122+
return;
123+
}
124+
118125
const workspaceProjects = await this.dbHelper.getProjects(event.workspaceId);
119126
const projectIds = workspaceProjects.map(project => project._id.toString());
120127

121128
await this.dbHelper.changeWorkspaceBlockedState(event.workspaceId, true);
122129
await this.redis.appendBannedProjects(projectIds);
123130

124-
this.sendSingleWorkspaceReport(workspaceProjects, workspace, 'Blocked');
131+
this.sendSingleWorkspaceReport(workspaceProjects, workspace, 'blocked');
125132
}
126133

127134
/**
@@ -137,6 +144,13 @@ export default class LimiterWorker extends Worker {
137144
return;
138145
}
139146

147+
/**
148+
* If workspace is already unblocked - do nothing
149+
*/
150+
if (workspace.isBlocked === false) {
151+
return;
152+
}
153+
140154
const workspaceProjects = await this.dbHelper.getProjects(event.workspaceId);
141155
const projectIds = workspaceProjects.map(project => project._id.toString());
142156

@@ -152,7 +166,7 @@ export default class LimiterWorker extends Worker {
152166
await this.dbHelper.changeWorkspaceBlockedState(event.workspaceId, false);
153167
await this.redis.removeBannedProjects(projectIds);
154168

155-
this.sendSingleWorkspaceReport(workspaceProjects, workspace, 'Unblocked');
169+
this.sendSingleWorkspaceReport(workspaceProjects, workspace, 'unblocked');
156170
}
157171

158172
/**
@@ -185,7 +199,7 @@ export default class LimiterWorker extends Worker {
185199
const projectIds = projectsToUpdate.map(project => project._id.toString());
186200

187201
this.redis.appendBannedProjects(projectIds);
188-
message += this.formSingleWorkspaceMessage(workspace, projectsToUpdate, 'Blocked');
202+
message += this.formSingleWorkspaceMessage(workspace, projectsToUpdate, 'blocked');
189203
}
190204
}));
191205

@@ -277,16 +291,17 @@ export default class LimiterWorker extends Worker {
277291
* @param type - workspace was blocked or unblocked
278292
* @returns {string} formatted html string
279293
*/
280-
private formSingleWorkspaceMessage(workspace: WorkspaceWithTariffPlan, projects: ProjectDBScheme[], type: 'Blocked' | 'Unblocked'): string {
281-
let message = `<b>${type} ${workspace.name} (id: ${workspace._id})
282-
quota: ${workspace.billingPeriodEventsCount} of ${workspace.tariffPlan.eventsLimit}</b>\n`;
294+
private formSingleWorkspaceMessage(workspace: WorkspaceWithTariffPlan, projects: ProjectDBScheme[], type: 'blocked' | 'unblocked'): string {
295+
const statusEmoji = type === 'blocked' ? '⛔️' : '✅';
283296

284-
if (projects.length === 0) {
285-
message += `<code>none, projects are already stored in redis</code>`;
297+
let message = `${statusEmoji} Workspace <b>${workspace.name}</b> ${type} <b>(id: <code>${workspace._id}</code>)</b>\n\n\
298+
Quota: ${workspace.billingPeriodEventsCount} of ${workspace.tariffPlan.eventsLimit}</b>\n\n`;
286299

300+
if (projects.length === 0) {
287301
return message;
288302
}
289303

304+
message += `Project ids ${type === 'blocked' ? 'added' : 'removed'} from Redis:\n`;
290305
message += `${projects.map(project => `• ${project.name} (id: <code>${project._id}</code>)`).join('\n')}`;
291306

292307
return message;
@@ -299,10 +314,10 @@ export default class LimiterWorker extends Worker {
299314
* @param workspace - blocked or unblocked workspace
300315
* @param type - workspace was blocked or unblocked
301316
*/
302-
private sendSingleWorkspaceReport(projects: ProjectDBScheme[], workspace: WorkspaceWithTariffPlan, type: 'Blocked' | 'Unblocked'): void {
317+
private sendSingleWorkspaceReport(projects: ProjectDBScheme[], workspace: WorkspaceWithTariffPlan, type: 'blocked' | 'unblocked'): void {
303318
const message = this.formSingleWorkspaceMessage(workspace, projects, type);
304319

305-
telegram.sendMessage(`🔐 <b>[ Limiter / Single ]</b>\n${message}`, telegram.TelegramBotURLs.Limiter);
320+
telegram.sendMessage(`${message}`, telegram.TelegramBotURLs.Limiter);
306321
}
307322

308323
/**

workers/paymaster/tests/index.test.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,7 @@ describe('PaymasterWorker', () => {
141141
*/
142142
expect(blockWorkspaceSpy).toHaveBeenNthCalledWith(1, 'cron-tasks/limiter', {
143143
type: 'block-workspace',
144-
payload: {
145-
workspaceId: workspace._id.toString(),
146-
},
144+
workspaceId: workspace._id.toString(),
147145
});
148146

149147
expect(blockWorkspaceSpy).toHaveBeenNthCalledWith(2, 'sender/email', {
@@ -196,9 +194,7 @@ describe('PaymasterWorker', () => {
196194

197195
expect(blockWorkspaceSpy).not.toHaveBeenCalledWith('cron-tasks/limiter', {
198196
type: 'block-workspace',
199-
payload: {
200-
workspaceId: workspace._id.toString(),
201-
},
197+
workspaceId: workspace._id.toString(),
202198
});
203199

204200
expect(blockWorkspaceSpy).not.toHaveBeenCalledWith('sender/email', {
@@ -250,9 +246,7 @@ describe('PaymasterWorker', () => {
250246

251247
expect(blockWorkspaceSpy).toHaveBeenNthCalledWith(1, 'cron-tasks/limiter', {
252248
type: 'block-workspace',
253-
payload: {
254-
workspaceId: workspace._id.toString(),
255-
},
249+
workspaceId: workspace._id.toString(),
256250
});
257251
expect(blockWorkspaceSpy).toHaveBeenNthCalledWith(2, 'sender/email', {
258252
type: 'block-workspace',

0 commit comments

Comments
 (0)