Skip to content

Commit 28ad26b

Browse files
committed
import computation done
1 parent c67493f commit 28ad26b

7 files changed

Lines changed: 359 additions & 3 deletions

File tree

src/adapters/controllers/PlanController.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
EditLongitudinalProfileParametersRequest,
2525
} from '@use-cases/plan/EditLongitudinalProfileParameters';
2626
import { ConvertComputation, ConvertComputationRequest } from '@use-cases/plan/ConvertComputation';
27+
import { ImportComputation, ImportComputationRequest } from '@use-cases/plan/ImportComputation';
2728

2829
export class PlanController {
2930
constructor(
@@ -43,6 +44,7 @@ export class PlanController {
4344
private readonly editTopoSettingUseCase: EditTopoSetting,
4445
private readonly editLongitudinalProfileParametersUseCase: EditLongitudinalProfileParameters,
4546
private readonly convertComputationUseCase: ConvertComputation,
47+
private readonly importComputationUseCase: ImportComputation,
4648
) {}
4749

4850
async createPlan(
@@ -504,4 +506,27 @@ export class PlanController {
504506
return handleError(e);
505507
}
506508
}
509+
510+
async importComputation(
511+
req: HttpRequest<ImportComputationRequest, { plan_id: string }, undefined, undefined, AuthenticateResponse>,
512+
): Promise<HttpResponse<void | Error>> {
513+
try {
514+
const error = PlanValidator.validateImportComputation(req.body);
515+
if (error) {
516+
return badRequest(error);
517+
}
518+
519+
await this.importComputationUseCase.execute({
520+
...req.body!,
521+
plan_id: req.params!.plan_id,
522+
options: {
523+
filter: { user: req.user!.id },
524+
},
525+
});
526+
527+
return noContent();
528+
} catch (e) {
529+
return handleError(e);
530+
}
531+
}
507532
}

src/adapters/validators/PlanValidator.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,19 @@ export class PlanValidator {
293293

294294
return null;
295295
}
296+
297+
static validateImportComputation(data: any): Error | null {
298+
const rules = {
299+
computation_id: 'required|string',
300+
replace: 'required|boolean',
301+
};
302+
303+
try {
304+
validator.validate(data, rules);
305+
} catch (e) {
306+
return new Error((e as Error).message);
307+
}
308+
309+
return null;
310+
}
296311
}

src/main/config/container.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import { EditTopoSetting } from '@use-cases/plan/EditTopoSetting';
5555
import { GoogleAuth } from '@use-cases/auth/GoogleAuth';
5656
import { EditLongitudinalProfileParameters } from '@use-cases/plan/EditLongitudinalProfileParameters';
5757
import { ConvertComputation } from '@use-cases/plan/ConvertComputation';
58+
import { ImportComputation } from '@use-cases/plan/ImportComputation';
5859

5960
export class Container {
6061
private instances = new Map<string, any>();
@@ -299,6 +300,24 @@ export function setupContainer(): Container {
299300
return new ConvertComputation(
300301
container.resolve<Logger>('Logger'),
301302
container.resolve<PlanRepositoryInterface>('PlanRepo'),
303+
container.resolve('ForwardComputationUseCase'),
304+
container.resolve('TraverseComputationUseCase'),
305+
container.resolve('EditCoordinateUseCase'),
306+
container.resolve('EditTopoBoundaryUseCase'),
307+
container.resolve('DifferentialLevelingUseCase'),
308+
container.resolve('EditElevationUseCase'),
309+
);
310+
});
311+
container.register('ImportComputationUseCase', () => {
312+
return new ImportComputation(
313+
container.resolve<Logger>('Logger'),
314+
container.resolve<PlanRepositoryInterface>('PlanRepo'),
315+
container.resolve('ForwardComputationUseCase'),
316+
container.resolve('TraverseComputationUseCase'),
317+
container.resolve('EditCoordinateUseCase'),
318+
container.resolve('EditTopoBoundaryUseCase'),
319+
container.resolve('DifferentialLevelingUseCase'),
320+
container.resolve('EditElevationUseCase'),
302321
);
303322
});
304323

@@ -364,6 +383,7 @@ export function setupContainer(): Container {
364383
container.resolve('EditTopoSettingUseCase'),
365384
container.resolve('EditLongitudinalProfileParametersUseCase'),
366385
container.resolve('ConvertComputationUseCase'),
386+
container.resolve('ImportComputationUseCase'),
367387
);
368388
});
369389

src/main/routes/plan-routes.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ export default (logger: Logger, authController: AuthController, planController:
4040
'/differential-leveling-data/edit/:plan_id',
4141
expressRouteAdapter(planController.editDifferentialLeveling.bind(planController)),
4242
);
43+
4344
router.get('/generate/:plan_id', expressRouteAdapter(planController.generatePlan.bind(planController)));
4445

4546
router.put(
4647
'/computation/convert/:plan_id',
4748
expressRouteAdapter(planController.convertComputation.bind(planController)),
4849
);
4950

51+
router.put('/import/:plan_id', expressRouteAdapter(planController.importComputation.bind(planController)));
52+
5053
return router;
5154
};

src/use-cases/auth/SendLoginOTP.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ export class SendLoginOTP {
4949

5050
// generate OTP
5151
const otp = await this.createOTP.execute({ identifier: user.id, type: `login_otp`, exp: 10 });
52-
console.log(otp);
5352
await this.sendOTP(user.email, 10, otp);
5453
}
5554

src/use-cases/plan/ConvertComputation.ts

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import { LongitudinalProfileParameters, PlanType, TopographicSetting } from '@do
33
import { PlanRepositoryInterface } from '@domain/interfaces/repositories/PlanRepositoryInterface';
44
import NotFoundError from '@domain/errors/NotFoundError';
55
import 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

713
export 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

Comments
 (0)