From 40c9660b77b4d29ada96f2d245b60ec673431ada Mon Sep 17 00:00:00 2001 From: Volodymyr Borysenko Date: Thu, 12 Nov 2026 00:59:56 -0800 Subject: [PATCH] Clear product traction when the field is emptied Fixes #115 --- src/lib/profile.ts | 7 +++++- tests/profile-traction-clear.test.ts | 34 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 tests/profile-traction-clear.test.ts diff --git a/src/lib/profile.ts b/src/lib/profile.ts index 728db0e..7c73e36 100644 --- a/src/lib/profile.ts +++ b/src/lib/profile.ts @@ -297,7 +297,12 @@ function productData( stage: product.stage, teamSize: product.teamSize ? Number(product.teamSize) : undefined, fundingStatus: product.fundingStatus, - tractionMetrics: product.tractionMetric ? ({ selfReported: product.tractionMetric, verification: "unverified self-reported" } as Prisma.InputJsonObject) : undefined, + tractionMetrics: + product.tractionMetric === null + ? Prisma.DbNull + : product.tractionMetric + ? ({ selfReported: product.tractionMetric, verification: "unverified self-reported" } as Prisma.InputJsonObject) + : undefined, websiteUrl: product.websiteUrl, demoUrl: product.demoUrl, repositoryUrl: product.repositoryUrl, diff --git a/tests/profile-traction-clear.test.ts b/tests/profile-traction-clear.test.ts new file mode 100644 index 0000000..e4ac401 --- /dev/null +++ b/tests/profile-traction-clear.test.ts @@ -0,0 +1,34 @@ +import { describe, expect, it } from "vitest"; +import { Prisma, type PrismaClient } from "@prisma/client"; +import { profileSaveSchema, saveProfile } from "@/lib/profile"; + +const profile = { username: "ada", visibility: "PUBLIC", messagingPermission: "EVERYONE", connectionPermission: "EVERYONE" }; + +async function savedTraction(tractionMetric: unknown) { + const updates: Array> = []; + const prisma = { + userProfile: { + findUnique: async () => ({ userId: "user-1" }), + upsert: async (args: { update: Record }) => args.update, + }, + product: { + updateMany: async (args: { data: Record }) => (updates.push(args.data), { count: 1 }), + }, + } as unknown as PrismaClient; + const input = profileSaveSchema.parse({ profile, product: { id: "product-1", name: "Engine", tractionMetric } }); + await saveProfile(prisma, "user-1", input); + return updates[0]!.tractionMetrics; +} + +describe("product traction metric", () => { + it("clears the stored traction when the field is emptied", async () => { + expect(await savedTraction("")).toBe(Prisma.DbNull); + expect(await savedTraction(" ")).toBe(Prisma.DbNull); + expect(await savedTraction(null)).toBe(Prisma.DbNull); + }); + + it("saves typed traction as unverified and leaves a missing field untouched", async () => { + expect(await savedTraction("40 paying teams")).toEqual({ selfReported: "40 paying teams", verification: "unverified self-reported" }); + expect(await savedTraction(undefined)).toBeUndefined(); + }); +});