Skip to content

Commit

Permalink
feat: check if max commits is greater than quantity in product creati…
Browse files Browse the repository at this point in the history
…on flow (#920)
albertfolch-redeemeum authored Nov 16, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent bff6331 commit 46ad9b1
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/components/product/utils/validationSchema.ts
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ import {
OPTIONS_LENGTH,
OPTIONS_PERIOD,
OPTIONS_UNIT,
ProductTypeValues,
TOKEN_CRITERIA,
TOKEN_TYPES,
TokenTypes
@@ -355,7 +356,42 @@ export const getTokenGatingValidationSchema = ({
}),
maxCommits: Yup.string()
.required(validationMessage.required)
.matches(/^\+?[1-9]\d*$/, "Value must greater than 0"),
.matches(/^\+?[1-9]\d*$/, "Value must greater than 0")
.test("notGreaterThan_initialQuantity", function (value, context) {
if (value && Number.isInteger(Number.parseInt(value))) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const formValues = context.from[1].value;
const isOneVariant =
formValues.productType.productVariant ===
ProductTypeValues.oneItemType;
const variantsQuantity = isOneVariant
? 0
: (
formValues.productVariants
.variants as Yup.InferType<ProductVariantsValidationSchema>["productVariants"]["variants"]
).reduce((acum, current) => {
acum = acum + current.quantity;
return acum;
}, 0);
const quantity = isOneVariant
? formValues.coreTermsOfSale.quantity
: variantsQuantity;

const asNumber = Number.parseInt(value);
const isGreater = asNumber > quantity;
if (isGreater) {
throw this.createError({
path: this.path,
message: isOneVariant
? `This is greater than quantity (${formValues.coreTermsOfSale.quantity})`
: `This is greater than adding up the quantity of all variants (${variantsQuantity})`
});
}
return true;
}
return false;
}),
tokenType: Yup.object({
value: Yup.string(),
label: Yup.string()

0 comments on commit 46ad9b1

Please sign in to comment.