2021-04-20 12:32:02 +02:00
|
|
|
import { Knex } from 'knex';
|
|
|
|
import { Logger, LogProvider } from '../logger';
|
|
|
|
|
2021-07-07 10:46:50 +02:00
|
|
|
import NotFoundError from '../error/notfound-error';
|
|
|
|
import { IEnvironmentOverview, IFeatureOverview } from '../types/model';
|
2021-08-12 15:04:37 +02:00
|
|
|
import {
|
|
|
|
IProject,
|
|
|
|
IProjectHealthUpdate,
|
|
|
|
IProjectInsert,
|
|
|
|
IProjectStore,
|
|
|
|
} from '../types/stores/project-store';
|
2020-09-28 21:54:44 +02:00
|
|
|
|
2021-07-07 10:46:50 +02:00
|
|
|
const COLUMNS = ['id', 'name', 'description', 'created_at', 'health'];
|
2020-09-28 21:54:44 +02:00
|
|
|
const TABLE = 'projects';
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
class ProjectStore implements IProjectStore {
|
2021-04-20 12:32:02 +02:00
|
|
|
private db: Knex;
|
|
|
|
|
|
|
|
private logger: Logger;
|
|
|
|
|
|
|
|
constructor(db: Knex, getLogger: LogProvider) {
|
2020-09-28 21:54:44 +02:00
|
|
|
this.db = db;
|
2021-08-12 15:04:37 +02:00
|
|
|
this.logger = getLogger('project-store.ts');
|
2020-09-28 21:54:44 +02:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
2021-04-20 12:32:02 +02:00
|
|
|
fieldToRow(data): IProjectInsert {
|
2020-09-28 21:54:44 +02:00
|
|
|
return {
|
|
|
|
id: data.id,
|
|
|
|
name: data.name,
|
|
|
|
description: data.description,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
destroy(): void {}
|
|
|
|
|
|
|
|
async exists(id: string): Promise<boolean> {
|
|
|
|
const result = await this.db.raw(
|
|
|
|
`SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE id = ?) AS present`,
|
|
|
|
[id],
|
|
|
|
);
|
|
|
|
const { present } = result.rows[0];
|
|
|
|
return present;
|
|
|
|
}
|
|
|
|
|
2021-04-20 12:32:02 +02:00
|
|
|
async getAll(): Promise<IProject[]> {
|
2020-09-28 21:54:44 +02:00
|
|
|
const rows = await this.db
|
|
|
|
.select(COLUMNS)
|
|
|
|
.from(TABLE)
|
|
|
|
.orderBy('name', 'asc');
|
|
|
|
|
|
|
|
return rows.map(this.mapRow);
|
|
|
|
}
|
|
|
|
|
2021-04-30 12:51:46 +02:00
|
|
|
async get(id: string): Promise<IProject> {
|
2020-09-28 21:54:44 +02:00
|
|
|
return this.db
|
|
|
|
.first(COLUMNS)
|
|
|
|
.from(TABLE)
|
|
|
|
.where({ id })
|
|
|
|
.then(this.mapRow);
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async hasProject(id: string): Promise<boolean> {
|
|
|
|
const result = await this.db.raw(
|
|
|
|
`SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE id = ?) AS present`,
|
|
|
|
[id],
|
|
|
|
);
|
|
|
|
const { present } = result.rows[0];
|
|
|
|
return present;
|
2020-09-28 21:54:44 +02:00
|
|
|
}
|
|
|
|
|
2021-07-07 10:46:50 +02:00
|
|
|
async updateHealth(healthUpdate: IProjectHealthUpdate): Promise<void> {
|
|
|
|
await this.db(TABLE)
|
|
|
|
.where({ id: healthUpdate.id })
|
|
|
|
.update({ health: healthUpdate.health });
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async create(project: IProjectInsert): Promise<IProject> {
|
|
|
|
const row = await this.db(TABLE)
|
2020-09-28 21:54:44 +02:00
|
|
|
.insert(this.fieldToRow(project))
|
2021-08-12 15:04:37 +02:00
|
|
|
.returning('*');
|
|
|
|
return this.mapRow(row);
|
2020-09-28 21:54:44 +02:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
2021-04-20 12:32:02 +02:00
|
|
|
async update(data): Promise<void> {
|
2020-09-28 21:54:44 +02:00
|
|
|
try {
|
|
|
|
await this.db(TABLE)
|
|
|
|
.where({ id: data.id })
|
|
|
|
.update(this.fieldToRow(data));
|
|
|
|
} catch (err) {
|
|
|
|
this.logger.error('Could not update project, error: ', err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async importProjects(projects: IProjectInsert[]): Promise<IProject[]> {
|
2021-03-12 11:08:10 +01:00
|
|
|
const rows = await this.db(TABLE)
|
|
|
|
.insert(projects.map(this.fieldToRow))
|
|
|
|
.returning(COLUMNS)
|
|
|
|
.onConflict('id')
|
|
|
|
.ignore();
|
|
|
|
if (rows.length > 0) {
|
2021-07-30 13:38:28 +02:00
|
|
|
await this.addGlobalEnvironment(rows);
|
2021-03-12 11:08:10 +01:00
|
|
|
return rows.map(this.mapRow);
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async addGlobalEnvironment(projects: any[]): Promise<void> {
|
|
|
|
const environments = projects.map((p) => ({
|
2021-07-30 13:38:28 +02:00
|
|
|
project_id: p.id,
|
|
|
|
environment_name: ':global:',
|
|
|
|
}));
|
|
|
|
await this.db('project_environments')
|
|
|
|
.insert(environments)
|
|
|
|
.onConflict(['project_id', 'environment_name'])
|
|
|
|
.ignore();
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async deleteAll(): Promise<void> {
|
2021-03-12 11:08:10 +01:00
|
|
|
await this.db(TABLE).del();
|
|
|
|
}
|
|
|
|
|
2021-07-07 10:46:50 +02:00
|
|
|
async delete(id: string): Promise<void> {
|
2020-09-28 21:54:44 +02:00
|
|
|
try {
|
2021-08-12 15:04:37 +02:00
|
|
|
await this.db(TABLE).where({ id }).del();
|
2020-09-28 21:54:44 +02:00
|
|
|
} catch (err) {
|
|
|
|
this.logger.error('Could not delete project, error: ', err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async deleteEnvironmentForProject(
|
|
|
|
id: string,
|
|
|
|
environment: string,
|
|
|
|
): Promise<void> {
|
2021-07-07 10:46:50 +02:00
|
|
|
await this.db('project_environments')
|
|
|
|
.where({
|
|
|
|
project_id: id,
|
|
|
|
environment_name: environment,
|
|
|
|
})
|
|
|
|
.del();
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async addEnvironmentToProject(
|
|
|
|
id: string,
|
|
|
|
environment: string,
|
|
|
|
): Promise<void> {
|
|
|
|
await this.db('project_environments')
|
|
|
|
.insert({ project_id: id, environment_name: environment })
|
|
|
|
.onConflict(['project_id', 'environment_name'])
|
|
|
|
.ignore();
|
|
|
|
}
|
|
|
|
|
|
|
|
async getEnvironmentsForProject(id: string): Promise<string[]> {
|
|
|
|
return this.db('project_environments')
|
|
|
|
.where({
|
|
|
|
project_id: id,
|
|
|
|
})
|
|
|
|
.returning('environment_name');
|
|
|
|
}
|
|
|
|
|
2021-07-07 10:46:50 +02:00
|
|
|
async getMembers(projectId: string): Promise<number> {
|
|
|
|
const rolesFromProject = this.db('role_permission')
|
|
|
|
.select('role_id')
|
|
|
|
.distinct()
|
|
|
|
.where({ project: projectId });
|
|
|
|
|
|
|
|
const numbers = await this.db('role_user')
|
|
|
|
.countDistinct('user_id as members')
|
|
|
|
.whereIn('role_id', rolesFromProject)
|
|
|
|
.first();
|
|
|
|
const { members } = numbers;
|
|
|
|
if (typeof members === 'string') {
|
|
|
|
return parseInt(members, 10);
|
|
|
|
}
|
|
|
|
return members;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getProjectOverview(
|
|
|
|
projectId: string,
|
|
|
|
archived: boolean = false,
|
|
|
|
): Promise<IFeatureOverview[]> {
|
|
|
|
const rows = await this.db('features')
|
|
|
|
.where({ project: projectId, archived })
|
|
|
|
.select(
|
|
|
|
'features.name as feature_name',
|
|
|
|
'features.type as type',
|
|
|
|
'features.created_at as created_at',
|
|
|
|
'features.last_seen_at as last_seen_at',
|
|
|
|
'features.stale as stale',
|
|
|
|
'feature_environments.enabled as enabled',
|
|
|
|
'feature_environments.environment as environment',
|
|
|
|
'environments.display_name as display_name',
|
|
|
|
)
|
|
|
|
.fullOuterJoin(
|
|
|
|
'feature_environments',
|
|
|
|
'feature_environments.feature_name',
|
|
|
|
'features.name',
|
|
|
|
)
|
|
|
|
.fullOuterJoin(
|
|
|
|
'environments',
|
|
|
|
'feature_environments.environment',
|
|
|
|
'environments.name',
|
|
|
|
);
|
|
|
|
if (rows.length > 0) {
|
|
|
|
const overview = rows.reduce((acc, r) => {
|
|
|
|
if (acc[r.feature_name] !== undefined) {
|
|
|
|
acc[r.feature_name].environments.push(
|
|
|
|
this.getEnvironment(r),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
acc[r.feature_name] = {
|
|
|
|
type: r.type,
|
|
|
|
name: r.feature_name,
|
|
|
|
createdAt: r.created_at,
|
|
|
|
lastSeenAt: r.last_seen_at,
|
|
|
|
stale: r.stale,
|
|
|
|
environments: [this.getEnvironment(r)],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
return Object.values(overview).map((o: IFeatureOverview) => ({
|
|
|
|
...o,
|
2021-08-12 15:04:37 +02:00
|
|
|
environments: o.environments.filter((f) => f.name),
|
2021-07-07 10:46:50 +02:00
|
|
|
}));
|
|
|
|
}
|
2021-07-14 13:17:16 +02:00
|
|
|
return [];
|
2021-07-07 10:46:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
private getEnvironment(r: any): IEnvironmentOverview {
|
|
|
|
return {
|
|
|
|
name: r.environment,
|
|
|
|
displayName: r.display_name,
|
|
|
|
enabled: r.enabled,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
2021-04-20 12:32:02 +02:00
|
|
|
mapRow(row): IProject {
|
2020-09-28 21:54:44 +02:00
|
|
|
if (!row) {
|
|
|
|
throw new NotFoundError('No project found');
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: row.id,
|
|
|
|
name: row.name,
|
|
|
|
description: row.description,
|
|
|
|
createdAt: row.created_at,
|
2021-07-07 10:46:50 +02:00
|
|
|
health: row.health || 100,
|
2020-09-28 21:54:44 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2021-04-20 12:32:02 +02:00
|
|
|
export default ProjectStore;
|
2020-09-28 21:54:44 +02:00
|
|
|
module.exports = ProjectStore;
|