From 95f6581821ff394e8af3937f01bb69274d3439d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Bj=C3=B6rk?= Date: Thu, 12 Dec 2024 16:32:26 +0100 Subject: [PATCH 1/2] add ids in the select --- src/routes/readCompanies.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/routes/readCompanies.ts b/src/routes/readCompanies.ts index 1b0cf1de..cef334be 100644 --- a/src/routes/readCompanies.ts +++ b/src/routes/readCompanies.ts @@ -183,6 +183,7 @@ router.get( }, goals: { select: { + id: true, description: true, year: true, baseYear: true, @@ -195,6 +196,7 @@ router.get( }, initiatives: { select: { + id: true, title: true, description: true, year: true, From 96a7ff2bdd483fbbaba41e30111258352a5986e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Bj=C3=B6rk?= Date: Mon, 16 Dec 2024 10:38:30 +0100 Subject: [PATCH 2/2] Feat: Fix deletion ability for scope1, scope2, scope1and2 (#472) * fix delete for scope * fix bugg in deletion --- src/lib/prisma.ts | 35 ++++++++++++++++++++++++++++++++--- src/routes/updateCompanies.ts | 21 +++++++++++++++------ 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/src/lib/prisma.ts b/src/lib/prisma.ts index 6a7292c7..5b3bb7a8 100644 --- a/src/lib/prisma.ts +++ b/src/lib/prisma.ts @@ -23,9 +23,18 @@ const tCO2e = 'tCO2e' export async function upsertScope1( emissions: Emissions, - scope1: OptionalNullable>, + scope1: OptionalNullable> | 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 }, @@ -60,9 +69,18 @@ export async function upsertScope1( export async function upsertScope2( emissions: Emissions, - scope2: OptionalNullable>, + scope2: OptionalNullable> | 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 }, @@ -97,9 +115,20 @@ export async function upsertScope2( export async function upsertScope1And2( emissions: Emissions, - scope1And2: OptionalNullable>, + scope1And2: OptionalNullable< + Omit + > | 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 }, diff --git a/src/routes/updateCompanies.ts b/src/routes/updateCompanies.ts index 8859b3e4..cfc23c64 100644 --- a/src/routes/updateCompanies.ts +++ b/src/routes/updateCompanies.ts @@ -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 @@ -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 @@ -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() @@ -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,