mirror of
https://github.com/Unleash/unleash.git
synced 2025-03-23 00:16:25 +01:00
feat: making context service transactional (#9063)
This commit is contained in:
parent
1c0431365e
commit
13fb7c4a63
@ -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;
|
||||
@ -50,7 +51,7 @@ interface DeleteLegalValueParam extends ContextParam {
|
||||
}
|
||||
|
||||
export class ContextController extends Controller {
|
||||
private contextService: ContextService;
|
||||
private transactionalContextService: WithTransactional<ContextService>;
|
||||
|
||||
private openApiService: OpenApiService;
|
||||
|
||||
@ -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',
|
||||
@ -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();
|
||||
}
|
||||
|
||||
@ -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,
|
||||
@ -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(
|
||||
@ -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();
|
||||
}
|
||||
@ -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();
|
||||
}
|
||||
@ -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();
|
||||
}
|
||||
@ -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();
|
||||
}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
@ -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),
|
||||
);
|
||||
|
@ -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,
|
||||
@ -434,6 +435,7 @@ export const createServices = (
|
||||
clientInstanceService,
|
||||
clientMetricsServiceV2,
|
||||
contextService,
|
||||
transactionalContextService,
|
||||
versionService,
|
||||
apiTokenService,
|
||||
emailService,
|
||||
|
@ -69,6 +69,7 @@ export interface IUnleashServices {
|
||||
clientInstanceService: ClientInstanceService;
|
||||
clientMetricsServiceV2: ClientMetricsServiceV2;
|
||||
contextService: ContextService;
|
||||
transactionalContextService: WithTransactional<ContextService>;
|
||||
emailService: EmailService;
|
||||
environmentService: EnvironmentService;
|
||||
transactionalEnvironmentService: WithTransactional<EnvironmentService>;
|
||||
|
Loading…
Reference in New Issue
Block a user