1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

Suggest changes updates (#2273)

* Make domain type and schema match

* Deleting change from changeset

* Add ability to merge
This commit is contained in:
sjaanus 2022-10-27 14:47:46 +02:00 committed by GitHub
parent e1883caf0c
commit dda1f19c70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 62 additions and 32 deletions

View File

@ -15,29 +15,29 @@ const T = {
SUGGEST_CHANGE_SET: 'suggest_change_set',
};
interface ISuggestChangesetInsert {
interface ISuggestChangesetRow {
id: number;
state: SuggestChangesetState;
environment: string;
state?: string;
project?: string;
created_by?: number;
created_at?: Date;
project: string;
created_at: Date;
created_by: number;
changeSetUsername: string;
changeSetAvatar: string;
changeId: number;
changeFeature: string;
changeAction: SuggestChangeAction;
changePayload: any;
changeCreatedAt: Date;
changeCreatedBy: number;
changeCreatedByUsername: string;
changeCreatedByAvatar: string;
}
interface ISuggestChangeInsert {
id: number;
action: SuggestChangeAction;
feature: string;
payload?: unknown;
created_by?: number;
created_at?: Date;
}
interface ISuggestChangesetRow extends ISuggestChangesetInsert {
changes?: ISuggestChange[];
}
const suggestChangeRowReducer = (acc, suggestChangeRow) => {
const suggestChangeRowReducer = (
acc: Record<number, ISuggestChangeset>,
suggestChangeRow: ISuggestChangesetRow,
): Record<number, ISuggestChangeset> => {
const {
changeId,
changeAction,
@ -199,7 +199,7 @@ export class SuggestChangeStore implements ISuggestChangeStore {
userId: number,
): Promise<ISuggestChangeset> => {
const [{ id }] = await this.db(T.SUGGEST_CHANGE_SET)
.insert<ISuggestChangesetInsert>({
.insert({
environment: suggestChangeSet.environment,
state: suggestChangeSet.state,
project: suggestChangeSet.project,
@ -207,26 +207,31 @@ export class SuggestChangeStore implements ISuggestChangeStore {
})
.returning('id');
suggestChangeSet.changes.forEach((change) => {
this.addChangeToSet(change, id, userId);
suggestChangeSet.features.forEach((feature) => {
feature.changes.forEach((change) => {
this.addChangeToSet(change, feature.name, id, userId);
});
});
return this.get(id);
};
addChangeToSet = async (
change: PartialSome<ISuggestChange, 'id' | 'createdBy' | 'createdAt'>,
change: ISuggestChange,
feature: string,
changeSetID: number,
userId: number,
): Promise<void> => {
await this.db(T.SUGGEST_CHANGE)
.insert<ISuggestChangeInsert>({
.insert({
action: change.action,
feature: change.feature,
feature: feature,
payload: change.payload,
suggest_change_set_id: changeSetID,
created_by: userId,
})
.onConflict(['action', 'suggest_change_set_id', 'feature'])
.merge()
.returning('id');
};
@ -234,6 +239,10 @@ export class SuggestChangeStore implements ISuggestChangeStore {
return this.db(T.SUGGEST_CHANGE_SET).where({ id }).del();
};
deleteChange = (id: number): Promise<void> => {
return this.db(T.SUGGEST_CHANGE).where({ id }).del();
};
deleteAll = (): Promise<void> => {
return this.db(T.SUGGEST_CHANGE_SET).del();
};
@ -247,8 +256,10 @@ export class SuggestChangeStore implements ISuggestChangeStore {
return result.rows[0].present;
};
mapRows = (rows?: any[]): ISuggestChangeset[] => {
const suggestChangeSets = rows.reduce(suggestChangeRowReducer, {});
mapRows = (rows?: ISuggestChangesetRow[]): ISuggestChangeset[] => {
const suggestChangeSets = rows.reduce<
Record<number, ISuggestChangeset>
>(suggestChangeRowReducer, {});
return Object.values(suggestChangeSets);
};

View File

@ -373,13 +373,17 @@ export interface ISuggestChangeset {
environment: string;
createdBy: Pick<User, 'id' | 'username' | 'imageUrl'>;
createdAt: Date;
features: ISuggestChangeFeature[];
}
export interface ISuggestChangeFeature {
name: string;
changes: ISuggestChange[];
}
export interface ISuggestChange {
id?: number;
action: SuggestChangeAction;
feature: string;
payload: any;
createdBy?: Pick<User, 'id' | 'username' | 'imageUrl'>;
createdAt?: Date;

View File

@ -17,12 +17,15 @@ export interface ISuggestChangeStore extends Store<ISuggestChangeset, number> {
addChangeToSet(
change: PartialSome<ISuggestChange, 'id' | 'createdBy' | 'createdAt'>,
feature: string,
changeSetID: number,
userId: number,
): Promise<void>;
get(id: number): Promise<ISuggestChangeset>;
deleteChange(changeId: number): Promise<void>;
updateState(
id: number,
state: SuggestChangesetState,

View File

@ -19,7 +19,8 @@ CREATE TABLE IF NOT EXISTS suggest_change (
payload jsonb not null default '[]'::jsonb,
created_by integer not null references users (id) ON DELETE CASCADE,
created_at timestamp default now(),
suggest_change_set_id integer NOT NULL REFERENCES suggest_change_set(id) ON DELETE CASCADE
suggest_change_set_id integer NOT NULL REFERENCES suggest_change_set(id) ON DELETE CASCADE,
UNIQUE (feature, action, suggest_change_set_id)
);
`,
callback,

View File

@ -23,15 +23,26 @@ export default class FakeSuggestChangeStore implements ISuggestChangeStore {
return Promise.resolve(undefined);
}
// eslint-disable-next-line no-unused-vars,@typescript-eslint/no-unused-vars
async deleteChange(id: number): Promise<void> {
return Promise.resolve(undefined);
}
addChangeToSet(
change: PartialSome<ISuggestChange, 'id' | 'createdBy' | 'createdAt'>,
feature: string,
changeSetID: number,
userId: number,
): Promise<void> {
const changeSet = this.suggestChanges.find((s) => s.id === changeSetID);
changeSet.changes.push({
createdBy: { id: userId, username: '', imageUrl: '' },
...change,
changeSet.features.push({
name: feature,
changes: [
{
createdBy: { id: userId, username: '', imageUrl: '' },
...change,
},
],
});
return Promise.resolve();
}