Skip to content

Commit

Permalink
feat: features overwrite warning (#4535)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew committed Aug 21, 2023
1 parent f114aa4 commit 2cfb99c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
Expand Up @@ -335,6 +335,11 @@ test('validate import data', async () => {
'The following features will not be imported as they are currently archived. To import them, please unarchive them first:',
affectedItems: [archivedFeature],
},
{
message:
'The following features already exist in this project and will be overwritten:',
affectedItems: ['existing_feature'],
},
],
permissions: [
{
Expand Down
14 changes: 13 additions & 1 deletion src/lib/features/export-import-toggles/export-import-service.ts
Expand Up @@ -151,13 +151,15 @@ export default class ExportImportService {
unsupportedContextFields,
archivedFeatures,
otherProjectFeatures,
existingProjectFeatures,
missingPermissions,
] = await Promise.all([
this.getUnsupportedStrategies(dto),
this.getUsedCustomStrategies(dto),
this.getUnsupportedContextFields(dto),
this.getArchivedFeatures(dto),
this.getOtherProjectFeatures(dto),
this.getExistingProjectFeatures(dto),
this.importPermissionsService.getMissingPermissions(
dto,
user,
Expand All @@ -176,6 +178,7 @@ export default class ExportImportService {
const warnings = ImportValidationMessages.compileWarnings(
usedCustomStrategies,
archivedFeatures,
existingProjectFeatures,
);
const permissions =
ImportValidationMessages.compilePermissionErrors(
Expand Down Expand Up @@ -299,7 +302,7 @@ export default class ExportImportService {
this.contextService.createContextField(
{
name: contextField.name,
description: contextField.description,
description: contextField.description || '',
legalValues: contextField.legalValues,
stickiness: contextField.stickiness,
},
Expand Down Expand Up @@ -529,6 +532,15 @@ export default class ExportImportService {
);
}

private async getExistingProjectFeatures(dto: ImportTogglesSchema) {
const existingProjectsFeatures =
await this.importTogglesStore.getFeaturesInProject(
dto.data.features.map((feature) => feature.name),
dto.project,
);
return existingProjectsFeatures;
}

private async getNewTagTypes(dto: ImportTogglesSchema) {
const existingTagTypes = (await this.tagTypeService.getAll()).map(
(tagType) => tagType.name,
Expand Down
Expand Up @@ -11,6 +11,11 @@ export interface IImportTogglesStore {
project: string,
): Promise<{ name: string; project: string }[]>;

getFeaturesInProject(
featureNames: string[],
project: string,
): Promise<string[]>;

deleteTagsForFeatures(tags: string[]): Promise<void>;

strategiesExistForFeatures(
Expand Down
12 changes: 12 additions & 0 deletions src/lib/features/export-import-toggles/import-toggles-store.ts
Expand Up @@ -74,6 +74,18 @@ export class ImportTogglesStore implements IImportTogglesStore {
return rows.map((row) => ({ name: row.name, project: row.project }));
}

async getFeaturesInProject(
featureNames: string[],
project: string,
): Promise<string[]> {
const rows = await this.db(T.features)
.select(['name', 'project'])
.where('project', project)
.where('archived_at', null)
.whereIn('name', featureNames);
return rows.map((row) => row.name);
}

async deleteTagsForFeatures(features: string[]): Promise<void> {
return this.db(T.featureTag).whereIn('feature_name', features).del();
}
Expand Down
Expand Up @@ -73,6 +73,7 @@ export class ImportValidationMessages {
static compileWarnings(
usedCustomStrategies: string[],
archivedFeatures: string[],
existingFeatures: string[],
): ImportTogglesValidateItemSchema[] {
const warnings: ImportTogglesValidateItemSchema[] = [];
if (usedCustomStrategies.length > 0) {
Expand All @@ -89,6 +90,13 @@ export class ImportValidationMessages {
affectedItems: archivedFeatures,
});
}
if (existingFeatures.length > 0) {
warnings.push({
message:
'The following features already exist in this project and will be overwritten:',
affectedItems: existingFeatures,
});
}
return warnings;
}
}

0 comments on commit 2cfb99c

Please sign in to comment.