Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion workers/limiter/src/dbHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,13 @@ export class DbHelper {
* @param planId - id of the plan to find
*/
private async resolvePlan(planId: WorkspaceDBScheme['tariffPlanId']): Promise<PlanDBScheme | null> {
/**
* Workspace may have no tariff plan assigned
*/
if (!planId) {
return null;
}

let plan = this.findPlanById(planId);

if (plan) {
Expand Down Expand Up @@ -252,7 +259,7 @@ export class DbHelper {
const plan = await this.resolvePlan(workspace.tariffPlanId);

if (!plan) {
throw new NonCriticalError(`Tariff plan ${workspace.tariffPlanId.toString()} not found for workspace ${id}`, {
throw new NonCriticalError(`Tariff plan ${workspace.tariffPlanId?.toString()} not found for workspace ${id}`, {
workspaceId: id,
});
}
Expand Down
54 changes: 54 additions & 0 deletions workers/limiter/tests/dbHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,60 @@ describe('DbHelper', () => {
).rejects.toThrow(NonCriticalError);
});

test('Should skip and report a workspace that has no tariffPlanId', async () => {
/**
* Arrange — a workspace document with a missing tariffPlanId
*/
const workspace = createWorkspaceMock({
plan: mockedPlans.eventsLimit10,
billingPeriodEventsCount: 0,
lastChargeDate: new Date(),
});

delete (workspace as Partial<WorkspaceDBScheme>).tariffPlanId;

await workspaceCollection.insertOne(workspace);

const hawkCatcherSpy = jest.spyOn(HawkCatcher, 'send').mockImplementation(() => undefined);

/**
* Act
*/
const workspaces = [];

for await (const resolved of dbHelper.getWorkspacesWithTariffPlans()) {
workspaces.push(resolved);
}

/**
* Assert — no crash, the workspace is skipped and reported
*/
expect(workspaces).toHaveLength(0);
expect(hawkCatcherSpy).toHaveBeenCalledTimes(1);
});

test('Should throw NonCriticalError when a single workspace has no tariffPlanId', async () => {
/**
* Arrange
*/
const workspace = createWorkspaceMock({
plan: mockedPlans.eventsLimit10,
billingPeriodEventsCount: 0,
lastChargeDate: new Date(),
});

delete (workspace as Partial<WorkspaceDBScheme>).tariffPlanId;

await workspaceCollection.insertOne(workspace);

/**
* Act & Assert
*/
await expect(
dbHelper.getWorkspacesWithTariffPlans(workspace._id.toString())
).rejects.toThrow(NonCriticalError);
});

test('fetchPlans should throw CriticalError when the plans collection is empty', async () => {
/**
* Arrange — a helper pointed at an empty plans collection
Expand Down
Loading