@@ -90,6 +90,7 @@ type BinRow = QueryResultRow & {
9090 status : string | null ;
9191 active : boolean | number ;
9292 updated_at : Date | string | null ;
93+ station_name ?: string | null ;
9394} ;
9495
9596type AlertRow = QueryResultRow & {
@@ -421,33 +422,22 @@ export async function cloudSetDemoHardwareTarget(
421422 }
422423
423424 const ownerFilter = identity . role === "admin" ? text ( payload . owner_username ) : identity . username ;
424- const targetScope = await pool ( ) . query < {
425- assigned_owner_username : string ;
426- bin_id : string ;
427- bin_index : number | string ;
428- } > (
429- `select station.assigned_owner_username, bin.bin_id, bin.bin_index
425+ const result = await pool ( ) . query < DemoHardwareTargetRow > (
426+ `insert into public.demo_hardware_targets
427+ (owner_username, station_id, bin_id, bin_index, selected_by, selected_at, active)
428+ select station.assigned_owner_username, station.station_id, bin.bin_id, bin.bin_index,
429+ $5, now(), true
430430 from public.bin_stations station
431431 join public.bins bin on bin.station_id = station.station_id
432432 where station.station_id = $1
433433 and bin.bin_index = $2
434434 and ($3::text = '' or bin.bin_id = $3)
435435 and ($4::text = '' or station.assigned_owner_username = $4)
436+ and nullif(station.assigned_owner_username, '') is not null
436437 and coalesce(station.active::text, '') not in ('0', 'false', 'f', 'no', '')
437438 and coalesce(bin.active::text, '') not in ('0', 'false', 'f', 'no', '')
438439 order by bin.bin_id
439- limit 1` ,
440- [ stationId , binIndex , requestedBinId , ownerFilter ]
441- ) ;
442- const scopedTarget = targetScope . rows [ 0 ] ;
443- if ( ! scopedTarget ?. assigned_owner_username ) {
444- return null ;
445- }
446-
447- const result = await pool ( ) . query < DemoHardwareTargetRow > (
448- `insert into public.demo_hardware_targets
449- (owner_username, station_id, bin_id, bin_index, selected_by, selected_at, active)
450- values ($1, $2, $3, $4, $5, now(), true)
440+ limit 1
451441 on conflict (owner_username) do update set
452442 station_id = excluded.station_id,
453443 bin_id = excluded.bin_id,
@@ -456,10 +446,13 @@ export async function cloudSetDemoHardwareTarget(
456446 selected_at = excluded.selected_at,
457447 active = true
458448 returning owner_username, station_id, bin_id, bin_index, selected_by, selected_at, active` ,
459- [ scopedTarget . assigned_owner_username , stationId , scopedTarget . bin_id , binIndex , identity . username ]
449+ [ stationId , binIndex , requestedBinId , ownerFilter , identity . username ]
460450 ) ;
461451
462452 const target = result . rows [ 0 ] ;
453+ if ( ! target ) {
454+ return null ;
455+ }
463456 return {
464457 ok : true ,
465458 target : {
@@ -636,8 +629,10 @@ async function derivedFullnessAlerts(ownerUsername: string, stationIds: string[]
636629 const result = await pool ( ) . query < BinRow > (
637630 `select (row_number() over (order by b.station_id, b.bin_index))::int as id,
638631 b.bin_id, b.station_id, b.command, b.bin_index, b.label,
639- ${ fillExpr } as fill_percent, b.status, b.active, b.updated_at
632+ ${ fillExpr } as fill_percent, b.status, b.active, b.updated_at,
633+ station.name as station_name
640634 from public.bins b
635+ join public.bin_stations station on station.station_id = b.station_id
641636 where ${ where . join ( " and " ) }
642637 order by ${ fillExpr } desc, b.updated_at desc` ,
643638 values
@@ -655,7 +650,7 @@ async function derivedFullnessAlerts(ownerUsername: string, stationIds: string[]
655650 device_id : "" ,
656651 severity : danger ? "danger" : "warning" ,
657652 title : danger ? "\u0054\u0068\u00f9\u006e\u0067\u0020\u0072\u00e1\u0063\u0020\u0111\u00e3\u0020\u0111\u1ea7\u0079" : "\u0054\u0068\u00f9\u006e\u0067\u0020\u0072\u00e1\u0063\u0020\u0067\u1ea7\u006e\u0020\u0111\u1ea7\u0079" ,
658- message : `\u0054\u0068\u00f9\u006e\u0067 ${ label } ${ danger ? "\u0111\u00e3\u0020\u0111\u1ea7\u0079" : "\u0067\u1ea7\u006e\u0020\u0111\u1ea7\u0079" } ${ Math . round ( fill ) } %.` ,
653+ message : `${ row . station_name || row . station_id } : \u0054\u0068\u00f9\u006e\u0067 ${ label } ${ danger ? "\u0111\u00e3\u0020\u0111\u1ea7\u0079" : "\u0067\u1ea7\u006e\u0020\u0111\u1ea7\u0079" } ${ Math . round ( fill ) } %.` ,
659654 status : "open" ,
660655 source : "derived_fullness" ,
661656 created_at : iso ( row . updated_at ) || now ,
0 commit comments