All files / src/lib/services state-service.ts

98.3% Statements 173/176
93.1% Branches 108/116
95% Functions 57/60
98.18% Lines 162/165

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 73261x 61x                                 61x                                                           61x 61x                                       61x                                                         174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x                                             2x 2x 1x     2x 12x   6x     2x 12x   6x                     34x 2x   34x   31x 7x               31x   20x 12x   8x     20x   20x           20x     20x             31x 18x               31x 15x               31x 14x           12x 52x     10x                     31x 6x             31x 6x               20x   85x                             20x     20x 7x     7x   20x   692x     20x   146x                     12x 33x                 12x 33x         12x                           20x 20x       20x 7x 7x 7x             20x         53x 53x 53x         53x                               18x 18x       18x 6x 6x 6x             18x         25x 25x                                 7x 7x     7x 4x 4x 4x           7x 15x   7x 4x     11x         4x                     15x 15x     15x 6x 6x 6x           15x 28x 17x     15x 8x     17x         8x                         14x     14x     14x 14x     14x 6x     6x 6x 6x 6x                                   14x           14x 14x               174x       1x                   14x 10x   1x       14x   4x     9x         4x       174x 1x               14x 11x 1x     14x 5x 10x         5x                   14x 12x 6x     14x 6x     6x         6x                 6x 4x     6x   2x                     6x   1x                                               19x                                                                                 19x                 563x       254x                 61x  
import { stateSchema } from './state-schema';
import {
    DROP_ENVIRONMENTS,
    DROP_FEATURE_TAGS,
    DROP_FEATURES,
    DROP_PROJECTS,
    DROP_STRATEGIES,
    DROP_TAG_TYPES,
    DROP_TAGS,
    ENVIRONMENT_IMPORT,
    FEATURE_IMPORT,
    FEATURE_TAG_IMPORT,
    PROJECT_IMPORT,
    STRATEGY_IMPORT,
    TAG_IMPORT,
    TAG_TYPE_IMPORT,
} from '../types/events';
 
import { filterEqual, filterExisting, parseFile, readFile } from './state-util';
 
import { IUnleashConfig } from '../types/option';
import {
    FeatureToggle,
    IEnvironment,
    IFeatureEnvironment,
    IFeatureStrategy,
    IImportData,
    IImportFile,
    IProject,
    ISegment,
    IStrategyConfig,
    ITag,
} from '../types/model';
import { Logger } from '../logger';
import {
    IFeatureTag,
    IFeatureTagStore,
} from '../types/stores/feature-tag-store';
import { IProjectStore } from '../types/stores/project-store';
import { ITagType, ITagTypeStore } from '../types/stores/tag-type-store';
import { ITagStore } from '../types/stores/tag-store';
import { IEventStore } from '../types/stores/event-store';
import { IStrategy, IStrategyStore } from '../types/stores/strategy-store';
import { IFeatureToggleStore } from '../types/stores/feature-toggle-store';
import { IFeatureStrategiesStore } from '../types/stores/feature-strategies-store';
import { IEnvironmentStore } from '../types/stores/environment-store';
import { IFeatureEnvironmentStore } from '../types/stores/feature-environment-store';
import { IUnleashStores } from '../types/stores';
import { DEFAULT_ENV } from '../util/constants';
import { GLOBAL_ENV } from '../types/environment';
import { ISegmentStore } from '../types/stores/segment-store';
import { PartialSome } from '../types/partial';
 
export interface IBackupOption {
    includeFeatureToggles: boolean;
    includeStrategies: boolean;
    includeProjects: boolean;
    includeTags: boolean;
}
 
interface IExportIncludeOptions {
    includeFeatureToggles?: boolean;
    includeStrategies?: boolean;
    includeProjects?: boolean;
    includeTags?: boolean;
    includeEnvironments?: boolean;
    includeSegments?: boolean;
}
 
export default class StateService {
    private logger: Logger;
 
    private toggleStore: IFeatureToggleStore;
 
    private featureStrategiesStore: IFeatureStrategiesStore;
 
    private strategyStore: IStrategyStore;
 
    private eventStore: IEventStore;
 
    private tagStore: ITagStore;
 
    private tagTypeStore: ITagTypeStore;
 
    private projectStore: IProjectStore;
 
    private featureEnvironmentStore: IFeatureEnvironmentStore;
 
    private featureTagStore: IFeatureTagStore;
 
    private environmentStore: IEnvironmentStore;
 
    private segmentStore: ISegmentStore;
 
    constructor(
        stores: IUnleashStores,
        { getLogger }: Pick<IUnleashConfig, 'getLogger'>,
    ) {
        this.eventStore = stores.eventStore;
        this.toggleStore = stores.featureToggleStore;
        this.strategyStore = stores.strategyStore;
        this.tagStore = stores.tagStore;
        this.featureStrategiesStore = stores.featureStrategiesStore;
        this.featureEnvironmentStore = stores.featureEnvironmentStore;
        this.tagTypeStore = stores.tagTypeStore;
        this.projectStore = stores.projectStore;
        this.featureTagStore = stores.featureTagStore;
        this.environmentStore = stores.environmentStore;
        this.segmentStore = stores.segmentStore;
        this.logger = getLogger('services/state-service.js');
    }
 
    async importFile({
        file,
        dropBeforeImport = false,
        userName = 'import-user',
        keepExisting = true,
    }: IImportFile): Promise<void> {
        return readFile(file)
            .then((data) => parseFile(file, data))
            .then((data) =>
                this.import({
                    data,
                    userName,
                    dropBeforeImport,
                    keepExisting,
                }),
            );
    }
 
    // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
    replaceGlobalEnvWithDefaultEnv(data: any) {
        data.environments?.forEach((e) => {
            if (e.name === GLOBAL_ENV) {
                e.name = DEFAULT_ENV;
            }
        });
        data.featureEnvironments?.forEach((fe) => {
            if (fe.environment === GLOBAL_ENV) {
                // eslint-disable-next-line no-param-reassign
                fe.environment = DEFAULT_ENV;
            }
        });
        data.featureStrategies?.forEach((fs) => {
            if (fs.environment === GLOBAL_ENV) {
                // eslint-disable-next-line no-param-reassign
                fs.environment = DEFAULT_ENV;
            }
        });
    }
 
    async import({
        data,
        userName = 'importUser',
        dropBeforeImport = false,
        keepExisting = true,
    }: IImportData): Promise<void> {
        if (data.version === 2) {
            this.replaceGlobalEnvWithDefaultEnv(data);
        }
        const importData = await stateSchema.validateAsync(data);
 
        if (importData.environments) {
            await this.importEnvironments({
                environments: data.environments,
                userName,
                dropBeforeImport,
                keepExisting,
            });
        }
 
        if (importData.features) {
            let projectData;
            if (!importData.version || importData.version === 1) {
                projectData = await this.convertLegacyFeatures(importData);
            } else {
                projectData = importData;
            }
            const { features, featureStrategies, featureEnvironments } =
                projectData;
 
            await this.importFeatures({
                features,
                userName,
                dropBeforeImport,
                keepExisting,
            });
            await this.importFeatureEnvironments({
                featureEnvironments,
            });
            await this.importFeatureStrategies({
                featureStrategies,
                dropBeforeImport,
                keepExisting,
            });
        }
 
        if (importData.strategies) {
            await this.importStrategies({
                strategies: data.strategies,
                userName,
                dropBeforeImport,
                keepExisting,
            });
        }
 
        if (importData.projects) {
            await this.importProjects({
                projects: data.projects,
                userName,
                dropBeforeImport,
                keepExisting,
            });
        }
 
        if (importData.tagTypes && importData.tags) {
            await this.importTagData({
                tagTypes: data.tagTypes,
                tags: data.tags,
                featureTags:
                    (data.featureTags || [])
                        .filter((t) =>
                            (data.features || []).some(
                                (f) => f.name === t.featureName,
                            ),
                        )
                        .map((t) => ({
                            featureName: t.featureName,
                            tagValue: t.tagValue || t.value,
                            tagType: t.tagType || t.type,
                        })) || [],
                userName,
                dropBeforeImport,
                keepExisting,
            });
        }
 
        if (importData.segments) {
            await this.importSegments(
                data.segments,
                userName,
                dropBeforeImport,
            );
        }
 
        if (importData.featureStrategySegments) {
            await this.importFeatureStrategySegments(
                data.featureStrategySegments,
            );
        }
    }
 
    // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
    async importFeatureEnvironments({ featureEnvironments }): Promise<void> {
        await Promise.all(
            featureEnvironments.map((env) =>
                this.featureEnvironmentStore.addEnvironmentToFeature(
                    env.featureName,
                    env.environment,
                    env.enabled,
                ),
            ),
        );
    }
 
    // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
    async importFeatureStrategies({
        featureStrategies,
        dropBeforeImport,
        keepExisting,
    }): Promise<void> {
        const oldFeatureStrategies = dropBeforeImport
            ? []
            : await this.featureStrategiesStore.getAll();
        if (dropBeforeImport) {
            this.logger.info(
                'Dropping existing strategies for feature toggles',
            );
            await this.featureStrategiesStore.deleteAll();
        }
        const strategiesToImport = keepExisting
            ? featureStrategies.filter(
                  (s) => !oldFeatureStrategies.some((o) => o.id === s.id),
              )
            : featureStrategies;
        await Promise.all(
            strategiesToImport.map((featureStrategy) =>
                this.featureStrategiesStore.createStrategyFeatureEnv(
                    featureStrategy,
                ),
            ),
        );
    }
 
    // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
    async convertLegacyFeatures({
        features,
    }): Promise<{ features; featureStrategies; featureEnvironments }> {
        const strategies = features.flatMap((f) =>
            f.strategies.map((strategy: IStrategyConfig) => ({
                featureName: f.name,
                projectId: f.project,
                constraints: strategy.constraints || [],
                parameters: strategy.parameters || {},
                environment: DEFAULT_ENV,
                strategyName: strategy.name,
            })),
        );
        const newFeatures = features;
        const featureEnvironments = features.map((feature) => ({
            featureName: feature.name,
            environment: DEFAULT_ENV,
            enabled: feature.enabled,
        }));
        return {
            features: newFeatures,
            featureStrategies: strategies,
            featureEnvironments,
        };
    }
 
    // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
    async importFeatures({
        features,
        userName,
        dropBeforeImport,
        keepExisting,
    }): Promise<void> {
        this.logger.info(`Importing ${features.length} feature toggles`);
        const oldToggles = dropBeforeImport
            ? []
            : await this.toggleStore.getAll();
 
        if (dropBeforeImport) {
            this.logger.info('Dropping existing feature toggles');
            await this.toggleStore.deleteAll();
            await this.eventStore.store({
                type: DROP_FEATURES,
                createdBy: userName,
                data: { name: 'all-features' },
            });
        }
 
        await Promise.all(
            features
                .filter(filterExisting(keepExisting, oldToggles))
                .filter(filterEqual(oldToggles))
                .map(async (feature) => {
                    const { name, project, variants = [] } = feature;
                    await this.toggleStore.create(feature.project, feature);
                    await this.toggleStore.saveVariants(
                        project,
                        name,
                        variants,
                    );
                    await this.eventStore.store({
                        type: FEATURE_IMPORT,
                        createdBy: userName,
                        data: feature,
                    });
                }),
        );
    }
 
    // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
    async importStrategies({
        strategies,
        userName,
        dropBeforeImport,
        keepExisting,
    }): Promise<void> {
        this.logger.info(`Importing ${strategies.length} strategies`);
        const oldStrategies = dropBeforeImport
            ? []
            : await this.strategyStore.getAll();
 
        if (dropBeforeImport) {
            this.logger.info('Dropping existing strategies');
            await this.strategyStore.dropCustomStrategies();
            await this.eventStore.store({
                type: DROP_STRATEGIES,
                createdBy: userName,
                data: { name: 'all-strategies' },
            });
        }
 
        await Promise.all(
            strategies
                .filter(filterExisting(keepExisting, oldStrategies))
                .filter(filterEqual(oldStrategies))
                .map((strategy) =>
                    this.strategyStore.importStrategy(strategy).then(() => {
                        this.eventStore.store({
                            type: STRATEGY_IMPORT,
                            createdBy: userName,
                            data: strategy,
                        });
                    }),
                ),
        );
    }
 
    // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
    async importEnvironments({
        environments,
        userName,
        dropBeforeImport,
        keepExisting,
    }): Promise<void> {
        this.logger.info(`Import ${environments.length} projects`);
        const oldEnvs = dropBeforeImport
            ? []
            : await this.environmentStore.getAll();
        if (dropBeforeImport) {
            this.logger.info('Dropping existing environments');
            await this.environmentStore.deleteAll();
            await this.eventStore.store({
                type: DROP_ENVIRONMENTS,
                createdBy: userName,
                data: { name: 'all-projects' },
            });
        }
        const envsImport = environments.filter((env) =>
            keepExisting ? !oldEnvs.some((old) => old.name === env.name) : true,
        );
        if (envsImport.length > 0) {
            const importedEnvs = await this.environmentStore.importEnvironments(
                envsImport,
            );
            const importedEnvironmentEvents = importedEnvs.map((env) => ({
                type: ENVIRONMENT_IMPORT,
                createdBy: userName,
                data: env,
            }));
            await this.eventStore.batchStore(importedEnvironmentEvents);
        }
    }
 
    // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
    async importProjects({
        projects,
        userName,
        dropBeforeImport,
        keepExisting,
    }): Promise<void> {
        this.logger.info(`Import ${projects.length} projects`);
        const oldProjects = dropBeforeImport
            ? []
            : await this.projectStore.getAll();
        if (dropBeforeImport) {
            this.logger.info('Dropping existing projects');
            await this.projectStore.deleteAll();
            await this.eventStore.store({
                type: DROP_PROJECTS,
                createdBy: userName,
                data: { name: 'all-projects' },
            });
        }
        const projectsToImport = projects.filter((project) =>
            keepExisting
                ? !oldProjects.some((old) => old.id === project.id)
                : true,
        );
        if (projectsToImport.length > 0) {
            const importedProjects = await this.projectStore.importProjects(
                projectsToImport,
            );
            const importedProjectEvents = importedProjects.map((project) => ({
                type: PROJECT_IMPORT,
                createdBy: userName,
                data: project,
            }));
            await this.eventStore.batchStore(importedProjectEvents);
        }
    }
 
    // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
    async importTagData({
        tagTypes,
        tags,
        featureTags,
        userName,
        dropBeforeImport,
        keepExisting,
    }): Promise<void> {
        this.logger.info(
            `Importing ${tagTypes.length} tagtypes, ${tags.length} tags and ${featureTags.length} feature tags`,
        );
        const oldTagTypes = dropBeforeImport
            ? []
            : await this.tagTypeStore.getAll();
        const oldTags = dropBeforeImport ? [] : await this.tagStore.getAll();
        const oldFeatureTags = dropBeforeImport
            ? []
            : await this.featureTagStore.getAll();
        if (dropBeforeImport) {
            this.logger.info(
                'Dropping all existing featuretags, tags and tagtypes',
            );
            await this.featureTagStore.deleteAll();
            await this.tagStore.deleteAll();
            await this.tagTypeStore.deleteAll();
            await this.eventStore.batchStore([
                {
                    type: DROP_FEATURE_TAGS,
                    createdBy: userName,
                    data: { name: 'all-feature-tags' },
                },
                {
                    type: DROP_TAGS,
                    createdBy: userName,
                    data: { name: 'all-tags' },
                },
                {
                    type: DROP_TAG_TYPES,
                    createdBy: userName,
                    data: { name: 'all-tag-types' },
                },
            ]);
        }
        await this.importTagTypes(
            tagTypes,
            keepExisting,
            oldTagTypes,
            userName,
        );
        await this.importTags(tags, keepExisting, oldTags, userName);
        await this.importFeatureTags(
            featureTags,
            keepExisting,
            oldFeatureTags,
            userName,
        );
    }
 
    compareFeatureTags: (old: IFeatureTag, tag: IFeatureTag) => boolean = (
        old,
        tag,
    ) =>
        old.featureName === tag.featureName &&
        old.tagValue === tag.tagValue &&
        old.tagType === tag.tagType;
 
    async importFeatureTags(
        featureTags: IFeatureTag[],
        keepExisting: boolean,
        oldFeatureTags: IFeatureTag[],
        userName: string,
    ): Promise<void> {
        const featureTagsToInsert = featureTags.filter((tag) =>
            keepExisting
                ? !oldFeatureTags.some((old) =>
                      this.compareFeatureTags(old, tag),
                  )
                : true,
        );
        if (featureTagsToInsert.length > 0) {
            const importedFeatureTags =
                await this.featureTagStore.importFeatureTags(
                    featureTagsToInsert,
                );
            const importedFeatureTagEvents = importedFeatureTags.map((tag) => ({
                type: FEATURE_TAG_IMPORT,
                createdBy: userName,
                data: tag,
            }));
            await this.eventStore.batchStore(importedFeatureTagEvents);
        }
    }
 
    compareTags = (old: ITag, tag: ITag): boolean =>
        old.type === tag.type && old.value === tag.value;
 
    async importTags(
        tags: ITag[],
        keepExisting: boolean,
        oldTags: ITag[],
        userName: string,
    ): Promise<void> {
        const tagsToInsert = tags.filter((tag) =>
            keepExisting
                ? !oldTags.some((old) => this.compareTags(old, tag))
                : true,
        );
        if (tagsToInsert.length > 0) {
            const importedTags = await this.tagStore.bulkImport(tagsToInsert);
            const importedTagEvents = importedTags.map((tag) => ({
                type: TAG_IMPORT,
                createdBy: userName,
                data: tag,
            }));
            await this.eventStore.batchStore(importedTagEvents);
        }
    }
 
    async importTagTypes(
        tagTypes: ITagType[],
        keepExisting: boolean,
        oldTagTypes: ITagType[] = [], // eslint-disable-line
        userName: string,
    ): Promise<void> {
        const tagTypesToInsert = tagTypes.filter((tagType) =>
            keepExisting
                ? !oldTagTypes.some((t) => t.name === tagType.name)
                : true,
        );
        if (tagTypesToInsert.length > 0) {
            const importedTagTypes = await this.tagTypeStore.bulkImport(
                tagTypesToInsert,
            );
            const importedTagTypeEvents = importedTagTypes.map((tagType) => ({
                type: TAG_TYPE_IMPORT,
                createdBy: userName,
                data: tagType,
            }));
            await this.eventStore.batchStore(importedTagTypeEvents);
        }
    }
 
    async importSegments(
        segments: PartialSome<ISegment, 'id'>[],
        userName: string,
        dropBeforeImport: boolean,
    ): Promise<void> {
        if (dropBeforeImport) {
            await this.segmentStore.deleteAll();
        }
 
        await Promise.all(
            segments.map((segment) =>
                this.segmentStore.create(segment, { username: userName }),
            ),
        );
    }
 
    async importFeatureStrategySegments(
        featureStrategySegments: {
            featureStrategyId: string;
            segmentId: number;
        }[],
    ): Promise<void> {
        await Promise.all(
            featureStrategySegments.map(({ featureStrategyId, segmentId }) =>
                this.segmentStore.addToStrategy(segmentId, featureStrategyId),
            ),
        );
    }
 
    async export({
        includeFeatureToggles = true,
        includeStrategies = true,
        includeProjects = true,
        includeTags = true,
        includeEnvironments = true,
        includeSegments = true,
    }: IExportIncludeOptions): Promise<{
        features: FeatureToggle[];
        strategies: IStrategy[];
        version: number;
        projects: IProject[];
        tagTypes: ITagType[];
        tags: ITag[];
        featureTags: IFeatureTag[];
        featureStrategies: IFeatureStrategy[];
        environments: IEnvironment[];
        featureEnvironments: IFeatureEnvironment[];
    }> {
        return Promise.all([
            includeFeatureToggles
                ? this.toggleStore.getAll({ archived: false })
                : Promise.resolve([]),
            includeStrategies
                ? this.strategyStore.getEditableStrategies()
                : Promise.resolve([]),
            this.projectStore && includeProjects
                ? this.projectStore.getAll()
                : Promise.resolve([]),
            includeTags ? this.tagTypeStore.getAll() : Promise.resolve([]),
            includeTags ? this.tagStore.getAll() : Promise.resolve([]),
            includeTags && includeFeatureToggles
                ? this.featureTagStore.getAll()
                : Promise.resolve([]),
            includeFeatureToggles
                ? this.featureStrategiesStore.getAll()
                : Promise.resolve([]),
            includeEnvironments
                ? this.environmentStore.getAll()
                : Promise.resolve([]),
            includeFeatureToggles
                ? this.featureEnvironmentStore.getAll()
                : Promise.resolve([]),
            includeSegments ? this.segmentStore.getAll() : Promise.resolve([]),
            includeSegments
                ? this.segmentStore.getAllFeatureStrategySegments()
                : Promise.resolve([]),
        ]).then(
            ([
                features,
                strategies,
                projects,
                tagTypes,
                tags,
                featureTags,
                featureStrategies,
                environments,
                featureEnvironments,
                segments,
                featureStrategySegments,
            ]) => ({
                version: 3,
                features,
                strategies,
                projects,
                tagTypes,
                tags,
                featureTags,
                featureStrategies: featureStrategies.filter((fS) =>
                    features.some((f) => fS.featureName === f.name),
                ),
                environments,
                featureEnvironments: featureEnvironments.filter((fE) =>
                    features.some((f) => fE.featureName === f.name),
                ),
                segments,
                featureStrategySegments,
            }),
        );
    }
}
 
module.exports = StateService;