fix discount creation
This commit is contained in:
parent
c53bc163b2
commit
029f9a763d
@ -1,4 +1,4 @@
|
|||||||
import { CreateAnyDiscountBody, CreateCartDiscountBody, CreateDiscountBodyBase, CreateLoyaltyDiscountBody, CreateProductDiscountBody, CreateServiceDiscountBody, Discount, DiscountType } from "@root/model/discount";
|
import { CreateDiscountBody, Discount, DiscountType } from "@root/model/discount";
|
||||||
import { ServiceType } from "@root/model/tariff";
|
import { ServiceType } from "@root/model/tariff";
|
||||||
import { authStore } from "@root/stores/auth";
|
import { authStore } from "@root/stores/auth";
|
||||||
|
|
||||||
@ -12,9 +12,9 @@ type CreateDiscount = (props: {
|
|||||||
discountFactor: number;
|
discountFactor: number;
|
||||||
discountDescription: string;
|
discountDescription: string;
|
||||||
discountName: string;
|
discountName: string;
|
||||||
/** ISO date string */
|
/** ISO string */
|
||||||
startDate: string;
|
startDate: string;
|
||||||
/** ISO date string */
|
/** ISO string */
|
||||||
endDate: string;
|
endDate: string;
|
||||||
serviceType: ServiceType;
|
serviceType: ServiceType;
|
||||||
discountType: DiscountType;
|
discountType: DiscountType;
|
||||||
@ -34,8 +34,9 @@ export const createDiscountJSON: CreateDiscount = ({
|
|||||||
discountType,
|
discountType,
|
||||||
privilegeId,
|
privilegeId,
|
||||||
}) => {
|
}) => {
|
||||||
let discountBase: CreateDiscountBodyBase = {
|
const discount: CreateDiscountBody = {
|
||||||
Name: discountName,
|
Name: discountName,
|
||||||
|
Layer: 1,
|
||||||
Description: discountDescription,
|
Description: discountDescription,
|
||||||
Condition: {
|
Condition: {
|
||||||
Period: {
|
Period: {
|
||||||
@ -45,42 +46,49 @@ export const createDiscountJSON: CreateDiscount = ({
|
|||||||
User: "",
|
User: "",
|
||||||
UserType: "",
|
UserType: "",
|
||||||
Coupon: "",
|
Coupon: "",
|
||||||
Usage: "",
|
Usage: 0,
|
||||||
|
PurchasesAmount: 0,
|
||||||
|
CartPurchasesAmount: 0,
|
||||||
|
Product: "",
|
||||||
|
Term: 0,
|
||||||
|
PriceFrom: 0,
|
||||||
|
Group: "",
|
||||||
},
|
},
|
||||||
Target: {
|
Target: {
|
||||||
Factor: discountFactor,
|
Factor: discountFactor,
|
||||||
TargetScope: "Sum",
|
TargetScope: "Sum",
|
||||||
Overhelm: false,
|
Overhelm: false,
|
||||||
|
TargetGroup: "",
|
||||||
|
Products: [{
|
||||||
|
ID: "",
|
||||||
|
Factor: 0,
|
||||||
|
Overhelm: false,
|
||||||
|
}],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let discount;
|
|
||||||
switch (discountType) {
|
switch (discountType) {
|
||||||
case "privilege":
|
case "privilege":
|
||||||
discount = discountBase as CreateProductDiscountBody;
|
|
||||||
discount.Layer = 1;
|
discount.Layer = 1;
|
||||||
discount.Condition.Product = privilegeId;
|
discount.Condition.Product = privilegeId;
|
||||||
discount.Condition.Term = discountMinValue.toString();
|
discount.Condition.Term = discountMinValue;
|
||||||
discount.Target.Products = [{
|
discount.Target.Products = [{
|
||||||
Factor: discountFactor,
|
Factor: discountFactor,
|
||||||
ID: "",
|
ID: privilegeId,
|
||||||
Overhelm: false,
|
Overhelm: false,
|
||||||
}];
|
}];
|
||||||
break;
|
break;
|
||||||
case "service":
|
case "service":
|
||||||
discount = discountBase as CreateServiceDiscountBody;
|
|
||||||
discount.Layer = 2;
|
discount.Layer = 2;
|
||||||
discount.Condition.PriceFrom = discountMinValue;
|
discount.Condition.PriceFrom = discountMinValue;
|
||||||
discount.Condition.Group = serviceType;
|
discount.Condition.Group = serviceType;
|
||||||
discount.Target.TargetGroup = serviceType;
|
discount.Target.TargetGroup = serviceType;
|
||||||
break;
|
break;
|
||||||
case "cartPurchasesAmount":
|
case "cartPurchasesAmount":
|
||||||
discount = discountBase as CreateCartDiscountBody;
|
|
||||||
discount.Layer = 3;
|
discount.Layer = 3;
|
||||||
discount.Condition.CartPurchasesAmount = cartPurchasesAmount;
|
discount.Condition.CartPurchasesAmount = cartPurchasesAmount;
|
||||||
break;
|
break;
|
||||||
case "purchasesAmount":
|
case "purchasesAmount":
|
||||||
discount = discountBase as CreateLoyaltyDiscountBody;
|
|
||||||
discount.Layer = 4;
|
discount.Layer = 4;
|
||||||
discount.Condition.PurchasesAmount = purchasesAmount;
|
discount.Condition.PurchasesAmount = purchasesAmount;
|
||||||
break;
|
break;
|
||||||
@ -88,7 +96,7 @@ export const createDiscountJSON: CreateDiscount = ({
|
|||||||
|
|
||||||
console.log("Constructed discount", discount);
|
console.log("Constructed discount", discount);
|
||||||
|
|
||||||
return makeRequest<CreateAnyDiscountBody, Discount>({
|
return makeRequest<CreateDiscountBody, Discount>({
|
||||||
url: "https://admin.pena.digital/price/discount",
|
url: "https://admin.pena.digital/price/discount",
|
||||||
method: "post",
|
method: "post",
|
||||||
useToken: true,
|
useToken: true,
|
||||||
@ -96,62 +104,3 @@ export const createDiscountJSON: CreateDiscount = ({
|
|||||||
body: discount,
|
body: discount,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const createDiscountFormData: CreateDiscount = ({
|
|
||||||
endDate,
|
|
||||||
startDate,
|
|
||||||
discountName,
|
|
||||||
cartPurchasesAmount,
|
|
||||||
discountDescription,
|
|
||||||
discountFactor,
|
|
||||||
discountMinValue,
|
|
||||||
purchasesAmount,
|
|
||||||
serviceType,
|
|
||||||
discountType,
|
|
||||||
privilegeId,
|
|
||||||
}) => {
|
|
||||||
const formdata = new FormData();
|
|
||||||
|
|
||||||
formdata.set("Name", discountName);
|
|
||||||
formdata.set("Description", discountDescription);
|
|
||||||
formdata.set("Condition.Period.From", startDate);
|
|
||||||
formdata.set("Condition.Period.To", endDate);
|
|
||||||
formdata.set("Condition.User", "");
|
|
||||||
formdata.set("Condition.UserType", "");
|
|
||||||
formdata.set("Condition.Coupon", "");
|
|
||||||
formdata.set("Condition.Usage", "");
|
|
||||||
formdata.set("Condition.Target.Factor", discountFactor.toString());
|
|
||||||
formdata.set("Condition.Target.FactoTargetScope", "Sum");
|
|
||||||
formdata.set("Condition.Target.Overhelm", "false");
|
|
||||||
|
|
||||||
switch (discountType) {
|
|
||||||
case "privilege":
|
|
||||||
formdata.set("discount.Layer", "1");
|
|
||||||
formdata.set("discount.Condition.Product", privilegeId);
|
|
||||||
formdata.set("discount.Condition.Term", discountMinValue.toString());
|
|
||||||
formdata.set("discount.Target.Products.Factor", discountMinValue.toString());
|
|
||||||
break;
|
|
||||||
case "service":
|
|
||||||
formdata.set("discount.Layer", "2");
|
|
||||||
formdata.set("discount.Condition.PriceFrom", discountMinValue.toString());
|
|
||||||
formdata.set("discount.Condition.Group", serviceType);
|
|
||||||
formdata.set("discount.Target.TargetGroup", serviceType);
|
|
||||||
break;
|
|
||||||
case "cartPurchasesAmount":
|
|
||||||
formdata.set("discount.Layer", "3");
|
|
||||||
formdata.set("discount.Condition.CartPurchasesAmount", cartPurchasesAmount.toString());
|
|
||||||
break;
|
|
||||||
case "purchasesAmount":
|
|
||||||
formdata.set("discount.Layer", "4");
|
|
||||||
formdata.set("discount.Condition.PurchasesAmount", purchasesAmount.toString());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return makeRequest<FormData, Discount>({
|
|
||||||
url: "https://admin.pena.digital/price/discount",
|
|
||||||
method: "post",
|
|
||||||
useToken: true,
|
|
||||||
bearer: true,
|
|
||||||
body: formdata,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
@ -55,47 +55,33 @@ export const discountTypes = {
|
|||||||
|
|
||||||
export type DiscountType = keyof typeof discountTypes;
|
export type DiscountType = keyof typeof discountTypes;
|
||||||
|
|
||||||
export type CreateDiscountBodyBase = {
|
export type CreateDiscountBody = {
|
||||||
Name: string;
|
Name: string;
|
||||||
|
Layer: 1 | 2 | 3 | 4;
|
||||||
Description: string;
|
Description: string;
|
||||||
Condition: {
|
Condition: {
|
||||||
Period: {
|
Period: {
|
||||||
|
/** ISO string */
|
||||||
From: string;
|
From: string;
|
||||||
|
/** ISO string */
|
||||||
To: string;
|
To: string;
|
||||||
};
|
};
|
||||||
User: string;
|
User: string;
|
||||||
UserType: string;
|
UserType: string;
|
||||||
Coupon: string;
|
Coupon: string;
|
||||||
Usage: string;
|
PurchasesAmount: number;
|
||||||
|
CartPurchasesAmount: number;
|
||||||
|
Product: string;
|
||||||
|
Term: number;
|
||||||
|
Usage: number;
|
||||||
|
PriceFrom: number;
|
||||||
|
Group: ServiceType | "";
|
||||||
};
|
};
|
||||||
Target: {
|
Target: {
|
||||||
Factor: number;
|
Factor: number;
|
||||||
TargetScope: "Sum" | "Group" | "Each";
|
TargetScope: "Sum" | "Group" | "Each";
|
||||||
Overhelm: boolean;
|
Overhelm: boolean;
|
||||||
};
|
TargetGroup: ServiceType | "";
|
||||||
};
|
|
||||||
|
|
||||||
export type CreateLoyaltyDiscountBody = CreateDiscountBodyBase & {
|
|
||||||
Layer: 4;
|
|
||||||
Condition: {
|
|
||||||
PurchasesAmount: number;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
export type CreateCartDiscountBody = CreateDiscountBodyBase & {
|
|
||||||
Layer: 3;
|
|
||||||
Condition: {
|
|
||||||
CartPurchasesAmount: number;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
export type CreateProductDiscountBody = CreateDiscountBodyBase & {
|
|
||||||
Layer: 1;
|
|
||||||
Condition: {
|
|
||||||
Product: string;
|
|
||||||
Term: string;
|
|
||||||
};
|
|
||||||
Target: {
|
|
||||||
Products: [{
|
Products: [{
|
||||||
ID: string;
|
ID: string;
|
||||||
Factor: number;
|
Factor: number;
|
||||||
@ -103,20 +89,3 @@ export type CreateProductDiscountBody = CreateDiscountBodyBase & {
|
|||||||
}];
|
}];
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CreateServiceDiscountBody = CreateDiscountBodyBase & {
|
|
||||||
Layer: 2;
|
|
||||||
Condition: {
|
|
||||||
PriceFrom: number;
|
|
||||||
Group: ServiceType;
|
|
||||||
};
|
|
||||||
Target: {
|
|
||||||
TargetGroup: ServiceType;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
export type CreateAnyDiscountBody =
|
|
||||||
| CreateLoyaltyDiscountBody
|
|
||||||
| CreateCartDiscountBody
|
|
||||||
| CreateProductDiscountBody
|
|
||||||
| CreateServiceDiscountBody;
|
|
@ -4,11 +4,11 @@ import Select, { SelectChangeEvent } from "@mui/material/Select";
|
|||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { SERVICE_LIST, ServiceType } from "@root/model/tariff";
|
import { SERVICE_LIST, ServiceType } from "@root/model/tariff";
|
||||||
import { CustomTextField } from "@root/kitUI/CustomTextField";
|
import { CustomTextField } from "@root/kitUI/CustomTextField";
|
||||||
import { addRealPrivileges, usePrivilegeStore, useRealPrivilegeStore } from "@root/stores/privileges";
|
import { addRealPrivileges, useRealPrivilegeStore } from "@root/stores/privileges";
|
||||||
import { addDiscounts } from "@root/stores/discounts";
|
import { addDiscounts } from "@root/stores/discounts";
|
||||||
import { enqueueSnackbar } from "notistack";
|
import { enqueueSnackbar } from "notistack";
|
||||||
import { DiscountType, discountTypes } from "@root/model/discount";
|
import { DiscountType, discountTypes } from "@root/model/discount";
|
||||||
import { createDiscountJSON, createDiscountFormData } from "@root/api/discounts";
|
import { createDiscountJSON } from "@root/api/discounts";
|
||||||
import usePrivileges from "@root/utils/hooks/usePrivileges";
|
import usePrivileges from "@root/utils/hooks/usePrivileges";
|
||||||
|
|
||||||
|
|
||||||
@ -45,7 +45,7 @@ export default function CreateDiscount() {
|
|||||||
if (!isFinite(discountFactor)) return enqueueSnackbar("Поле discountFactor не число");
|
if (!isFinite(discountFactor)) return enqueueSnackbar("Поле discountFactor не число");
|
||||||
if (!isFinite(cartPurchasesAmount)) return enqueueSnackbar("Поле cartPurchasesAmount не число");
|
if (!isFinite(cartPurchasesAmount)) return enqueueSnackbar("Поле cartPurchasesAmount не число");
|
||||||
if (!isFinite(discountMinValue)) return enqueueSnackbar("Поле discountMinValue не число");
|
if (!isFinite(discountMinValue)) return enqueueSnackbar("Поле discountMinValue не число");
|
||||||
if (!privilegeIdField) return enqueueSnackbar("Привилегия не выбрана");
|
if (discountType === "privilege" && !privilegeIdField) return enqueueSnackbar("Привилегия не выбрана");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const createdDiscount = await createDiscountJSON({
|
const createdDiscount = await createDiscountJSON({
|
||||||
|
Loading…
Reference in New Issue
Block a user