Skip to content

Commit

Permalink
fix billing_cycle_anchor validation bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ding-stripe committed Jun 16, 2022
1 parent 861a95c commit c814250
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
25 changes: 11 additions & 14 deletions billing-simulator/components/SimulatorValidationSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,18 @@ import { calculateUpdate } from "../utils/timelineHelper";

function arrayUptoValidate(array) {
let length = array.length - 1;
console.log(length);
array.pop();
console.log(array);
return array.every((value, index) => {
let nextIndex = index + 1;
console.log("nextIndex:" + nextIndex);
console.log("value.up_to:" + value.up_to);
if (nextIndex < length) {
console.log("array[nextIndex].up_to:" + array[nextIndex].up_to);
console.log(
"value.up_to < array[nextIndex].up_to:" + value.up_to <
array[nextIndex].up_to
);
return value.up_to * 1 < array[nextIndex].up_to * 1;
} else {
return true;
}
});
}

function SimulatorValidationSchema(parameter) {
function SimulatorValidationSchema() {
return Yup.object({
create_date: Yup.string().required("Required").nullable(),
billing_cycle_anchor: Yup.string()
Expand All @@ -44,13 +35,19 @@ function SimulatorValidationSchema(parameter) {
name: "billing_cycle_anchor",
exclusive: false,
params: {},
message: `billing_cycle_anchor cannot be later than next natural billing date ${calculateUpdate(
parameter
)} for plan`,
message: `billing_cycle_anchor cannot be later than next natural billing date for plan`,
test: function (value) {
// You can access the price field with `this.parent`.
if (value !== null) {
return new Date(value) <= calculateUpdate(parameter);
return (
new Date(value) <=
calculateUpdate(
this.parent.trial_end,
this.parent.create_date,
this.parent.interval,
this.parent.interval_count
)
);
} else return true;
},
}),
Expand Down
4 changes: 2 additions & 2 deletions billing-simulator/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
XIcon,
} from "@heroicons/react/solid";

import { Formik, Form, Field, FieldArray, ErrorMessage } from "formik";
import { Formik, Form, Field, FieldArray } from "formik";
import SimulatorValidationSchema from "../components/SimulatorValidationSchema";

import * as htmlToImage from "html-to-image";
Expand Down Expand Up @@ -130,7 +130,7 @@ export default function Home() {
{ up_to: "inf", unit_amount: 1000, flat_amount: 0 },
],
}}
validationSchema={SimulatorValidationSchema(parameter)}
validationSchema={SimulatorValidationSchema()}
onSubmit={(values) => {
gtag.event({
action: "submit_form",
Expand Down
28 changes: 14 additions & 14 deletions billing-simulator/utils/timelineHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,20 +237,20 @@ export const widthCalculator = (eventPoint, windowWidth) => {
};
};

export const calculateUpdate = (parameter) => {
const endDate =
parameter.billing_cycle_anchor !== null
? parameter.billing_cycle_anchor
: parameter.trial_end !== null
? parameter.trial_end
: parameter.create_date;
export const calculateUpdate = (
trial_end,
create_date,
interval,
interval_count
) => {
const endDate = trial_end !== null ? trial_end : create_date;
const updateDate =
parameter.interval === "year"
? addYears(new Date(endDate), parameter.interval_count * 1)
: parameter.interval === "month"
? addMonths(new Date(endDate), parameter.interval_count * 1)
: parameter.interval === "week"
? addWeeks(new Date(endDate), parameter.interval_count * 1)
: addDays(new Date(endDate), parameter.interval_count * 1);
interval === "year"
? addYears(new Date(endDate), interval_count * 1)
: interval === "month"
? addMonths(new Date(endDate), interval_count * 1)
: interval === "week"
? addWeeks(new Date(endDate), interval_count * 1)
: addDays(new Date(endDate), interval_count * 1);
return updateDate;
};

0 comments on commit c814250

Please sign in to comment.