feat(MED-131): handle analysis order
This commit is contained in:
88
lib/services/order.service.ts
Normal file
88
lib/services/order.service.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
|
||||
import type { Tables } from '@kit/supabase/database';
|
||||
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
import type { StoreOrder } from '@medusajs/types';
|
||||
|
||||
export async function createOrder({
|
||||
medusaOrder,
|
||||
}: {
|
||||
medusaOrder: StoreOrder;
|
||||
}) {
|
||||
const supabase = getSupabaseServerClient();
|
||||
|
||||
const analysisElementIds = medusaOrder.items
|
||||
?.filter(({ product }) => product?.handle?.startsWith('analysis-element-'))
|
||||
.map(({ product }) => Number(product?.handle.replace('analysis-element-', '')))
|
||||
.filter((id) => !Number.isNaN(id)) as number[];
|
||||
const { data: { user } } = await supabase.auth.getUser();
|
||||
if (!user) {
|
||||
throw new Error('User not found');
|
||||
}
|
||||
const orderResult = await supabase.schema('medreport')
|
||||
.from('analysis_orders')
|
||||
.insert({
|
||||
analysis_element_ids: analysisElementIds,
|
||||
analysis_ids: [],
|
||||
status: 'QUEUED',
|
||||
user_id: user.id,
|
||||
medusa_order_id: medusaOrder.id,
|
||||
})
|
||||
.select('id')
|
||||
.single()
|
||||
.throwOnError();
|
||||
|
||||
if (orderResult.error || !orderResult.data?.id) {
|
||||
throw new Error(`Failed to create order, message=${orderResult.error}, data=${JSON.stringify(orderResult)}`);
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateOrder({
|
||||
orderId,
|
||||
orderStatus,
|
||||
}: {
|
||||
orderId: number;
|
||||
orderStatus: Tables<{ schema: 'medreport' }, 'analysis_orders'>['status'];
|
||||
}) {
|
||||
const { error } = await getSupabaseServerClient()
|
||||
.schema('medreport')
|
||||
.from('analysis_orders')
|
||||
.update({
|
||||
status: orderStatus,
|
||||
})
|
||||
.eq('id', orderId)
|
||||
.throwOnError();
|
||||
if (error) {
|
||||
throw new Error(`Failed to update order, message=${error}, data=${JSON.stringify(error)}`);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getOrder({
|
||||
medusaOrderId,
|
||||
}: {
|
||||
medusaOrderId: string;
|
||||
}) {
|
||||
const query = getSupabaseServerAdminClient()
|
||||
.schema('medreport')
|
||||
.from('analysis_orders')
|
||||
.select('*')
|
||||
.eq('medusa_order_id', medusaOrderId)
|
||||
|
||||
const { data: order } = await query.single().throwOnError();
|
||||
return order;
|
||||
}
|
||||
|
||||
export async function getOrders({
|
||||
orderStatus,
|
||||
}: {
|
||||
orderStatus?: Tables<{ schema: 'medreport' }, 'analysis_orders'>['status'];
|
||||
} = {}) {
|
||||
const query = getSupabaseServerClient()
|
||||
.schema('medreport')
|
||||
.from('analysis_orders')
|
||||
.select('*')
|
||||
if (orderStatus) {
|
||||
query.eq('status', orderStatus);
|
||||
}
|
||||
const orders = await query.throwOnError();
|
||||
return orders.data;
|
||||
}
|
||||
Reference in New Issue
Block a user