import { Injectable } from '@nestjs/common';
import { DatabaseService } from '../../../infrastructure/database/database.service';
@Injectable()
export class FloorReadModelService {
constructor(private readonly database: DatabaseService) {}
async get(input: {
organizationId: string;
locationId: string;
at: string;
locale: string;
}) {
const areas = await this.database.query<any>(
`select
a.id,
coalesce(at.name, a.name) as name,
a.sort_order
from areas a
left join area_translations at
on at.area_id=a.id and at.locale=$3
where a.organization_id=$1
and a.location_id=$2
and a.status='active'
order by a.sort_order, lower(coalesce(at.name,a.name))`,
[input.organizationId, input.locationId, input.locale],
);
const tables = await this.database.query<any>(
`select
st.id,
st.area_id,
st.table_number,
coalesce(st.display_name, st.table_number) as display_name,
st.min_capacity,
st.max_capacity,
st.x, st.y, st.width, st.height,
exists(
select 1
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 '2 hours')
and r.end_at > $3::timestamptz
) as reserved_nearby
from service_tables st
where st.organization_id=$1
and st.location_id=$2
and st.status='active'
order by st.table_number`,
[input.organizationId, input.locationId, input.at],
);
return { areas, tables };
}
}
