feat(MED-161): improve query
This commit is contained in:
@@ -165,7 +165,7 @@ async function createProducts({
|
|||||||
medusa.admin.product.list({
|
medusa.admin.product.list({
|
||||||
category_id: allCategories.map(({ id }) => id),
|
category_id: allCategories.map(({ id }) => id),
|
||||||
}),
|
}),
|
||||||
getAnalysisElements({}),
|
getAnalysisElements({ getAll: true }),
|
||||||
getAnalysisPackagesType(),
|
getAnalysisPackagesType(),
|
||||||
getProductDefaultFields({ medusa }),
|
getProductDefaultFields({ medusa }),
|
||||||
])
|
])
|
||||||
|
|||||||
@@ -7,11 +7,15 @@ export type AnalysisElement = Tables<{ schema: 'medreport' }, 'analysis_elements
|
|||||||
};
|
};
|
||||||
|
|
||||||
export async function getAnalysisElements({
|
export async function getAnalysisElements({
|
||||||
|
getAll,
|
||||||
originalIds,
|
originalIds,
|
||||||
ids,
|
ids,
|
||||||
|
analysisGroupId,
|
||||||
}: {
|
}: {
|
||||||
|
getAll?: boolean;
|
||||||
originalIds?: string[];
|
originalIds?: string[];
|
||||||
ids?: number[];
|
ids?: number[];
|
||||||
|
analysisGroupId?: number;
|
||||||
}): Promise<AnalysisElement[]> {
|
}): Promise<AnalysisElement[]> {
|
||||||
const query = getSupabaseServerAdminClient()
|
const query = getSupabaseServerAdminClient()
|
||||||
.schema('medreport')
|
.schema('medreport')
|
||||||
@@ -19,14 +23,26 @@ export async function getAnalysisElements({
|
|||||||
.select(`*, analysis_groups(*)`)
|
.select(`*, analysis_groups(*)`)
|
||||||
.order('order', { ascending: true });
|
.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)]);
|
query.in('analysis_id_original', [...new Set(originalIds)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(ids)) {
|
if (hasIdsFilter) {
|
||||||
query.in('id', ids);
|
query.in('id', ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (hasAnalysisGroupIdFilter) {
|
||||||
|
query.eq('parent_analysis_group_id', analysisGroupId);
|
||||||
|
}
|
||||||
|
|
||||||
const { data: analysisElements, error } = await query;
|
const { data: analysisElements, error } = await query;
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user