From e5822fd55dbd38a0057d33af4f4e55659f414f3b Mon Sep 17 00:00:00 2001 From: Karli Date: Wed, 17 Sep 2025 11:16:18 +0300 Subject: [PATCH] feat(MED-161): improve query --- .../job/handler/sync-analysis-groups-store.ts | 2 +- lib/services/analysis-element.service.ts | 20 +++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/app/api/job/handler/sync-analysis-groups-store.ts b/app/api/job/handler/sync-analysis-groups-store.ts index ccf9c0a..7f6ee7b 100644 --- a/app/api/job/handler/sync-analysis-groups-store.ts +++ b/app/api/job/handler/sync-analysis-groups-store.ts @@ -165,7 +165,7 @@ async function createProducts({ medusa.admin.product.list({ category_id: allCategories.map(({ id }) => id), }), - getAnalysisElements({}), + getAnalysisElements({ getAll: true }), getAnalysisPackagesType(), getProductDefaultFields({ medusa }), ]) diff --git a/lib/services/analysis-element.service.ts b/lib/services/analysis-element.service.ts index 9049254..e4f3aff 100644 --- a/lib/services/analysis-element.service.ts +++ b/lib/services/analysis-element.service.ts @@ -7,11 +7,15 @@ export type AnalysisElement = Tables<{ schema: 'medreport' }, 'analysis_elements }; export async function getAnalysisElements({ + getAll, originalIds, ids, + analysisGroupId, }: { + getAll?: boolean; originalIds?: string[]; ids?: number[]; + analysisGroupId?: number; }): Promise { const query = getSupabaseServerAdminClient() .schema('medreport') @@ -19,14 +23,26 @@ export async function getAnalysisElements({ .select(`*, analysis_groups(*)`) .order('order', { ascending: true }); - if (Array.isArray(originalIds)) { + const hasOriginalIdsFilter = Array.isArray(originalIds); + const hasIdsFilter = Array.isArray(ids); + const hasAnalysisGroupIdFilter = typeof analysisGroupId === 'number'; + + if (!hasOriginalIdsFilter && !hasIdsFilter && !hasAnalysisGroupIdFilter && getAll !== true) { + throw new Error('Either originalIds, ids, or analysisGroupId must be provided'); + } + + if (hasOriginalIdsFilter) { query.in('analysis_id_original', [...new Set(originalIds)]); } - if (Array.isArray(ids)) { + if (hasIdsFilter) { query.in('id', ids); } + if (hasAnalysisGroupIdFilter) { + query.eq('parent_analysis_group_id', analysisGroupId); + } + const { data: analysisElements, error } = await query; if (error) {