diff --git a/workers/limiter/src/dbHelper.ts b/workers/limiter/src/dbHelper.ts index a2ad3726..7ad4c3e5 100644 --- a/workers/limiter/src/dbHelper.ts +++ b/workers/limiter/src/dbHelper.ts @@ -109,23 +109,6 @@ export class DbHelper { await this.workspacesCollection.bulkWrite(operations); } - /** - * Method to change workspace isBlocked state - * - * @param workspaceId - id of the workspace to be changed - * @param isBlocked - new isBlocked state of the workspace - */ - public async changeWorkspaceBlockedState(workspaceId: string, isBlocked: boolean): Promise { - await this.workspacesCollection.updateOne( - { _id: new ObjectId(workspaceId) }, - { - $set: { - isBlocked, - }, - } - ); - } - /** * Returns total event counts for last billing period * diff --git a/workers/limiter/src/index.ts b/workers/limiter/src/index.ts index b53061ef..8bb2d2c3 100644 --- a/workers/limiter/src/index.ts +++ b/workers/limiter/src/index.ts @@ -108,6 +108,8 @@ export default class LimiterWorker extends Worker { * @param event - event to handle */ private async handleBlockWorkspaceEvent(event: BlockWorkspaceEvent): Promise { + this.logger.info('handle block workspace event', event); + const workspace = await this.dbHelper.getWorkspacesWithTariffPlans(event.workspaceId); if (!workspace) { @@ -126,7 +128,13 @@ export default class LimiterWorker extends Worker { const workspaceProjects = await this.dbHelper.getProjects(event.workspaceId); const projectIds = workspaceProjects.map(project => project._id.toString()); - await this.dbHelper.changeWorkspaceBlockedState(event.workspaceId, true); + const { updatedWorkspace } = await this.prepareWorkspaceUsageUpdate(workspace, workspaceProjects); + + updatedWorkspace.isBlocked = true; + await this.dbHelper.updateWorkspacesEventsCountAndIsBlocked([updatedWorkspace]); + + this.logger.info('workspace blocked in db ', event.workspaceId) + await this.redis.appendBannedProjects(projectIds); this.sendSingleWorkspaceReport(workspaceProjects, workspace, 'blocked'); @@ -159,16 +167,18 @@ export default class LimiterWorker extends Worker { /** * If workspace should be blocked by quota - then do not unblock it */ - const { shouldBeBlockedByQuota } = await this.prepareWorkspaceUsageUpdate(workspace, workspaceProjects); + const { shouldBeBlockedByQuota, updatedWorkspace } = await this.prepareWorkspaceUsageUpdate(workspace, workspaceProjects); if (shouldBeBlockedByQuota) { return; } - await this.dbHelper.changeWorkspaceBlockedState(event.workspaceId, false); + updatedWorkspace.isBlocked = false; + + await this.dbHelper.updateWorkspacesEventsCountAndIsBlocked([updatedWorkspace]); await this.redis.removeBannedProjects(projectIds); - this.sendSingleWorkspaceReport(workspaceProjects, workspace, 'unblocked'); + this.sendSingleWorkspaceReport(workspaceProjects, updatedWorkspace, 'unblocked'); } /** @@ -229,6 +239,8 @@ export default class LimiterWorker extends Worker { private async prepareWorkspaceUsageUpdate( workspace: WorkspaceWithTariffPlan, projects: ProjectDBScheme[] ): Promise { + this.logger.info('prepareWorkspaceUsageUpdate'); + /** * If last charge date is not specified, then we skip checking it * In the next time the Paymaster worker starts, it will set lastChargeDate for this workspace @@ -247,6 +259,9 @@ export default class LimiterWorker extends Worker { const since = Math.floor(new Date(workspace.lastChargeDate).getTime() / MS_IN_SEC); const workspaceEventsCount = await this.dbHelper.getEventsCountByProjects(projects, since); + + this.logger.info(`workspace ${workspace._id} events count since last charge date: ${workspaceEventsCount}`); + const usedQuota = workspaceEventsCount / workspace.tariffPlan.eventsLimit; const quotaNotification = NOTIFY_ABOUT_LIMIT.reverse().find(quota => quota < usedQuota); diff --git a/workers/limiter/tests/dbHelper.test.ts b/workers/limiter/tests/dbHelper.test.ts index 6c4c8f0f..bdff1cb5 100644 --- a/workers/limiter/tests/dbHelper.test.ts +++ b/workers/limiter/tests/dbHelper.test.ts @@ -272,34 +272,6 @@ describe('DbHelper', () => { }); }); - describe('changeWorkspaceBlockedState', () => { - test('Should change workspace blocked state', async () => { - /** - * Arrange - */ - const workspace = createWorkspaceMock({ - plan: mockedPlans.eventsLimit10, - billingPeriodEventsCount: 0, - lastChargeDate: new Date(), - isBlocked: false, - }); - - await workspaceCollection.insertOne(workspace); - - /** - * Act - */ - await dbHelper.changeWorkspaceBlockedState(workspace._id.toString(), true); - - /** - * Assert - */ - const updatedWorkspace = await workspaceCollection.findOne({ _id: workspace._id }); - - expect(updatedWorkspace.isBlocked).toBe(true); - }); - }); - describe('getEventsCountByProject', () => { test('Should count events and repetitions for a project', async () => { /**