From 3afc4ba49e05dd09ff4aace7f765869b6c6473e4 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Tue, 1 Aug 2023 18:20:08 +0100 Subject: [PATCH] fixing bigint issue with creating portfolio --- src/app/api/portfolio/route.ts | 10 +++++++--- src/app/components/home/ModalSubmit.tsx | 3 ++- src/app/db/migrations/meta/_journal.json | 7 +++++++ src/app/db/schema/property.ts | 4 ++-- src/app/utils.ts | 5 +++++ tsconfig.json | 4 ++-- 6 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/app/api/portfolio/route.ts b/src/app/api/portfolio/route.ts index f83b1b8..8b04ca0 100644 --- a/src/app/api/portfolio/route.ts +++ b/src/app/api/portfolio/route.ts @@ -8,9 +8,10 @@ import { import { NextRequest, NextResponse } from "next/server"; import { db } from "@/app/db/db"; import { z } from "zod"; +import { serializeBigInt } from "@/app/utils"; const createPortfolioSchema = z.object({ - userId: z.bigint(), + userId: z.string(), portfolioName: z.string(), budget: z.number().optional(), goal: z.enum(PortfolioGoal), @@ -53,7 +54,7 @@ export async function POST(request: NextRequest) { const response = await db .insert(portfolioUsers) .values({ - userId: userId, + userId: BigInt(userId), portfolioId: newPortfolioId, role: role, }) @@ -62,7 +63,10 @@ export async function POST(request: NextRequest) { if (response.length !== 1) { throw new Error("Got back more than one row from insert - investigate"); } - return new NextResponse(JSON.stringify(response[0]), { status: 201 }); + + return new NextResponse(JSON.stringify(response[0], serializeBigInt), { + status: 201, + }); } catch (error) { console.error(error); return new NextResponse(JSON.stringify({ msg: "Insternal server error" }), { diff --git a/src/app/components/home/ModalSubmit.tsx b/src/app/components/home/ModalSubmit.tsx index c073c91..1401845 100644 --- a/src/app/components/home/ModalSubmit.tsx +++ b/src/app/components/home/ModalSubmit.tsx @@ -26,8 +26,9 @@ const createPortfolio = async ({ status, role, }: CreatePortfolioArgs) => { + // We convert the the bigint to a string since big ints are not serialisable and we don't want to loose precision const requestBody = JSON.stringify({ - userId: userId, + userId: userId.toString(), portfolioName: portfolioName, budget: budget, goal: goal, diff --git a/src/app/db/migrations/meta/_journal.json b/src/app/db/migrations/meta/_journal.json index d94a9ef..2fe0bc7 100644 --- a/src/app/db/migrations/meta/_journal.json +++ b/src/app/db/migrations/meta/_journal.json @@ -155,6 +155,13 @@ "when": 1690902069171, "tag": "0021_eager_titanium_man", "breakpoints": true + }, + { + "idx": 22, + "version": "5", + "when": 1690906901377, + "tag": "0022_smiling_callisto", + "breakpoints": true } ] } \ No newline at end of file diff --git a/src/app/db/schema/property.ts b/src/app/db/schema/property.ts index 49acac0..99131ee 100644 --- a/src/app/db/schema/property.ts +++ b/src/app/db/schema/property.ts @@ -135,8 +135,8 @@ export const propertyDetailsEpc = pgTable("property_details_epc", { windowsRating: smallint("windows_rating"), heating: text("heating"), heatingRating: smallint("heating_rating"), - heatingContols: text("heating_contols"), - heatingContolsRating: smallint("heating_contols_rating"), + heatingControls: text("heating_controls"), + heatingControlsRating: smallint("heating_controls_rating"), hotWater: text("hot_water"), hotWaterRating: smallint("hot_water_rating"), lighting: text("lighting"), diff --git a/src/app/utils.ts b/src/app/utils.ts index acccdc8..e6e85bd 100644 --- a/src/app/utils.ts +++ b/src/app/utils.ts @@ -1,3 +1,8 @@ +export const serializeBigInt = (_: any, value: any): string | any => { + // Simple utility function to convert BigInts to strings with JSON.stringify + return typeof value === "bigint" ? value.toString() : value; +}; + export function sapToEpc(sapPoints: number): string { if (sapPoints <= 0 || sapPoints > 100) { throw new Error("SAP points should be between 1 and 100."); diff --git a/tsconfig.json b/tsconfig.json index 6468155..7f10c94 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,8 +1,8 @@ { "compilerOptions": { "allowSyntheticDefaultImports": true, - "target": "es5", - // "target": "ESNext", + // "target": "es5", + "target": "ESNext", "lib": ["dom", "dom.iterable", "esnext"], "allowJs": true, "skipLibCheck": true,