diff --git a/src/validation/CampaignSchema.test.ts b/src/validation/CampaignSchema.test.ts index ba2b81027..87390cdc4 100644 --- a/src/validation/CampaignSchema.test.ts +++ b/src/validation/CampaignSchema.test.ts @@ -1,7 +1,20 @@ import { parseISO } from "date-fns"; import { produce } from "immer"; -import { CampaignFormat, CampaignPacingStrategies } from "../graphql/types"; +import { + BillingType, + CampaignFormat, + CampaignPacingStrategies, +} from "graphql/types"; import { CampaignSchema } from "./CampaignSchema"; +import { AdvertiserPriceFragment } from "graphql/advertiser.generated"; + +const prices: AdvertiserPriceFragment[] = [ + { + format: CampaignFormat.PushNotification, + price: "6", + billingType: BillingType.Cpm, + }, +]; const validCampaign = { name: "some campaign", @@ -22,7 +35,7 @@ const validCampaign = { }; it("should pass on a valid object", () => { - CampaignSchema.validateSync(validCampaign); + CampaignSchema(prices).validateSync(validCampaign); }); it("should fail if the campaign start date is in past", () => { @@ -31,7 +44,7 @@ it("should fail if the campaign start date is in past", () => { }); expect(() => - CampaignSchema.validateSync(c), + CampaignSchema(prices).validateSync(c), ).toThrowErrorMatchingInlineSnapshot( `"Start Date must be minimum of 2 days from today"`, ); diff --git a/src/validation/CampaignSchema.tsx b/src/validation/CampaignSchema.tsx index 06c853eba..796156132 100644 --- a/src/validation/CampaignSchema.tsx +++ b/src/validation/CampaignSchema.tsx @@ -15,6 +15,7 @@ import { TrailingAsteriskRegex } from "validation/regex"; import { CreativeSchema } from "validation/CreativeSchema"; import { BillingType, CampaignFormat } from "graphql/types"; import { AdvertiserPriceFragment } from "graphql/advertiser.generated"; +import BigNumber from "bignumber.js"; export const MIN_PER_DAY = 33; export const MIN_PER_CAMPAIGN = 100; @@ -81,7 +82,7 @@ export const CampaignSchema = (prices: AdvertiserPriceFragment[]) => prices, CampaignFormat.PushNotification, BillingType.Cpc, - 0.1, + "0.1", schema, ), }) @@ -93,7 +94,7 @@ export const CampaignSchema = (prices: AdvertiserPriceFragment[]) => prices, CampaignFormat.PushNotification, BillingType.Cpm, - 5, + "5", schema, ), }) @@ -105,7 +106,7 @@ export const CampaignSchema = (prices: AdvertiserPriceFragment[]) => prices, CampaignFormat.NewsDisplayAd, BillingType.Cpm, - 9, + "9", schema, ), }) @@ -178,12 +179,15 @@ export function findPrice( prices: AdvertiserPriceFragment[], format: CampaignFormat, billingType: BillingType, - defaultPrice: number, + defaultPrice: string, schema: NumberSchema, ) { const found = prices.find( (p) => p.format === format && p.billingType === billingType, ); - const price = Number(found?.price) ?? defaultPrice; - return schema.min(price, `${billingType} price must be ${price} or higher`); + const price = BigNumber(found?.price ?? defaultPrice); + return schema.min( + price.toNumber(), + `${billingType} price must be ${price} or higher`, + ); }