-
-
Notifications
You must be signed in to change notification settings - Fork 721
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: segment read model used in export-import (#6418)
- Loading branch information
Showing
5 changed files
with
110 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { IFeatureStrategySegment, ISegment } from '../../types'; | ||
import { ISegmentReadModel } from './segment-read-model-type'; | ||
|
||
export class FakeSegmentReadModel implements ISegmentReadModel { | ||
async getAll(): Promise<ISegment[]> { | ||
return []; | ||
} | ||
|
||
async getAllFeatureStrategySegments(): Promise<IFeatureStrategySegment[]> { | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { IFeatureStrategySegment, ISegment } from '../../types'; | ||
|
||
export interface ISegmentReadModel { | ||
getAll(): Promise<ISegment[]>; | ||
getAllFeatureStrategySegments(): Promise<IFeatureStrategySegment[]>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { IConstraint, IFeatureStrategySegment, ISegment } from '../../types'; | ||
import { ISegmentReadModel } from './segment-read-model-type'; | ||
import NotFoundError from '../../error/notfound-error'; | ||
import { Db } from '../../db/db'; | ||
|
||
interface ISegmentRow { | ||
id: number; | ||
name: string; | ||
description?: string; | ||
segment_project_id?: string; | ||
created_by?: string; | ||
created_at: Date; | ||
constraints: IConstraint[]; | ||
} | ||
|
||
const COLUMNS = [ | ||
'id', | ||
'name', | ||
'description', | ||
'segment_project_id', | ||
'created_by', | ||
'created_at', | ||
'constraints', | ||
]; | ||
|
||
interface IFeatureStrategySegmentRow { | ||
feature_strategy_id: string; | ||
segment_id: number; | ||
created_at?: Date; | ||
} | ||
|
||
export class SegmentReadModel implements ISegmentReadModel { | ||
private db: Db; | ||
|
||
constructor(db: Db) { | ||
this.db = db; | ||
} | ||
|
||
prefixColumns(): string[] { | ||
return COLUMNS.map((c) => `segments.${c}`); | ||
} | ||
|
||
mapRow(row?: ISegmentRow): ISegment { | ||
if (!row) { | ||
throw new NotFoundError('No row'); | ||
} | ||
|
||
return { | ||
id: row.id, | ||
name: row.name, | ||
description: row.description, | ||
project: row.segment_project_id || undefined, | ||
constraints: row.constraints, | ||
createdBy: row.created_by, | ||
createdAt: row.created_at, | ||
}; | ||
} | ||
|
||
async getAll(): Promise<ISegment[]> { | ||
const rows: ISegmentRow[] = await this.db | ||
.select(this.prefixColumns()) | ||
.from('segments') | ||
.orderBy('segments.name', 'asc'); | ||
|
||
return rows.map(this.mapRow); | ||
} | ||
|
||
async getAllFeatureStrategySegments(): Promise<IFeatureStrategySegment[]> { | ||
const rows: IFeatureStrategySegmentRow[] = await this.db | ||
.select(['segment_id', 'feature_strategy_id']) | ||
.from('feature_strategy_segment'); | ||
|
||
return rows.map((row) => ({ | ||
featureStrategyId: row.feature_strategy_id, | ||
segmentId: row.segment_id, | ||
})); | ||
} | ||
} |