import { Injectable } from '@nestjs/common';
import { DatabaseService } from '../../../infrastructure/database/database.service';
@Injectable()
export class FloorLiveReadModelService {
constructor(private readonly database: DatabaseService) {}
async get(input: {
organizationId: string;
locationId: string;
at: string;
locale: string;
}) {
return this.database.query<any>(
`select
st.id,
st.area_id,
coalesce(a_t.name, a.name) as area_name,
st.table_number,
coalesce(st.display_name, st.table_number) as display_name,
st.max_capacity,
st.x, st.y, st.width, st.height,
(
select jsonb_build_object(
'reservationId', r.id,
'guestName', r.guest_name_snapshot,
'partySize', r.party_size,
'status', r.status,
'startAt', r.start_at,
'endAt', r.end_at
)
from reservation_table_assignments rta
join reservations r on r.id=rta.reservation_id
where rta.service_table_id=st.id
and r.status in ('confirmed','arrived','seated')
and r.start_at < ($3::timestamptz + interval '3 hours')
and r.end_at > ($3::timestamptz - interval '1 hour')
order by r.start_at
limit 1
) as nearby_reservation
from service_tables st
join areas a on a.id=st.area_id
left join area_translations a_t
on a_t.area_id=a.id and a_t.locale=$4
where st.organization_id=$1
and st.location_id=$2
and st.status='active'
order by a.sort_order, st.table_number`,
[input.organizationId, input.locationId, input.at, input.locale],
);
}
}
