feat(MED-105): sort analysis result elements by date
This commit is contained in:
@@ -9,12 +9,10 @@ import { Button } from '@kit/ui/shadcn/button';
|
|||||||
|
|
||||||
import { loadUserAnalysis } from '../../_lib/server/load-user-analysis';
|
import { loadUserAnalysis } from '../../_lib/server/load-user-analysis';
|
||||||
import Analysis from './_components/analysis';
|
import Analysis from './_components/analysis';
|
||||||
import { listProductTypes } from '@lib/data/products';
|
|
||||||
import pathsConfig from '~/config/paths.config';
|
import pathsConfig from '~/config/paths.config';
|
||||||
import { redirect } from 'next/navigation';
|
import { redirect } from 'next/navigation';
|
||||||
import { getOrders } from '~/lib/services/order.service';
|
import { getOrders } from '~/lib/services/order.service';
|
||||||
import { AnalysisElement, getAnalysisElements } from '~/lib/services/analysis-element.service';
|
import { getAnalysisElements } from '~/lib/services/analysis-element.service';
|
||||||
import type { UserAnalysisElement } from '@kit/accounts/types/accounts';
|
|
||||||
import { loadCurrentUserAccount } from '@/app/home/(user)/_lib/server/load-user-account';
|
import { loadCurrentUserAccount } from '@/app/home/(user)/_lib/server/load-user-account';
|
||||||
import { createPageViewLog } from '~/lib/services/audit/pageView.service';
|
import { createPageViewLog } from '~/lib/services/audit/pageView.service';
|
||||||
|
|
||||||
@@ -33,12 +31,12 @@ async function AnalysisResultsPage() {
|
|||||||
throw new Error('Account not found');
|
throw new Error('Account not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
const analysisList = await loadUserAnalysis();
|
const analysisResponses = await loadUserAnalysis();
|
||||||
|
const analysisResponseElements = analysisResponses?.flatMap(({ elements }) => elements);
|
||||||
|
|
||||||
const orders = await getOrders().catch(() => null);
|
const orders = await getOrders().catch(() => null);
|
||||||
const { productTypes } = await listProductTypes();
|
|
||||||
|
|
||||||
if (!orders || !productTypes) {
|
if (!orders) {
|
||||||
redirect(pathsConfig.auth.signIn);
|
redirect(pathsConfig.auth.signIn);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,17 +49,20 @@ async function AnalysisResultsPage() {
|
|||||||
...new Set(orders?.flatMap((order) => order.analysis_element_ids).filter(Boolean) as number[]),
|
...new Set(orders?.flatMap((order) => order.analysis_element_ids).filter(Boolean) as number[]),
|
||||||
];
|
];
|
||||||
const analysisElements = await getAnalysisElements({ ids: analysisElementIds });
|
const analysisElements = await getAnalysisElements({ ids: analysisElementIds });
|
||||||
const analysisElementsWithResults = analysisElements.reduce((acc, curr) => {
|
const analysisElementsWithResults = analysisResponseElements
|
||||||
const analysisResponseWithElementResults = analysisList?.find((result) => result.elements.some((element) => element.analysis_element_original_id === curr.analysis_id_original));
|
?.sort((a, b) => {
|
||||||
const elementResults = analysisResponseWithElementResults?.elements.find((element) => element.analysis_element_original_id === curr.analysis_id_original) as UserAnalysisElement | undefined;
|
if (!a.updated_at || !b.updated_at) {
|
||||||
return {
|
return 0;
|
||||||
...acc,
|
}
|
||||||
[curr.id]: {
|
return new Date(b.updated_at).getTime() - new Date(a.updated_at).getTime();
|
||||||
analysisElement: curr,
|
})
|
||||||
results: elementResults,
|
.map((results) => {
|
||||||
},
|
return {
|
||||||
}
|
results,
|
||||||
}, {} as Record<number, { analysisElement: AnalysisElement; results: UserAnalysisElement | undefined }>);
|
}
|
||||||
|
});
|
||||||
|
const analysisElementsWithoutResults = analysisElements
|
||||||
|
.filter((element) => !analysisElementsWithResults?.some(({ results }) => results.analysis_element_original_id === element.analysis_id_original));
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PageBody>
|
<PageBody>
|
||||||
@@ -71,7 +72,7 @@ async function AnalysisResultsPage() {
|
|||||||
<Trans i18nKey="analysis-results:pageTitle" />
|
<Trans i18nKey="analysis-results:pageTitle" />
|
||||||
</h4>
|
</h4>
|
||||||
<p className="text-muted-foreground text-sm">
|
<p className="text-muted-foreground text-sm">
|
||||||
{analysisList && analysisList.length > 0 ? (
|
{analysisResponses && analysisResponses.length > 0 ? (
|
||||||
<Trans i18nKey="analysis-results:description" />
|
<Trans i18nKey="analysis-results:description" />
|
||||||
) : (
|
) : (
|
||||||
<Trans i18nKey="analysis-results:descriptionEmpty" />
|
<Trans i18nKey="analysis-results:descriptionEmpty" />
|
||||||
@@ -85,9 +86,18 @@ async function AnalysisResultsPage() {
|
|||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex flex-col gap-2">
|
<div className="flex flex-col gap-2">
|
||||||
{Object.entries(analysisElementsWithResults).map(([id, { analysisElement, results }]) => {
|
{analysisElementsWithResults?.map(({ results }) => {
|
||||||
|
const analysisElement = analysisElements.find((element) => element.analysis_id_original === results.analysis_element_original_id);
|
||||||
|
if (!analysisElement) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<Analysis key={id} analysisElement={analysisElement} results={results} />
|
<Analysis key={results.id} analysisElement={analysisElement} results={results} />
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
{analysisElementsWithoutResults?.map((element) => {
|
||||||
|
return (
|
||||||
|
<Analysis key={element.analysis_id_original} analysisElement={element} />
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { redirect } from 'next/navigation';
|
|||||||
import { listOrders } from '~/medusa/lib/data/orders';
|
import { listOrders } from '~/medusa/lib/data/orders';
|
||||||
import { createI18nServerInstance } from '@/lib/i18n/i18n.server';
|
import { createI18nServerInstance } from '@/lib/i18n/i18n.server';
|
||||||
import { listProductTypes } from '@lib/data/products';
|
import { listProductTypes } from '@lib/data/products';
|
||||||
import { retrieveCustomer } from '@lib/data/customer';
|
|
||||||
import { PageBody } from '@kit/ui/makerkit/page';
|
import { PageBody } from '@kit/ui/makerkit/page';
|
||||||
import pathsConfig from '~/config/paths.config';
|
import pathsConfig from '~/config/paths.config';
|
||||||
import { Trans } from '@kit/ui/trans';
|
import { Trans } from '@kit/ui/trans';
|
||||||
@@ -21,11 +20,10 @@ export async function generateMetadata() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function OrdersPage() {
|
async function OrdersPage() {
|
||||||
const customer = await retrieveCustomer();
|
|
||||||
const orders = await listOrders().catch(() => null);
|
const orders = await listOrders().catch(() => null);
|
||||||
const { productTypes } = await listProductTypes();
|
const { productTypes } = await listProductTypes();
|
||||||
|
|
||||||
if (!customer || !orders || !productTypes) {
|
if (!orders || !productTypes) {
|
||||||
redirect(pathsConfig.auth.signIn);
|
redirect(pathsConfig.auth.signIn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user