Skip to content

Commit

Permalink
feat: making context service transactional (#9063)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew authored Jan 6, 2025
1 parent 1c04313 commit 13fb7c4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
46 changes: 25 additions & 21 deletions src/lib/features/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import type { UpdateContextFieldSchema } from '../../openapi/spec/update-context
import type { CreateContextFieldSchema } from '../../openapi/spec/create-context-field-schema';
import { extractUserIdFromUser } from '../../util';
import type { LegalValueSchema } from '../../openapi';
import type { WithTransactional } from '../../db/transaction';

interface ContextParam {
contextField: string;
Expand All @@ -50,7 +51,7 @@ interface DeleteLegalValueParam extends ContextParam {
}

export class ContextController extends Controller {
private contextService: ContextService;
private transactionalContextService: WithTransactional<ContextService>;

private openApiService: OpenApiService;

Expand All @@ -59,14 +60,17 @@ export class ContextController extends Controller {
constructor(
config: IUnleashConfig,
{
contextService,
transactionalContextService,
openApiService,
}: Pick<IUnleashServices, 'contextService' | 'openApiService'>,
}: Pick<
IUnleashServices,
'transactionalContextService' | 'openApiService'
>,
) {
super(config);
this.openApiService = openApiService;
this.logger = config.getLogger('/admin-api/context.ts');
this.contextService = contextService;
this.transactionalContextService = transactionalContextService;

this.route({
method: 'get',
Expand Down Expand Up @@ -257,7 +261,9 @@ export class ContextController extends Controller {
res: Response<ContextFieldsSchema>,
): Promise<void> {
res.status(200)
.json(serializeDates(await this.contextService.getAll()))
.json(
serializeDates(await this.transactionalContextService.getAll()),
)
.end();
}

Expand All @@ -268,7 +274,7 @@ export class ContextController extends Controller {
try {
const name = req.params.contextField;
const contextField =
await this.contextService.getContextField(name);
await this.transactionalContextService.getContextField(name);
this.openApiService.respondWithValidation(
200,
res,
Expand All @@ -286,9 +292,8 @@ export class ContextController extends Controller {
): Promise<void> {
const value = req.body;

const result = await this.contextService.createContextField(
value,
req.audit,
const result = await this.transactionalContextService.transactional(
(service) => service.createContextField(value, req.audit),
);

this.openApiService.respondWithValidation(
Expand All @@ -307,9 +312,8 @@ export class ContextController extends Controller {
const name = req.params.contextField;
const contextField = req.body;

await this.contextService.updateContextField(
{ ...contextField, name },
req.audit,
await this.transactionalContextService.transactional((service) =>
service.updateContextField({ ...contextField, name }, req.audit),
);
res.status(200).end();
}
Expand All @@ -321,9 +325,8 @@ export class ContextController extends Controller {
const name = req.params.contextField;
const legalValue = req.body;

await this.contextService.updateLegalValue(
{ name, legalValue },
req.audit,
await this.transactionalContextService.transactional((service) =>
service.updateLegalValue({ name, legalValue }, req.audit),
);
res.status(200).end();
}
Expand All @@ -335,9 +338,8 @@ export class ContextController extends Controller {
const name = req.params.contextField;
const legalValue = req.params.legalValue;

await this.contextService.deleteLegalValue(
{ name, legalValue },
req.audit,
await this.transactionalContextService.transactional((service) =>
service.deleteLegalValue({ name, legalValue }, req.audit),
);
res.status(200).end();
}
Expand All @@ -348,7 +350,9 @@ export class ContextController extends Controller {
): Promise<void> {
const name = req.params.contextField;

await this.contextService.deleteContextField(name, req.audit);
await this.transactionalContextService.transactional((service) =>
service.deleteContextField(name, req.audit),
);
res.status(200).end();
}

Expand All @@ -358,7 +362,7 @@ export class ContextController extends Controller {
): Promise<void> {
const { name } = req.body;

await this.contextService.validateName(name);
await this.transactionalContextService.validateName(name);
res.status(200).end();
}

Expand All @@ -369,7 +373,7 @@ export class ContextController extends Controller {
const { contextField } = req.params;
const { user } = req;
const contextFields =
await this.contextService.getStrategiesByContextField(
await this.transactionalContextService.getStrategiesByContextField(
contextField,
extractUserIdFromUser(user),
);
Expand Down
4 changes: 3 additions & 1 deletion src/lib/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,10 @@ export const createServices = (
? new FeatureLifecycleReadModel(db, config.flagResolver)
: new FakeFeatureLifecycleReadModel();

const contextService = db
const transactionalContextService = db
? withTransactional(createContextService(config), db)
: withFakeTransactional(createFakeContextService(config));
const contextService = transactionalContextService;
const emailService = new EmailService(config);
const featureTypeService = new FeatureTypeService(
stores,
Expand Down Expand Up @@ -434,6 +435,7 @@ export const createServices = (
clientInstanceService,
clientMetricsServiceV2,
contextService,
transactionalContextService,
versionService,
apiTokenService,
emailService,
Expand Down
1 change: 1 addition & 0 deletions src/lib/types/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export interface IUnleashServices {
clientInstanceService: ClientInstanceService;
clientMetricsServiceV2: ClientMetricsServiceV2;
contextService: ContextService;
transactionalContextService: WithTransactional<ContextService>;
emailService: EmailService;
environmentService: EnvironmentService;
transactionalEnvironmentService: WithTransactional<EnvironmentService>;
Expand Down

0 comments on commit 13fb7c4

Please sign in to comment.