Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add backend compatibility with specific products #1242

Merged
merged 3 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading