From 8621bb37ffe6f6a6e366217b484e9d905bbfea6d Mon Sep 17 00:00:00 2001 From: Ian Krieger Date: Mon, 25 Sep 2023 11:02:24 -0400 Subject: [PATCH] fix: use method for price --- src/user/library/index.test.ts | 40 +++++++++++++++++++++++++++++++++- src/user/library/index.ts | 17 ++++++++++----- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/user/library/index.test.ts b/src/user/library/index.test.ts index 548fc1ad..762b3dd9 100644 --- a/src/user/library/index.test.ts +++ b/src/user/library/index.test.ts @@ -1,6 +1,11 @@ import { CampaignFragment } from "graphql/campaign.generated"; import { describe, expect, it } from "vitest"; -import { editCampaignValues, transformEditForm, transformNewForm } from "."; +import { + editCampaignValues, + transformEditForm, + transformNewForm, + transformPrice, +} from "."; import { CampaignFormat, CampaignPacingStrategies, @@ -157,6 +162,35 @@ describe("pricing logic (read)", () => { }); }); +describe("pricing logic (write)", () => { + it("should convert from CPM to per-impression values when populating a CPM creative", () => { + const result = transformPrice({ + billingType: "cpm", + price: "9", + }); + + expect(result).toEqual("0.009"); + }); + + it("should not convert CPC to per-impression values when populating a CPC creative", () => { + const result = transformPrice({ + billingType: "cpc", + price: "9", + }); + + expect(result).toEqual("9"); + }); + + it("should not convert CPV to per-impression values when populating a CPV creative", () => { + const result = transformPrice({ + billingType: "cpv", + price: "9", + }); + + expect(result).toEqual("9"); + }); +}); + describe("new form tests", () => { const dateString = new Date().toLocaleString(); @@ -504,6 +538,7 @@ describe("edit form tests", () => { "creativeSetId": "11111", }, ], + "billingType": "cpm", "id": "11111", "oses": [ { @@ -511,6 +546,7 @@ describe("edit form tests", () => { "name": "macos", }, ], + "price": "6", "segments": [ { "code": "5678", @@ -525,6 +561,7 @@ describe("edit form tests", () => { "creativeSetId": "22222", }, ], + "billingType": "cpm", "id": "22222", "oses": [ { @@ -532,6 +569,7 @@ describe("edit form tests", () => { "name": "linux", }, ], + "price": "6", "segments": [ { "code": "5678", diff --git a/src/user/library/index.ts b/src/user/library/index.ts index 22e9002c..63d2f8ba 100644 --- a/src/user/library/index.ts +++ b/src/user/library/index.ts @@ -31,7 +31,6 @@ export function transformNewForm( form: CampaignForm, userId?: string, ): CreateCampaignInput { - const price = BigNumber(form.price); return { currency: form.currency, externalId: "", @@ -51,10 +50,7 @@ export function transformNewForm( budget: form.budget, adSets: form.adSets.map((adSet) => ({ name: adSet.name, - price: - form.billingType === "cpm" - ? price.dividedBy(1000).toString() - : price.toString(), + price: transformPrice(form), billingType: form.billingType, perDay: form.format === CampaignFormat.PushNotification ? 4 : 6, segments: adSet.segments.map((s) => ({ code: s.code, name: s.name })), @@ -69,6 +65,15 @@ export function transformNewForm( }; } +export const transformPrice = ( + f: Pick, +) => { + const price = BigNumber(f.price); + return f.billingType === "cpm" + ? price.dividedBy(1000).toString() + : price.toString(); +}; + function transformConversion(conv: Conversion[]) { if (conv.length <= 0) { return []; @@ -210,6 +215,8 @@ export function transformEditForm( paymentType: form.paymentType, adSets: form.adSets.map((adSet) => ({ id: adSet.id, + billingType: form.billingType, + price: transformPrice(form), segments: adSet.segments.map((v) => ({ code: v.code, name: v.name })), oses: adSet.oses.map((v) => ({ code: v.code, name: v.name })), ads: adSet.creatives