Skip to content

Commit

Permalink
group plots by alert date in eudr dashboard detail alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Mar 14, 2024
1 parent d8010b5 commit f9617d5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
15 changes: 13 additions & 2 deletions api/src/modules/eudr-alerts/dashboard/dashboard-detail.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class ByVolume {
percentage: number;
@ApiProperty()
volume: number;
@ApiProperty()
geoRegionId: string;
@ApiProperty()
plotName: string;
}

class ByArea {
Expand All @@ -75,9 +79,16 @@ class DashBoardDetailAlerts {

class AlertValues {
@ApiProperty()
geoRegionId: string;
alertDate: string;
@ApiProperty({ type: () => AlertPlots, isArray: true })
plots: AlertPlots[];
}

class AlertPlots {
@ApiProperty()
alertCount: number;
geoRegionId: string;
@ApiProperty()
plotName: string;
@ApiProperty()
alertCount: number;
}
7 changes: 4 additions & 3 deletions api/src/modules/eudr-alerts/dashboard/dashboard.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ export type EntityMetadata = {
supplierId: string;
supplierName: string;
companyId: string;
adminRegionId: string;
adminRegionName: string;
materialId: string;
materialName: string;
adminRegionId: string;
adminRegionName: string;
totalBaselineVolume: number;
geoRegionCount: number;
knownGeoRegions: number;
totalSourcingLocations: number;
isoA3: string;
};

Expand Down
38 changes: 31 additions & 7 deletions api/src/modules/eudr-alerts/dashboard/eudr-dashboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ export class EudrDashboardService {
);
const sourcingRecords: SourcingRecord[] = [];
for (const geoRegion of geoRegions) {
geoRegion.geoRegionId = geoRegion.geoRegionId ?? 'Unknown';
geoRegion.geoRegionId = geoRegion.geoRegionId ?? null;
geoRegion.plotName = geoRegion.plotName ?? 'Unknown';
if (!geoRegionMap.get(geoRegion.geoRegionId)) {
geoRegionMap.set(geoRegion.geoRegionId, {
Expand All @@ -533,7 +533,7 @@ export class EudrDashboardService {
.createQueryBuilder(SourcingRecord, 'sr')
.leftJoin(SourcingLocation, 'sl', 'sr.sourcingLocationId = sl.id')
.leftJoin(GeoRegion, 'gr', 'gr.id = sl.geoRegionId');
if (geoRegion.geoRegionId === 'Unknown') {
if (!geoRegion.geoRegionId) {
queryBuilder.andWhere('sl.geoRegionId IS NULL');
} else {
queryBuilder.andWhere('sl.geoRegionId = :geoRegionId', {
Expand Down Expand Up @@ -601,11 +601,13 @@ export class EudrDashboardService {
startAlertDate: startAlertDate,
endAlertDate: endAlertDate,
totalAlerts,
values: alertsOutput.map((alert: AlertsOutput) => ({
geoRegionId: alert.geoRegionId,
alertCount: alert.alertCount || null,
plotName: geoRegionMap.get(alert.geoRegionId)?.plotName,
})),
// values: alertsOutput.map((alert: AlertsOutput) => ({
// alertDate: alert.alertDate.value,
// geoRegionId: alert.geoRegionId,
// alertCount: alert.alertCount || null,
// plotName: geoRegionMap.get(alert.geoRegionId)?.plotName,
// })),
values: groupAlertsByDate(alertsOutput, geoRegionMap),
};

result.alerts = alerts;
Expand Down Expand Up @@ -635,3 +637,25 @@ const aggregateUnknownGeoRegionVolumeValues = (arr: any[]): any[] => {
});
return finalRecords;
};

const groupAlertsByDate = (
alerts: AlertsOutput[],
geoRegionMap: Map<string, any>,
): any[] => {
const alertsByDate: any = alerts.reduce((acc: any, cur: AlertsOutput) => {
const date: string = cur.alertDate.value.toString();
if (!acc[date]) {
acc[date] = [];
}
acc[date].push({
plotName: geoRegionMap.get(cur.geoRegionId)?.plotName,
geoRegionId: cur.geoRegionId,
alertCount: cur.alertCount,
});
return acc;
}, {});
return Object.keys(alertsByDate).map((key) => ({
alertDate: key,
plots: alertsByDate[key],
}));
};
1 change: 0 additions & 1 deletion api/src/modules/eudr-alerts/eudr.repositoty.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export class EUDRAlertDates {

export interface EUDRAlertDatabaseResult {
supplierid: string;
tpl: number;
dfs: number;
sda: number;
}
Expand Down

0 comments on commit f9617d5

Please sign in to comment.