feat: podpora simulace objednávek z Bolt Food ve vývoji

This commit is contained in:
2026-06-12 19:44:20 +02:00
parent bcec015c37
commit 34f6ba520e
13 changed files with 662 additions and 4 deletions
+5 -1
View File
@@ -51,4 +51,8 @@
# Admin heslo pro správu seznamu obchodů na stránce /objednani.
# Bez hesla nelze přidávat ani odebírat obchody ze seznamu (POST/DELETE na /api/stores vrátí 403).
# ADMIN_PASSWORD=
# ADMIN_PASSWORD=
# Interval (ms) scheduleru sledování objednávek Bolt Food. Výchozí 60000 (60 s).
# Pro vývoj se simulací lze zkrátit (min. 1000), aby se změny stavu projevily rychleji.
# BOLT_POLL_INTERVAL_MS=3000
+134
View File
@@ -0,0 +1,134 @@
import crypto from 'crypto';
/**
* Vývojový simulátor sledování objednávek Bolt Food.
*
* Drží in-memory registr „simulovaných" objednávek klíčovaný tokenem. Funkce
* pollBoltOrder v boltTracking.ts se na začátku podívá, zda je token simulovaný,
* a pokud ano, vrátí vyfabrikovaný stav místo dotazu na reálné Bolt API.
*
* Registr se plní výhradně přes dev endpointy (gated requireDevMode), takže
* v produkci zůstává prázdný a chování pollBoltOrder se nijak nemění.
*
* Postup stavů je řízen ručně (krokováním), bez časové osy — viz advance/setState.
*/
/** Jeden krok simulace — odpovídá tomu, co vrací Bolt API a co čte BoltOrderProgress. */
export interface SimStep {
order_state: string;
courier_state?: string;
etaSeconds?: number;
}
/** Tvar objednávky, který vrací pollBoltOrder (shodný s interface BoltOrder). */
export interface SimulatedBoltOrder {
order_id: number;
order_state: string;
expected_time_to_client_in_seconds?: number;
courier?: { state?: string } | null;
}
interface Simulation {
groupId: string;
token: string;
orderId: number;
steps: SimStep[];
index: number;
/** Jednorázové ruční přepsání aktuálního stavu (má přednost před steps[index]). */
override?: SimStep;
}
/**
* Výchozí scénář „happy path". Stavy a stavy kurýra odpovídají mapování ve
* client/src/components/BoltOrderProgress.tsx (Přijato → Příprava → Na cestě → Doručeno).
*/
export const DEFAULT_SCENARIO: SimStep[] = [
{ order_state: 'accepted', etaSeconds: 1800 },
{ order_state: 'preparing', etaSeconds: 1500 },
{ order_state: 'waiting_delivery', courier_state: 'arrived_to_provider', etaSeconds: 900 },
{ order_state: 'in_delivery', courier_state: 'heading_to_client', etaSeconds: 300 },
{ order_state: 'delivered', courier_state: 'delivered', etaSeconds: 0 },
];
const byToken = new Map<string, Simulation>();
function findByGroup(groupId: string): Simulation | undefined {
for (const sim of byToken.values()) {
if (sim.groupId === groupId) return sim;
}
return undefined;
}
/** Sestaví Bolt objednávku z aktuálního kroku simulace. */
function buildOrder(sim: Simulation): SimulatedBoltOrder {
const step = sim.override ?? sim.steps[sim.index];
return {
order_id: sim.orderId,
order_state: step.order_state,
expected_time_to_client_in_seconds: step.etaSeconds,
courier: step.courier_state ? { state: step.courier_state } : null,
};
}
/** Je token registrovaný v simulátoru? */
export function isBoltSimulated(token: string): boolean {
return byToken.has(token);
}
/** Vrátí simulovaný stav objednávky pro daný token (nebo null, pokud není simulovaný). */
export function getSimulatedBoltOrder(token: string): SimulatedBoltOrder | null {
const sim = byToken.get(token);
return sim ? buildOrder(sim) : null;
}
/**
* Spustí novou simulaci pro skupinu. Pokud už pro skupinu simulace běží, nahradí ji.
* Vrátí vygenerovaný 64-hex token (validní pro extractBoltToken).
*/
export function startBoltSimulation(groupId: string, steps: SimStep[] = DEFAULT_SCENARIO): string {
stopBoltSimulationByGroup(groupId);
const token = crypto.randomBytes(32).toString('hex');
byToken.set(token, {
groupId,
token,
orderId: crypto.randomInt(100000, 999999),
steps: steps.length ? steps : DEFAULT_SCENARIO,
index: 0,
});
return token;
}
/** Posune simulaci skupiny na další krok (a zruší případný ruční override). Vrátí aktuální krok. */
export function advanceBoltSimulation(groupId: string): SimStep {
const sim = findByGroup(groupId);
if (!sim) throw new Error('Pro skupinu neběží žádná simulace Bolt');
sim.override = undefined;
sim.index = Math.min(sim.index + 1, sim.steps.length - 1);
return sim.steps[sim.index];
}
/** Nastaví konkrétní stav simulace skupiny (ruční override — užitečné pro edge-case stavy). */
export function setBoltSimulationStep(groupId: string, step: SimStep): SimStep {
const sim = findByGroup(groupId);
if (!sim) throw new Error('Pro skupinu neběží žádná simulace Bolt');
sim.override = step;
return step;
}
/** Ukončí simulaci pro skupinu. */
export function stopBoltSimulationByGroup(groupId: string): void {
const sim = findByGroup(groupId);
if (sim) byToken.delete(sim.token);
}
/** Vrátí přehled simulace skupiny (pro stavové zobrazení v UI). */
export function getBoltSimulation(groupId: string): { token: string; index: number; total: number; current: SimStep } | null {
const sim = findByGroup(groupId);
if (!sim) return null;
return {
token: sim.token,
index: sim.index,
total: sim.steps.length,
current: sim.override ?? sim.steps[sim.index],
};
}
+14 -3
View File
@@ -6,6 +6,7 @@ import { getToday } from './service';
import { formatDate } from './utils';
import { getWebsocket } from './websocket';
import { ClientData, GroupState } from '../../types/gen/types.gen';
import { isBoltSimulated, getSimulatedBoltOrder } from './boltSimulator';
const storage = getStorage();
const lease = createLeaderLease('luncher:bolt:leader');
@@ -58,6 +59,11 @@ interface BoltOrder {
/** Dotáže se veřejného Bolt API na stav sdílené objednávky. Vrátí null, pokud objednávka už neexistuje. */
export async function pollBoltOrder(token: string): Promise<BoltOrder | null> {
// DEV simulace: simulované tokeny obsluhuje boltSimulator místo reálného Bolt API.
// V produkci je registr vždy prázdný, takže se sem nikdy nedostane.
if (isBoltSimulated(token)) {
return getSimulatedBoltOrder(token);
}
const res = await axios.post(BOLT_POLLING_URL, { token }, {
params: {
version: 'FW.1.111',
@@ -158,10 +164,15 @@ export async function checkBoltTracking(): Promise<void> {
}
}
/** Spustí scheduler pro sledování Bolt objednávek (každou minutu). */
/**
* Spustí scheduler pro sledování Bolt objednávek. Interval je 60 s, lze ho ale
* zkrátit přes env BOLT_POLL_INTERVAL_MS (užitečné při vývoji se simulací).
*/
export function startBoltTrackingScheduler(): void {
boltInterval = setInterval(checkBoltTracking, 60_000);
console.log('Bolt tracking: scheduler spuštěn');
const parsed = Number(process.env.BOLT_POLL_INTERVAL_MS);
const intervalMs = Number.isFinite(parsed) && parsed >= 1000 ? parsed : 60_000;
boltInterval = setInterval(checkBoltTracking, intervalMs);
console.log(`Bolt tracking: scheduler spuštěn (interval ${intervalMs} ms)`);
}
/** Stopne scheduler sledování. Volá se při graceful shutdown. */
+80
View File
@@ -6,6 +6,12 @@ import { getWebsocket } from "../websocket";
import { getLogin } from "../auth";
import { parseToken } from "../utils";
import webpush from 'web-push';
import { ClientData, GroupState } from "../../../types/gen/types.gen";
import {
startBoltSimulation, advanceBoltSimulation, setBoltSimulationStep,
stopBoltSimulationByGroup, getBoltSimulation,
} from "../boltSimulator";
import { checkBoltTracking } from "../boltTracking";
const router = express.Router();
const storage = getStorage();
@@ -195,4 +201,78 @@ router.post("/testPush", async (req, res, next) => {
} catch (e: any) { next(e) }
});
// --- DEV simulace sledování Bolt Food ---
/** Nastaví/zruší sledovací token skupiny a zajistí stav ORDERED. Vrátí aktualizovaná data. */
async function applyBoltToken(groupId: string, token: string | undefined): Promise<ClientData> {
const key = `${formatDate(getToday())}_extra`;
return storage.updateData<ClientData>(key, current => {
const d = current;
const group = d?.groups?.find(g => g.id === groupId);
if (!group) throw new Error('Skupina nebyla nalezena');
group.boltTrackingToken = token;
if (token) {
group.state = GroupState.ORDERED;
} else {
group.boltOrderState = undefined;
group.boltCourierState = undefined;
}
return d!;
});
}
/** Spustí simulaci sledování Bolt pro skupinu a provede první poll. */
router.post("/bolt/simulate", async (req: Request<{}, any, any>, res, next) => {
try {
const groupId = req.body?.groupId;
if (!groupId) return res.status(400).json({ error: 'Chybí groupId' });
const token = startBoltSimulation(groupId);
await applyBoltToken(groupId, token);
await checkBoltTracking(); // okamžitý první poll → stav "accepted" + websocket
res.status(200).json({ success: true, token, simulation: getBoltSimulation(groupId) });
} catch (e: any) { next(e); }
});
/** Posune simulaci na další krok a přepošle aktualizovaný stav klientům. */
router.post("/bolt/advance", async (req: Request<{}, any, any>, res, next) => {
try {
const groupId = req.body?.groupId;
if (!groupId) return res.status(400).json({ error: 'Chybí groupId' });
advanceBoltSimulation(groupId);
await checkBoltTracking();
res.status(200).json({ success: true, simulation: getBoltSimulation(groupId) });
} catch (e: any) { next(e); }
});
/** Nastaví konkrétní stav simulace (ruční override, např. pro stav "cancelled"). */
router.post("/bolt/state", async (req: Request<{}, any, any>, res, next) => {
try {
const { groupId, order_state, courier_state, etaSeconds } = req.body ?? {};
if (!groupId || !order_state) return res.status(400).json({ error: 'Chybí groupId nebo order_state' });
setBoltSimulationStep(groupId, { order_state, courier_state, etaSeconds });
await checkBoltTracking();
res.status(200).json({ success: true, simulation: getBoltSimulation(groupId) });
} catch (e: any) { next(e); }
});
/** Spustí jeden tik scheduleru okamžitě (bez čekání na interval). */
router.post("/bolt/poll", async (_req, res, next) => {
try {
await checkBoltTracking();
res.status(200).json({ success: true });
} catch (e: any) { next(e); }
});
/** Ukončí simulaci skupiny a odebere sledovací token. */
router.delete("/bolt/simulate", async (req: Request<{}, any, any>, res, next) => {
try {
const groupId = req.body?.groupId;
if (!groupId) return res.status(400).json({ error: 'Chybí groupId' });
stopBoltSimulationByGroup(groupId);
const updated = await applyBoltToken(groupId, undefined);
getWebsocket()?.emit('message', updated);
res.status(200).json({ success: true });
} catch (e: any) { next(e); }
});
export default router;
+58
View File
@@ -4,6 +4,7 @@ import getStorage from '../storage';
import { addStore } from '../stores';
import { createGroup, setGroupState, setGroupBoltTracking } from '../groups';
import { extractBoltToken, computeDeliveryHHMM, checkBoltTracking } from '../boltTracking';
import { startBoltSimulation, advanceBoltSimulation, setBoltSimulationStep, stopBoltSimulationByGroup } from '../boltSimulator';
import { ClientData, GroupState } from '../../../types/gen/types.gen';
import { formatDate } from '../utils';
@@ -263,3 +264,60 @@ describe('checkBoltTracking', () => {
expect(mockedAxios.post).not.toHaveBeenCalled();
});
});
describe('DEV simulace (boltSimulator + checkBoltTracking)', () => {
const extraKey = () => `${formatDate(new Date())}_extra`;
let groupId: string;
beforeEach(async () => {
const d = await createGroup(CREATOR, STORE);
groupId = d.groups![0].id;
await setGroupState(CREATOR, groupId, GroupState.LOCKED);
await setGroupState(CREATOR, groupId, GroupState.ORDERED);
// Simulátor vygeneruje validní 64-hex token a přiřadíme ho skupině jako reálný dev endpoint
const token = startBoltSimulation(groupId);
await setGroupBoltTracking(CREATOR, groupId, `https://food.bolt.eu/sharedActiveOrder/${token}`);
});
afterEach(() => stopBoltSimulationByGroup(groupId));
async function getGroup() {
const data = await storage.getData<ClientData>(extraKey());
return data!.groups!.find(g => g.id === groupId)!;
}
test('simulovaný token nevolá reálné Bolt API', async () => {
await checkBoltTracking();
expect(mockedAxios.post).not.toHaveBeenCalled();
});
test('první poll nastaví stav accepted a ETA', async () => {
await checkBoltTracking();
const g = await getGroup();
expect(g.boltOrderState).toBe('accepted');
expect(g.deliveryAt).toBe(computeDeliveryHHMM(1800));
});
test('advance posune sekvenci na preparing', async () => {
await checkBoltTracking();
advanceBoltSimulation(groupId);
await checkBoltTracking();
expect((await getGroup()).boltOrderState).toBe('preparing');
});
test('ruční nastavení stavu (override) se projeví při pollu', async () => {
setBoltSimulationStep(groupId, { order_state: 'in_delivery', courier_state: 'heading_to_client', etaSeconds: 300 });
await checkBoltTracking();
const g = await getGroup();
expect(g.boltOrderState).toBe('in_delivery');
expect(g.boltCourierState).toBe('heading_to_client');
});
test('terminální stav delivered ukončí sledování (smaže token)', async () => {
setBoltSimulationStep(groupId, { order_state: 'delivered' });
await checkBoltTracking();
const g = await getGroup();
expect(g.boltOrderState).toBe('delivered');
expect(g.boltTrackingToken).toBeUndefined();
});
});