feat(MED-161): improve query

This commit is contained in:
2025-09-17 11:16:18 +03:00
parent c51808d899
commit e5822fd55d
2 changed files with 19 additions and 3 deletions

View File

@@ -165,7 +165,7 @@ async function createProducts({
medusa.admin.product.list({
category_id: allCategories.map(({ id }) => id),
}),
getAnalysisElements({}),
getAnalysisElements({ getAll: true }),
getAnalysisPackagesType(),
getProductDefaultFields({ medusa }),
])

View File

@@ -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<AnalysisElement[]> {
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) {