From 1761b7163d9657c4d8947df07d0978db82a0b2ef Mon Sep 17 00:00:00 2001 From: Augustine Putman Date: Mon, 16 Jun 2025 13:20:28 -0400 Subject: [PATCH] Added logic to calculate Ebitda margins and migration to retroactively correct them --- app/actions/add-deal.ts | 7 ++++++- .../migrations/20241204061757_init/migration.sql | 14 ++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app/actions/add-deal.ts b/app/actions/add-deal.ts index 28bc288..6a9e6b4 100644 --- a/app/actions/add-deal.ts +++ b/app/actions/add-deal.ts @@ -27,6 +27,11 @@ import React from "react"; const AddDealToDB = withAuthServerAction( async (user: User, values: NewDealFormSchemaType) => { try { + if (values.revenue > 0 && values.ebitda > 0) { + const ebitda_margin = values.ebitda / values.revenue; + } else { + const ebitda_margin = -1; + } const addedDeal = await prismaDB.deal.create({ data: { title: values.title, @@ -38,7 +43,7 @@ const AddDealToDB = withAuthServerAction( workPhone: values.work_phone, revenue: values.revenue, ebitda: values.ebitda, - ebitdaMargin: values.ebitda_margin, + ebitdaMargin: ebitda_margin, grossRevenue: values.gross_revenue, companyLocation: values.company_location, brokerage: values.brokerage, diff --git a/prisma/migrations/20241204061757_init/migration.sql b/prisma/migrations/20241204061757_init/migration.sql index 2a1de26..62baeca 100644 --- a/prisma/migrations/20241204061757_init/migration.sql +++ b/prisma/migrations/20241204061757_init/migration.sql @@ -1,7 +1,9 @@ --- CreateTable -CREATE TABLE "User" ( - "id" TEXT NOT NULL, - "name" TEXT NOT NULL, +---Add Ebitda margin where needed data exists +UPDATE "Deal" + SET "ebitdaMargin" = ("ebitda"/"revenue") + WHERE NOT "revenue" = 0 AND NOT "ebitda" = 0; - CONSTRAINT "User_pkey" PRIMARY KEY ("id") -); +---Set Ebitda Margin to -1 when necessary data does not exist +UPDATE "Deal" + SET "ebitdaMargin" = -1 + WHERE "revenue" = 0 OR "ebitda" = 0;