Skip to content

Commit

Permalink
fix: use method for price
Browse files Browse the repository at this point in the history
  • Loading branch information
IanKrieger committed Sep 25, 2023
1 parent 647c49a commit 8621bb3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
40 changes: 39 additions & 1 deletion src/user/library/index.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -504,13 +538,15 @@ describe("edit form tests", () => {
"creativeSetId": "11111",
},
],
"billingType": "cpm",
"id": "11111",
"oses": [
{
"code": "1234",
"name": "macos",
},
],
"price": "6",
"segments": [
{
"code": "5678",
Expand All @@ -525,13 +561,15 @@ describe("edit form tests", () => {
"creativeSetId": "22222",
},
],
"billingType": "cpm",
"id": "22222",
"oses": [
{
"code": "1234",
"name": "linux",
},
],
"price": "6",
"segments": [
{
"code": "5678",
Expand Down
17 changes: 12 additions & 5 deletions src/user/library/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export function transformNewForm(
form: CampaignForm,
userId?: string,
): CreateCampaignInput {
const price = BigNumber(form.price);
return {
currency: form.currency,
externalId: "",
Expand All @@ -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 })),
Expand All @@ -69,6 +65,15 @@ export function transformNewForm(
};
}

export const transformPrice = (
f: Pick<CampaignForm, "price" | "billingType">,
) => {
const price = BigNumber(f.price);
return f.billingType === "cpm"
? price.dividedBy(1000).toString()
: price.toString();
};

function transformConversion(conv: Conversion[]) {
if (conv.length <= 0) {
return [];
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8621bb3

Please sign in to comment.