Skip to content

Commit

Permalink
chore: set price at the Ad Set level
Browse files Browse the repository at this point in the history
  • Loading branch information
IanKrieger committed Sep 21, 2023
1 parent 6034f53 commit b7f9c17
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 126 deletions.
10 changes: 6 additions & 4 deletions src/graphql/types.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 7 additions & 87 deletions src/user/library/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { CampaignFragment } from "graphql/campaign.generated";
import { describe, expect, it } from "vitest";
import {
editCampaignValues,
transformCreative,
transformEditForm,
transformNewForm,
} from ".";
import { editCampaignValues, transformEditForm, transformNewForm } from ".";
import {
CampaignFormat,
CampaignPacingStrategies,
Expand Down Expand Up @@ -137,7 +132,7 @@ describe("pricing logic (read)", () => {
});
});
const campaignForm = editCampaignValues(campaign, "abc");
expect(campaignForm.price).toEqual(7);
expect(campaignForm.price).toEqual("7");
expect(campaignForm.billingType).toEqual("cpm");
});

Expand All @@ -152,7 +147,7 @@ describe("pricing logic (read)", () => {
});
});
const campaignForm = editCampaignValues(campaign, "abc");
expect(campaignForm.price).toEqual(1);
expect(campaignForm.price).toEqual("1");
expect(campaignForm.billingType).toEqual("cpc");
});

Expand All @@ -161,56 +156,11 @@ describe("pricing logic (read)", () => {
c.adSets = [];
});
const formObject = editCampaignValues(campaign, "abc");
expect(formObject.price).toEqual(100);
expect(formObject.price).toEqual("100");
expect(formObject.billingType).toEqual("cpm");
});
});

describe("pricing logic (write)", () => {
const creative: Creative = {
payloadNotification: {
title: "some title",
body: "body",
targetUrl: "some url",
},
advertiserId: "some id",
state: "draft",
type: { code: "notification_all_v1" },
name: "some name",
included: true,
};

it("should convert from CPM to per-impression values when populating a CPM creative", () => {
const inputObject = transformCreative(creative, {
billingType: "cpm",
price: 9,
});

expect(inputObject.price).toEqual("0.009");
expect(inputObject.priceType).toEqual(ConfirmationType.View);
});

it("should not convert CPC to per-impression values when populating a CPC creative", () => {
const inputObject = transformCreative(creative, {
billingType: "cpc",
price: 9,
});

expect(inputObject.price).toEqual("9");
expect(inputObject.priceType).toEqual(ConfirmationType.Click);
});

it("should not convert CPV to per-impression values when populating a CPV creative", () => {
const inputObject = transformCreative(creative, {
billingType: "cpv",
price: 9,
});

expect(inputObject.price).toEqual("9");
expect(inputObject.priceType).toEqual(ConfirmationType.Landed);
});
});

describe("new form tests", () => {
const dateString = new Date().toLocaleString();

Expand Down Expand Up @@ -254,7 +204,7 @@ describe("new form tests", () => {
isCreating: false,
name: "Test",
paymentType: PaymentType.Radom,
price: 6,
price: "6",
startAt: dateString,
state: "draft",
type: "paid",
Expand All @@ -270,8 +220,6 @@ describe("new form tests", () => {
"ads": [
{
"creativeId": "11111",
"price": "0.006",
"priceType": "VIEW",
},
],
"billingType": "cpm",
Expand All @@ -284,6 +232,7 @@ describe("new form tests", () => {
},
],
"perDay": 1,
"price": "0.006",
"segments": [
{
"code": "5678",
Expand Down Expand Up @@ -316,29 +265,6 @@ describe("new form tests", () => {
}
`);
});

it("should transform a creative", () => {
creative.payloadNotification = {
title: "valid",
targetUrl: "valid",
body: "valid",
};

creative.payloadSearch = {
title: "invalid",
targetUrl: "invalid",
body: "invalid",
};

const res = transformCreative(creative, form);
expect(res).toMatchInlineSnapshot(`
{
"creativeId": "11111",
"price": "0.006",
"priceType": "VIEW",
}
`);
});
});

describe("edit form tests", () => {
Expand Down Expand Up @@ -569,7 +495,7 @@ describe("edit form tests", () => {
"isCreating": false,
"name": "My first campaign",
"paymentType": "RADOM",
"price": 6000,
"price": "6000",
"startAt": undefined,
"state": "active",
"type": "paid",
Expand All @@ -588,8 +514,6 @@ describe("edit form tests", () => {
{
"creativeId": "1234",
"creativeSetId": "11111",
"price": "6",
"priceType": "VIEW",
},
],
"id": "11111",
Expand All @@ -611,14 +535,10 @@ describe("edit form tests", () => {
{
"creativeId": "1234",
"creativeSetId": "22222",
"price": "6",
"priceType": "VIEW",
},
{
"creativeId": "1235",
"creativeSetId": "22222",
"price": "6",
"priceType": "VIEW",
},
],
"id": "22222",
Expand Down
41 changes: 8 additions & 33 deletions src/user/library/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {
CampaignFormat,
CampaignPacingStrategies,
ConfirmationType,
CreateAdInput,
CreateCampaignInput,
UpdateCampaignInput,
} from "graphql/types";
Expand Down Expand Up @@ -33,6 +31,7 @@ export function transformNewForm(
form: CampaignForm,
userId?: string,
): CreateCampaignInput {
const price = BigNumber(form.price);
return {
currency: form.currency,
externalId: "",
Expand All @@ -52,6 +51,10 @@ export function transformNewForm(
budget: form.budget,
adSets: form.adSets.map((adSet) => ({
name: adSet.name,
price:
form.billingType === "cpm"
? price.dividedBy(1000).toString()
: price.toString(),
billingType: form.billingType,
perDay: 1,
segments: adSet.segments.map((s) => ({ code: s.code, name: s.name })),
Expand All @@ -60,7 +63,7 @@ export function transformNewForm(
conversions: transformConversion(adSet.conversions),
ads: adSet.creatives
.filter((c) => c.included)
.map((ad) => transformCreative(ad, form)),
.map((ad) => ({ creativeId: ad.id })),
})),
paymentType: form.paymentType,
};
Expand All @@ -78,34 +81,6 @@ function transformConversion(conv: Conversion[]) {
}));
}

export function transformCreative(
creative: Creative,
campaign: Pick<CampaignForm, "price" | "billingType">,
): CreateAdInput {
let price: BigNumber;
let priceType: ConfirmationType;

if (campaign.billingType === "cpm") {
price = BigNumber(campaign.price).dividedBy(1000);
priceType = ConfirmationType.View;
} else if (campaign.billingType === "cpv") {
price = BigNumber(campaign.price);
priceType = ConfirmationType.Landed;
} else {
price = BigNumber(campaign.price);
priceType = ConfirmationType.Click;
}

const createInput: CreateAdInput = {
price: price.toString(),
priceType: priceType,
};

createInput.creativeId = creative.id;

return createInput;
}

export function editCampaignValues(
campaign: CampaignFragment,
advertiserId: string,
Expand Down Expand Up @@ -147,7 +122,7 @@ export function editCampaignValues(
advertiserId,
newCreative: initialCreative,
currency: campaign.currency,
price: price.toNumber(),
price: price.toString(),
billingType: billingType,
validateStart: false,
budget: campaign.budget,
Expand Down Expand Up @@ -243,7 +218,7 @@ export function transformEditForm(
ads: adSet.creatives
.filter((c) => c.included)
.map((ad) => ({
...transformCreative(ad, form),
creativeId: ad.id,
creativeSetId: adSet.id,
})),
})),
Expand Down
4 changes: 2 additions & 2 deletions src/user/views/adsManager/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type CampaignForm = {
state: string;
type: "paid";
// this is per click for CPC campaigns, but per thousand views for CPM campaigns
price: number;
price: string;
billingType: Billing;
paymentType: PaymentType;
};
Expand Down Expand Up @@ -118,7 +118,7 @@ export const initialCampaign = (advertiser: IAdvertiser): CampaignForm => {
newCreative: initialCreative,
billingType: "cpm",
currency: "USD",
price: 6,
price: "6",
adSets: [
{
...initialAdSet,
Expand Down

0 comments on commit b7f9c17

Please sign in to comment.