From 490477539744145a96860445a5909482a7576919 Mon Sep 17 00:00:00 2001 From: ArtChaos189 Date: Tue, 13 Jun 2023 23:22:14 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B8=D0=B2=D0=B8=D0=BB=D0=B5?= =?UTF-8?q?=D0=B3=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/model/cart.ts | 197 +++++++++--------- src/model/discount.ts | 44 ++++ .../DiscountManagement/DiscountDataGrid.tsx | 18 +- 3 files changed, 154 insertions(+), 105 deletions(-) create mode 100644 src/model/discount.ts diff --git a/src/model/cart.ts b/src/model/cart.ts index a751a1f..1ab6cbc 100644 --- a/src/model/cart.ts +++ b/src/model/cart.ts @@ -1,145 +1,144 @@ import { ServiceType, Privilege, Tariff } from "./tariff"; - interface DiscountBase { - _id: string; - name: string; - description: string; - /** Этап применения скидки */ - layer?: number; - disabled?: boolean; + _id: string; + name: string; + description: string; + /** Этап применения скидки */ + layer?: number; + disabled?: boolean; } export interface PurchasesAmountDiscount extends DiscountBase { - conditionType: "purchasesAmount"; - condition: { - purchasesAmount: number; - }; - /** Множитель, на который умножается сумма при применении скидки */ - factor: number; + conditionType: "purchasesAmount"; + condition: { + purchasesAmount: number; + }; + /** Множитель, на который умножается сумма при применении скидки */ + factor: number; } export interface CartPurchasesAmountDiscount extends DiscountBase { - conditionType: "cartPurchasesAmount"; - condition: { - cartPurchasesAmount: number; - }; - /** Множитель, на который умножается сумма при применении скидки */ - factor: number; + conditionType: "cartPurchasesAmount"; + condition: { + cartPurchasesAmount: number; + }; + /** Множитель, на который умножается сумма при применении скидки */ + factor: number; } export interface PrivilegeDiscount extends DiscountBase { - conditionType: "privilege"; - condition: { - privilege: { - id: string; - /** Скидка применяется, если значение больше или равно этому значению */ - value: number; - }; - }; - target: { - products: Array<{ - privilegeId: string; - /** Множитель, на который умножается сумма при применении скидки */ - factor: number; - }>; + conditionType: "privilege"; + condition: { + privilege: { + id: string; + /** Скидка применяется, если значение больше или равно этому значению */ + value: number; }; + }; + target: { + products: Array<{ + privilegeId: string; + /** Множитель, на который умножается сумма при применении скидки */ + factor: number; + }>; + }; } export interface ServiceDiscount extends DiscountBase { - conditionType: "service"; - condition: { - service: { - id: ServiceType; - /** Скидка применяется, если значение больше или равно этому значению */ - value: number; - }; - }; - target: { - service: ServiceType; - /** Множитель, на который умножается сумма при применении скидки */ - factor: number; + conditionType: "service"; + condition: { + service: { + id: ServiceType; + /** Скидка применяется, если значение больше или равно этому значению */ + value: number; }; + }; + target: { + service: ServiceType; + /** Множитель, на который умножается сумма при применении скидки */ + factor: number; + }; } export interface UserTypeDiscount extends DiscountBase { - conditionType: "userType"; - condition: { - userType: string; - }; - target: { - IsAllProducts: boolean; - /** Множитель, на который умножается сумма при применении скидки */ - factor: number; - }; - overwhelm: boolean; + conditionType: "userType"; + condition: { + userType: string; + }; + target: { + IsAllProducts: boolean; + /** Множитель, на который умножается сумма при применении скидки */ + factor: number; + }; + overwhelm: boolean; } export interface UserDiscount extends DiscountBase { - conditionType: "user"; - condition: { - coupon: string; - user: string; - }; - target: { - products: Array<{ - privilegeId: string; - /** Множитель, на который умножается сумма при применении скидки */ - factor: number; - }>; - }; - overwhelm: boolean; + conditionType: "user"; + condition: { + coupon: string; + user: string; + }; + target: { + products: Array<{ + privilegeId: string; + /** Множитель, на который умножается сумма при применении скидки */ + factor: number; + }>; + }; + overwhelm: boolean; } export type AnyDiscount = - | PurchasesAmountDiscount - | CartPurchasesAmountDiscount - | PrivilegeDiscount - | ServiceDiscount - | UserTypeDiscount - | UserDiscount; + | PurchasesAmountDiscount + | CartPurchasesAmountDiscount + | PrivilegeDiscount + | ServiceDiscount + | UserTypeDiscount + | UserDiscount; export type DiscountConditionType = AnyDiscount["conditionType"]; export interface Promocode { - id: string; - name: string; - endless: boolean; - from: string; - dueTo: string; - privileges: Privilege[]; + id: string; + name: string; + endless: boolean; + from: string; + dueTo: string; + privileges: Privilege[]; } export interface CartItem { - id: string; - tariff: Tariff; - /** Посчитанная цена пункта корзины */ - price: number; + id: string; + tariff: Tariff; + /** Посчитанная цена пункта корзины */ + price: number; } /** Пункт корзины с уже примененными скидками */ export interface CartItemTotal { - /** Массив с id примененных скидок */ - envolvedDiscounts: (PrivilegeDiscount | UserDiscount)[]; - totalPrice: number; - tariff: Tariff; + /** Массив с id примененных скидок */ + envolvedDiscounts: (PrivilegeDiscount | UserDiscount)[]; + totalPrice: number; + tariff: Tariff; } export type ServiceToPriceMap = { - [Key in ServiceType]: number; + [Key in ServiceType]: number; }; export type ServiceToDiscountMap = { - [Key in ServiceType]: ServiceDiscount | null; + [Key in ServiceType]: ServiceDiscount | null; }; export interface CartTotal { - items: CartItemTotal[]; - totalPrice: number; - priceByService: ServiceToPriceMap; - /** Скидки по сервисам */ - discountsByService: ServiceToDiscountMap; - /** Учтенные скидки типов userType, cartPurchasesAmount, totalPurchasesAmount */ - envolvedCartDiscounts: (UserTypeDiscount | CartPurchasesAmountDiscount | PurchasesAmountDiscount)[]; - couponState: "applied" | "not found" | null; -} \ No newline at end of file + items: CartItemTotal[]; + totalPrice: number; + priceByService: ServiceToPriceMap; + /** Скидки по сервисам */ + discountsByService: ServiceToDiscountMap; + /** Учтенные скидки типов userType, cartPurchasesAmount, totalPurchasesAmount */ + envolvedCartDiscounts: (UserTypeDiscount | CartPurchasesAmountDiscount | PurchasesAmountDiscount)[]; + couponState: "applied" | "not found" | null; +} diff --git a/src/model/discount.ts b/src/model/discount.ts new file mode 100644 index 0000000..49f98bc --- /dev/null +++ b/src/model/discount.ts @@ -0,0 +1,44 @@ +export type Discount = { + ID: string; + Name: string; + Layer: number; + Description: string; + Condition: { + Period?: { + From: string; + To: string; + }; + User?: string; + UserType?: string; + Coupon?: string; + PurchasesAmount?: number; + CartPurchasesAmount?: number; + Product?: string; + Term?: string; + Usage?: string; + PriceFrom?: number; + Group?: string; + }; + Target: { + Products?: { + ID: string; + Factor: number; + Overhelm: boolean; + }[]; + Factor?: number; + TargetScope?: string; + TargetGroup?: string; + Overhelm?: boolean; + }; + Audit: { + UpdatedAt: string; + CreatedAt: string; + DeletedAt: string; + Deleted: boolean; + }; + Deprecated: boolean; +}; + +export type DiscountData = { + Discounts: Discount[]; +}; diff --git a/src/pages/dashboard/Content/DiscountManagement/DiscountDataGrid.tsx b/src/pages/dashboard/Content/DiscountManagement/DiscountDataGrid.tsx index 8806cdc..297140f 100644 --- a/src/pages/dashboard/Content/DiscountManagement/DiscountDataGrid.tsx +++ b/src/pages/dashboard/Content/DiscountManagement/DiscountDataGrid.tsx @@ -1,3 +1,5 @@ +import { useEffect, useState } from "react"; +import { enqueueSnackbar } from "notistack"; import { Box, Button, styled, useTheme } from "@mui/material"; import { DataGrid, GridColDef, GridRowsProp, GridToolbar } from "@mui/x-data-grid"; import { findDiscountFactor, formatDiscountFactor } from "@root/kitUI/Cart/calc"; @@ -8,8 +10,8 @@ import { useDiscountStore, } from "@root/stores/discounts"; import axios from "axios"; -import { enqueueSnackbar } from "notistack"; -import { useEffect, useState } from "react"; + +import type { DiscountData } from "@root/model/discount"; const BoxButton = styled("div")(({ theme }) => ({ [theme.breakpoints.down(400)]: { @@ -64,17 +66,21 @@ const columns: GridColDef[] = [ export default function DiscountDataGrid() { const theme = useTheme(); - const discounts = useDiscountStore((state) => state.discounts); + const exampleDiscounts = useDiscountStore((state) => state.discounts); const selectedDiscountIds = useDiscountStore((state) => state.selectedDiscountIds); - const [discount, setDiscount] = useState(); + const [discount, setDiscount] = useState(); + + const mergeDiscount = [...(discount?.Discounts ?? []), ...exampleDiscounts]; + + console.log(mergeDiscount); useEffect(() => { const axiosDiscount = async () => { try { const { data } = await axios({ method: "get", - url: "https://penahub.gitlab.yandexcloud.net/price/discount/all", + url: "https://admin.pena.digital/price/discounts", }); setDiscount(data); console.log(data); @@ -86,7 +92,7 @@ export default function DiscountDataGrid() { axiosDiscount(); }, []); - const discountsGridData: GridRowsProp = discounts.map((discount) => { + const discountsGridData: GridRowsProp = exampleDiscounts.map((discount) => { const value = (discount.condition as any).purchasesAmount ?? (discount.condition as any).cartPurchasesAmount ??