Skip to content

Commit 1aebd62

Browse files
committed
Extract benchmark feature guard into factory pattern
1 parent 306f5df commit 1aebd62

3 files changed

Lines changed: 42 additions & 17 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {
2+
benchmarkFeatureGuard,
3+
BenchmarkFeatureGuard,
4+
} from './benchmark-feature-guard';
5+
6+
export const getBenchmarkFeatureGuard = (): BenchmarkFeatureGuard => {
7+
return benchmarkFeatureGuard;
8+
};
Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
import { AppSystemProp, logger, system } from '@openops/server-shared';
22
import { throwFeatureDisabledError } from './errors';
33

4-
export async function assertBenchmarkFeatureEnabled(
5-
projectId: string,
6-
provider?: string,
7-
): Promise<void> {
8-
if (system.getBoolean(AppSystemProp.FINOPS_BENCHMARK_ENABLED) !== true) {
9-
logger.info(
10-
'Benchmark access denied: FINOPS_BENCHMARK_ENABLED flag is not enabled',
11-
{ provider, projectId },
12-
);
13-
throwFeatureDisabledError('Benchmark feature is not enabled');
14-
}
15-
}
4+
export type BenchmarkFeatureGuard = {
5+
assertBenchmarkFeatureEnabled(
6+
projectId: string,
7+
organizationId: string,
8+
provider?: string,
9+
): Promise<void>;
10+
};
11+
12+
export const benchmarkFeatureGuard: BenchmarkFeatureGuard = {
13+
async assertBenchmarkFeatureEnabled(
14+
projectId: string,
15+
_organizationId: string,
16+
provider?: string,
17+
): Promise<void> {
18+
if (system.getBoolean(AppSystemProp.FINOPS_BENCHMARK_ENABLED) !== true) {
19+
logger.info(
20+
'Benchmark access denied: FINOPS_BENCHMARK_ENABLED flag is not enabled',
21+
{ provider, projectId },
22+
);
23+
throwFeatureDisabledError('Benchmark feature is not enabled');
24+
}
25+
},
26+
};

packages/server/api/src/app/benchmark/benchmark.controller.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
PrincipalType,
1414
} from '@openops/shared';
1515
import { StatusCodes } from 'http-status-codes';
16-
import { assertBenchmarkFeatureEnabled } from './benchmark-feature-guard';
16+
import { getBenchmarkFeatureGuard } from './benchmark-feature-guard-factory';
1717
import { getBenchmarkStatus, listBenchmarks } from './benchmark-status.service';
1818
import { createBenchmark } from './create-benchmark.service';
1919
import { resolveWizardNavigation } from './wizard.service';
@@ -23,8 +23,9 @@ export const benchmarkController: FastifyPluginAsyncTypebox = async (app) => {
2323
'/:provider/wizard',
2424
WizardStepRequestOptions,
2525
async (request, reply) => {
26-
await assertBenchmarkFeatureEnabled(
26+
await getBenchmarkFeatureGuard().assertBenchmarkFeatureEnabled(
2727
request.principal.projectId,
28+
request.principal.organization.id,
2829
request.params.provider,
2930
);
3031

@@ -44,8 +45,9 @@ export const benchmarkController: FastifyPluginAsyncTypebox = async (app) => {
4445
'/:provider',
4546
CreateBenchmarkRequestOptions,
4647
async (request, reply) => {
47-
await assertBenchmarkFeatureEnabled(
48+
await getBenchmarkFeatureGuard().assertBenchmarkFeatureEnabled(
4849
request.principal.projectId,
50+
request.principal.organization.id,
4951
request.params.provider,
5052
);
5153

@@ -59,8 +61,9 @@ export const benchmarkController: FastifyPluginAsyncTypebox = async (app) => {
5961
},
6062
);
6163
app.get('/', ListBenchmarksRequestOptions, async (request, reply) => {
62-
await assertBenchmarkFeatureEnabled(
64+
await getBenchmarkFeatureGuard().assertBenchmarkFeatureEnabled(
6365
request.principal.projectId,
66+
request.principal.organization.id,
6467
request.query.provider,
6568
);
6669
const items = await listBenchmarks({
@@ -74,7 +77,10 @@ export const benchmarkController: FastifyPluginAsyncTypebox = async (app) => {
7477
'/:benchmarkId/status',
7578
BenchmarkStatusRequestOptions,
7679
async (request, reply) => {
77-
await assertBenchmarkFeatureEnabled(request.principal.projectId);
80+
await getBenchmarkFeatureGuard().assertBenchmarkFeatureEnabled(
81+
request.principal.projectId,
82+
request.principal.organization.id,
83+
);
7884
const status = await getBenchmarkStatus({
7985
benchmarkId: request.params.benchmarkId,
8086
projectId: request.principal.projectId,

0 commit comments

Comments
 (0)