import { AggregateRoot, AggregateID } from '@mobicoop/ddd-library'; import { CreateRouteProps, RouteProps, Route } from './route.types'; import { v4 } from 'uuid'; import { RouteNotFoundException } from './route.errors'; export class RouteEntity extends AggregateRoot { protected readonly _id: AggregateID; static create = async (create: CreateRouteProps): Promise => { const route: Route = await create.georouter.route( create.waypoints, create.georouterSettings, ); if (!route) throw new RouteNotFoundException(); const routeProps: RouteProps = { distance: route.distance, duration: route.duration, fwdAzimuth: route.fwdAzimuth, backAzimuth: route.backAzimuth, distanceAzimuth: route.distanceAzimuth, points: route.points, }; return new RouteEntity({ id: v4(), props: routeProps, }); }; validate(): void { // entity business rules validation to protect it's invariant before saving entity to a database } }