Skip to content

Commit

Permalink
Merge pull request #474 from Klimatbyran/staging
Browse files Browse the repository at this point in the history
Update garbo production
  • Loading branch information
hugo-nl authored Dec 16, 2024
2 parents 32cfbc3 + 96a7ff2 commit a769ec5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
35 changes: 32 additions & 3 deletions src/lib/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,18 @@ const tCO2e = 'tCO2e'

export async function upsertScope1(
emissions: Emissions,
scope1: OptionalNullable<Omit<Scope1, 'id' | 'metadataId' | 'unit'>>,
scope1: OptionalNullable<Omit<Scope1, 'id' | 'metadataId' | 'unit'>> | null,
metadata: Metadata
) {
if (scope1 === null) {
if (emissions.scope1Id) {
await prisma.scope1.delete({
where: { id: emissions.scope1Id },
})
}
return null
}

return emissions.scope1Id
? prisma.scope1.update({
where: { id: emissions.scope1Id },
Expand Down Expand Up @@ -60,9 +69,18 @@ export async function upsertScope1(

export async function upsertScope2(
emissions: Emissions,
scope2: OptionalNullable<Omit<Scope2, 'id' | 'metadataId' | 'unit'>>,
scope2: OptionalNullable<Omit<Scope2, 'id' | 'metadataId' | 'unit'>> | null,
metadata: Metadata
) {
if (scope2 === null) {
if (emissions.scope2Id) {
await prisma.scope2.delete({
where: { id: emissions.scope2Id },
})
}
return null
}

return emissions.scope2Id
? prisma.scope2.update({
where: { id: emissions.scope2Id },
Expand Down Expand Up @@ -97,9 +115,20 @@ export async function upsertScope2(

export async function upsertScope1And2(
emissions: Emissions,
scope1And2: OptionalNullable<Omit<Scope1And2, 'id' | 'metadataId' | 'unit'>>,
scope1And2: OptionalNullable<
Omit<Scope1And2, 'id' | 'metadataId' | 'unit'>
> | null,
metadata: Metadata
) {
if (scope1And2 === null) {
if (emissions.scope1And2Id) {
await prisma.scope1And2.delete({
where: { id: emissions.scope1And2Id },
})
}
return null
}

return emissions.scope1And2Id
? prisma.scope1And2.update({
where: { id: emissions.scope1And2Id },
Expand Down
2 changes: 2 additions & 0 deletions src/routes/readCompanies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ router.get(
},
goals: {
select: {
id: true,
description: true,
year: true,
baseYear: true,
Expand All @@ -195,6 +196,7 @@ router.get(
},
initiatives: {
select: {
id: true,
title: true,
description: true,
year: true,
Expand Down
21 changes: 15 additions & 6 deletions src/routes/updateCompanies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,9 @@ export const emissionsSchema = z
.object({
total: z.number(),
})
.optional(),
.optional()
.nullable()
.describe('Sending null means deleting the scope'),
scope2: z
.object({
mb: z
Expand All @@ -291,7 +293,9 @@ export const emissionsSchema = z
'At least one property of `mb`, `lb` and `unknown` must be defined if scope2 is provided',
}
)
.optional(),
.optional()
.nullable()
.describe('Sending null means deleting the scope'),
scope3: z
.object({
categories: z
Expand All @@ -307,7 +311,11 @@ export const emissionsSchema = z
.optional(),
biogenic: z.object({ total: z.number() }).optional(),
statedTotalEmissions: statedTotalEmissionsSchema,
scope1And2: z.object({ total: z.number() }).optional(),
scope1And2: z
.object({ total: z.number() })
.optional()
.nullable()
.describe('Sending null means deleting the scope'),
})
.optional()

Expand Down Expand Up @@ -471,10 +479,11 @@ router.post(
// There seems to be a type error in zod which doesn't take into account optional objects.

await Promise.allSettled([
scope1 && upsertScope1(dbEmissions, scope1, metadata),
scope2 && upsertScope2(dbEmissions, scope2, metadata),
scope1 !== undefined && upsertScope1(dbEmissions, scope1, metadata),
scope2 !== undefined && upsertScope2(dbEmissions, scope2, metadata),
scope3 && upsertScope3(dbEmissions, scope3, metadata),
scope1And2 && upsertScope1And2(dbEmissions, scope1And2, metadata),
scope1And2 !== undefined &&
upsertScope1And2(dbEmissions, scope1And2, metadata),
statedTotalEmissions &&
upsertStatedTotalEmissions(
dbEmissions,
Expand Down

0 comments on commit a769ec5

Please sign in to comment.