From 2c95dfefd13fe0016f0c2931ebcb9949c9bf86d8 Mon Sep 17 00:00:00 2001 From: sjaanus Date: Wed, 26 Oct 2022 12:26:15 +0200 Subject: [PATCH] Suggest changes refactoring and schema fixes (#2250) * Draft done * Update GET endpoint * Update to latest schema * Fixes --- src/lib/db/suggest-change-store.ts | 45 +++++++++++-------- src/lib/types/model.ts | 7 +-- src/lib/types/stores/suggest-change-store.ts | 12 +++-- .../fixtures/fake-suggest-change-store.ts | 25 +++++------ 4 files changed, 45 insertions(+), 44 deletions(-) diff --git a/src/lib/db/suggest-change-store.ts b/src/lib/db/suggest-change-store.ts index b088cd4931..9cfe3d0689 100644 --- a/src/lib/db/suggest-change-store.ts +++ b/src/lib/db/suggest-change-store.ts @@ -8,7 +8,6 @@ import { ISuggestChangeset, SuggestChangeAction, } from '../types/model'; -import User from '../types/user'; const T = { SUGGEST_CHANGE: 'suggest_change', @@ -61,15 +60,17 @@ const suggestChangeRowReducer = (acc, suggestChangeRow) => { imageUrl: suggestChangeSet.changeSetAvatar, }, createdAt: suggestChangeSet.created_at, - changes: [], + features: [], }; } const currentSuggestChangeSet = acc[suggestChangeSet.id]; if (changeId) { - currentSuggestChangeSet.changes.push({ + const featureObject = currentSuggestChangeSet.features.find( + (feature) => feature.name === changeFeature, + ); + const change = { id: changeId, - feature: changeFeature, action: changeAction, payload: changePayload, createdAt: changeCreatedAt, @@ -78,7 +79,15 @@ const suggestChangeRowReducer = (acc, suggestChangeRow) => { username: changeCreatedByUsername, imageUrl: changeCreatedByAvatar, }, - }); + }; + if (featureObject) { + featureObject.changes.push(change); + } else { + currentSuggestChangeSet.features.push({ + name: changeFeature, + changes: [change], + }); + } } return acc; }; @@ -117,6 +126,8 @@ export class SuggestChangeStore implements ISuggestChangeStore { 'changeUser.id', ) .select( + 'changeSet.state', + 'changeSet.id', 'changeSet.environment', 'projects.name as project', 'changeSet.created_at', @@ -146,19 +157,17 @@ export class SuggestChangeStore implements ISuggestChangeStore { return this.mapRows(rows); }; - getDraftForUser = async ( - user: User, + getDraftsForUser = async ( + userId: number, project: string, - environment: string, - ): Promise => { + ): Promise => { const rows = await this.buildSuggestChangeSetChangesQuery().where({ - 'changeSet.created_by': user.id, + 'changeSet.created_by': userId, state: 'Draft', project: project, - environment: environment, }); - const change = this.mapRows(rows)[0]; - return change; + const changesets = this.mapRows(rows); + return changesets; }; getForEnvironment = async ( @@ -184,19 +193,19 @@ export class SuggestChangeStore implements ISuggestChangeStore { ISuggestChangeset, 'id' | 'createdBy' | 'createdAt' >, - user: User, + userId: number, ): Promise => { const [{ id }] = await this.db(T.SUGGEST_CHANGE_SET) .insert({ environment: suggestChangeSet.environment, state: suggestChangeSet.state, project: suggestChangeSet.project, - created_by: user.id, + created_by: userId, }) .returning('id'); suggestChangeSet.changes.forEach((change) => { - this.addChangeToSet(change, id, user); + this.addChangeToSet(change, id, userId); }); return this.get(id); @@ -205,7 +214,7 @@ export class SuggestChangeStore implements ISuggestChangeStore { addChangeToSet = async ( change: PartialSome, changeSetID: number, - user: User, + userId: number, ): Promise => { await this.db(T.SUGGEST_CHANGE) .insert({ @@ -213,7 +222,7 @@ export class SuggestChangeStore implements ISuggestChangeStore { feature: change.feature, payload: change.payload, suggest_change_set_id: changeSetID, - created_by: user.id, + created_by: userId, }) .returning('id'); }; diff --git a/src/lib/types/model.ts b/src/lib/types/model.ts index 1512edb64b..a3479c516f 100644 --- a/src/lib/types/model.ts +++ b/src/lib/types/model.ts @@ -376,16 +376,11 @@ export interface ISuggestChangeset { changes: ISuggestChange[]; } -export interface ISuggestChangePayload { - environment: string; - data: unknown; -} - export interface ISuggestChange { id?: number; action: SuggestChangeAction; feature: string; - payload: ISuggestChangePayload; + payload: any; createdBy?: Pick; createdAt?: Date; } diff --git a/src/lib/types/stores/suggest-change-store.ts b/src/lib/types/stores/suggest-change-store.ts index 604287e3ce..21ce66c5db 100644 --- a/src/lib/types/stores/suggest-change-store.ts +++ b/src/lib/types/stores/suggest-change-store.ts @@ -1,7 +1,6 @@ import { Store } from './store'; import { ISuggestChange, ISuggestChangeset } from '../model'; import { PartialSome } from '../partial'; -import User from '../user'; export interface ISuggestChangeStore extends Store { create( @@ -9,13 +8,13 @@ export interface ISuggestChangeStore extends Store { ISuggestChangeset, 'id' | 'createdBy' | 'createdAt' >, - user: Partial>, + userId: number, ): Promise; addChangeToSet( change: PartialSome, changeSetID: number, - user: User, + userId: number, ): Promise; get(id: number): Promise; @@ -24,11 +23,10 @@ export interface ISuggestChangeStore extends Store { getForProject(project: string): Promise; - getDraftForUser( - user: User, + getDraftsForUser( + userId: number, project: string, - environment: string, - ): Promise; + ): Promise; getForEnvironment(environment: string): Promise; } diff --git a/src/test/fixtures/fake-suggest-change-store.ts b/src/test/fixtures/fake-suggest-change-store.ts index 28b9983630..3d9f31d413 100644 --- a/src/test/fixtures/fake-suggest-change-store.ts +++ b/src/test/fixtures/fake-suggest-change-store.ts @@ -1,7 +1,6 @@ import { ISuggestChangeStore } from '../../lib/types/stores/suggest-change-store'; import { ISuggestChange, ISuggestChangeset } from '../../lib/types/model'; import { PartialSome } from '../../lib/types/partial'; -import User from '../../lib/types/user'; export default class FakeSuggestChangeStore implements ISuggestChangeStore { suggestChanges: ISuggestChangeset[] = []; @@ -23,11 +22,13 @@ export default class FakeSuggestChangeStore implements ISuggestChangeStore { addChangeToSet( change: PartialSome, changeSetID: number, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - user: User, + userId: number, ): Promise { const changeSet = this.suggestChanges.find((s) => s.id === changeSetID); - changeSet.changes.push(change); + changeSet.changes.push({ + createdBy: { id: userId, username: '', imageUrl: '' }, + ...change, + }); return Promise.resolve(); } @@ -39,17 +40,15 @@ export default class FakeSuggestChangeStore implements ISuggestChangeStore { ); } - getDraftForUser( - user: User, + getDraftsForUser( + userId: number, project: string, - environment: string, - ): Promise { + ): Promise { return Promise.resolve( - this.suggestChanges.find( + this.suggestChanges.filter( (changeSet) => changeSet.project === project && - changeSet.environment === environment && - changeSet.createdBy.id === user.id, + changeSet.createdBy.id === userId, ), ); } @@ -64,12 +63,12 @@ export default class FakeSuggestChangeStore implements ISuggestChangeStore { create( suggestChangeSet: PartialSome, - user: Partial>, + userId: number, ): Promise { this.suggestChanges.push({ id: 1, ...suggestChangeSet, - createdBy: { id: user.id, username: user.email, imageUrl: '' }, + createdBy: { id: userId, username: '', imageUrl: '' }, }); return Promise.resolve(undefined); }