feat(GRO-2156): travel buffer + reorder endpoint (Phase 2.2) (#180)
CI / Test (push) Successful in 27s
CI / Lint & Typecheck (push) Successful in 30s
CI / Lint & Typecheck (pull_request) Successful in 25s
CI / Test (pull_request) Successful in 24s
CI / Build & Push Docker Images (push) Successful in 43s
CI / Build & Push Docker Images (pull_request) Successful in 27s
CI / Test (push) Successful in 27s
CI / Lint & Typecheck (push) Successful in 30s
CI / Lint & Typecheck (pull_request) Successful in 25s
CI / Test (pull_request) Successful in 24s
CI / Build & Push Docker Images (push) Successful in 43s
CI / Build & Push Docker Images (pull_request) Successful in 27s
This commit was merged in pull request #180.
This commit is contained in:
+181
-3
@@ -19,7 +19,10 @@ import type { AppEnv, StaffRow } from "../middleware/rbac.js";
|
||||
import {
|
||||
optimizeRoute,
|
||||
resolveRouteGoogleApiKey,
|
||||
detectScheduleConflicts,
|
||||
recomputeLegsForOrder,
|
||||
type RouteStopInput,
|
||||
type StopConflictFlags,
|
||||
} from "../services/routeOptimization.js";
|
||||
|
||||
export const routesRouter = new Hono<AppEnv>();
|
||||
@@ -34,6 +37,11 @@ const optimizeBodySchema = z.object({
|
||||
date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, "date must be YYYY-MM-DD"),
|
||||
});
|
||||
|
||||
const reorderBodySchema = z.object({
|
||||
// New visiting order expressed as routeStops.id values, first-to-last.
|
||||
stopOrder: z.array(z.string().uuid()).min(1),
|
||||
});
|
||||
|
||||
/**
|
||||
* Resolves the target staffId for the request and enforces the groomer-own /
|
||||
* manager authorization rule. Groomers may only act on their own route; if a
|
||||
@@ -96,6 +104,31 @@ async function loadRouteStops(db: ReturnType<typeof getDb>, routeId: string) {
|
||||
.orderBy(asc(routeStops.stopOrder));
|
||||
}
|
||||
|
||||
type LoadedRouteStop = Awaited<ReturnType<typeof loadRouteStops>>[number];
|
||||
|
||||
/**
|
||||
* Annotates persisted stops with "tight schedule" conflict flags for the
|
||||
* frontend. Conflicts are derived at read time from the live appointment times,
|
||||
* persisted travel estimates and buffers — never auto-resolved by moving stops.
|
||||
*/
|
||||
function annotateConflicts(stops: LoadedRouteStop[]): {
|
||||
stops: Array<LoadedRouteStop & { conflict: StopConflictFlags }>;
|
||||
hasConflicts: boolean;
|
||||
conflictCount: number;
|
||||
} {
|
||||
const flags = detectScheduleConflicts(
|
||||
stops.map((s) => ({
|
||||
appointmentStartTime: s.appointmentStartTime,
|
||||
appointmentEndTime: s.appointmentEndTime,
|
||||
travelMinsFromPrev: s.travelMinsFromPrev,
|
||||
bufferMins: s.bufferMins,
|
||||
}))
|
||||
);
|
||||
const annotated = stops.map((s, i) => ({ ...s, conflict: flags[i]! }));
|
||||
const conflictCount = flags.filter((f) => f.hasConflict).length;
|
||||
return { stops: annotated, hasConflicts: conflictCount > 0, conflictCount };
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/routes/daily?staffId=&date=
|
||||
* Fetches (creating a draft if absent) the daily route for a groomer, with all
|
||||
@@ -130,7 +163,13 @@ routesRouter.get("/daily", zValidator("query", dailyQuerySchema), async (c) => {
|
||||
}
|
||||
|
||||
const stops = await loadRouteStops(db, route!.id);
|
||||
return c.json({ route, stops });
|
||||
const annotated = annotateConflicts(stops);
|
||||
return c.json({
|
||||
route,
|
||||
stops: annotated.stops,
|
||||
hasConflicts: annotated.hasConflicts,
|
||||
conflictCount: annotated.conflictCount,
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -262,7 +301,9 @@ routesRouter.post(
|
||||
s.travelDistanceKmFromPrev == null
|
||||
? null
|
||||
: s.travelDistanceKmFromPrev.toFixed(2),
|
||||
bufferMins,
|
||||
// Buffer applies between consecutive stops; the first stop has no
|
||||
// predecessor, so it carries no travel buffer.
|
||||
bufferMins: i === 0 ? 0 : bufferMins,
|
||||
}))
|
||||
);
|
||||
}
|
||||
@@ -271,9 +312,12 @@ routesRouter.post(
|
||||
});
|
||||
|
||||
const stops = await loadRouteStops(db, route.id);
|
||||
const annotated = annotateConflicts(stops);
|
||||
return c.json({
|
||||
route,
|
||||
stops,
|
||||
stops: annotated.stops,
|
||||
hasConflicts: annotated.hasConflicts,
|
||||
conflictCount: annotated.conflictCount,
|
||||
provider: optimized.provider,
|
||||
chunked: optimized.chunked,
|
||||
subRouteCount: optimized.subRouteCount,
|
||||
@@ -282,3 +326,137 @@ routesRouter.post(
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* PATCH /api/routes/:routeId/reorder { stopOrder: string[] }
|
||||
* Persists a manual stop order (array of routeStops.id, first-to-last), then
|
||||
* re-runs the buffer logic: each leg's travel is re-estimated for the new
|
||||
* adjacency, the default travel buffer is re-applied between consecutive stops,
|
||||
* route totals are recomputed, and tight-schedule conflicts are re-flagged.
|
||||
* Appointments are never moved. Auth: groomer (own route) or manager.
|
||||
*/
|
||||
routesRouter.patch(
|
||||
"/:routeId/reorder",
|
||||
zValidator("json", reorderBodySchema),
|
||||
async (c) => {
|
||||
const db = getDb();
|
||||
const routeId = c.req.param("routeId");
|
||||
if (!z.string().uuid().safeParse(routeId).success) {
|
||||
return c.json({ error: "routeId must be a UUID" }, 400);
|
||||
}
|
||||
const { stopOrder: newOrderIds } = c.req.valid("json");
|
||||
|
||||
const [route] = await db
|
||||
.select()
|
||||
.from(groomerRoutes)
|
||||
.where(eq(groomerRoutes.id, routeId));
|
||||
if (!route) {
|
||||
return c.json({ error: "Route not found" }, 404);
|
||||
}
|
||||
|
||||
// Reuse the groomer-own / manager authorization rule against the route owner.
|
||||
const resolved = resolveTargetStaffId(c.get("staff"), route.staffId);
|
||||
if ("error" in resolved) {
|
||||
return c.json({ error: resolved.error }, resolved.status);
|
||||
}
|
||||
|
||||
const existing = await db
|
||||
.select({
|
||||
id: routeStops.id,
|
||||
latitude: routeStops.latitude,
|
||||
longitude: routeStops.longitude,
|
||||
})
|
||||
.from(routeStops)
|
||||
.where(eq(routeStops.routeId, routeId));
|
||||
|
||||
// The new order must be an exact permutation of the route's current stops.
|
||||
const existingIds = new Set(existing.map((s) => s.id));
|
||||
if (newOrderIds.length !== existing.length) {
|
||||
return c.json(
|
||||
{
|
||||
error: `stopOrder must list every stop exactly once (expected ${existing.length}, got ${newOrderIds.length})`,
|
||||
},
|
||||
400
|
||||
);
|
||||
}
|
||||
const seen = new Set<string>();
|
||||
for (const id of newOrderIds) {
|
||||
if (!existingIds.has(id)) {
|
||||
return c.json({ error: `unknown stop id: ${id}` }, 400);
|
||||
}
|
||||
if (seen.has(id)) {
|
||||
return c.json({ error: `duplicate stop id: ${id}` }, 400);
|
||||
}
|
||||
seen.add(id);
|
||||
}
|
||||
|
||||
const [settings] = await db.select().from(businessSettings).limit(1);
|
||||
const bufferMins = settings?.defaultTravelBufferMins ?? 15;
|
||||
|
||||
const byId = new Map(existing.map((s) => [s.id, s]));
|
||||
const legs = recomputeLegsForOrder(
|
||||
newOrderIds.map((id) => {
|
||||
const s = byId.get(id)!;
|
||||
return { latitude: s.latitude, longitude: s.longitude };
|
||||
})
|
||||
);
|
||||
|
||||
const totalTravelMins = legs.reduce(
|
||||
(sum, l) => sum + (l.travelMinsFromPrev ?? 0),
|
||||
0
|
||||
);
|
||||
const totalDistanceKm =
|
||||
Math.round(
|
||||
legs.reduce((sum, l) => sum + (l.travelDistanceKmFromPrev ?? 0), 0) * 100
|
||||
) / 100;
|
||||
|
||||
const now = new Date();
|
||||
await db.transaction(async (tx) => {
|
||||
// Two-pass update: park stopOrder in a non-colliding negative range first
|
||||
// so the unique(routeId, stopOrder) constraint never trips mid-reorder.
|
||||
for (let i = 0; i < newOrderIds.length; i++) {
|
||||
await tx
|
||||
.update(routeStops)
|
||||
.set({ stopOrder: -(i + 1), updatedAt: now })
|
||||
.where(eq(routeStops.id, newOrderIds[i]!));
|
||||
}
|
||||
for (let i = 0; i < newOrderIds.length; i++) {
|
||||
const leg = legs[i]!;
|
||||
await tx
|
||||
.update(routeStops)
|
||||
.set({
|
||||
stopOrder: i + 1,
|
||||
travelMinsFromPrev: leg.travelMinsFromPrev,
|
||||
travelDistanceKmFromPrev:
|
||||
leg.travelDistanceKmFromPrev == null
|
||||
? null
|
||||
: leg.travelDistanceKmFromPrev.toFixed(2),
|
||||
bufferMins: i === 0 ? 0 : bufferMins,
|
||||
updatedAt: now,
|
||||
})
|
||||
.where(eq(routeStops.id, newOrderIds[i]!));
|
||||
}
|
||||
await tx
|
||||
.update(groomerRoutes)
|
||||
.set({
|
||||
totalTravelMins,
|
||||
totalDistanceKm: totalDistanceKm.toFixed(2),
|
||||
updatedAt: now,
|
||||
})
|
||||
.where(eq(groomerRoutes.id, routeId));
|
||||
});
|
||||
|
||||
const [updatedRoute] = await db
|
||||
.select()
|
||||
.from(groomerRoutes)
|
||||
.where(eq(groomerRoutes.id, routeId));
|
||||
const stops = await loadRouteStops(db, routeId);
|
||||
const annotated = annotateConflicts(stops);
|
||||
return c.json({
|
||||
route: updatedRoute,
|
||||
stops: annotated.stops,
|
||||
hasConflicts: annotated.hasConflicts,
|
||||
conflictCount: annotated.conflictCount,
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user