@@ -3,6 +3,12 @@ import { LongitudinalProfileParameters, PlanType, TopographicSetting } from '@do
33import { PlanRepositoryInterface } from '@domain/interfaces/repositories/PlanRepositoryInterface' ;
44import NotFoundError from '@domain/errors/NotFoundError' ;
55import BadRequestError from '@domain/errors/BadRequestError' ;
6+ import { ForwardComputation } from '@use-cases/traversing/ForwardComputation' ;
7+ import { EditCoordinates } from '@use-cases/plan/EditCoordinates' ;
8+ import { EditTopoBoundary } from '@use-cases/plan/EditTopoBoundary' ;
9+ import { TraverseComputation } from '@use-cases/traversing/TraverseComputation' ;
10+ import { DifferentialLeveling } from '@use-cases/leveling/DifferentialLeveling' ;
11+ import { EditElevation } from '@use-cases/plan/EditElevation' ;
612
713export interface ConvertComputationRequest {
814 plan_id : string ;
@@ -14,13 +20,19 @@ export class ConvertComputation {
1420 constructor (
1521 private readonly logger : Logger ,
1622 private readonly planRepo : PlanRepositoryInterface ,
23+ private readonly forwardComputationUseCase : ForwardComputation ,
24+ private readonly traverseComputationUseCase : TraverseComputation ,
25+ private readonly editCoordinatesUseCase : EditCoordinates ,
26+ private readonly editTopoBoundaryUseCase : EditTopoBoundary ,
27+ private readonly differentialLevelingUseCase : DifferentialLeveling ,
28+ private readonly editElevationUseCase : EditElevation ,
1729 ) { }
1830
1931 async execute ( data : ConvertComputationRequest ) : Promise < void > {
2032 this . logger . debug ( 'Convert Computation execute' ) ;
2133
2234 // get plan
23- const plan = await this . planRepo . getPlanById ( data . plan_id , data . options ) ;
35+ let plan = await this . planRepo . getPlanById ( data . plan_id , data . options ) ;
2436 if ( ! plan ) {
2537 throw new NotFoundError ( 'Plan not found' ) ;
2638 }
@@ -61,11 +73,95 @@ export class ConvertComputation {
6173 throw new BadRequestError ( 'Only Computations can be converted to a plan' ) ;
6274 }
6375
64- await this . planRepo . editPlan ( plan . id , {
76+ plan = await this . planRepo . editPlan ( plan . id , {
6577 computation_only : false ,
6678 type : data . type ,
6779 longitudinal_profile_parameters : longitudinalProfileParameters ,
6880 topographic_setting : topographicSetting ,
6981 } ) ;
82+
83+ if ( ! plan ) {
84+ throw new NotFoundError ( 'Plan not found' ) ;
85+ }
86+
87+ // add coordinates
88+ if ( plan . forward_computation_data ) {
89+ const forwardComputationResult = this . forwardComputationUseCase . execute ( plan . forward_computation_data ) ;
90+
91+ const coordinates = [ forwardComputationResult . start ] ;
92+ for ( let i = 0 ; i < forwardComputationResult . computed_legs . length ; i ++ ) {
93+ coordinates . push ( forwardComputationResult . computed_legs [ i ] . to ) ;
94+ coordinates . push ( forwardComputationResult . computed_legs [ i ] . from ) ;
95+ }
96+
97+ // depending on the type of plan
98+ if ( plan . type === PlanType . CADASTRAL || plan . type === PlanType . LAYOUT ) {
99+ // update coordinates
100+ await this . editCoordinatesUseCase . execute ( {
101+ plan_id : plan . id ,
102+ coordinates : coordinates ,
103+ } ) ;
104+ }
105+
106+ if ( plan . type === PlanType . TOPOGRAPHIC ) {
107+ // update topo boundaries
108+ await this . editTopoBoundaryUseCase . execute ( {
109+ plan_id : plan . id ,
110+ boundary : {
111+ coordinates : coordinates ,
112+ } ,
113+ } ) ;
114+ }
115+ }
116+
117+ if ( plan . traverse_computation_data ) {
118+ const traverseComputationResult = this . traverseComputationUseCase . execute ( plan . traverse_computation_data ) ;
119+
120+ const coordinates = [ ] ;
121+ for ( let i = 0 ; i < traverseComputationResult . traverse_legs . length ; i ++ ) {
122+ coordinates . push ( traverseComputationResult . traverse_legs [ i ] . to ) ;
123+ coordinates . push ( traverseComputationResult . traverse_legs [ i ] . from ) ;
124+ }
125+
126+ // depending on the type of plan
127+ if ( plan . type === PlanType . CADASTRAL || plan . type === PlanType . LAYOUT ) {
128+ // update coordinates
129+ await this . editCoordinatesUseCase . execute ( {
130+ plan_id : plan . id ,
131+ coordinates : coordinates ,
132+ } ) ;
133+ }
134+
135+ if ( plan . type === PlanType . TOPOGRAPHIC ) {
136+ // update topo boundaries
137+ await this . editTopoBoundaryUseCase . execute ( {
138+ plan_id : plan . id ,
139+ boundary : {
140+ coordinates : coordinates ,
141+ } ,
142+ } ) ;
143+ }
144+ }
145+
146+ if ( plan . differential_leveling_data ) {
147+ const differentialLevelResult = this . differentialLevelingUseCase . execute ( plan . differential_leveling_data ) ;
148+
149+ const elevations = [ ] ;
150+ for ( let i = 0 ; i < differentialLevelResult . stations . length ; i ++ ) {
151+ elevations . push ( {
152+ id : differentialLevelResult . stations [ i ] . stn as string ,
153+ chainage : differentialLevelResult . stations [ i ] . chainage as string ,
154+ elevation : differentialLevelResult . stations [ i ] . reduced_level as number ,
155+ } ) ;
156+ }
157+
158+ if ( plan . type === PlanType . ROUTE ) {
159+ // update elevations
160+ await this . editElevationUseCase . execute ( {
161+ plan_id : plan . id ,
162+ elevations : elevations ,
163+ } ) ;
164+ }
165+ }
70166 }
71167}
0 commit comments