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

Make domain type and schema match

This commit is contained in:
sjaanus 2022-10-27 13:26:21 +03:00
parent 922ec7126f
commit 079f46c0db
No known key found for this signature in database
GPG Key ID: 20E007C0248BA7FF
4 changed files with 47 additions and 31 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,22 +207,25 @@ 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,
@ -247,8 +250,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,6 +17,7 @@ export interface ISuggestChangeStore extends Store<ISuggestChangeset, number> {
addChangeToSet(
change: PartialSome<ISuggestChange, 'id' | 'createdBy' | 'createdAt'>,
feature: string,
changeSetID: number,
userId: number,
): Promise<void>;

View File

@ -25,13 +25,19 @@ export default class FakeSuggestChangeStore implements ISuggestChangeStore {
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();
}