Skip to content

Commit

Permalink
Subsection: update info panel calculation of costs, fix unique key warni
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannaPeanut committed Apr 18, 2024
1 parent 9e2ca43 commit b8541e1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useQuery } from "@blitzjs/rpc"
import getStatsInfopanelSubsections from "../queries/getStatsInfopanelSubsectionSubsubsections"
import { formatGerKm, formatGerPercentage } from "./utils/formatNumericInfo"
import { Fragment } from "react"

type Props = {
subsectionSlug: string
Expand Down Expand Up @@ -29,15 +30,15 @@ export const SubsectionInfoPanelCellSubsubsections: React.FC<Props> = ({
{subsection.subsubsections.length === 0
? "auf diesem PA wurden noch keine Führungen eingetragen"
: Object.entries(subsubsectionsCategoryCount).map(([key, value]) => (
<>
<Fragment key={key}>
<div className="font-bold">{key}</div>

{Object.entries(value).map(([k, v]) => (
<div key={k}>
<span>{k}</span>: <span>{k === "Summe" ? formatGerKm(v) : v}</span>
</div>
))}
</>
</Fragment>
))}
</div>
</>
Expand Down
23 changes: 14 additions & 9 deletions src/subsections/queries/getStatsInfopanelSubsectionCosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,33 @@ export default resolver.pipe(

const newSubsection = await db.subsection.findFirst(query)

// filter subsubsections with costs
const subsubsectionsWithCosts = newSubsection?.subsubsections.filter(
(subsubsection) => subsubsection?.costEstimate,
)
// filter brf ("Bestandsregelführung") (existing infra and line / ROUTE geometry type)
const brf = newSubsection?.subsubsections?.filter(
(sub) => sub.type === "ROUTE" && sub.isExistingInfra,
)

// sum of costs of subsubsections with costs
const subsubsectionsWithCostsAccCosts = subsubsectionsWithCosts?.reduce(
(acc, a) => acc + (a?.costEstimate || 0),
0,
)
const subsubsectionsWithCostsLengthKm = subsubsectionsWithCosts?.reduce(
(acc, a) => acc + (a?.lengthKm || 0),
0,
)
// sum of km of subsubsections with costs plus km of brf (existing infra, because they don't cost anything)
const subsubsectionsWithCostsLengthKm =
subsubsectionsWithCosts?.reduce((acc, a) => acc + (a?.lengthKm || 0), 0) ||
0 + (brf?.reduce((acc, a) => acc + (a?.lengthKm || 0), 0) || 0)

// RF with costs
const rfWithCosts = newSubsection?.subsubsections?.filter(
(sub) => sub.type === "ROUTE" && sub.costEstimate && !sub.isExistingInfra,
)
// SF with costs
const sfWithCosts = newSubsection?.subsubsections?.filter(
(sub) => sub.type === "AREA" && sub.costEstimate && !sub.isExistingInfra,
)
const brfWithCosts = newSubsection?.subsubsections?.filter(
(sub) => sub.type === "ROUTE" && sub.isExistingInfra,
)

const costStructure = {
RF: {
Expand All @@ -90,9 +95,9 @@ export default resolver.pipe(
sumLengthKm: sfWithCosts?.reduce((acc, a) => acc + (a?.lengthKm || 0), 0),
},
BRF: {
numberSubsubs: brfWithCosts?.length,
numberSubsubs: brf?.length,
costs: undefined,
sumLengthKm: brfWithCosts?.reduce((acc, a) => acc + (a?.lengthKm || 0), 0),
sumLengthKm: brf?.reduce((acc, a) => acc + (a?.lengthKm || 0), 0),
},
}

Expand Down

0 comments on commit b8541e1

Please sign in to comment.