This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
calculate and compare report data during change detection
- Loading branch information
Showing
9 changed files
with
178 additions
and
40 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
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
20 changes: 20 additions & 0 deletions
20
src/report/core/use-cases/create-report-calculation-use-case.service.spec.ts
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,20 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { CreateReportCalculationUseCase } from './create-report-calculation-use-case.service'; | ||
|
||
describe('CreateReportCalculationUseCaseService', () => { | ||
let service: CreateReportCalculationUseCase; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [CreateReportCalculationUseCase], | ||
}).compile(); | ||
|
||
service = module.get<CreateReportCalculationUseCase>( | ||
CreateReportCalculationUseCase, | ||
); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
74 changes: 74 additions & 0 deletions
74
src/report/core/use-cases/create-report-calculation-use-case.service.ts
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,74 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Reference } from '../../../domain/reference'; | ||
import { filter, map, merge, Observable, take } from 'rxjs'; | ||
import { | ||
ReportCalculation, | ||
ReportCalculationStatus, | ||
} from '../../../domain/report-calculation'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { Report } from '../../../domain/report'; | ||
import { DefaultReportStorage } from '../../storage/report-storage.service'; | ||
|
||
@Injectable() | ||
export class CreateReportCalculationUseCase { | ||
constructor(private reportStorage: DefaultReportStorage) {} | ||
|
||
startReportCalculation( | ||
report: Report, | ||
): Observable<CreateReportCalculationOutcome> { | ||
const calculation = new ReportCalculation( | ||
`ReportCalculation:${uuidv4()}`, | ||
new Reference(report.id), | ||
); | ||
return this.reportStorage | ||
.storeCalculation(calculation) | ||
.pipe( | ||
map((calculation) => new CreateReportCalculationSuccess(calculation)), | ||
); | ||
} | ||
|
||
getCompletedReportCalculation( | ||
reportCalculation: Reference, | ||
): Observable<ReportCalculation> { | ||
return merge( | ||
this.reportStorage.fetchCalculation(reportCalculation).pipe( | ||
map((calc) => { | ||
if (!calc) { | ||
throw new Error('Report calculation not found'); | ||
// TODO: can this really return undefined? Looks like it would throw instead (which seems a good way to handle it to me) | ||
} | ||
return calc as ReportCalculation; | ||
}), | ||
), | ||
this.reportStorage.reportCalculationUpdated, | ||
).pipe( | ||
filter((calcUpdate) => calcUpdate?.id === reportCalculation.id), | ||
filter( | ||
(calcUpdate) => | ||
calcUpdate?.status === ReportCalculationStatus.FINISHED_SUCCESS || | ||
calcUpdate?.status === ReportCalculationStatus.FINISHED_ERROR, | ||
), | ||
take(1), | ||
); | ||
} | ||
} | ||
|
||
export type CreateReportCalculationOutcome = | ||
| CreateReportCalculationSuccess | ||
| CreateReportCalculationFailed; | ||
|
||
export class CreateReportCalculationSuccess { | ||
constructor(public result: ReportCalculation) {} | ||
} | ||
|
||
export class CreateReportCalculationFailed { | ||
constructor( | ||
public errorMessage: string, | ||
public errorCode: CreateReportCalculationError, | ||
public error?: any, | ||
) {} | ||
} | ||
|
||
export enum CreateReportCalculationError { | ||
NotImplemented, | ||
} |
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