Skip to content

Commit

Permalink
fix: include price in graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
IanKrieger committed Sep 25, 2023
1 parent d2f425e commit 212d230
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 42 deletions.
4 changes: 4 additions & 0 deletions src/graphql/ad-set.generated.tsx

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

1 change: 1 addition & 0 deletions src/graphql/ad-set.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
fragment AdSet on AdSet {
id
price
createdAt
billingType
name
Expand Down
4 changes: 4 additions & 0 deletions src/graphql/campaign.generated.tsx

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

58 changes: 21 additions & 37 deletions src/user/library/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { AdSetForm, CampaignForm, Creative } from "user/views/adsManager/types";
import _ from "lodash";
import { AdFragment, AdSetFragment } from "graphql/ad-set.generated";
import { CreativeFragment } from "graphql/creative.generated";
import { DeepPartial } from "@apollo/client/utilities";

const BASE_CPM_CAMPAIGN_FRAGMENT: Readonly<CampaignFragment> = {
id: "3495317a-bb47-4daf-8d3e-14cdc0e87457",
Expand Down Expand Up @@ -45,6 +46,7 @@ const BASE_CPM_CAMPAIGN_FRAGMENT: Readonly<CampaignFragment> = {
{
id: "39644642-b56a-430a-90f8-8917651bb62f",
createdAt: "2023-07-11T16:13:31.286Z",
price: "0.006",
billingType: "cpm",
name: "Demo ad set",
totalMax: 10,
Expand Down Expand Up @@ -123,12 +125,9 @@ describe("pricing logic (read)", () => {

it("should convert per-impression values to CPM when populating CampaignForm", () => {
const campaign = produce(BASE_CPM_CAMPAIGN_FRAGMENT, (c) => {
c.adSets.forEach((adset) => {
(c.adSets ?? []).forEach((adset) => {
adset.billingType = "cpm";
adset.ads?.forEach((ad) => {
ad.price = "0.007";
ad.priceType = ConfirmationType.View;
});
adset.price = "0.007";
});
});
const campaignForm = editCampaignValues(campaign, "abc");
Expand All @@ -140,10 +139,7 @@ describe("pricing logic (read)", () => {
const campaign = produce(BASE_CPM_CAMPAIGN_FRAGMENT, (c) => {
c.adSets.forEach((adset) => {
adset.billingType = "cpc";
adset.ads?.forEach((ad) => {
ad.price = "1";
ad.priceType = ConfirmationType.View;
});
adset.price = "1";
});
});
const campaignForm = editCampaignValues(campaign, "abc");
Expand Down Expand Up @@ -282,36 +278,23 @@ describe("edit form tests", () => {
type: { code: "notification_v1_all" },
};

const ad: AdFragment = {
const ad: Partial<AdFragment> = {
id: "1",
creative: creative,
state: "active",
price: "6",
priceType: ConfirmationType.View,
};

const ad2: AdFragment = {
id: "2",
creative: creative,
state: "deleted",
price: "6",
priceType: ConfirmationType.View,
};

const ad3: AdFragment = {
const ad2: Partial<AdFragment> = {
id: "3",
creative: {
...creative,
id: "1235",
name: "a different creative",
},
state: "active",
price: "6",
priceType: ConfirmationType.View,
};

const adSet: AdSetFragment = {
const adSet: DeepPartial<AdSetFragment> = {
ads: [ad, ad2],
price: "6",
billingType: "cpm",
conversions: [],
createdAt: undefined,
Expand All @@ -323,8 +306,9 @@ describe("edit form tests", () => {
totalMax: 100,
};

const adSet2: AdSetFragment = {
ads: [ad, ad3],
const adSet2: DeepPartial<AdSetFragment> = {
ads: [ad],
price: "6",
billingType: "cpm",
conversions: [],
createdAt: undefined,
Expand All @@ -336,7 +320,7 @@ describe("edit form tests", () => {
totalMax: 100,
};

const campaignFragment: CampaignFragment = {
const campaignFragment: DeepPartial<CampaignFragment> = {
adSets: [adSet, adSet2],
advertiser: { id: "12345" },
budget: 100,
Expand All @@ -362,8 +346,8 @@ describe("edit form tests", () => {
};

const editForm = editCampaignValues(
campaignFragment,
campaignFragment.advertiser.id,
campaignFragment as CampaignFragment,
campaignFragment?.advertiser?.id ?? "",
);
it("should result in a valid campaign form", () => {
const omitted = _.omit(editForm, ["newCreative"]);
Expand Down Expand Up @@ -395,7 +379,7 @@ describe("edit form tests", () => {
"advertiserId": "12345",
"createdAt": undefined,
"id": "1235",
"included": false,
"included": true,
"name": "a different creative",
"payloadInlineContent": undefined,
"payloadNotification": {
Expand Down Expand Up @@ -451,7 +435,7 @@ describe("edit form tests", () => {
"advertiserId": "12345",
"createdAt": undefined,
"id": "1235",
"included": true,
"included": false,
"name": "a different creative",
"payloadInlineContent": undefined,
"payloadNotification": {
Expand Down Expand Up @@ -515,6 +499,10 @@ describe("edit form tests", () => {
"creativeId": "1234",
"creativeSetId": "11111",
},
{
"creativeId": "1235",
"creativeSetId": "11111",
},
],
"id": "11111",
"oses": [
Expand All @@ -536,10 +524,6 @@ describe("edit form tests", () => {
"creativeId": "1234",
"creativeSetId": "22222",
},
{
"creativeId": "1235",
"creativeSetId": "22222",
},
],
"id": "22222",
"oses": [
Expand Down
7 changes: 2 additions & 5 deletions src/user/library/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,11 @@ export function editCampaignValues(
campaign: CampaignFragment,
advertiserId: string,
): CampaignForm {
const ads: AdFragment[] = _.filter(
_.flatMap(campaign.adSets, "ads"),
(a) => a.state !== "deleted",
);
const ads: AdFragment[] = _.flatMap(campaign.adSets, "ads");

const billingType = (_.head(campaign.adSets)?.billingType ??
"cpm") as Billing;
const rawPrice = BigNumber(_.head(ads)?.price ?? "0.1");
const rawPrice = BigNumber(_.head(campaign.adSets)?.price ?? "0.1");
const price = billingType === "cpm" ? rawPrice.multipliedBy(1000) : rawPrice;

return {
Expand Down

0 comments on commit 212d230

Please sign in to comment.