Files
Suman/apps/suman-bosna/src/lib/util/sale-scenario.ts
Nermin Karic ada6fd857c design upgrade
2026-11-23 00:59:47 +01:00

159 lines
4.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { HttpTypes } from "@medusajs/types"
/**
* Sale scenarios set per product in Medusa Admin (product-inquiry widget):
* - in_stock: available now, add to cart
* - made_to_order: add to cart, delivered after `lead_time_days`
* - quote_only: no price or cart, customer sends an inquiry
*/
export type SaleScenario = "in_stock" | "made_to_order" | "quote_only"
export type SaleInfo = {
scenario: SaleScenario
/** Short status, e.g. "Na stanju" */
label: string
/** One line with delivery detail, e.g. "Isporuka za 14 dana" */
detail: string
leadTimeDays: number
allowCustomization: boolean
quoteSubtitle: string
/** Tailwind classes for the small status dot */
dotClass: string
}
type Metadata = Record<string, unknown> | null | undefined
export function getSaleInfo(metadata: Metadata): SaleInfo {
const raw = metadata?.sale_scenario
const scenario: SaleScenario =
raw === "quote_only" || raw === "made_to_order" ? raw : "in_stock"
const leadTimeDays = Number(metadata?.lead_time_days) || 14
const allowCustomization = Boolean(metadata?.allow_customization)
const quoteSubtitle =
(metadata?.quote_subtitle as string) ||
"Izrada po mjeri sa profesionalnom montažom i garancijom"
if (scenario === "quote_only") {
return {
scenario,
label: "Na upit",
detail: "Izrada po mjeri, cijena na upit",
leadTimeDays,
allowCustomization,
quoteSubtitle,
dotClass: "bg-primary",
}
}
if (scenario === "made_to_order") {
return {
scenario,
label: "Po narudžbi",
detail: `Isporuka za ${leadTimeDays} dana`,
leadTimeDays,
allowCustomization,
quoteSubtitle,
dotClass: "bg-amber-500",
}
}
return {
scenario,
label: "Na stanju",
detail: "Isporuka za 24–48h",
leadTimeDays,
allowCustomization,
quoteSubtitle,
dotClass: "bg-emerald-500",
}
}
/** Metadata keys that drive behaviour and are not shown as specs */
const CONTROL_KEYS = new Set([
"sale_scenario",
"lead_time_days",
"allow_customization",
"quote_subtitle",
"badge",
"custom_template",
"hero_image",
])
const SPEC_LABELS: Record<string, string> = {
fabric: "Platno",
fabric_weight: "Platno",
canvas: "Platno",
mast_diameter: "Stub",
mast_construction: "Stub",
ribs_profile: "Krakovi",
uv_protection: "UV zaštita",
water_column: "Vodeni stub",
wind_resistance: "Otpornost na vjetar",
wind_vent: "Lufter",
warranty_years: "Garancija",
opening_mechanism: "Mehanizam otvaranja",
rotation: "Rotacija",
free_space: "Slobodan prostor",
durability: "Izdržljivost",
design_system: "Dizajn",
special_feature: "Posebnost",
origin: "Porijeklo",
weight: "Težina",
motor: "Motorizacija",
profile: "Profil",
drainage: "Odvod vode",
lamellas: "Lamele",
snow_load: "Nosivost snijega",
automation: "Automatika",
construction: "Konstrukcija",
four_seasons: "Cjelogodišnje korištenje",
zip_compatible: "Kompatibilnost",
arms: "Ruke",
tilt_angle: "Nagib",
system: "Sistem",
insulation: "Izolacija",
frame: "Okvir",
branding: "Brendiranje",
options_available: "Dodatna oprema",
}
function prettifyKey(key: string) {
const text = key.replace(/_/g, " ")
return text.charAt(0).toUpperCase() + text.slice(1)
}
/** Human-readable spec rows from product metadata */
export function getProductSpecs(product: HttpTypes.StoreProduct) {
const metadata = (product.metadata ?? {}) as Record<string, unknown>
return Object.entries(metadata)
.filter(
([key, value]) =>
!CONTROL_KEYS.has(key) &&
(typeof value === "string" || typeof value === "number") &&
String(value).trim() !== ""
)
.map(([key, value]) => ({
key,
label: SPEC_LABELS[key] ?? prettifyKey(key),
value: String(value),
}))
}
/**
* Full-width hero image. Uses `metadata.hero_image` when set in Medusa Admin,
* otherwise a neutral placeholder until real photography is uploaded.
*/
export function getProductHeroImage(product: HttpTypes.StoreProduct) {
const custom = product.metadata?.hero_image
if (typeof custom === "string" && custom.trim()) {
return custom
}
return "https://placehold.co/2400x1350/c9ced6/c9ced6"
}
/** Card image for listings, same placeholder logic as the hero */
export function getProductCardImage(title?: string | null) {
const text = encodeURIComponent(title ?? "Suman").replace(/%20/g, "+")
return `https://placehold.co/900x1100/e8ebef/8a919c?text=${text}`
}