mega-plant/lib/clock-api.ts
David 29a035a4f2 Clock app: vehicle-QR diesel book-out with meter photo; user access levels
- Scanning a vehicle QR (its register code, e.g. TLB04) books diesel from the main
  tank; litres, optional odometer and a required meter photo (stored protected,
  viewable from the Booked out table)
- Settings: users are Full or Clock-app-only; clock-only users are confined to /clock
- Clock app: tapped tab highlights at once plus a loading screen; remove the
  "scanned on this device" list

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-22 12:25:15 +02:00

22 lines
1.4 KiB
TypeScript

import 'server-only';
import { getSession } from './auth';
import { ClockError } from './clock';
import { clockOriginAllowed } from './clock-origin';
export async function clockAccess(request:Request,options:{multipart?:boolean}={}) {
const session=await getSession();
if(!session)return Response.json({error:'Please sign in.'},{status:401});
if(!session.passwordChanged)return Response.json({error:'Change your starter password first.'},{status:403});
if(request.method==='POST') {
if(!clockOriginAllowed(request.headers))return Response.json({error:'Request origin is not allowed.'},{status:403});
const type=request.headers.get('content-type')||'';
if(!type.startsWith('application/json') && !(options.multipart && type.startsWith('multipart/form-data')))return Response.json({error:'Supply JSON.'},{status:415});
}
return session;
}
export function clockFailure(error:unknown) {
if(error instanceof ClockError)return Response.json({error:error.message},{status:error.status,headers:{'Cache-Control':'no-store'}});
console.error('Clock request failed',error instanceof Error?error.message:'Database unavailable');
return Response.json({error:'Clock service is unavailable. Retry the same confirmation safely.'},{status:503,headers:{'Cache-Control':'no-store'}});
}
export function clockJson(body:unknown) {return Response.json(body,{headers:{'Cache-Control':'private, no-store'}});}