Skip to content

Commit

Permalink
Suggest changes updates (#2273)
Browse files Browse the repository at this point in the history
* Make domain type and schema match

* Deleting change from changeset

* Add ability to merge
  • Loading branch information
sjaanus committed Oct 27, 2022
1 parent e1883ca commit dda1f19
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 32 deletions.
65 changes: 38 additions & 27 deletions src/lib/db/suggest-change-store.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -199,41 +199,50 @@ 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,
created_by: userId,
})
.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');
};

delete = (id: number): Promise<void> => {
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();
};
Expand All @@ -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);
};

Expand Down
6 changes: 5 additions & 1 deletion src/lib/types/model.ts
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/lib/types/stores/suggest-change-store.ts
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/migrations/20221810114644-add-suggest-changes-table.js
Expand Up @@ -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,
Expand Down
17 changes: 14 additions & 3 deletions src/test/fixtures/fake-suggest-change-store.ts
Expand Up @@ -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();
}
Expand Down

0 comments on commit dda1f19

Please sign in to comment.