2023-09-27 14:33:51 +02:00
|
|
|
import { Db } from '../../db/db';
|
|
|
|
import { IDependentFeaturesReadModel } from './dependent-features-read-model-type';
|
2023-09-27 15:07:20 +02:00
|
|
|
import { IDependency } from '../../types';
|
2023-09-27 14:33:51 +02:00
|
|
|
|
|
|
|
export class DependentFeaturesReadModel implements IDependentFeaturesReadModel {
|
|
|
|
private db: Db;
|
|
|
|
|
|
|
|
constructor(db: Db) {
|
|
|
|
this.db = db;
|
|
|
|
}
|
|
|
|
|
2023-10-06 13:39:16 +02:00
|
|
|
async getOrphanParents(parentsAndChildren: string[]): Promise<string[]> {
|
|
|
|
const rows = await this.db('dependent_features')
|
|
|
|
.distinct('parent')
|
|
|
|
.whereIn('parent', parentsAndChildren)
|
|
|
|
.andWhere(function () {
|
|
|
|
this.whereIn('parent', function () {
|
|
|
|
this.select('parent')
|
|
|
|
.from('dependent_features')
|
|
|
|
.whereNotIn('child', parentsAndChildren);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return rows.map((row) => row.parent);
|
|
|
|
}
|
|
|
|
|
2023-10-04 09:27:53 +02:00
|
|
|
async getChildren(parents: string[]): Promise<string[]> {
|
|
|
|
const rows = await this.db('dependent_features').whereIn(
|
2023-09-27 14:33:51 +02:00
|
|
|
'parent',
|
2023-10-04 09:27:53 +02:00
|
|
|
parents,
|
2023-09-27 14:33:51 +02:00
|
|
|
);
|
|
|
|
|
2023-10-04 09:27:53 +02:00
|
|
|
return [...new Set(rows.map((row) => row.child))];
|
2023-09-27 14:33:51 +02:00
|
|
|
}
|
|
|
|
|
2023-09-27 15:07:20 +02:00
|
|
|
async getParents(child: string): Promise<IDependency[]> {
|
2023-09-27 14:33:51 +02:00
|
|
|
const rows = await this.db('dependent_features').where('child', child);
|
|
|
|
|
2023-09-27 15:07:20 +02:00
|
|
|
return rows.map((row) => ({
|
|
|
|
feature: row.parent,
|
|
|
|
enabled: row.enabled,
|
|
|
|
variants: row.variants,
|
|
|
|
}));
|
2023-09-27 14:33:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async getParentOptions(child: string): Promise<string[]> {
|
2023-09-27 15:17:04 +02:00
|
|
|
const result = await this.db('features')
|
|
|
|
.where('features.name', child)
|
|
|
|
.select('features.project');
|
2023-09-27 14:33:51 +02:00
|
|
|
if (result.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
2023-09-27 15:17:04 +02:00
|
|
|
const rows = await this.db('features')
|
|
|
|
.leftJoin(
|
|
|
|
'dependent_features',
|
|
|
|
'features.name',
|
|
|
|
'dependent_features.child',
|
|
|
|
)
|
|
|
|
.where('features.project', result[0].project)
|
|
|
|
.andWhere('features.name', '!=', child)
|
|
|
|
.andWhere('dependent_features.child', null)
|
2023-09-28 13:37:52 +02:00
|
|
|
.andWhere('features.archived_at', null)
|
2023-09-27 15:17:04 +02:00
|
|
|
.select('features.name');
|
2023-09-27 14:33:51 +02:00
|
|
|
|
|
|
|
return rows.map((item) => item.name);
|
|
|
|
}
|
2023-10-04 12:16:52 +02:00
|
|
|
|
|
|
|
async hasDependencies(feature: string): Promise<boolean> {
|
|
|
|
const parents = await this.db('dependent_features')
|
|
|
|
.where('parent', feature)
|
|
|
|
.orWhere('child', feature)
|
|
|
|
.limit(1);
|
|
|
|
|
|
|
|
return parents.length > 0;
|
|
|
|
}
|
2023-09-27 14:33:51 +02:00
|
|
|
}
|