export class Reservation { constructor(private state: DurableObjectState, private env: Env) {} // POST /schedule ⇒ programa async fetch(req: Request) { const url = new URL(req.url); if (url.pathname === "/schedule") { const { className, epoch } = await req.json(); await this.state.storage.put("className", className); await this.state.setAlarm(epoch); // precisión al segundo return new Response("ok"); } if (url.pathname === "/cancel") { await this.state.storage.deleteAll(); await this.state.setAlarm(); // cancela return new Response("canceled"); } return new Response("404", { status: 404 }); } // se dispara exactamente en `epoch` async alarm() { const className = await this.state.storage.get("className"); await fetch(`${this.env.VERCEL_URL}/reserva`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ className }), }); await this.state.storage.deleteAll(); // limpia } } export default { fetch(req: Request, env: Env) { const id = env.RESERVATION.idFromName("singleton"); return env.RESERVATION.get(id).fetch(req); }, };