1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

Suggest changes refactoring and schema fixes (#2250)

* Draft done

* Update GET endpoint

* Update to latest schema

* Fixes
This commit is contained in:
sjaanus 2022-10-26 12:26:15 +02:00 committed by GitHub
parent d8df7e5815
commit 2c95dfefd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 44 deletions

View File

@ -8,7 +8,6 @@ import {
ISuggestChangeset, ISuggestChangeset,
SuggestChangeAction, SuggestChangeAction,
} from '../types/model'; } from '../types/model';
import User from '../types/user';
const T = { const T = {
SUGGEST_CHANGE: 'suggest_change', SUGGEST_CHANGE: 'suggest_change',
@ -61,15 +60,17 @@ const suggestChangeRowReducer = (acc, suggestChangeRow) => {
imageUrl: suggestChangeSet.changeSetAvatar, imageUrl: suggestChangeSet.changeSetAvatar,
}, },
createdAt: suggestChangeSet.created_at, createdAt: suggestChangeSet.created_at,
changes: [], features: [],
}; };
} }
const currentSuggestChangeSet = acc[suggestChangeSet.id]; const currentSuggestChangeSet = acc[suggestChangeSet.id];
if (changeId) { if (changeId) {
currentSuggestChangeSet.changes.push({ const featureObject = currentSuggestChangeSet.features.find(
(feature) => feature.name === changeFeature,
);
const change = {
id: changeId, id: changeId,
feature: changeFeature,
action: changeAction, action: changeAction,
payload: changePayload, payload: changePayload,
createdAt: changeCreatedAt, createdAt: changeCreatedAt,
@ -78,7 +79,15 @@ const suggestChangeRowReducer = (acc, suggestChangeRow) => {
username: changeCreatedByUsername, username: changeCreatedByUsername,
imageUrl: changeCreatedByAvatar, imageUrl: changeCreatedByAvatar,
}, },
}); };
if (featureObject) {
featureObject.changes.push(change);
} else {
currentSuggestChangeSet.features.push({
name: changeFeature,
changes: [change],
});
}
} }
return acc; return acc;
}; };
@ -117,6 +126,8 @@ export class SuggestChangeStore implements ISuggestChangeStore {
'changeUser.id', 'changeUser.id',
) )
.select( .select(
'changeSet.state',
'changeSet.id',
'changeSet.environment', 'changeSet.environment',
'projects.name as project', 'projects.name as project',
'changeSet.created_at', 'changeSet.created_at',
@ -146,19 +157,17 @@ export class SuggestChangeStore implements ISuggestChangeStore {
return this.mapRows(rows); return this.mapRows(rows);
}; };
getDraftForUser = async ( getDraftsForUser = async (
user: User, userId: number,
project: string, project: string,
environment: string, ): Promise<ISuggestChangeset[]> => {
): Promise<ISuggestChangeset> => {
const rows = await this.buildSuggestChangeSetChangesQuery().where({ const rows = await this.buildSuggestChangeSetChangesQuery().where({
'changeSet.created_by': user.id, 'changeSet.created_by': userId,
state: 'Draft', state: 'Draft',
project: project, project: project,
environment: environment,
}); });
const change = this.mapRows(rows)[0]; const changesets = this.mapRows(rows);
return change; return changesets;
}; };
getForEnvironment = async ( getForEnvironment = async (
@ -184,19 +193,19 @@ export class SuggestChangeStore implements ISuggestChangeStore {
ISuggestChangeset, ISuggestChangeset,
'id' | 'createdBy' | 'createdAt' 'id' | 'createdBy' | 'createdAt'
>, >,
user: User, userId: number,
): Promise<ISuggestChangeset> => { ): Promise<ISuggestChangeset> => {
const [{ id }] = await this.db(T.SUGGEST_CHANGE_SET) const [{ id }] = await this.db(T.SUGGEST_CHANGE_SET)
.insert<ISuggestChangesetInsert>({ .insert<ISuggestChangesetInsert>({
environment: suggestChangeSet.environment, environment: suggestChangeSet.environment,
state: suggestChangeSet.state, state: suggestChangeSet.state,
project: suggestChangeSet.project, project: suggestChangeSet.project,
created_by: user.id, created_by: userId,
}) })
.returning('id'); .returning('id');
suggestChangeSet.changes.forEach((change) => { suggestChangeSet.changes.forEach((change) => {
this.addChangeToSet(change, id, user); this.addChangeToSet(change, id, userId);
}); });
return this.get(id); return this.get(id);
@ -205,7 +214,7 @@ export class SuggestChangeStore implements ISuggestChangeStore {
addChangeToSet = async ( addChangeToSet = async (
change: PartialSome<ISuggestChange, 'id' | 'createdBy' | 'createdAt'>, change: PartialSome<ISuggestChange, 'id' | 'createdBy' | 'createdAt'>,
changeSetID: number, changeSetID: number,
user: User, userId: number,
): Promise<void> => { ): Promise<void> => {
await this.db(T.SUGGEST_CHANGE) await this.db(T.SUGGEST_CHANGE)
.insert<ISuggestChangeInsert>({ .insert<ISuggestChangeInsert>({
@ -213,7 +222,7 @@ export class SuggestChangeStore implements ISuggestChangeStore {
feature: change.feature, feature: change.feature,
payload: change.payload, payload: change.payload,
suggest_change_set_id: changeSetID, suggest_change_set_id: changeSetID,
created_by: user.id, created_by: userId,
}) })
.returning('id'); .returning('id');
}; };

View File

@ -376,16 +376,11 @@ export interface ISuggestChangeset {
changes: ISuggestChange[]; changes: ISuggestChange[];
} }
export interface ISuggestChangePayload {
environment: string;
data: unknown;
}
export interface ISuggestChange { export interface ISuggestChange {
id?: number; id?: number;
action: SuggestChangeAction; action: SuggestChangeAction;
feature: string; feature: string;
payload: ISuggestChangePayload; payload: any;
createdBy?: Pick<User, 'id' | 'username' | 'imageUrl'>; createdBy?: Pick<User, 'id' | 'username' | 'imageUrl'>;
createdAt?: Date; createdAt?: Date;
} }

View File

@ -1,7 +1,6 @@
import { Store } from './store'; import { Store } from './store';
import { ISuggestChange, ISuggestChangeset } from '../model'; import { ISuggestChange, ISuggestChangeset } from '../model';
import { PartialSome } from '../partial'; import { PartialSome } from '../partial';
import User from '../user';
export interface ISuggestChangeStore extends Store<ISuggestChangeset, number> { export interface ISuggestChangeStore extends Store<ISuggestChangeset, number> {
create( create(
@ -9,13 +8,13 @@ export interface ISuggestChangeStore extends Store<ISuggestChangeset, number> {
ISuggestChangeset, ISuggestChangeset,
'id' | 'createdBy' | 'createdAt' 'id' | 'createdBy' | 'createdAt'
>, >,
user: Partial<Pick<User, 'username' | 'email'>>, userId: number,
): Promise<ISuggestChangeset>; ): Promise<ISuggestChangeset>;
addChangeToSet( addChangeToSet(
change: PartialSome<ISuggestChange, 'id' | 'createdBy' | 'createdAt'>, change: PartialSome<ISuggestChange, 'id' | 'createdBy' | 'createdAt'>,
changeSetID: number, changeSetID: number,
user: User, userId: number,
): Promise<void>; ): Promise<void>;
get(id: number): Promise<ISuggestChangeset>; get(id: number): Promise<ISuggestChangeset>;
@ -24,11 +23,10 @@ export interface ISuggestChangeStore extends Store<ISuggestChangeset, number> {
getForProject(project: string): Promise<ISuggestChangeset[]>; getForProject(project: string): Promise<ISuggestChangeset[]>;
getDraftForUser( getDraftsForUser(
user: User, userId: number,
project: string, project: string,
environment: string, ): Promise<ISuggestChangeset[]>;
): Promise<ISuggestChangeset>;
getForEnvironment(environment: string): Promise<ISuggestChangeset[]>; getForEnvironment(environment: string): Promise<ISuggestChangeset[]>;
} }

View File

@ -1,7 +1,6 @@
import { ISuggestChangeStore } from '../../lib/types/stores/suggest-change-store'; import { ISuggestChangeStore } from '../../lib/types/stores/suggest-change-store';
import { ISuggestChange, ISuggestChangeset } from '../../lib/types/model'; import { ISuggestChange, ISuggestChangeset } from '../../lib/types/model';
import { PartialSome } from '../../lib/types/partial'; import { PartialSome } from '../../lib/types/partial';
import User from '../../lib/types/user';
export default class FakeSuggestChangeStore implements ISuggestChangeStore { export default class FakeSuggestChangeStore implements ISuggestChangeStore {
suggestChanges: ISuggestChangeset[] = []; suggestChanges: ISuggestChangeset[] = [];
@ -23,11 +22,13 @@ export default class FakeSuggestChangeStore implements ISuggestChangeStore {
addChangeToSet( addChangeToSet(
change: PartialSome<ISuggestChange, 'id' | 'createdBy' | 'createdAt'>, change: PartialSome<ISuggestChange, 'id' | 'createdBy' | 'createdAt'>,
changeSetID: number, changeSetID: number,
// eslint-disable-next-line @typescript-eslint/no-unused-vars userId: number,
user: User,
): Promise<void> { ): Promise<void> {
const changeSet = this.suggestChanges.find((s) => s.id === changeSetID); 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(); return Promise.resolve();
} }
@ -39,17 +40,15 @@ export default class FakeSuggestChangeStore implements ISuggestChangeStore {
); );
} }
getDraftForUser( getDraftsForUser(
user: User, userId: number,
project: string, project: string,
environment: string, ): Promise<ISuggestChangeset[]> {
): Promise<ISuggestChangeset> {
return Promise.resolve( return Promise.resolve(
this.suggestChanges.find( this.suggestChanges.filter(
(changeSet) => (changeSet) =>
changeSet.project === project && changeSet.project === project &&
changeSet.environment === environment && changeSet.createdBy.id === userId,
changeSet.createdBy.id === user.id,
), ),
); );
} }
@ -64,12 +63,12 @@ export default class FakeSuggestChangeStore implements ISuggestChangeStore {
create( create(
suggestChangeSet: PartialSome<ISuggestChangeset, 'id'>, suggestChangeSet: PartialSome<ISuggestChangeset, 'id'>,
user: Partial<Pick<User, 'id' | 'username' | 'email'>>, userId: number,
): Promise<ISuggestChangeset> { ): Promise<ISuggestChangeset> {
this.suggestChanges.push({ this.suggestChanges.push({
id: 1, id: 1,
...suggestChangeSet, ...suggestChangeSet,
createdBy: { id: user.id, username: user.email, imageUrl: '' }, createdBy: { id: userId, username: '', imageUrl: '' },
}); });
return Promise.resolve(undefined); return Promise.resolve(undefined);
} }