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 { authStore } from "@root/stores/auth";
|
||||
|
||||
@ -12,9 +12,9 @@ type CreateDiscount = (props: {
|
||||
discountFactor: number;
|
||||
discountDescription: string;
|
||||
discountName: string;
|
||||
/** ISO date string */
|
||||
/** ISO string */
|
||||
startDate: string;
|
||||
/** ISO date string */
|
||||
/** ISO string */
|
||||
endDate: string;
|
||||
serviceType: ServiceType;
|
||||
discountType: DiscountType;
|
||||
@ -34,8 +34,9 @@ export const createDiscountJSON: CreateDiscount = ({
|
||||
discountType,
|
||||
privilegeId,
|
||||
}) => {
|
||||
let discountBase: CreateDiscountBodyBase = {
|
||||
const discount: CreateDiscountBody = {
|
||||
Name: discountName,
|
||||
Layer: 1,
|
||||
Description: discountDescription,
|
||||
Condition: {
|
||||
Period: {
|
||||
@ -45,42 +46,49 @@ export const createDiscountJSON: CreateDiscount = ({
|
||||
User: "",
|
||||
UserType: "",
|
||||
Coupon: "",
|
||||
Usage: "",
|
||||
Usage: 0,
|
||||
PurchasesAmount: 0,
|
||||
CartPurchasesAmount: 0,
|
||||
Product: "",
|
||||
Term: 0,
|
||||
PriceFrom: 0,
|
||||
Group: "",
|
||||
},
|
||||
Target: {
|
||||
Factor: discountFactor,
|
||||
TargetScope: "Sum",
|
||||
Overhelm: false,
|
||||
TargetGroup: "",
|
||||
Products: [{
|
||||
ID: "",
|
||||
Factor: 0,
|
||||
Overhelm: false,
|
||||
}],
|
||||
},
|
||||
};
|
||||
|
||||
let discount;
|
||||
switch (discountType) {
|
||||
case "privilege":
|
||||
discount = discountBase as CreateProductDiscountBody;
|
||||
discount.Layer = 1;
|
||||
discount.Condition.Product = privilegeId;
|
||||
discount.Condition.Term = discountMinValue.toString();
|
||||
discount.Condition.Term = discountMinValue;
|
||||
discount.Target.Products = [{
|
||||
Factor: discountFactor,
|
||||
ID: "",
|
||||
ID: privilegeId,
|
||||
Overhelm: false,
|
||||
}];
|
||||
break;
|
||||
case "service":
|
||||
discount = discountBase as CreateServiceDiscountBody;
|
||||
discount.Layer = 2;
|
||||
discount.Condition.PriceFrom = discountMinValue;
|
||||
discount.Condition.Group = serviceType;
|
||||
discount.Target.TargetGroup = serviceType;
|
||||
break;
|
||||
case "cartPurchasesAmount":
|
||||
discount = discountBase as CreateCartDiscountBody;
|
||||
discount.Layer = 3;
|
||||
discount.Condition.CartPurchasesAmount = cartPurchasesAmount;
|
||||
break;
|
||||
case "purchasesAmount":
|
||||
discount = discountBase as CreateLoyaltyDiscountBody;
|
||||
discount.Layer = 4;
|
||||
discount.Condition.PurchasesAmount = purchasesAmount;
|
||||
break;
|
||||
@ -88,7 +96,7 @@ export const createDiscountJSON: CreateDiscount = ({
|
||||
|
||||
console.log("Constructed discount", discount);
|
||||
|
||||
return makeRequest<CreateAnyDiscountBody, Discount>({
|
||||
return makeRequest<CreateDiscountBody, Discount>({
|
||||
url: "https://admin.pena.digital/price/discount",
|
||||
method: "post",
|
||||
useToken: true,
|
||||
@ -96,62 +104,3 @@ export const createDiscountJSON: CreateDiscount = ({
|
||||
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,68 +55,37 @@ export const discountTypes = {
|
||||
|
||||
export type DiscountType = keyof typeof discountTypes;
|
||||
|
||||
export type CreateDiscountBodyBase = {
|
||||
export type CreateDiscountBody = {
|
||||
Name: string;
|
||||
Layer: 1 | 2 | 3 | 4;
|
||||
Description: string;
|
||||
Condition: {
|
||||
Period: {
|
||||
/** ISO string */
|
||||
From: string;
|
||||
/** ISO string */
|
||||
To: string;
|
||||
};
|
||||
User: string;
|
||||
UserType: string;
|
||||
Coupon: string;
|
||||
Usage: string;
|
||||
PurchasesAmount: number;
|
||||
CartPurchasesAmount: number;
|
||||
Product: string;
|
||||
Term: number;
|
||||
Usage: number;
|
||||
PriceFrom: number;
|
||||
Group: ServiceType | "";
|
||||
};
|
||||
Target: {
|
||||
Factor: number;
|
||||
TargetScope: "Sum" | "Group" | "Each";
|
||||
Overhelm: boolean;
|
||||
};
|
||||
};
|
||||
|
||||
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: {
|
||||
TargetGroup: ServiceType | "";
|
||||
Products: [{
|
||||
ID: string;
|
||||
Factor: number;
|
||||
Overhelm: false;
|
||||
}];
|
||||
};
|
||||
};
|
||||
|
||||
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 { SERVICE_LIST, ServiceType } from "@root/model/tariff";
|
||||
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 { enqueueSnackbar } from "notistack";
|
||||
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";
|
||||
|
||||
|
||||
@ -45,7 +45,7 @@ export default function CreateDiscount() {
|
||||
if (!isFinite(discountFactor)) return enqueueSnackbar("Поле discountFactor не число");
|
||||
if (!isFinite(cartPurchasesAmount)) return enqueueSnackbar("Поле cartPurchasesAmount не число");
|
||||
if (!isFinite(discountMinValue)) return enqueueSnackbar("Поле discountMinValue не число");
|
||||
if (!privilegeIdField) return enqueueSnackbar("Привилегия не выбрана");
|
||||
if (discountType === "privilege" && !privilegeIdField) return enqueueSnackbar("Привилегия не выбрана");
|
||||
|
||||
try {
|
||||
const createdDiscount = await createDiscountJSON({
|
||||
|
Loading…
Reference in New Issue
Block a user