2023-02-20 16:06:10 +01:00
import {
FeatureStrategySchema ,
ImportTogglesValidateItemSchema ,
2023-02-21 10:15:57 +01:00
} from '../../openapi' ;
import { IContextFieldDto } from '../../types/stores/context-field-store' ;
2023-10-11 09:38:57 +02:00
import { FeatureNameCheckResultWithFeaturePattern } from '../feature-toggle/feature-toggle-service' ;
2023-09-12 15:36:19 +02:00
import { ProjectFeaturesLimit } from './import-toggles-store-type' ;
2023-02-20 16:06:10 +01:00
2023-08-24 10:05:21 +02:00
export interface IErrorsParams {
projectName : string ;
strategies : FeatureStrategySchema [ ] ;
contextFields : IContextFieldDto [ ] ;
otherProjectFeatures : string [ ] ;
duplicateFeatures : string [ ] ;
2023-09-13 10:50:37 +02:00
featureNameCheckResult : FeatureNameCheckResultWithFeaturePattern ;
2023-09-12 15:36:19 +02:00
featureLimitResult : ProjectFeaturesLimit ;
2023-10-12 12:43:43 +02:00
segments : string [ ] ;
2023-10-13 13:17:54 +02:00
dependencies : string [ ] ;
2023-08-24 10:05:21 +02:00
}
export interface IWarningParams {
usedCustomStrategies : string [ ] ;
archivedFeatures : string [ ] ;
existingFeatures : string [ ] ;
}
2023-02-20 16:06:10 +01:00
export class ImportValidationMessages {
static compilePermissionErrors (
missingPermissions : string [ ] ,
) : ImportTogglesValidateItemSchema [ ] {
const errors : ImportTogglesValidateItemSchema [ ] = [ ] ;
if ( missingPermissions . length > 0 ) {
errors . push ( {
message :
'We detected you are missing the following permissions:' ,
affectedItems : missingPermissions ,
} ) ;
}
return errors ;
}
2023-08-24 10:05:21 +02:00
static compileErrors ( {
projectName ,
strategies ,
contextFields ,
otherProjectFeatures ,
duplicateFeatures ,
2023-09-12 10:19:40 +02:00
featureNameCheckResult ,
2023-09-12 15:36:19 +02:00
featureLimitResult ,
2023-10-12 12:43:43 +02:00
segments ,
2023-10-13 13:17:54 +02:00
dependencies ,
2023-08-24 10:05:21 +02:00
} : IErrorsParams ) : ImportTogglesValidateItemSchema [ ] {
2023-02-20 16:06:10 +01:00
const errors : ImportTogglesValidateItemSchema [ ] = [ ] ;
if ( strategies . length > 0 ) {
errors . push ( {
message :
2023-10-13 13:17:54 +02:00
'We detected the following custom strategy that needs to be created first:' ,
2023-02-20 16:06:10 +01:00
affectedItems : strategies.map ( ( strategy ) = > strategy . name ) ,
} ) ;
}
if ( contextFields . length > 0 ) {
errors . push ( {
message :
'We detected the following context fields that do not have matching legal values with the imported ones:' ,
affectedItems : contextFields.map (
( contextField ) = > contextField . name ,
) ,
} ) ;
}
2023-08-24 10:05:21 +02:00
if ( otherProjectFeatures . length > 0 ) {
2023-02-20 16:06:10 +01:00
errors . push ( {
2023-08-24 10:05:21 +02:00
message : ` You cannot import a features that already exist in other projects. You already have the following features defined outside of project ${ projectName } : ` ,
affectedItems : otherProjectFeatures ,
2023-02-20 16:06:10 +01:00
} ) ;
}
2023-08-24 10:05:21 +02:00
if ( duplicateFeatures . length > 0 ) {
2023-02-20 16:06:10 +01:00
errors . push ( {
message :
2023-08-24 10:05:21 +02:00
'We detected the following features are duplicate in your import data:' ,
affectedItems : duplicateFeatures ,
2023-02-20 16:06:10 +01:00
} ) ;
}
2023-09-12 10:19:40 +02:00
if ( featureNameCheckResult . state === 'invalid' ) {
const baseError = ` Features imported into this project must match the project's feature naming pattern: " ${ featureNameCheckResult . featureNaming . pattern } ". ` ;
const exampleInfo = featureNameCheckResult . featureNaming . example
? ` For example: " ${ featureNameCheckResult . featureNaming . example } ". `
: '' ;
const descriptionInfo = featureNameCheckResult . featureNaming
. description
? ` The pattern is described as follows: " ${ featureNameCheckResult . featureNaming . description } " `
: '' ;
errors . push ( {
message : ` ${ baseError } ${ exampleInfo } ${ descriptionInfo } The following features do not match the pattern: ` ,
affectedItems : [ . . . featureNameCheckResult . invalidNames ] . sort ( ) ,
} ) ;
}
2023-09-12 15:36:19 +02:00
if (
featureLimitResult . currentFeaturesCount +
featureLimitResult . newFeaturesCount >
featureLimitResult . limit
) {
errors . push ( {
message : ` We detected you want to create ${ featureLimitResult . newFeaturesCount } new features to a project that already has ${ featureLimitResult . currentFeaturesCount } existing features, exceeding the maximum limit of ${ featureLimitResult . limit } . ` ,
affectedItems : [ ] ,
} ) ;
}
2023-02-20 16:06:10 +01:00
2023-10-12 12:43:43 +02:00
if ( segments . length > 0 ) {
errors . push ( {
message :
2023-10-13 13:17:54 +02:00
'We detected the following segments that need to be created first:' ,
2023-10-12 12:43:43 +02:00
affectedItems : segments ,
} ) ;
}
2023-10-13 13:17:54 +02:00
if ( dependencies . length > 0 ) {
errors . push ( {
message :
'We detected the following dependencies that need to be created first:' ,
affectedItems : dependencies ,
} ) ;
}
2023-02-20 16:06:10 +01:00
return errors ;
}
2023-08-24 10:05:21 +02:00
static compileWarnings ( {
usedCustomStrategies ,
existingFeatures ,
archivedFeatures ,
} : IWarningParams ) : ImportTogglesValidateItemSchema [ ] {
2023-02-20 16:06:10 +01:00
const warnings : ImportTogglesValidateItemSchema [ ] = [ ] ;
if ( usedCustomStrategies . length > 0 ) {
warnings . push ( {
message :
'The following strategy types will be used in import. Please make sure the strategy type parameters are configured as in source environment:' ,
affectedItems : usedCustomStrategies ,
} ) ;
}
if ( archivedFeatures . length > 0 ) {
warnings . push ( {
message :
'The following features will not be imported as they are currently archived. To import them, please unarchive them first:' ,
affectedItems : archivedFeatures ,
} ) ;
}
2023-08-21 11:09:09 +02:00
if ( existingFeatures . length > 0 ) {
warnings . push ( {
message :
'The following features already exist in this project and will be overwritten:' ,
affectedItems : existingFeatures ,
} ) ;
}
2023-02-20 16:06:10 +01:00
return warnings ;
}
}