feat(shop): add dynamic linked fees to modules

This commit is contained in:
DanielS
2026-07-22 15:09:05 +02:00
parent acd8d6575c
commit 1c31918768
8 changed files with 208 additions and 21 deletions

View File

@@ -302,8 +302,31 @@ export function WysiwygAdminClient({
<div className="flex flex-wrap gap-1">
{(prod.modules || []).length > 0 ? (
(prod.modules || []).map((m) => (
<span key={m.id} className="inline-flex items-center gap-1 px-2 py-0.5 rounded text-[10px] bg-white/10 text-slate-300 font-medium">
{m.name}
<span key={m.id} className="inline-flex items-center gap-2 px-2.5 py-1 rounded text-[10px] bg-white/10 text-slate-300 font-medium">
<span>{m.name}</span>
<select
value={m.linked_fee_product_id || ''}
onChange={async (e) => {
const val = e.target.value || null
const updatedModules = (prod.modules || []).map(mod =>
mod.id === m.id ? { ...mod, linked_fee_product_id: val } : mod
)
setProducts(products.map(p => p.id === prod.id ? { ...p, modules: updatedModules } : p))
await updateProduct(prod.id, {}, updatedModules)
}}
className="bg-slate-900 border border-white/10 text-[9px] text-slate-400 focus:ring-0 cursor-pointer rounded px-1 py-0.5 outline-none max-w-[120px]"
title="Verknüpfte Gebühr"
>
<option value="">Keine Gebühr</option>
{products
.filter(p => p.billing_interval === 'monthly')
.map(p => (
<option key={p.id} value={p.id}>
+ {p.name} ({p.base_price} )
</option>
))
}
</select>
<button
onClick={async () => {
const updatedModules = (prod.modules || []).filter(mod => mod.id !== m.id)

View File

@@ -77,6 +77,7 @@ export async function POST(request: Request) {
const orderItemsList: any[] = [];
let total = 0;
const feeProductIds = new Set<string>();
for (const item of groupItems) {
const itemSnapshot = buildOrderSnapshot(
@@ -93,8 +94,47 @@ export async function POST(request: Request) {
}));
orderItemsList.push(...itemsWithDevice);
total += itemSnapshot.total;
// Echte linked fee products ermitteln
if (item.selections) {
for (const catId in item.selections) {
const sel = item.selections[catId];
sel.moduleIds?.forEach((mId: string) => {
for (const p of products) {
const mod = p.modules?.find((m: any) => m.id === mId);
if (mod && mod.linked_fee_product_id) {
feeProductIds.add(mod.linked_fee_product_id);
}
}
});
}
}
}
feeProductIds.forEach(feeProdId => {
const feeProduct = products.find((p: any) => p.id === feeProdId);
if (feeProduct) {
const isIntervalMatch =
(type === 'purchase' && feeProduct.billing_interval === 'one_time') ||
(type === 'subscription' && feeProduct.billing_interval === 'monthly');
if (isIntervalMatch) {
orderItemsList.push({
category_id: 'dienste-gebuehren',
category_name: 'Dienste & Gebühren',
product_id: feeProduct.id,
product_name: feeProduct.name,
base_price: Number(feeProduct.base_price),
billing_interval: feeProduct.billing_interval,
selected_modules: [],
item_total: Number(feeProduct.base_price),
device_name: 'Zusatzleistung'
});
total += Number(feeProduct.base_price);
}
}
});
const orderSnapshot = {
schema_version: 1,
billing_cycle: type === 'purchase' ? 'one_time' : 'monthly',