@vicacha-devs/medusa-plugin-barcode 0.0.0-stage → 0.1.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.
- package/.medusa/server/src/admin/index.cjs +3960 -0
- package/.medusa/server/src/admin/index.js +3938 -0
- package/.medusa/server/src/api/admin/barcodes/[barcode]/route.js +43 -0
- package/.medusa/server/src/api/admin/barcodes/batch/route.js +9 -0
- package/.medusa/server/src/api/admin/barcodes/bulk-generate/route.js +9 -0
- package/.medusa/server/src/api/admin/barcodes/cache/route.js +25 -0
- package/.medusa/server/src/api/admin/barcodes/count-sessions/[id]/apply/route.js +13 -0
- package/.medusa/server/src/api/admin/barcodes/count-sessions/[id]/scan/route.js +13 -0
- package/.medusa/server/src/api/admin/barcodes/count-sessions/route.js +27 -0
- package/.medusa/server/src/api/admin/barcodes/create-from-barcode/route.js +9 -0
- package/.medusa/server/src/api/admin/barcodes/lookup/route.js +12 -0
- package/.medusa/server/src/api/admin/barcodes/print/route.js +11 -0
- package/.medusa/server/src/api/admin/barcodes/route.js +27 -0
- package/.medusa/server/src/api/admin/barcodes/scan/route.js +23 -0
- package/.medusa/server/src/api/admin/barcodes/tags/[id]/route.js +28 -0
- package/.medusa/server/src/api/admin/barcodes/tags/route.js +25 -0
- package/.medusa/server/src/api/admin/barcodes/templates/route.js +40 -0
- package/.medusa/server/src/api/admin/plugin/route.js +4 -0
- package/.medusa/server/src/api/admin/variants/barcode/[code]/route.js +28 -0
- package/.medusa/server/src/api/middlewares.js +73 -0
- package/.medusa/server/src/api/store/barcodes/[code]/route.js +28 -0
- package/.medusa/server/src/api/store/barcodes/[code]/verify/route.js +20 -0
- package/.medusa/server/src/api/store/plugin/route.js +4 -0
- package/.medusa/server/src/api/store/variants/barcode/[code]/route.js +28 -0
- package/.medusa/server/src/api/validators.js +173 -0
- package/.medusa/server/src/hooks/index.js +202 -0
- package/.medusa/server/src/index.js +5 -0
- package/.medusa/server/src/jobs/purge-expired-barcode-cache.js +10 -0
- package/.medusa/server/src/modules/barcode/index.js +16 -0
- package/.medusa/server/src/modules/barcode/migrations/Migration20260522000000.js +165 -0
- package/.medusa/server/src/modules/barcode/models/index.js +162 -0
- package/.medusa/server/src/modules/barcode/operations/context.js +2 -0
- package/.medusa/server/src/modules/barcode/operations/data-service.js +10 -0
- package/.medusa/server/src/modules/barcode/operations/generation.js +33 -0
- package/.medusa/server/src/modules/barcode/operations/index.js +9 -0
- package/.medusa/server/src/modules/barcode/operations/labels.js +57 -0
- package/.medusa/server/src/modules/barcode/operations/lookup.js +135 -0
- package/.medusa/server/src/modules/barcode/operations/maintenance.js +37 -0
- package/.medusa/server/src/modules/barcode/operations/stock-count.js +129 -0
- package/.medusa/server/src/modules/barcode/operations/tags.js +298 -0
- package/.medusa/server/src/modules/barcode/providers/index.js +721 -0
- package/.medusa/server/src/modules/barcode/service.js +161 -0
- package/.medusa/server/src/modules/barcode/types.js +21 -0
- package/.medusa/server/src/modules/barcode/utils/barcode-parser.js +399 -0
- package/.medusa/server/src/modules/barcode/utils/label-printer.js +533 -0
- package/.medusa/server/src/modules/barcode/utils/normalize-product.js +443 -0
- package/.medusa/server/src/modules/barcode/utils/security.js +114 -0
- package/.medusa/server/src/modules/barcode/utils/svg-renderer.js +492 -0
- package/.medusa/server/src/rbac.js +130 -0
- package/.medusa/server/src/sdk/index.js +164 -0
- package/.medusa/server/src/subscribers/product-created.js +34 -0
- package/.medusa/server/src/types/index.js +2 -0
- package/.medusa/server/src/ui/index.js +138 -0
- package/.medusa/server/src/workflows/apply-inventory-count.js +68 -0
- package/.medusa/server/src/workflows/batch-barcode-import.js +153 -0
- package/.medusa/server/src/workflows/bulk-generate-variant-barcodes.js +143 -0
- package/.medusa/server/src/workflows/generate-barcode-tag.js +44 -0
- package/.medusa/server/src/workflows/helpers.js +15 -0
- package/.medusa/server/src/workflows/index.js +7 -0
- package/.medusa/server/src/workflows/lookup-and-create-product.js +215 -0
- package/.medusa/server/src/workflows/scan-inventory-count.js +15 -0
- package/LICENSE +21 -0
- package/README.md +844 -2
- package/package.json +134 -3
- package/src/__tests__/barcode.spec.ts +836 -0
- package/src/__tests__/medusa-shim.mjs +331 -0
- package/src/__tests__/register-ts-resolver.mjs +135 -0
- package/src/admin/README.md +134 -0
- package/src/admin/components/barcode-studio-header.tsx +73 -0
- package/src/admin/components/batch-import-section.tsx +99 -0
- package/src/admin/components/dynamic-qr-tag-drawer-form.tsx +230 -0
- package/src/admin/components/dynamic-qr-tag-form.tsx +129 -0
- package/src/admin/components/dynamic-qr-tags-table.tsx +136 -0
- package/src/admin/components/gtin-lookup-section.tsx +155 -0
- package/src/admin/components/index.tsx +14 -0
- package/src/admin/components/label-designer-section.tsx +151 -0
- package/src/admin/components/label-output-section.tsx +63 -0
- package/src/admin/components/overview-cards.tsx +98 -0
- package/src/admin/components/product-barcode-card.tsx +127 -0
- package/src/admin/components/providers-cache-table.tsx +61 -0
- package/src/admin/components/providers-overview-section.tsx +49 -0
- package/src/admin/components/rbac-guard.tsx +153 -0
- package/src/admin/components/stock-take-scanner-section.tsx +116 -0
- package/src/admin/components/stock-take-sessions-section.tsx +109 -0
- package/src/admin/hooks/use-barcode-studio.ts +466 -0
- package/src/admin/hooks/use-product-barcodes.ts +139 -0
- package/src/admin/i18n/README.md +130 -0
- package/src/admin/i18n/index.ts +36 -0
- package/src/admin/i18n/json/ar.json +157 -0
- package/src/admin/i18n/json/de.json +157 -0
- package/src/admin/i18n/json/en.json +157 -0
- package/src/admin/i18n/json/es.json +157 -0
- package/src/admin/i18n/json/fr.json +157 -0
- package/src/admin/i18n/json/it.json +157 -0
- package/src/admin/i18n/json/nl.json +157 -0
- package/src/admin/i18n/json/pl.json +157 -0
- package/src/admin/i18n/json/pt.json +157 -0
- package/src/admin/i18n/json/tr.json +157 -0
- package/src/admin/routes/barcodes/lookup/page.tsx +75 -0
- package/src/admin/routes/barcodes/page.tsx +212 -0
- package/src/admin/routes/barcodes/print/page.tsx +67 -0
- package/src/admin/routes/barcodes/providers/page.tsx +66 -0
- package/src/admin/routes/barcodes/stock-take/page.tsx +74 -0
- package/src/admin/routes/barcodes/tags/@create/page.tsx +56 -0
- package/src/admin/routes/barcodes/tags/page.tsx +72 -0
- package/src/admin/tsconfig.json +20 -0
- package/src/admin/vite-env.d.ts +1 -0
- package/src/admin/widgets/product-barcode-widget.tsx +43 -0
- package/src/api/README.md +117 -0
- package/src/api/admin/barcodes/[barcode]/route.ts +64 -0
- package/src/api/admin/barcodes/batch/route.ts +12 -0
- package/src/api/admin/barcodes/bulk-generate/route.ts +12 -0
- package/src/api/admin/barcodes/cache/route.ts +35 -0
- package/src/api/admin/barcodes/count-sessions/[id]/apply/route.ts +17 -0
- package/src/api/admin/barcodes/count-sessions/[id]/scan/route.ts +17 -0
- package/src/api/admin/barcodes/count-sessions/route.ts +43 -0
- package/src/api/admin/barcodes/create-from-barcode/route.ts +16 -0
- package/src/api/admin/barcodes/lookup/route.ts +19 -0
- package/src/api/admin/barcodes/print/route.ts +18 -0
- package/src/api/admin/barcodes/route.ts +36 -0
- package/src/api/admin/barcodes/scan/route.ts +32 -0
- package/src/api/admin/barcodes/tags/[id]/route.ts +41 -0
- package/src/api/admin/barcodes/tags/route.ts +36 -0
- package/src/api/admin/barcodes/templates/route.ts +51 -0
- package/src/api/admin/plugin/route.ts +5 -0
- package/src/api/admin/variants/barcode/[code]/route.ts +36 -0
- package/src/api/middlewares.ts +91 -0
- package/src/api/store/barcodes/[code]/route.ts +43 -0
- package/src/api/store/barcodes/[code]/verify/route.ts +35 -0
- package/src/api/store/plugin/route.ts +5 -0
- package/src/api/store/variants/barcode/[code]/route.ts +36 -0
- package/src/api/validators.ts +217 -0
- package/src/hooks/index.ts +270 -0
- package/src/index.ts +4 -0
- package/src/jobs/README.md +54 -0
- package/src/jobs/purge-expired-barcode-cache.ts +18 -0
- package/src/links/README.md +44 -0
- package/src/modules/README.md +85 -0
- package/src/modules/barcode/index.ts +18 -0
- package/src/modules/barcode/migrations/Migration20260522000000.ts +225 -0
- package/src/modules/barcode/models/index.ts +168 -0
- package/src/modules/barcode/operations/context.ts +14 -0
- package/src/modules/barcode/operations/data-service.ts +10 -0
- package/src/modules/barcode/operations/generation.ts +57 -0
- package/src/modules/barcode/operations/index.ts +8 -0
- package/src/modules/barcode/operations/labels.ts +90 -0
- package/src/modules/barcode/operations/lookup.ts +202 -0
- package/src/modules/barcode/operations/maintenance.ts +53 -0
- package/src/modules/barcode/operations/stock-count.ts +217 -0
- package/src/modules/barcode/operations/tags.ts +425 -0
- package/src/modules/barcode/providers/index.ts +958 -0
- package/src/modules/barcode/service.ts +243 -0
- package/src/modules/barcode/types.ts +297 -0
- package/src/modules/barcode/utils/barcode-parser.ts +453 -0
- package/src/modules/barcode/utils/label-printer.ts +641 -0
- package/src/modules/barcode/utils/normalize-product.ts +524 -0
- package/src/modules/barcode/utils/security.ts +136 -0
- package/src/modules/barcode/utils/svg-renderer.ts +574 -0
- package/src/providers/README.md +105 -0
- package/src/rbac.ts +144 -0
- package/src/sdk/index.ts +253 -0
- package/src/subscribers/README.md +73 -0
- package/src/subscribers/product-created.ts +45 -0
- package/src/types/index.ts +1 -0
- package/src/types/medusa-ui.d.ts +118 -0
- package/src/ui/index.tsx +311 -0
- package/src/workflows/README.md +56 -0
- package/src/workflows/apply-inventory-count.ts +115 -0
- package/src/workflows/batch-barcode-import.ts +218 -0
- package/src/workflows/bulk-generate-variant-barcodes.ts +214 -0
- package/src/workflows/generate-barcode-tag.ts +85 -0
- package/src/workflows/helpers.ts +21 -0
- package/src/workflows/index.ts +6 -0
- package/src/workflows/lookup-and-create-product.ts +298 -0
- package/src/workflows/scan-inventory-count.ts +45 -0
- package/tsconfig.json +40 -0
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
import { MedusaError, Modules } from "@medusajs/framework/utils"
|
|
2
|
+
|
|
3
|
+
import type { BarcodeOpsContext } from "./context"
|
|
4
|
+
import type { BarcodeScanAction, BarcodeSymbology } from "../types"
|
|
5
|
+
import {
|
|
6
|
+
buildGs1DigitalLink,
|
|
7
|
+
generateInternalEan13,
|
|
8
|
+
parseBarcode,
|
|
9
|
+
} from "../utils/barcode-parser"
|
|
10
|
+
import {
|
|
11
|
+
createAuthenticityHmac,
|
|
12
|
+
generateShortScanCode,
|
|
13
|
+
verifyAuthenticityHmac,
|
|
14
|
+
} from "../utils/security"
|
|
15
|
+
import { renderBarcodeSvg } from "../utils/svg-renderer"
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Creates or updates a dynamic `BarcodeTag` (1D barcode or 2D QR/DataMatrix token)
|
|
19
|
+
* with storefront scan action, optional GS1 Digital Link, and HMAC-SHA256 anti-counterfeit signature.
|
|
20
|
+
*/
|
|
21
|
+
export async function createOrUpdateBarcodeTag(
|
|
22
|
+
ctx: BarcodeOpsContext,
|
|
23
|
+
input: {
|
|
24
|
+
code?: string
|
|
25
|
+
target_type?: "product" | "variant" | "order" | "custom"
|
|
26
|
+
target_id: string
|
|
27
|
+
product_id?: string | null
|
|
28
|
+
variant_id?: string | null
|
|
29
|
+
label?: string | null
|
|
30
|
+
symbology?: BarcodeSymbology
|
|
31
|
+
scan_action?: BarcodeScanAction
|
|
32
|
+
gs1_gtin?: string | null
|
|
33
|
+
batch_lot?: string | null
|
|
34
|
+
serial_number?: string | null
|
|
35
|
+
target_url?: string | null
|
|
36
|
+
svg_url?: string | null
|
|
37
|
+
metadata?: Record<string, unknown> | null
|
|
38
|
+
}
|
|
39
|
+
) {
|
|
40
|
+
if (!input.target_id || !String(input.target_id).trim()) {
|
|
41
|
+
throw new MedusaError(
|
|
42
|
+
MedusaError.Types.INVALID_DATA,
|
|
43
|
+
"target_id is required to create a BarcodeTag"
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const symbology: BarcodeSymbology = input.symbology || "qr"
|
|
48
|
+
const scanAction: BarcodeScanAction = input.scan_action || "view"
|
|
49
|
+
const targetType = input.target_type || "product"
|
|
50
|
+
|
|
51
|
+
let code = input.code?.trim() || ""
|
|
52
|
+
if (!code) {
|
|
53
|
+
if (symbology === "ean13") {
|
|
54
|
+
code = generateInternalEan13(
|
|
55
|
+
`${targetType}:${input.target_id}`,
|
|
56
|
+
ctx.options.internalBarcodePrefix || "200"
|
|
57
|
+
)
|
|
58
|
+
} else {
|
|
59
|
+
code = generateShortScanCode("mpb")
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const hmacSignature = createAuthenticityHmac(
|
|
64
|
+
{
|
|
65
|
+
code,
|
|
66
|
+
targetId: input.target_id,
|
|
67
|
+
serialNumber: input.serial_number ?? null,
|
|
68
|
+
batchLot: input.batch_lot ?? null,
|
|
69
|
+
},
|
|
70
|
+
ctx.hmacSecret
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
const baseUrl = (
|
|
74
|
+
ctx.options.storefrontBaseUrl || "https://store.example.com"
|
|
75
|
+
).replace(/\/$/, "")
|
|
76
|
+
|
|
77
|
+
let resolvedUrl = input.target_url || null
|
|
78
|
+
if (!resolvedUrl) {
|
|
79
|
+
if (scanAction === "gs1_digital_link" && (input.gs1_gtin || code)) {
|
|
80
|
+
resolvedUrl = buildGs1DigitalLink({
|
|
81
|
+
baseUrl,
|
|
82
|
+
gtin: input.gs1_gtin || code,
|
|
83
|
+
batchLot: input.batch_lot,
|
|
84
|
+
serial: input.serial_number,
|
|
85
|
+
})
|
|
86
|
+
} else if (scanAction === "verify") {
|
|
87
|
+
resolvedUrl = `${baseUrl}/barcodes/${encodeURIComponent(
|
|
88
|
+
code
|
|
89
|
+
)}/verify?sig=${encodeURIComponent(hmacSignature)}`
|
|
90
|
+
} else if (scanAction === "add_to_cart") {
|
|
91
|
+
const variantParam = input.variant_id || input.target_id
|
|
92
|
+
resolvedUrl = `${baseUrl}/cart/add?variant_id=${encodeURIComponent(
|
|
93
|
+
variantParam
|
|
94
|
+
)}&code=${encodeURIComponent(code)}`
|
|
95
|
+
} else {
|
|
96
|
+
resolvedUrl = `${baseUrl}/products/${encodeURIComponent(
|
|
97
|
+
input.product_id || input.target_id
|
|
98
|
+
)}?code=${encodeURIComponent(code)}`
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const encodedPayload =
|
|
103
|
+
symbology === "qr" || symbology === "datamatrix" ? resolvedUrl : code
|
|
104
|
+
|
|
105
|
+
const svgMarkup = renderBarcodeSvg({
|
|
106
|
+
code: encodedPayload,
|
|
107
|
+
symbology,
|
|
108
|
+
title: input.label || undefined,
|
|
109
|
+
includeText: symbology !== "qr" && symbology !== "datamatrix",
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
const existing = await ctx.svc.listBarcodeTags({ code }, { take: 1 })
|
|
113
|
+
const payload = {
|
|
114
|
+
code,
|
|
115
|
+
target_type: targetType,
|
|
116
|
+
target_id: input.target_id,
|
|
117
|
+
product_id: input.product_id ?? null,
|
|
118
|
+
variant_id: input.variant_id ?? null,
|
|
119
|
+
label: input.label ?? null,
|
|
120
|
+
symbology,
|
|
121
|
+
scan_action: scanAction,
|
|
122
|
+
gs1_gtin: input.gs1_gtin ?? null,
|
|
123
|
+
batch_lot: input.batch_lot ?? null,
|
|
124
|
+
serial_number: input.serial_number ?? null,
|
|
125
|
+
hmac_signature: hmacSignature,
|
|
126
|
+
target_url: resolvedUrl,
|
|
127
|
+
svg_markup: svgMarkup,
|
|
128
|
+
svg_url: input.svg_url ?? null,
|
|
129
|
+
is_active: true,
|
|
130
|
+
metadata: input.metadata ?? null,
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (existing[0]) {
|
|
134
|
+
return await ctx.svc.updateBarcodeTags({
|
|
135
|
+
id: existing[0].id,
|
|
136
|
+
...payload,
|
|
137
|
+
})
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return await ctx.svc.createBarcodeTags({
|
|
141
|
+
...payload,
|
|
142
|
+
scan_count: 0,
|
|
143
|
+
last_scanned_at: null,
|
|
144
|
+
})
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Resolves a storefront or admin scan against `BarcodeTag`, `BarcodeProduct`,
|
|
149
|
+
* and optionally Medusa's `ProductVariant` table (`barcode` / `ean` / `upc`),
|
|
150
|
+
* incrementing scan analytics when a tag is matched and optionally hydrating the variant and product.
|
|
151
|
+
*/
|
|
152
|
+
export async function resolveBarcodeScan(
|
|
153
|
+
ctx: BarcodeOpsContext,
|
|
154
|
+
codeOrBarcode: string,
|
|
155
|
+
options?: {
|
|
156
|
+
scope?: { resolve: (key: string) => unknown }
|
|
157
|
+
hydrateVariant?: boolean
|
|
158
|
+
}
|
|
159
|
+
) {
|
|
160
|
+
const clean = String(codeOrBarcode || "").trim()
|
|
161
|
+
if (!clean) {
|
|
162
|
+
throw new MedusaError(
|
|
163
|
+
MedusaError.Types.INVALID_DATA,
|
|
164
|
+
"Scan code is required"
|
|
165
|
+
)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const parsed = parseBarcode(clean)
|
|
169
|
+
const tags = await ctx.svc.listBarcodeTags({ code: clean }, { take: 1 })
|
|
170
|
+
let tag = tags[0] || null
|
|
171
|
+
|
|
172
|
+
if (tag) {
|
|
173
|
+
const nextCount = Number(tag.scan_count || 0) + 1
|
|
174
|
+
tag = await ctx.svc.updateBarcodeTags({
|
|
175
|
+
id: tag.id,
|
|
176
|
+
scan_count: nextCount,
|
|
177
|
+
last_scanned_at: new Date(),
|
|
178
|
+
})
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const barcodeProducts = await ctx.svc.listBarcodeProducts(
|
|
182
|
+
{ barcode: parsed.normalized },
|
|
183
|
+
{ take: 1 }
|
|
184
|
+
)
|
|
185
|
+
const barcodeProduct = barcodeProducts[0] || null
|
|
186
|
+
|
|
187
|
+
let resolvedProductId: string | null =
|
|
188
|
+
tag?.product_id || barcodeProduct?.product_id || null
|
|
189
|
+
let resolvedVariantId: string | null =
|
|
190
|
+
tag?.variant_id || barcodeProduct?.variant_id || null
|
|
191
|
+
let hydratedVariant: Record<string, unknown> | null = null
|
|
192
|
+
let hydratedProduct: Record<string, unknown> | null = null
|
|
193
|
+
|
|
194
|
+
type ProductModuleLike = {
|
|
195
|
+
listProductVariants?: (
|
|
196
|
+
filter: Record<string, unknown>,
|
|
197
|
+
config?: Record<string, unknown>
|
|
198
|
+
) => Promise<Array<Record<string, unknown>>>
|
|
199
|
+
listProducts?: (
|
|
200
|
+
filter: Record<string, unknown>,
|
|
201
|
+
config?: Record<string, unknown>
|
|
202
|
+
) => Promise<Array<Record<string, unknown>>>
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
let productService: ProductModuleLike | null = null
|
|
206
|
+
if (options?.scope && typeof options.scope.resolve === "function") {
|
|
207
|
+
for (const key of [Modules.PRODUCT, "product"]) {
|
|
208
|
+
try {
|
|
209
|
+
const svc = options.scope.resolve(key)
|
|
210
|
+
if (svc && typeof svc === "object") {
|
|
211
|
+
productService = svc as ProductModuleLike
|
|
212
|
+
break
|
|
213
|
+
}
|
|
214
|
+
} catch {
|
|
215
|
+
// ignore if Product module is not registered in scope
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Fallback: if neither BarcodeTag nor BarcodeProduct matched, query ProductVariant by barcode/ean/upc
|
|
221
|
+
if (
|
|
222
|
+
!tag &&
|
|
223
|
+
!barcodeProduct &&
|
|
224
|
+
productService &&
|
|
225
|
+
typeof productService.listProductVariants === "function"
|
|
226
|
+
) {
|
|
227
|
+
const candidateCodes = Array.from(new Set([parsed.normalized, clean]))
|
|
228
|
+
const candidateFields = ["barcode", "ean", "upc"] as const
|
|
229
|
+
|
|
230
|
+
outer: for (const codeCandidate of candidateCodes) {
|
|
231
|
+
for (const field of candidateFields) {
|
|
232
|
+
try {
|
|
233
|
+
const variants = await productService.listProductVariants(
|
|
234
|
+
{ [field]: codeCandidate },
|
|
235
|
+
{ take: 10 }
|
|
236
|
+
)
|
|
237
|
+
if (Array.isArray(variants) && variants.length > 0) {
|
|
238
|
+
const matched = variants.find(
|
|
239
|
+
(v) =>
|
|
240
|
+
v.barcode === parsed.normalized ||
|
|
241
|
+
v.barcode === clean ||
|
|
242
|
+
v.ean === parsed.normalized ||
|
|
243
|
+
v.ean === clean ||
|
|
244
|
+
v.upc === parsed.normalized ||
|
|
245
|
+
v.upc === clean ||
|
|
246
|
+
(variants.length === 1 &&
|
|
247
|
+
v.barcode === undefined &&
|
|
248
|
+
v.ean === undefined &&
|
|
249
|
+
v.upc === undefined)
|
|
250
|
+
)
|
|
251
|
+
if (matched) {
|
|
252
|
+
hydratedVariant = matched
|
|
253
|
+
if (matched.id) {
|
|
254
|
+
resolvedVariantId = String(matched.id)
|
|
255
|
+
}
|
|
256
|
+
if (matched.product_id) {
|
|
257
|
+
resolvedProductId = String(matched.product_id)
|
|
258
|
+
}
|
|
259
|
+
break outer
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
} catch {
|
|
263
|
+
// continue trying next field
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Hydrate variant and product if requested (default true when scope is passed)
|
|
270
|
+
if (productService && options?.hydrateVariant !== false) {
|
|
271
|
+
if (
|
|
272
|
+
!hydratedVariant &&
|
|
273
|
+
resolvedVariantId &&
|
|
274
|
+
typeof productService.listProductVariants === "function"
|
|
275
|
+
) {
|
|
276
|
+
try {
|
|
277
|
+
const variants = await productService.listProductVariants(
|
|
278
|
+
{ id: [resolvedVariantId] },
|
|
279
|
+
{ take: 10 }
|
|
280
|
+
)
|
|
281
|
+
if (Array.isArray(variants) && variants.length > 0) {
|
|
282
|
+
hydratedVariant =
|
|
283
|
+
variants.find((v) => v.id === resolvedVariantId) ||
|
|
284
|
+
variants[0] ||
|
|
285
|
+
null
|
|
286
|
+
if (
|
|
287
|
+
hydratedVariant &&
|
|
288
|
+
!resolvedProductId &&
|
|
289
|
+
hydratedVariant.product_id
|
|
290
|
+
) {
|
|
291
|
+
resolvedProductId = String(hydratedVariant.product_id)
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
} catch {
|
|
295
|
+
// non-fatal hydration error
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
if (
|
|
300
|
+
!hydratedProduct &&
|
|
301
|
+
resolvedProductId &&
|
|
302
|
+
typeof productService.listProducts === "function"
|
|
303
|
+
) {
|
|
304
|
+
try {
|
|
305
|
+
const products = await productService.listProducts(
|
|
306
|
+
{ id: [resolvedProductId] },
|
|
307
|
+
{ take: 10 }
|
|
308
|
+
)
|
|
309
|
+
if (Array.isArray(products) && products.length > 0) {
|
|
310
|
+
hydratedProduct =
|
|
311
|
+
products.find((p) => p.id === resolvedProductId) ||
|
|
312
|
+
products[0] ||
|
|
313
|
+
null
|
|
314
|
+
if (
|
|
315
|
+
hydratedProduct &&
|
|
316
|
+
!hydratedVariant &&
|
|
317
|
+
Array.isArray(hydratedProduct.variants)
|
|
318
|
+
) {
|
|
319
|
+
const productVariants = hydratedProduct.variants as Array<
|
|
320
|
+
Record<string, unknown>
|
|
321
|
+
>
|
|
322
|
+
const matchedVar =
|
|
323
|
+
(resolvedVariantId
|
|
324
|
+
? productVariants.find((v) => v.id === resolvedVariantId)
|
|
325
|
+
: undefined) ||
|
|
326
|
+
productVariants.find(
|
|
327
|
+
(v) =>
|
|
328
|
+
v.barcode === parsed.normalized ||
|
|
329
|
+
v.barcode === clean ||
|
|
330
|
+
v.ean === parsed.normalized ||
|
|
331
|
+
v.upc === parsed.normalized
|
|
332
|
+
) ||
|
|
333
|
+
productVariants[0] ||
|
|
334
|
+
null
|
|
335
|
+
if (matchedVar) {
|
|
336
|
+
hydratedVariant = matchedVar
|
|
337
|
+
if (!resolvedVariantId && matchedVar.id) {
|
|
338
|
+
resolvedVariantId = String(matchedVar.id)
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
} catch {
|
|
344
|
+
// non-fatal hydration error
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
return {
|
|
350
|
+
code: clean,
|
|
351
|
+
parsed,
|
|
352
|
+
found: Boolean(tag || barcodeProduct || hydratedVariant || hydratedProduct),
|
|
353
|
+
action: (tag?.scan_action as BarcodeScanAction) || "view",
|
|
354
|
+
target_url: tag?.target_url || null,
|
|
355
|
+
product_id: resolvedProductId,
|
|
356
|
+
variant_id: resolvedVariantId,
|
|
357
|
+
variant: hydratedVariant,
|
|
358
|
+
product: hydratedProduct,
|
|
359
|
+
tag,
|
|
360
|
+
barcode_product: barcodeProduct,
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Verifies an anti-counterfeit HMAC-SHA256 signature for a `BarcodeTag`.
|
|
366
|
+
*/
|
|
367
|
+
export async function verifyAuthenticityToken(
|
|
368
|
+
ctx: BarcodeOpsContext,
|
|
369
|
+
code: string,
|
|
370
|
+
providedSignature: string
|
|
371
|
+
) {
|
|
372
|
+
const cleanCode = String(code || "").trim()
|
|
373
|
+
if (!cleanCode || !providedSignature) {
|
|
374
|
+
return {
|
|
375
|
+
valid: false,
|
|
376
|
+
code: cleanCode,
|
|
377
|
+
reason: "Missing code or signature",
|
|
378
|
+
tag: null,
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const tags = await ctx.svc.listBarcodeTags({ code: cleanCode }, { take: 1 })
|
|
383
|
+
const tag = tags[0]
|
|
384
|
+
if (!tag || !tag.is_active) {
|
|
385
|
+
return {
|
|
386
|
+
valid: false,
|
|
387
|
+
code: cleanCode,
|
|
388
|
+
reason: "Tag not found or inactive",
|
|
389
|
+
tag: null,
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const isValid = verifyAuthenticityHmac(
|
|
394
|
+
{
|
|
395
|
+
code: tag.code,
|
|
396
|
+
targetId: tag.target_id,
|
|
397
|
+
serialNumber: tag.serial_number,
|
|
398
|
+
batchLot: tag.batch_lot,
|
|
399
|
+
},
|
|
400
|
+
providedSignature,
|
|
401
|
+
ctx.hmacSecret
|
|
402
|
+
)
|
|
403
|
+
|
|
404
|
+
if (isValid) {
|
|
405
|
+
const nextCount = Number(tag.scan_count || 0) + 1
|
|
406
|
+
const updated = await ctx.svc.updateBarcodeTags({
|
|
407
|
+
id: tag.id,
|
|
408
|
+
scan_count: nextCount,
|
|
409
|
+
last_scanned_at: new Date(),
|
|
410
|
+
})
|
|
411
|
+
return {
|
|
412
|
+
valid: true,
|
|
413
|
+
code: cleanCode,
|
|
414
|
+
reason: "Authentic verified tag",
|
|
415
|
+
tag: updated,
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return {
|
|
420
|
+
valid: false,
|
|
421
|
+
code: cleanCode,
|
|
422
|
+
reason: "Invalid HMAC signature",
|
|
423
|
+
tag: null,
|
|
424
|
+
}
|
|
425
|
+
}
|