Skip to content

Commit

Permalink
Merge pull request #470 from Klimatbyran/staging
Browse files Browse the repository at this point in the history
Release cruD operation on scope3-categories
  • Loading branch information
hugo-nl authored Dec 12, 2024
2 parents 426ba1e + 5fb9398 commit c3ee069
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/lib/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export async function upsertScope1And2(
export async function upsertScope3(
emissions: Emissions,
scope3: {
categories?: { category: number; total: number }[]
categories?: { category: number; total: number | null }[]
statedTotalEmissions?: OptionalNullable<
Omit<StatedTotalEmissions, 'id' | 'metadataId' | 'unit' | 'scope3Id'>
>
Expand Down Expand Up @@ -167,12 +167,28 @@ export async function upsertScope3(
},
})

await prisma.scope3Category.deleteMany({
where: {
scope3Id: updatedScope3.id,
category: {
in: (scope3.categories ?? [])
.filter((c) => c.total === null)
.map((c) => c.category),
},
},
})

// Upsert only the scope 3 categories from the request body
await Promise.all(
(scope3.categories ?? []).map((scope3Category) => {
const matching = updatedScope3.categories.find(
({ category }) => scope3Category.category === category
)

if (scope3Category.total === null) {
return null
}

return prisma.scope3Category.upsert({
where: {
id: matching?.id ?? 0,
Expand Down
2 changes: 1 addition & 1 deletion src/routes/updateCompanies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export const emissionsSchema = z
.array(
z.object({
category: z.number().int().min(1).max(16),
total: z.number(),
total: z.number().nullable(),
})
)
.optional(),
Expand Down

0 comments on commit c3ee069

Please sign in to comment.