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:
parent
d8df7e5815
commit
2c95dfefd1
@ -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<ISuggestChangeset> => {
|
||||
): Promise<ISuggestChangeset[]> => {
|
||||
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<ISuggestChangeset> => {
|
||||
const [{ id }] = await this.db(T.SUGGEST_CHANGE_SET)
|
||||
.insert<ISuggestChangesetInsert>({
|
||||
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<ISuggestChange, 'id' | 'createdBy' | 'createdAt'>,
|
||||
changeSetID: number,
|
||||
user: User,
|
||||
userId: number,
|
||||
): Promise<void> => {
|
||||
await this.db(T.SUGGEST_CHANGE)
|
||||
.insert<ISuggestChangeInsert>({
|
||||
@ -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');
|
||||
};
|
||||
|
@ -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<User, 'id' | 'username' | 'imageUrl'>;
|
||||
createdAt?: Date;
|
||||
}
|
||||
|
@ -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<ISuggestChangeset, number> {
|
||||
create(
|
||||
@ -9,13 +8,13 @@ export interface ISuggestChangeStore extends Store<ISuggestChangeset, number> {
|
||||
ISuggestChangeset,
|
||||
'id' | 'createdBy' | 'createdAt'
|
||||
>,
|
||||
user: Partial<Pick<User, 'username' | 'email'>>,
|
||||
userId: number,
|
||||
): Promise<ISuggestChangeset>;
|
||||
|
||||
addChangeToSet(
|
||||
change: PartialSome<ISuggestChange, 'id' | 'createdBy' | 'createdAt'>,
|
||||
changeSetID: number,
|
||||
user: User,
|
||||
userId: number,
|
||||
): Promise<void>;
|
||||
|
||||
get(id: number): Promise<ISuggestChangeset>;
|
||||
@ -24,11 +23,10 @@ export interface ISuggestChangeStore extends Store<ISuggestChangeset, number> {
|
||||
|
||||
getForProject(project: string): Promise<ISuggestChangeset[]>;
|
||||
|
||||
getDraftForUser(
|
||||
user: User,
|
||||
getDraftsForUser(
|
||||
userId: number,
|
||||
project: string,
|
||||
environment: string,
|
||||
): Promise<ISuggestChangeset>;
|
||||
): Promise<ISuggestChangeset[]>;
|
||||
|
||||
getForEnvironment(environment: string): Promise<ISuggestChangeset[]>;
|
||||
}
|
||||
|
25
src/test/fixtures/fake-suggest-change-store.ts
vendored
25
src/test/fixtures/fake-suggest-change-store.ts
vendored
@ -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<ISuggestChange, 'id' | 'createdBy' | 'createdAt'>,
|
||||
changeSetID: number,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
user: User,
|
||||
userId: number,
|
||||
): Promise<void> {
|
||||
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<ISuggestChangeset> {
|
||||
): Promise<ISuggestChangeset[]> {
|
||||
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<ISuggestChangeset, 'id'>,
|
||||
user: Partial<Pick<User, 'id' | 'username' | 'email'>>,
|
||||
userId: number,
|
||||
): Promise<ISuggestChangeset> {
|
||||
this.suggestChanges.push({
|
||||
id: 1,
|
||||
...suggestChangeSet,
|
||||
createdBy: { id: user.id, username: user.email, imageUrl: '' },
|
||||
createdBy: { id: userId, username: '', imageUrl: '' },
|
||||
});
|
||||
return Promise.resolve(undefined);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user