Skip to content

Commit

Permalink
fix: Add backend compatibility with specific products (#1242)
Browse files Browse the repository at this point in the history
## Describe your changes

Fixes: #

---------

Co-authored-by: Didrik Munther <[email protected]>
  • Loading branch information
hampfh and didrikmunther authored Aug 4, 2024
1 parent 2c590c9 commit e929012
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Product } from "@/shared/hooks/api/useDashboard"
import { formatCurrency } from "@/utils/format_currency"

export function DiscountCard({ product }: { product: Product }) {
export function DiscountCard({
product
}: {
product: Omit<Product, "child_products" | "specific_products">
}) {
return (
<div className="flex items-center justify-center">
<div className="w-full rounded bg-gradient-to-r from-yellow-500 to-yellow-300 p-1">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ import { cn } from "@/utils/cx"
import { formatCurrency } from "@/utils/format_currency"
import { cx } from "class-variance-authority"

export function ProductOrderingCard({ product }: { product: Product }) {
export function ProductOrderingCard({
product
}: {
product: Omit<Product, "child_products" | "specific_products">
}) {
const { updateCache } = useDashboard()
const { data: orders } = useOrders()
const { data: products } = useProducts()
Expand Down
35 changes: 31 additions & 4 deletions apps/dashboard/src/forms/fr_accounting/product.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,55 @@ import { belongsToSection } from "@/forms/fr_accounting/accounting_utilities"
import { DiscountCard } from "@/forms/fr_accounting/components/DiscountCard"
import { ProductOrderingCard } from "@/forms/fr_accounting/components/ProductOrderingCard"
import { Category, Product } from "@/shared/hooks/api/useDashboard"
import { useOrders } from "@/shared/hooks/api/useOrders"
import { useProducts } from "@/shared/hooks/api/useProducts"
import { RegistrationSection } from "@/shared/vars"
import React, { useMemo } from "react"
import { FormWrapper } from "../FormWrapper"

export function ProductFormPage({ section }: { section: RegistrationSection }) {
const { data: allProducts } = useProducts()
const { data: orders } = useOrders()

const selectedPackage = orders.find(
order =>
order.product.registration_section?.name ===
RegistrationSection.Packages
)
const products = useMemo(
() =>
allProducts.filter(
() => [
...allProducts.filter(
product =>
belongsToSection(product, section) &&
product.display_in_product_list
),
[allProducts, section]
...(selectedPackage?.product.specific_products
.filter(
specificProduct =>
specificProduct.specific_product.registration_section !=
null &&
specificProduct.specific_product.registration_section
.name === section
)
.map(specificProduct => specificProduct.specific_product) ?? [])
],
[allProducts, section, selectedPackage?.product.specific_products]
)

const categorizedProducts = useMemo(
() =>
Object.entries(
products.reduce<
Record<string, { category?: Category; products: Product[] }>
Record<
string,
{
category?: Category
products: Omit<
Product,
"child_products" | "specific_products"
>[]
}
>
>(
// Split products into categories
(total, current) => {
Expand Down
8 changes: 4 additions & 4 deletions apps/dashboard/src/shared/hooks/api/useDashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HOST } from "@/shared/vars"
import { HOST, RegistrationSection } from "@/shared/vars"
import { useQuery, useQueryClient } from "@tanstack/react-query"
import { useNavigate, useParams } from "@tanstack/react-router"

Expand Down Expand Up @@ -93,9 +93,9 @@ export interface Category {
allow_multiple_purchases: boolean
}

export interface RegistrationSection {
export interface ProductRegistrationSection {
id: string
name: string
name: RegistrationSection
description: string
hide_from_registration: boolean
}
Expand All @@ -114,7 +114,7 @@ export interface Product {
description: string
category: Category | null
display_in_product_list: boolean
registration_section: RegistrationSection | null
registration_section: ProductRegistrationSection | null
child_products: ChildProduct[]
specific_products: Array<{
unit_price: number
Expand Down
8 changes: 8 additions & 0 deletions dashboard/api/registration/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,16 @@ def order_is_allowed(fair, company):

package = orders.filter(product__category__name="Package").first()
if package is None:
print("No package")
return False

# for order in orders:
# parent_products = order.product.specificproduct_set.all()

# if parent_products.count() > 0 and not package in parent_products:
# print(order.product, parent_products, "Parent not in package")
# return False

return True


Expand Down

0 comments on commit e929012

Please sign in to comment.