Skip to content

Commit

Permalink
chore: split interfaces for import and export (#5004)
Browse files Browse the repository at this point in the history
## About the changes
This splits the interfaces for import and export, especially because the
import functionality has to be replaced in enterprise repo.

This is a breaking change because of the service renames, but I'll have
the PR for the other repository ready so we reduce the time to fix. I
intentionally avoided doing it backward compatible because of time.
  • Loading branch information
gastonfournier committed Oct 12, 2023
1 parent cfcf9de commit 7343183
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 32 deletions.
39 changes: 18 additions & 21 deletions src/lib/features/export-import-toggles/export-import-controller.ts
@@ -1,7 +1,10 @@
import { Response } from 'express';
import Controller from '../../routes/controller';
import { Logger } from '../../logger';
import ExportImportService from './export-import-service';
import ExportImportService, {
IExportService,
IImportService,
} from './export-import-service';
import { OpenApiService } from '../../services';
import {
TransactionCreator,
Expand Down Expand Up @@ -32,43 +35,42 @@ import ApiUser from '../../types/api-user';
class ExportImportController extends Controller {
private logger: Logger;

/** @deprecated gradually rolling out exportImportV2 */
private exportImportService: ExportImportService;
private exportService: IExportService;

/** @deprecated gradually rolling out exportImportV2 */
/** @deprecated gradually rolling out importService */
private transactionalExportImportService: (
db: UnleashTransaction,
) => Pick<ExportImportService, 'import' | 'validate'>;
) => IImportService;

private exportImportServiceV2: WithTransactional<ExportImportService>;
private importService: WithTransactional<IImportService>;

private openApiService: OpenApiService;

/** @deprecated gradually rolling out exportImportV2 */
/** @deprecated gradually rolling out importService */
private readonly startTransaction: TransactionCreator<UnleashTransaction>;

constructor(
config: IUnleashConfig,
{
exportImportService,
exportService,
transactionalExportImportService,
exportImportServiceV2,
importService,
openApiService,
}: Pick<
IUnleashServices,
| 'exportImportService'
| 'exportImportServiceV2'
| 'exportService'
| 'importService'
| 'openApiService'
| 'transactionalExportImportService'
>,
startTransaction: TransactionCreator<UnleashTransaction>,
) {
super(config);
this.logger = config.getLogger('/admin-api/export-import.ts');
this.exportImportService = exportImportService;
this.exportService = exportService;
this.transactionalExportImportService =
transactionalExportImportService;
this.exportImportServiceV2 = exportImportServiceV2;
this.importService = importService;
this.startTransaction = startTransaction;
this.openApiService = openApiService;
this.route({
Expand Down Expand Up @@ -141,12 +143,7 @@ class ExportImportController extends Controller {
const query = req.body;
const userName = extractUsername(req);

const useTransactionalDecorator = this.config.flagResolver.isEnabled(
'transactionalDecorator',
);
const data = useTransactionalDecorator
? await this.exportImportServiceV2.export(query, userName)
: await this.exportImportService.export(query, userName);
const data = await this.exportService.export(query, userName);

this.openApiService.respondWithValidation(
200,
Expand All @@ -168,7 +165,7 @@ class ExportImportController extends Controller {
'transactionalDecorator',
);
const validation = useTransactionalDecorator
? await this.exportImportServiceV2.transactional((service) =>
? await this.importService.transactional((service) =>
service.validate(dto, user),
)
: await this.startTransaction(async (tx) =>
Expand Down Expand Up @@ -203,7 +200,7 @@ class ExportImportController extends Controller {
);

if (useTransactionalDecorator) {
await this.exportImportServiceV2.transactional((service) =>
await this.importService.transactional((service) =>
service.import(dto, user),
);
} else {
Expand Down
20 changes: 19 additions & 1 deletion src/lib/features/export-import-toggles/export-import-service.ts
Expand Up @@ -50,7 +50,25 @@ import { ImportValidationMessages } from './import-validation-messages';
import { findDuplicates } from '../../util/findDuplicates';
import { FeatureNameCheckResultWithFeaturePattern } from '../feature-toggle/feature-toggle-service';

export default class ExportImportService {
export type IImportService = {
validate(
dto: ImportTogglesSchema,
user: User,
): Promise<ImportTogglesValidateSchema>;

import(dto: ImportTogglesSchema, user: User): Promise<void>;
};

export type IExportService = {
export(
query: ExportQuerySchema,
userName: string,
): Promise<ExportResultSchema>;
};

export default class ExportImportService
implements IExportService, IImportService
{
private logger: Logger;

private toggleStore: IFeatureToggleStore;
Expand Down
6 changes: 3 additions & 3 deletions src/lib/services/index.ts
Expand Up @@ -299,7 +299,7 @@ export const createServices = (
const exportImportService = db
? createExportImportTogglesService(db, config)
: createFakeExportImportTogglesService(config);
const exportImportServiceV2 = db
const importService = db
? withTransactional(deferredExportImportTogglesService(config), db)
: withFakeTransactional(createFakeExportImportTogglesService(config));
const transactionalExportImportService = (txDb: Knex.Transaction) =>
Expand Down Expand Up @@ -403,9 +403,9 @@ export const createServices = (
instanceStatsService,
favoritesService,
maintenanceService,
exportImportService,
exportService: exportImportService,
transactionalExportImportService,
exportImportServiceV2,
importService,
schedulerService,
configurationRevisionService,
transactionalFeatureToggleService,
Expand Down
14 changes: 7 additions & 7 deletions src/lib/types/services.ts
Expand Up @@ -39,7 +39,10 @@ import MaintenanceService from '../services/maintenance-service';
import { AccountService } from '../services/account-service';
import { SchedulerService } from '../services/scheduler-service';
import { Knex } from 'knex';
import ExportImportService from '../features/export-import-toggles/export-import-service';
import {
IExportService,
IImportService,
} from '../features/export-import-toggles/export-import-service';
import { ISegmentService } from '../segments/segment-service-interface';
import ConfigurationRevisionService from '../features/feature-toggle/configuration-revision-service';
import EventAnnouncerService from 'lib/services/event-announcer-service';
Expand Down Expand Up @@ -89,16 +92,13 @@ export interface IUnleashServices {
instanceStatsService: InstanceStatsService;
favoritesService: FavoritesService;
maintenanceService: MaintenanceService;
/** @deprecated prefer exportImportServiceV2, we're doing a gradual rollout */
exportImportService: ExportImportService;
exportImportServiceV2: WithTransactional<ExportImportService>;
exportService: IExportService;
importService: WithTransactional<IImportService>;
configurationRevisionService: ConfigurationRevisionService;
schedulerService: SchedulerService;
eventAnnouncerService: EventAnnouncerService;
/** @deprecated prefer exportImportServiceV2, we're doing a gradual rollout */
transactionalExportImportService: (
db: Knex.Transaction,
) => Pick<ExportImportService, 'import' | 'validate'>;
transactionalExportImportService: (db: Knex.Transaction) => IImportService;
transactionalFeatureToggleService: (
db: Knex.Transaction,
) => FeatureToggleService;
Expand Down

0 comments on commit 7343183

Please sign in to comment.