From 52eac590a4e8a6317cf3cc934d4172c7c11d1f6c Mon Sep 17 00:00:00 2001 From: Danel Kungla Date: Tue, 30 Sep 2025 18:05:32 +0300 Subject: [PATCH] feat: add location handling for TTO orders and update related components --- app/home/(user)/(dashboard)/order/page.tsx | 8 ++-- .../(user)/_components/orders/order-block.tsx | 3 ++ .../_components/orders/order-items-table.tsx | 11 +++++- lib/services/order.service.ts | 39 ++++++++++++++++++- lib/types/order.ts | 1 + public/locales/et/orders.json | 3 +- .../20250930175100_update_tto_tables.sql | 4 ++ 7 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 supabase/migrations/20250930175100_update_tto_tables.sql diff --git a/app/home/(user)/(dashboard)/order/page.tsx b/app/home/(user)/(dashboard)/order/page.tsx index ee168ff..284988e 100644 --- a/app/home/(user)/(dashboard)/order/page.tsx +++ b/app/home/(user)/(dashboard)/order/page.tsx @@ -53,14 +53,14 @@ async function OrdersPage() { /> {medusaOrders.map((medusaOrder) => { - const analysisOrder = analysisOrders.find( - ({ medusa_order_id }) => medusa_order_id === medusaOrder.id, - ); - if (!medusaOrder) { return null; } + const analysisOrder = analysisOrders.find( + ({ medusa_order_id }) => medusa_order_id === medusaOrder.id, + ); + const medusaOrderItems = medusaOrder.items || []; const medusaOrderItemsAnalysisPackages = medusaOrderItems.filter( (item) => item.product_type_id === analysisPackagesTypeId, diff --git a/app/home/(user)/_components/orders/order-block.tsx b/app/home/(user)/_components/orders/order-block.tsx index 08daf05..cee8b17 100644 --- a/app/home/(user)/_components/orders/order-block.tsx +++ b/app/home/(user)/_components/orders/order-block.tsx @@ -11,6 +11,7 @@ import OrderItemsTable from './order-items-table'; export default function OrderBlock({ analysisOrder, + ttoLocation, medusaOrderStatus, itemsAnalysisPackage, itemsTtoService, @@ -18,6 +19,7 @@ export default function OrderBlock({ medusaOrderId, }: { analysisOrder?: AnalysisOrder; + ttoLocation?: { name: string }; medusaOrderStatus: string; itemsAnalysisPackage: StoreOrderLineItem[]; itemsTtoService: StoreOrderLineItem[]; @@ -67,6 +69,7 @@ export default function OrderBlock({ order={{ status: medusaOrderStatus.toUpperCase(), medusaOrderId, + location: ttoLocation?.name, }} /> )} diff --git a/app/home/(user)/_components/orders/order-items-table.tsx b/app/home/(user)/_components/orders/order-items-table.tsx index 09d50a9..acc9006 100644 --- a/app/home/(user)/_components/orders/order-items-table.tsx +++ b/app/home/(user)/_components/orders/order-items-table.tsx @@ -61,6 +61,11 @@ export default function OrderItemsTable({ + {order.location && ( + + + + )} @@ -83,7 +88,11 @@ export default function OrderItemsTable({ {formatDate(orderItem.created_at, 'dd.MM.yyyy HH:mm')} - + {order.location && ( + + {order.location} + + )} ({ + ...order, + location: await getTtoLocation(order.location_sync_id), + })), + ); + + return ordersWithLocation; +} + +export async function getTtoLocation(syncId?: number | null) { + if (!syncId) { + return null; + } + + const client = getSupabaseServerClient(); + + const { + data: { user }, + } = await client.auth.getUser(); + + if (!user) { + throw new Error('Unauthorized'); + } + + const { data, error } = await client + .schema('medreport') + .from('connected_online_locations') + .select('name') + .eq('sync_id', syncId) + .single(); + + if (error) { + throw new Error('Could not receive online locations: ', error); + } + + return data; } diff --git a/lib/types/order.ts b/lib/types/order.ts index 92ce3f7..3083039 100644 --- a/lib/types/order.ts +++ b/lib/types/order.ts @@ -9,4 +9,5 @@ export type Order = { medusaOrderId?: string; id?: number; status?: string; + location?: string; }; diff --git a/public/locales/et/orders.json b/public/locales/et/orders.json index a57ff1c..d6896f3 100644 --- a/public/locales/et/orders.json +++ b/public/locales/et/orders.json @@ -7,7 +7,8 @@ "ttoService": "Broneering", "otherOrders": "Tellimus", "createdAt": "Tellitud", - "status": "Olek" + "status": "Olek", + "location": "Asukoht" }, "status": { "QUEUED": "Esitatud", diff --git a/supabase/migrations/20250930175100_update_tto_tables.sql b/supabase/migrations/20250930175100_update_tto_tables.sql new file mode 100644 index 0000000..6b86c0e --- /dev/null +++ b/supabase/migrations/20250930175100_update_tto_tables.sql @@ -0,0 +1,4 @@ +ALTER TABLE medreport.connected_online_reservation +ADD CONSTRAINT fk_reservation_location_sync_id +FOREIGN KEY (location_sync_id) +REFERENCES medreport.connected_online_locations(sync_id); \ No newline at end of file