@hszrdm/create-commerce 1.17.0 → 1.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. package/package.json +1 -1
  2. package/templates/backend/.env.example +19 -24
  3. package/templates/backend/medusa-config.ts +14 -1
  4. package/templates/backend/package.json +1 -1
  5. package/templates/backend/scripts/core-backup.mjs +3 -1
  6. package/templates/backend/scripts/core-restore.mjs +17 -2
  7. package/templates/backend/scripts/lib/core-backup-common.mjs +32 -0
  8. package/templates/backend/src/scripts/core-init.ts +0 -5
  9. package/templates/storefront/package.json +3 -1
  10. package/templates/storefront/src/app/[countryCode]/products/[handle]/page.tsx +2 -2
  11. package/templates/storefront/src/app/layout.tsx +6 -0
  12. package/templates/storefront/src/components/account-access-gate.tsx +88 -0
  13. package/templates/storefront/src/components/account-dashboard.tsx +112 -24
  14. package/templates/storefront/src/components/account-verification-panel.tsx +145 -0
  15. package/templates/storefront/src/components/address-street-fields.tsx +47 -0
  16. package/templates/storefront/src/components/checkout-flow.tsx +132 -34
  17. package/templates/storefront/src/components/product-card.tsx +2 -1
  18. package/templates/storefront/src/lib/core/account.ts +49 -8
  19. package/templates/storefront/src/lib/core/checkout-addresses.ts +14 -6
  20. package/templates/storefront/src/lib/core/checkout-feedback-state.ts +15 -0
  21. package/templates/storefront/src/lib/core/checkout.ts +18 -19
  22. package/templates/storefront/src/lib/core/commerce.ts +11 -0
  23. package/templates/storefront/src/lib/core/structured-address.ts +92 -0
  24. package/templates/storefront/src/lib/core/use-checkout-feedback.ts +53 -0
  25. package/templates/storefront/tests/account-verification.test.mjs +40 -0
  26. package/templates/storefront/tests/checkout-addresses.test.mjs +59 -0
  27. package/templates/storefront/tests/checkout-feedback.test.mjs +27 -0
  28. package/templates/storefront/tests/product-image.test.mjs +31 -0
  29. package/templates/storefront/tsconfig.json +1 -0
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Hungarian address parts on top of Medusa-native address_1/address_2 and metadata.
3
+ * No database schema extension or migration.
4
+ */
5
+ export type AddressDetails = {
6
+ street_name: string
7
+ street_type: string
8
+ house_number: string
9
+ house_number_suffix: string
10
+ building: string
11
+ staircase: string
12
+ floor: string
13
+ door: string
14
+ address_extra: string
15
+ }
16
+ export type StructuredAddress = AddressDetails & {
17
+ country_code: string
18
+ address_1: string
19
+ address_2: string
20
+ }
21
+ export const CUSTOMER_STRUCTURED_ADDRESS_KEY = "core_structured_address"
22
+ export const CART_STRUCTURED_ADDRESSES_KEY = "core_structured_addresses"
23
+ const keys = [
24
+ "street_name", "street_type", "house_number", "house_number_suffix",
25
+ "building", "staircase", "floor", "door", "address_extra",
26
+ ] as const
27
+
28
+ function record(value: unknown): Record<string, unknown> {
29
+ return value && typeof value === "object" && !Array.isArray(value)
30
+ ? value as Record<string, unknown> : {}
31
+ }
32
+ function string(value: unknown) {
33
+ return typeof value === "string" ? value.trim() : ""
34
+ }
35
+ export function isHungarianAddress(address: Pick<StructuredAddress, "country_code">) {
36
+ return address.country_code.trim().toUpperCase() === "HU"
37
+ }
38
+ export function emptyAddressDetails(): AddressDetails {
39
+ return {
40
+ street_name: "", street_type: "", house_number: "", house_number_suffix: "",
41
+ building: "", staircase: "", floor: "", door: "", address_extra: "",
42
+ }
43
+ }
44
+ export function addressLine1(address: StructuredAddress): string {
45
+ if (!isHungarianAddress(address)) return address.address_1.trim()
46
+ const street = [address.street_name, address.street_type].map(v => v.trim()).filter(Boolean).join(" ")
47
+ const number = address.house_number.trim() +
48
+ (address.house_number_suffix.trim() ? "/" + address.house_number_suffix.trim() : "")
49
+ // Keep the native line if no structured parts have been entered yet.
50
+ return [street, number].filter(Boolean).join(" ") || address.address_1.trim()
51
+ }
52
+ export function addressLine2(address: StructuredAddress): string {
53
+ if (!isHungarianAddress(address)) return address.address_2.trim()
54
+ return [
55
+ address.building.trim() ? "Épület: " + address.building.trim() : "",
56
+ address.staircase.trim() ? "Lépcsőház: " + address.staircase.trim() : "",
57
+ address.floor.trim() ? "Emelet: " + address.floor.trim() : "",
58
+ address.door.trim() ? "Ajtó: " + address.door.trim() : "",
59
+ address.address_extra.trim(),
60
+ ].filter(Boolean).join(", ")
61
+ }
62
+ export function addressStructuredSnapshot(address: StructuredAddress) {
63
+ if (!isHungarianAddress(address)) return null
64
+ const result: Record<string, string | number> = {
65
+ version: 1,
66
+ country_code: "hu",
67
+ address_1: addressLine1(address),
68
+ address_2: addressLine2(address),
69
+ }
70
+ for (const key of keys) result[key] = address[key].trim()
71
+ return result
72
+ }
73
+ /** Do not use stale split data after a native address update. */
74
+ export function addressDetailsFromRecord(source: unknown, override?: unknown): AddressDetails {
75
+ const data = record(source)
76
+ const candidate = record(override === undefined
77
+ ? record(data.metadata)[CUSTOMER_STRUCTURED_ADDRESS_KEY] : override)
78
+ const matches = candidate.version === 1
79
+ && string(data.country_code).toLowerCase() === "hu"
80
+ && string(candidate.country_code).toLowerCase() === "hu"
81
+ && string(candidate.address_1) === string(data.address_1)
82
+ && string(candidate.address_2) === string(data.address_2)
83
+ if (!matches) return emptyAddressDetails()
84
+ const parts = emptyAddressDetails()
85
+ for (const key of keys) parts[key] = string(candidate[key])
86
+ return parts
87
+ }
88
+ export function missingAddressParts(address: StructuredAddress): (keyof AddressDetails)[] {
89
+ if (!isHungarianAddress(address)) return []
90
+ return (["street_name", "street_type", "house_number"] as const)
91
+ .filter(key => !address[key].trim())
92
+ }
@@ -0,0 +1,53 @@
1
+ "use client"
2
+
3
+ import { useCallback, useEffect, useRef, useState } from "react"
4
+ import { useToast } from "@/components/toast-provider"
5
+ import {
6
+ checkoutErrorMessage,
7
+ checkoutNoticeMessage,
8
+ shouldShowCheckoutNotice,
9
+ } from "@/lib/core/checkout-feedback-state"
10
+
11
+ /**
12
+ * Common Core checkout notification bridge.
13
+ * Keep the existing inline field validation and payment retry UI; only
14
+ * transient checkout errors/success messages are mirrored into toast notices.
15
+ */
16
+ export function useCheckoutFeedback(cartError?: string | null) {
17
+ const { showToast } = useToast()
18
+ const [error, setError] = useState<string | null>(null)
19
+ const [message, setMessage] = useState<string | null>(null)
20
+ const lastError = useRef<string | null>(null)
21
+ const lastMessage = useRef<string | null>(null)
22
+
23
+ const activeError = checkoutErrorMessage(error, cartError)
24
+ const activeMessage = checkoutNoticeMessage(message)
25
+
26
+ useEffect(() => {
27
+ if (!activeError) {
28
+ lastError.current = null
29
+ return
30
+ }
31
+ if (!shouldShowCheckoutNotice(lastError.current, activeError)) return
32
+ lastError.current = activeError
33
+ showToast({ tone: "error", message: activeError, durationMs: 7000 })
34
+ }, [activeError, showToast])
35
+
36
+ useEffect(() => {
37
+ if (!activeMessage) {
38
+ lastMessage.current = null
39
+ return
40
+ }
41
+ if (!shouldShowCheckoutNotice(lastMessage.current, activeMessage)) return
42
+ lastMessage.current = activeMessage
43
+ showToast({ tone: "success", message: activeMessage, durationMs: 7000 })
44
+ }, [activeMessage, showToast])
45
+
46
+ // Form-wide errors that do not need to persist as checkout state.
47
+ const notifyError = useCallback((text: string) => {
48
+ const next = checkoutNoticeMessage(text)
49
+ if (next) showToast({ tone: "error", message: next, durationMs: 7000 })
50
+ }, [showToast])
51
+
52
+ return { error, setError, message, setMessage, notifyError }
53
+ }
@@ -0,0 +1,40 @@
1
+ import assert from "node:assert/strict"
2
+ import { readFileSync } from "node:fs"
3
+ import { join } from "node:path"
4
+ import test from "node:test"
5
+ import { fileURLToPath } from "node:url"
6
+
7
+ // Resolve relative to the test file, not process.cwd(): this test is copied
8
+ // into the create-commerce template and also runs from the generator root.
9
+ const srcDir = fileURLToPath(new URL("../src/", import.meta.url))
10
+ const read = (file) => readFileSync(join(srcDir, file), "utf8")
11
+
12
+ test("account requires verified email and retains one shared Core verification API", () => {
13
+ const account = read("lib/core/account.ts")
14
+ const dashboard = read("components/account-dashboard.tsx")
15
+ const gate = read("components/account-access-gate.tsx")
16
+ const panel = read("components/account-verification-panel.tsx")
17
+
18
+ assert.match(account, /isAccountEmailVerified/)
19
+ assert.match(account, /email_verified_address/)
20
+ assert.match(account, /emailVerificationStatus/)
21
+ assert.match(account, /requestEmailVerification/)
22
+ assert.match(account, /confirmEmailVerification/)
23
+ assert.match(dashboard, /if \(!isAccountEmailVerified\(customer\)\)/)
24
+ assert.match(dashboard, /AccountVerificationPanel/)
25
+ assert.match(gate, /router\.replace\("\/account"\)/)
26
+ assert.match(gate, /state === "unverified"/)
27
+ assert.match(panel, /retry_after_seconds/)
28
+ assert.match(panel, /resend_available_at/)
29
+ assert.match(panel, /retrySeconds > 0/)
30
+ assert.match(panel, /onLogout/)
31
+ assert.match(panel, /onVerified/)
32
+ assert.doesNotMatch(panel, /localStorage|sessionStorage/)
33
+ })
34
+
35
+ test("cart ownership is transferred only after email confirmation", () => {
36
+ const dashboard = read("components/account-dashboard.tsx")
37
+ assert.match(dashboard, /if \(registered\.customer && isAccountEmailVerified\(registered\.customer\)\)/)
38
+ assert.match(dashboard, /if \(signedIn && isAccountEmailVerified\(signedIn\)\)/)
39
+ assert.match(dashboard, /if \(updated && isAccountEmailVerified\(updated\)\)/)
40
+ })
@@ -1,5 +1,11 @@
1
1
  import test from "node:test"
2
2
  import assert from "node:assert/strict"
3
+ import {
4
+ CART_STRUCTURED_ADDRESSES_KEY,
5
+ CUSTOMER_STRUCTURED_ADDRESS_KEY,
6
+ addressLine1, addressLine2, addressStructuredSnapshot,
7
+ missingAddressParts,
8
+ } from "../src/lib/core/structured-address.ts"
3
9
  import {
4
10
  CART_CHECKOUT_ADDRESS,
5
11
  NEW_CHECKOUT_ADDRESS,
@@ -146,3 +152,56 @@ test("partial cart with shipping but no billing must still be explicitly saved",
146
152
  assert.equal(initial.billingSelection, "cadr_billing")
147
153
  assert.equal(initial.addressOnCart, false)
148
154
  })
155
+
156
+ test("HU split street fields produce native Medusa lines and preserve details", () => {
157
+ const draft = {
158
+ ...emptyCheckoutAddress("HU", "kata@example.com"),
159
+ street_name: "Fő", street_type: "utca", house_number: "12",
160
+ house_number_suffix: "A", floor: "2", door: "4",
161
+ }
162
+ assert.deepEqual(missingAddressParts(draft), [])
163
+ assert.equal(addressLine1(draft), "Fő utca 12/A")
164
+ assert.equal(addressLine2(draft), "Emelet: 2, Ajtó: 4")
165
+ const payload = checkoutAddressPayload(draft)
166
+ assert.equal(payload.address_1, "Fő utca 12/A")
167
+ assert.equal(payload.address_2, "Emelet: 2, Ajtó: 4")
168
+ assert.equal("street_name" in payload, false)
169
+ const snapshot = addressStructuredSnapshot(draft)
170
+ const saved = { ...payload, metadata: { [CUSTOMER_STRUCTURED_ADDRESS_KEY]: snapshot } }
171
+ const hydrated = checkoutAddressFromRecord(saved, "HU", draft.email)
172
+ assert.equal(hydrated.street_name, "Fő")
173
+ assert.equal(hydrated.house_number, "12")
174
+ assert.equal(hydrated.house_number_suffix, "A")
175
+ })
176
+
177
+ test("cart structured details restore independently for shipping and billing", () => {
178
+ const shipping = { ...emptyCheckoutAddress(), street_name: "Duna", street_type: "utca", house_number: "8" }
179
+ const billing = { ...emptyCheckoutAddress(), street_name: "Fő", street_type: "út", house_number: "2" }
180
+ const cart = {
181
+ email: "kata@example.com",
182
+ shipping_address: checkoutAddressPayload(shipping),
183
+ billing_address: checkoutAddressPayload(billing),
184
+ metadata: { [CART_STRUCTURED_ADDRESSES_KEY]: {
185
+ shipping: addressStructuredSnapshot(shipping),
186
+ billing: addressStructuredSnapshot(billing),
187
+ } },
188
+ }
189
+ const initial = initialCheckoutAddresses({ country: "HU", cart })
190
+ assert.equal(initial.shipping.street_name, "Duna")
191
+ assert.equal(initial.billing.street_name, "Fő")
192
+ assert.equal(initial.billing.street_type, "út")
193
+ assert.equal(initial.addressOnCart, true)
194
+ })
195
+
196
+ test("foreign addresses stay free-form, and stale metadata is ignored", () => {
197
+ const foreign = { ...emptyCheckoutAddress("DE"), address_1: "Schönhauser Allee 3", address_2: "2. OG" }
198
+ assert.deepEqual(missingAddressParts(foreign), [])
199
+ assert.equal(addressLine1(foreign), "Schönhauser Allee 3")
200
+ assert.equal(addressLine2(foreign), "2. OG")
201
+ assert.equal(addressStructuredSnapshot(foreign), null)
202
+ const hu = { ...emptyCheckoutAddress(), street_name: "Teszt", street_type: "utca", house_number: "10" }
203
+ const metadata = { [CUSTOMER_STRUCTURED_ADDRESS_KEY]: addressStructuredSnapshot(hu) }
204
+ const changed = checkoutAddressFromRecord({ ...checkoutAddressPayload(hu), address_1: "Másik utca 3", metadata }, "HU")
205
+ assert.equal(changed.street_name, "")
206
+ assert.deepEqual(missingAddressParts(changed), ["street_name", "street_type", "house_number"])
207
+ })
@@ -0,0 +1,27 @@
1
+ import test from "node:test"
2
+ import assert from "node:assert/strict"
3
+ import {
4
+ checkoutErrorMessage,
5
+ checkoutNoticeMessage,
6
+ shouldShowCheckoutNotice,
7
+ } from "../src/lib/core/checkout-feedback-state.ts"
8
+
9
+ test("checkout feedback prioritizes the local error over cart errors", () => {
10
+ assert.equal(checkoutErrorMessage("Form error", "Cart error"), "Form error")
11
+ assert.equal(checkoutErrorMessage(null, "Cart error"), "Cart error")
12
+ assert.equal(checkoutErrorMessage(" ", "Cart error"), "Cart error")
13
+ assert.equal(checkoutErrorMessage("", null), null)
14
+ })
15
+
16
+ test("checkout feedback ignores blank messages and normalizes whitespace", () => {
17
+ assert.equal(checkoutNoticeMessage(" Saved! "), "Saved!")
18
+ assert.equal(checkoutNoticeMessage(" "), null)
19
+ assert.equal(checkoutNoticeMessage(undefined), null)
20
+ })
21
+
22
+ test("checkout feedback does not repeat unchanged, empty notices", () => {
23
+ assert.equal(shouldShowCheckoutNotice(null, "Saved"), true)
24
+ assert.equal(shouldShowCheckoutNotice("Saved", "Saved"), false)
25
+ assert.equal(shouldShowCheckoutNotice("Saved", "Error"), true)
26
+ assert.equal(shouldShowCheckoutNotice(null, null), false)
27
+ })
@@ -0,0 +1,31 @@
1
+ import assert from "node:assert/strict"
2
+ import { readFileSync } from "node:fs"
3
+ import test from "node:test"
4
+ import { productImageUrl } from "../src/lib/core/commerce.ts"
5
+
6
+ test("a configured thumbnail takes precedence over gallery images", () => {
7
+ assert.equal(productImageUrl({
8
+ thumbnail: "https://example.com/primary.webp",
9
+ images: [{ url: "https://example.com/gallery.webp" }],
10
+ }), "https://example.com/primary.webp")
11
+ })
12
+
13
+ test("a product with no thumbnail uses its first usable gallery image", () => {
14
+ assert.equal(productImageUrl({
15
+ thumbnail: null,
16
+ images: [{ url: "" }, { url: " https://example.com/gallery.webp " }],
17
+ }), "https://example.com/gallery.webp")
18
+ })
19
+
20
+ test("missing, empty or malformed image data stays an empty image", () => {
21
+ assert.equal(productImageUrl({ images: [{ invalid: "x" }] }), "")
22
+ assert.equal(productImageUrl({ thumbnail: " ", images: null }), "")
23
+ assert.equal(productImageUrl(null), "")
24
+ })
25
+
26
+ test("catalog cards and product details share the fallback resolver", () => {
27
+ const card = readFileSync(new URL("../src/components/product-card.tsx", import.meta.url), "utf8")
28
+ const detail = readFileSync(new URL("../src/app/[countryCode]/products/[handle]/page.tsx", import.meta.url), "utf8")
29
+ assert.match(card, /const thumbnail = productImageUrl\(product\)/)
30
+ assert.match(detail, /const thumbnail = productImageUrl\(product\)/)
31
+ })
@@ -10,6 +10,7 @@
10
10
  "skipLibCheck": true,
11
11
  "strict": true,
12
12
  "noEmit": true,
13
+ "allowImportingTsExtensions": true,
13
14
  "esModuleInterop": true,
14
15
  "module": "esnext",
15
16
  "moduleResolution": "bundler",