fixing bigint issue with creating portfolio

This commit is contained in:
Khalim Conn-Kowlessar 2023-08-01 18:20:08 +01:00
parent e0161d1b5f
commit 3afc4ba49e
6 changed files with 25 additions and 8 deletions

View file

@ -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" }), {

View file

@ -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,

View file

@ -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
}
]
}

View file

@ -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"),

View file

@ -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.");

View file

@ -1,8 +1,8 @@
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"target": "es5",
// "target": "ESNext",
// "target": "es5",
"target": "ESNext",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,