@vicacha-devs/medusa-plugin-barcode 0.0.0-stage → 0.0.1
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 +129 -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,54 @@
|
|
|
1
|
+
# Scheduled Jobs
|
|
2
|
+
|
|
3
|
+
Background cron jobs registered by `medusa-plugin-barcode` under `src/jobs/`.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
jobs/
|
|
7
|
+
└── purge-expired-barcode-cache.ts purges expired barcode_cache rows every 6 hours
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## `purge-expired-barcode-cache`
|
|
13
|
+
|
|
14
|
+
| Property | Value |
|
|
15
|
+
| -------------------- | ------------------------------------------- |
|
|
16
|
+
| **File** | `src/jobs/purge-expired-barcode-cache.ts` |
|
|
17
|
+
| **Job Name** | `"purge-expired-barcode-cache"` |
|
|
18
|
+
| **Cron Schedule** | `"0 */6 * * *"` (every 6 hours at minute 0) |
|
|
19
|
+
| **Service Delegate** | `barcodeService.purgeExpiredCache()` |
|
|
20
|
+
|
|
21
|
+
### Behavior
|
|
22
|
+
|
|
23
|
+
When `cacheEnabled: true` (the default), external GTIN/ISBN lookup responses from the 10-provider chain are cached in the `barcode_cache` table with an `expires_at` timestamp (`now + cacheTtlSeconds`, default `86400` seconds / 24 hours).
|
|
24
|
+
|
|
25
|
+
Every 6 hours, `purgeExpiredBarcodeCacheJob` resolves `BARCODE_MODULE` (`"barcode"`) from the `MedusaContainer` and executes `barcodeService.purgeExpiredCache()`, deleting all `BarcodeCache` rows whose `expires_at` timestamp is earlier than the current time.
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import type { MedusaContainer } from "@medusajs/framework/types"
|
|
29
|
+
import type { BarcodeModuleService } from "../modules/barcode/service"
|
|
30
|
+
import { BARCODE_MODULE } from "../modules/barcode/types"
|
|
31
|
+
|
|
32
|
+
export default async function purgeExpiredBarcodeCacheJob(
|
|
33
|
+
container: MedusaContainer
|
|
34
|
+
) {
|
|
35
|
+
const barcodeService = container.resolve(
|
|
36
|
+
BARCODE_MODULE
|
|
37
|
+
) as BarcodeModuleService
|
|
38
|
+
await barcodeService.purgeExpiredCache()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const config = {
|
|
42
|
+
name: "purge-expired-barcode-cache",
|
|
43
|
+
schedule: "0 */6 * * *",
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## Manual cache purge
|
|
50
|
+
|
|
51
|
+
In addition to the scheduled job, administrators can inspect or purge cached lookups on demand via:
|
|
52
|
+
|
|
53
|
+
- **REST API**: `DELETE /admin/barcodes/cache` (purges expired rows) or `DELETE /admin/barcodes/cache?all=true` (clears all cached entries).
|
|
54
|
+
- **Admin UI**: `/app/barcodes/providers` ("Providers & Cache" route).
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { MedusaContainer } from "@medusajs/framework/types"
|
|
2
|
+
|
|
3
|
+
import type { BarcodeModuleService } from "../modules/barcode/service"
|
|
4
|
+
import { BARCODE_MODULE } from "../modules/barcode/types"
|
|
5
|
+
|
|
6
|
+
export default async function purgeExpiredBarcodeCacheJob(
|
|
7
|
+
container: MedusaContainer
|
|
8
|
+
) {
|
|
9
|
+
const barcodeService = container.resolve(
|
|
10
|
+
BARCODE_MODULE
|
|
11
|
+
) as BarcodeModuleService
|
|
12
|
+
await barcodeService.purgeExpiredCache()
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const config = {
|
|
16
|
+
name: "purge-expired-barcode-cache",
|
|
17
|
+
schedule: "0 */6 * * *",
|
|
18
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Links
|
|
2
|
+
|
|
3
|
+
Cross-module entity references that associate `medusa-plugin-barcode` models (`BarcodeProduct`, `BarcodeTag`, `BarcodeCountSession`, `BarcodeCountItem`) with Medusa core entities (`Product`, `ProductVariant`, `Order`, `StockLocation`, and `InventoryItem`) while preserving strict module isolation.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
links/
|
|
7
|
+
└── README.md documentation for cross-module ID references and optional defineLink joins
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## How `medusa-plugin-barcode` links to Medusa core entities
|
|
13
|
+
|
|
14
|
+
Instead of creating hard foreign-key constraints across module boundaries, `medusa-plugin-barcode` stores indexed plain-string reference IDs on its DML models in `src/modules/barcode/models/index.ts` and coordinates lifecycle changes through Medusa v2 workflows (`src/workflows/index.ts`):
|
|
15
|
+
|
|
16
|
+
| Barcode Model | Reference Columns | Target Medusa Core Entity | Indexing & Resolution |
|
|
17
|
+
| --------------------- | ------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
18
|
+
| `BarcodeProduct` | `product_id`, `variant_id` | `Product`, `ProductVariant` (`Modules.PRODUCT`) | Indexed on `barcode` (unique), `product_id`, and `variant_id`. Created/rolled back by `lookupAndCreateProductWorkflow` and `batchBarcodeImportWorkflow`. |
|
|
19
|
+
| `BarcodeTag` | `target_type` (`product` \| `variant` \| `order` \| `custom`), `target_id`, `product_id`, `variant_id` | `Product`, `ProductVariant` (`Modules.PRODUCT`), `Order` (`Modules.ORDER`) | Indexed on `code` (unique), `[target_type, target_id]`, `product_id`, and `variant_id`. Created by `generateBarcodeTagWorkflow` and `bulkGenerateVariantBarcodesWorkflow`. |
|
|
20
|
+
| `BarcodeCountSession` | `location_id` | `StockLocation` (`Modules.STOCK_LOCATION`) | Indexed on `status` and `location_id`. Used by `applyInventoryCountWorkflow` when reconciling stock levels. |
|
|
21
|
+
| `BarcodeCountItem` | `product_id`, `variant_id`, `sku` | `Product`, `ProductVariant` (`Modules.PRODUCT`), `InventoryItem` (`Modules.INVENTORY`) | Indexed on `session_id` and `[session_id, barcode]` (unique). Reconciled against `Modules.INVENTORY` by `sku` and `location_id`. |
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Adding a declarative `defineLink` (optional)
|
|
26
|
+
|
|
27
|
+
If your host application wants to traverse `BarcodeProduct` or `BarcodeTag` directly through `query.graph` / `remoteQuery` alongside Medusa's `Product` model, you can add a link file in `src/links/` (for example `src/links/product-barcode.ts`):
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { defineLink } from "@medusajs/framework/utils"
|
|
31
|
+
import ProductModule from "@medusajs/medusa/product"
|
|
32
|
+
import BarcodeModule from "../modules/barcode"
|
|
33
|
+
|
|
34
|
+
export default defineLink(ProductModule.linkable.product, {
|
|
35
|
+
linkable: BarcodeModule.linkable.barcodeProduct,
|
|
36
|
+
isList: true,
|
|
37
|
+
})
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
After adding a `defineLink` file, sync the link table in the host application:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
npx medusa db:migrate
|
|
44
|
+
```
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# Modules
|
|
2
|
+
|
|
3
|
+
The `barcode` module (`BARCODE_MODULE = "barcode"`) implements the data layer, 10-provider GTIN/ISBN lookup chain, GS1 parser, pure SVG renderer, thermal/A4 label printing engine, and anti-counterfeit HMAC security utilities for `medusa-plugin-barcode`.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
modules/
|
|
7
|
+
└── barcode/
|
|
8
|
+
├── index.ts exports BARCODE_MODULE ("barcode") + Module() definition
|
|
9
|
+
├── types.ts DTOs, plugin options, symbologies, provider & label types
|
|
10
|
+
├── service.ts BarcodeModuleService (alias: BarcodeService) extending MedusaService
|
|
11
|
+
├── models/
|
|
12
|
+
│ └── index.ts 6 DML models (Product, Cache, Tag, Template, Session, Item)
|
|
13
|
+
├── providers/
|
|
14
|
+
│ └── index.ts BaseBarcodeProvider, ProviderRegistry & 10 lookup providers
|
|
15
|
+
└── utils/
|
|
16
|
+
├── barcode-parser.ts Right-to-left GS1 Modulo-10, UPC-E, GS1-128 AI & Digital Link
|
|
17
|
+
├── normalize-product.ts mergeNormalizedProducts & mapToMedusaCreateProduct
|
|
18
|
+
├── svg-renderer.ts Zero-dependency 1D/2D vector SVG barcode & QR/DataMatrix renderer
|
|
19
|
+
├── label-printer.ts 7 thermal/A4 presets, HTML+SVG sheet & Zebra ZPL II generator
|
|
20
|
+
└── security.ts HMAC-SHA256 signer/verifier, SSRF guard & escapeHtml
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Data models (`src/modules/barcode/models/index.ts`)
|
|
26
|
+
|
|
27
|
+
All 6 models are defined with Medusa v2's DML (`model.define`) and registered on `BarcodeModuleService`:
|
|
28
|
+
|
|
29
|
+
| Model | Table Name | Key Fields | Purpose |
|
|
30
|
+
| ---------------------- | ------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
|
|
31
|
+
| `BarcodeProduct` | `barcode_product` | `barcode` (unique), `barcode_type`, `product_id`, `variant_id`, `provider`, `product_data`, `status` (`created` \| `linked` \| `updated`), `last_synced_at` | Links a GTIN/ISBN barcode to a Medusa `Product` and `ProductVariant` alongside normalized provider metadata |
|
|
32
|
+
| `BarcodeCache` | `barcode_cache` | `barcode` (unique), `barcode_type`, `provider`, `found`, `response_data`, `expires_at` (indexed), `hit_count` | Caches external provider lookup responses with TTL expiration and hit tracking |
|
|
33
|
+
| `BarcodeTag` | `barcode_tag` | `code` (unique), `target_type` (`product` \| `variant` \| `order` \| `custom`), `target_id`, `product_id`, `variant_id`, `symbology`, `scan_action` (`view` \| `add_to_cart` \| `verify` \| `gs1_digital_link` \| `custom`), `gs1_gtin`, `batch_lot`, `serial_number`, `hmac_signature`, `svg_markup`, `svg_url`, `scan_count`, `last_scanned_at`, `is_active` | Stores generated 1D/2D barcodes and dynamic QR tags with GS1 attributes, scan analytics, and anti-counterfeit `HMAC-SHA256` seals |
|
|
34
|
+
| `BarcodeLabelTemplate` | `barcode_label_template` | `name`, `preset_id`, `layout_type` (`thermal_roll` \| `a4_sheet`), `width_mm`, `height_mm`, `columns`, `rows`, `margin_top_mm`, `margin_left_mm`, `gap_x_mm`, `gap_y_mm`, `symbology`, `show_title`, `show_variant_title`, `show_sku`, `show_price`, `show_compare_at_price`, `show_brand`, `show_qr`, `custom_header`, `custom_footer`, `dpi`, `is_default` | Persists customizable thermal roll and A4 sheet label layout templates |
|
|
35
|
+
| `BarcodeCountSession` | `barcode_count_session` | `name`, `location_id`, `status` (`draft` \| `in_progress` \| `completed` \| `cancelled`), `notes`, `total_expected`, `total_counted`, `total_variance`, `total_scans`, `completed_at` | Tracks warehouse physical inventory stock-take audit sessions |
|
|
36
|
+
| `BarcodeCountItem` | `barcode_count_item` | `session_id`, `barcode` (unique per session), `variant_id`, `product_id`, `title`, `variant_title`, `sku`, `expected_quantity`, `counted_quantity`, `variance`, `last_scanned_at` | Tracks per-SKU/barcode expected vs. counted quantities and variance inside a `BarcodeCountSession` |
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## Service (`BarcodeModuleService` / `BarcodeService`)
|
|
41
|
+
|
|
42
|
+
**Service token:** `BARCODE_MODULE = "barcode"`
|
|
43
|
+
|
|
44
|
+
`BarcodeModuleService` extends `MedusaService(BarcodeModels)`. Every public method is `async` and throws `MedusaError` from `@medusajs/framework/utils` on invalid input or missing entities.
|
|
45
|
+
|
|
46
|
+
| Method | Description |
|
|
47
|
+
| ----------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
48
|
+
| `getModuleOptions()` | Returns resolved `BarcodeModuleOptions` merged with defaults |
|
|
49
|
+
| `getProviderRegistry()` | Returns the active `ProviderRegistry` with all configured lookup providers |
|
|
50
|
+
| `lookupBarcode(rawBarcode, options?)` | Validates/normalizes a GTIN/ISBN, checks `BarcodeCache`, queries the 10-provider chain (`first-hit` or `deep-merge`), and caches the result |
|
|
51
|
+
| `upsertBarcodeProductLink(data)` | Creates or updates a `BarcodeProduct` mapping between a barcode and a Medusa `product_id` / `variant_id` |
|
|
52
|
+
| `generateInternalVariantBarcode(input)` | Generates a deterministic or sequential GS1 in-store (`200`–`299`) 13-digit EAN-13 barcode with valid check digit |
|
|
53
|
+
| `createOrUpdateBarcodeTag(input)` | Renders 1D/2D SVG markup, computes an `HMAC-SHA256` signature (`signAuthenticityHmac`), builds the target/GS1 Digital Link URL, and upserts `BarcodeTag` |
|
|
54
|
+
| `resolveStoreScan(code)` | Resolves a scanned barcode or QR tag on the storefront, increments `scan_count`, updates `last_scanned_at`, and returns the scan action + target URL |
|
|
55
|
+
| `verifyTagAuthenticity(code, signature)` | Verifies a tag's anti-counterfeit `HMAC-SHA256` seal in constant time (`crypto.timingSafeEqual`) |
|
|
56
|
+
| `listAllLabelTemplates()` | Returns the 7 built-in presets (`BUILTIN_LABEL_TEMPLATES`) plus custom `BarcodeLabelTemplate` rows |
|
|
57
|
+
| `printLabels(items, templateIdOrSpec?)` | Renders both a print-ready HTML+SVG document (`html`) and raw Zebra ZPL II commands (`zpl`) |
|
|
58
|
+
| `startCountSession(input)` | Creates a `BarcodeCountSession` and optional initial `BarcodeCountItem` rows |
|
|
59
|
+
| `recordCountSessionScan(input)` | Records a `+1`, `-1`, or `set_quantity` barcode scan inside an active session and recalculates item & session variance |
|
|
60
|
+
| `completeCountSession(sessionId)` | Marks a count session as `"completed"` and returns all line items for inventory reconciliation |
|
|
61
|
+
| `purgeExpiredCache()` / `clearAllCache()` | Deletes expired or all rows from `barcode_cache` |
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
## Utilities (`src/modules/barcode/utils/`)
|
|
66
|
+
|
|
67
|
+
| File | Key Exports | Responsibility |
|
|
68
|
+
| ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
69
|
+
| `barcode-parser.ts` | `computeGs1CheckDigit`, `validateGs1Checksum`, `expandUpcEToUpcA`, `upcToEan13`, `parseGs1ApplicationIdentifiers`, `buildGs1DigitalLink`, `generateGs1InStoreEan13`, `parseBarcode` | Spec-compliant right-to-left `3,1,3,1` GS1 Modulo-10 checksum math across `EAN-8`, `UPC-E`, `UPC-A`, `EAN-13`, `ISBN-13`, and `GTIN-14`, plus GS1-128 Application Identifiers (`(01)`, `(10)`, `(11)`, `(15)`, `(17)`, `(21)`, `(30)`, `(37)`, `(310n)`) and GS1 Digital Link URIs |
|
|
70
|
+
| `normalize-product.ts` | `mergeNormalizedProducts`, `mapToMedusaCreateProduct` | Deep-merges partial `NormalizedBarcodeProduct` results across multiple providers and maps them to Medusa v2 `createProducts` payloads |
|
|
71
|
+
| `svg-renderer.ts` | `renderBarcodeSvg` | Pure TypeScript vector SVG generator for 1D (`ean13`, `isbn13`, `upca`, `upce`, `ean8`, `gtin14`, `code128`, `gs1-128`, `code39`) and 2D (`qr`, `datamatrix`) symbologies |
|
|
72
|
+
| `label-printer.ts` | `BUILTIN_LABEL_TEMPLATES`, `getLabelTemplateById`, `formatLabelPrice`, `renderPrintableLabels` | 7 thermal roll (`50×25`, `57×32`, `100×50`, `30×15` Jewelry) & A4 sheet (`24`/`40`/`65`-up) presets, multi-currency price formatting, HTML+SVG `@page` generator, and native `203`/`300 DPI` Zebra ZPL II (`^XA...^XZ`) generator |
|
|
73
|
+
| `security.ts` | `escapeHtml`, `validateExternalImageUrl`, `signAuthenticityHmac`, `verifyAuthenticityHmac` | XSS escaping for SVG/HTML labels, SSRF guard blocking private/loopback/metadata IPs on external image fetches, and constant-time `HMAC-SHA256` signature generation/verification |
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
## Generating migrations
|
|
78
|
+
|
|
79
|
+
After editing a model in `models/index.ts`, regenerate migrations from the plugin root:
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
npx medusa plugin:db:generate
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Migration files are applied in the host application via `npx medusa db:migrate`.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Module } from "@medusajs/framework/utils"
|
|
2
|
+
|
|
3
|
+
import { BarcodeModuleService, BarcodeService } from "./service"
|
|
4
|
+
import { BARCODE_MODULE } from "./types"
|
|
5
|
+
|
|
6
|
+
export { BARCODE_MODULE, BarcodeModuleService, BarcodeService }
|
|
7
|
+
export * from "./models"
|
|
8
|
+
export * from "./providers"
|
|
9
|
+
export * from "./types"
|
|
10
|
+
export * from "./utils/barcode-parser"
|
|
11
|
+
export * from "./utils/label-printer"
|
|
12
|
+
export * from "./utils/normalize-product"
|
|
13
|
+
export * from "./utils/security"
|
|
14
|
+
export * from "./utils/svg-renderer"
|
|
15
|
+
|
|
16
|
+
export default Module(BARCODE_MODULE, {
|
|
17
|
+
service: BarcodeModuleService,
|
|
18
|
+
})
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { Migration } from "@medusajs/framework/mikro-orm/migrations"
|
|
2
|
+
|
|
3
|
+
export class Migration20260522000000 extends Migration {
|
|
4
|
+
override async up(): Promise<void> {
|
|
5
|
+
// 1. barcode_product
|
|
6
|
+
this.addSql(
|
|
7
|
+
`create table if not exists "barcode_product" (` +
|
|
8
|
+
`"id" text not null, ` +
|
|
9
|
+
`"barcode" text not null, ` +
|
|
10
|
+
`"barcode_type" text not null default 'ean13', ` +
|
|
11
|
+
`"product_id" text not null, ` +
|
|
12
|
+
`"variant_id" text null, ` +
|
|
13
|
+
`"provider" text not null, ` +
|
|
14
|
+
`"product_data" jsonb not null default '{}'::jsonb, ` +
|
|
15
|
+
`"last_synced_at" timestamptz null, ` +
|
|
16
|
+
`"status" text check ("status" in ('created', 'linked', 'updated')) not null default 'created', ` +
|
|
17
|
+
`"metadata" jsonb null, ` +
|
|
18
|
+
`"created_at" timestamptz not null default now(), ` +
|
|
19
|
+
`"updated_at" timestamptz not null default now(), ` +
|
|
20
|
+
`"deleted_at" timestamptz null, ` +
|
|
21
|
+
`constraint "barcode_product_pkey" primary key ("id")` +
|
|
22
|
+
`);`
|
|
23
|
+
)
|
|
24
|
+
this.addSql(
|
|
25
|
+
`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_barcode_product_barcode_unique" ON "barcode_product" ("barcode") WHERE deleted_at IS NULL;`
|
|
26
|
+
)
|
|
27
|
+
this.addSql(
|
|
28
|
+
`CREATE INDEX IF NOT EXISTS "IDX_barcode_product_product_id" ON "barcode_product" ("product_id") WHERE deleted_at IS NULL;`
|
|
29
|
+
)
|
|
30
|
+
this.addSql(
|
|
31
|
+
`CREATE INDEX IF NOT EXISTS "IDX_barcode_product_variant_id" ON "barcode_product" ("variant_id") WHERE deleted_at IS NULL;`
|
|
32
|
+
)
|
|
33
|
+
this.addSql(
|
|
34
|
+
`CREATE INDEX IF NOT EXISTS "IDX_barcode_product_deleted_at" ON "barcode_product" ("deleted_at") WHERE deleted_at IS NULL;`
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
// 2. barcode_cache
|
|
38
|
+
this.addSql(
|
|
39
|
+
`create table if not exists "barcode_cache" (` +
|
|
40
|
+
`"id" text not null, ` +
|
|
41
|
+
`"barcode" text not null, ` +
|
|
42
|
+
`"barcode_type" text not null default 'ean13', ` +
|
|
43
|
+
`"provider" text not null, ` +
|
|
44
|
+
`"found" boolean not null default false, ` +
|
|
45
|
+
`"response_data" jsonb not null default '{}'::jsonb, ` +
|
|
46
|
+
`"expires_at" timestamptz not null, ` +
|
|
47
|
+
`"hit_count" integer not null default 1, ` +
|
|
48
|
+
`"created_at" timestamptz not null default now(), ` +
|
|
49
|
+
`"updated_at" timestamptz not null default now(), ` +
|
|
50
|
+
`"deleted_at" timestamptz null, ` +
|
|
51
|
+
`constraint "barcode_cache_pkey" primary key ("id")` +
|
|
52
|
+
`);`
|
|
53
|
+
)
|
|
54
|
+
this.addSql(
|
|
55
|
+
`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_barcode_cache_barcode_unique" ON "barcode_cache" ("barcode") WHERE deleted_at IS NULL;`
|
|
56
|
+
)
|
|
57
|
+
this.addSql(
|
|
58
|
+
`CREATE INDEX IF NOT EXISTS "IDX_barcode_cache_expires_at" ON "barcode_cache" ("expires_at") WHERE deleted_at IS NULL;`
|
|
59
|
+
)
|
|
60
|
+
this.addSql(
|
|
61
|
+
`CREATE INDEX IF NOT EXISTS "IDX_barcode_cache_deleted_at" ON "barcode_cache" ("deleted_at") WHERE deleted_at IS NULL;`
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
// 3. barcode_tag
|
|
65
|
+
this.addSql(
|
|
66
|
+
`create table if not exists "barcode_tag" (` +
|
|
67
|
+
`"id" text not null, ` +
|
|
68
|
+
`"code" text not null, ` +
|
|
69
|
+
`"target_type" text check ("target_type" in ('product', 'variant', 'order', 'custom')) not null default 'product', ` +
|
|
70
|
+
`"target_id" text not null, ` +
|
|
71
|
+
`"product_id" text null, ` +
|
|
72
|
+
`"variant_id" text null, ` +
|
|
73
|
+
`"label" text null, ` +
|
|
74
|
+
`"symbology" text not null default 'qr', ` +
|
|
75
|
+
`"scan_action" text check ("scan_action" in ('view', 'add_to_cart', 'verify', 'gs1_digital_link', 'custom')) not null default 'view', ` +
|
|
76
|
+
`"gs1_gtin" text null, ` +
|
|
77
|
+
`"batch_lot" text null, ` +
|
|
78
|
+
`"serial_number" text null, ` +
|
|
79
|
+
`"hmac_signature" text null, ` +
|
|
80
|
+
`"target_url" text null, ` +
|
|
81
|
+
`"svg_markup" text null, ` +
|
|
82
|
+
`"svg_url" text null, ` +
|
|
83
|
+
`"scan_count" integer not null default 0, ` +
|
|
84
|
+
`"last_scanned_at" timestamptz null, ` +
|
|
85
|
+
`"is_active" boolean not null default true, ` +
|
|
86
|
+
`"metadata" jsonb null, ` +
|
|
87
|
+
`"created_at" timestamptz not null default now(), ` +
|
|
88
|
+
`"updated_at" timestamptz not null default now(), ` +
|
|
89
|
+
`"deleted_at" timestamptz null, ` +
|
|
90
|
+
`constraint "barcode_tag_pkey" primary key ("id")` +
|
|
91
|
+
`);`
|
|
92
|
+
)
|
|
93
|
+
this.addSql(
|
|
94
|
+
`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_barcode_tag_code_unique" ON "barcode_tag" ("code") WHERE deleted_at IS NULL;`
|
|
95
|
+
)
|
|
96
|
+
this.addSql(
|
|
97
|
+
`CREATE INDEX IF NOT EXISTS "IDX_barcode_tag_target" ON "barcode_tag" ("target_type", "target_id") WHERE deleted_at IS NULL;`
|
|
98
|
+
)
|
|
99
|
+
this.addSql(
|
|
100
|
+
`CREATE INDEX IF NOT EXISTS "IDX_barcode_tag_product_id" ON "barcode_tag" ("product_id") WHERE deleted_at IS NULL;`
|
|
101
|
+
)
|
|
102
|
+
this.addSql(
|
|
103
|
+
`CREATE INDEX IF NOT EXISTS "IDX_barcode_tag_variant_id" ON "barcode_tag" ("variant_id") WHERE deleted_at IS NULL;`
|
|
104
|
+
)
|
|
105
|
+
this.addSql(
|
|
106
|
+
`CREATE INDEX IF NOT EXISTS "IDX_barcode_tag_deleted_at" ON "barcode_tag" ("deleted_at") WHERE deleted_at IS NULL;`
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
// 4. barcode_label_template
|
|
110
|
+
this.addSql(
|
|
111
|
+
`create table if not exists "barcode_label_template" (` +
|
|
112
|
+
`"id" text not null, ` +
|
|
113
|
+
`"name" text not null, ` +
|
|
114
|
+
`"preset_id" text not null default 'thermal_50x25', ` +
|
|
115
|
+
`"layout_type" text check ("layout_type" in ('thermal_roll', 'a4_sheet')) not null default 'thermal_roll', ` +
|
|
116
|
+
`"width_mm" numeric not null default 50, ` +
|
|
117
|
+
`"height_mm" numeric not null default 25, ` +
|
|
118
|
+
`"columns" integer not null default 1, ` +
|
|
119
|
+
`"rows" integer not null default 1, ` +
|
|
120
|
+
`"margin_top_mm" numeric not null default 2, ` +
|
|
121
|
+
`"margin_left_mm" numeric not null default 2, ` +
|
|
122
|
+
`"gap_x_mm" numeric not null default 0, ` +
|
|
123
|
+
`"gap_y_mm" numeric not null default 2, ` +
|
|
124
|
+
`"symbology" text not null default 'ean13', ` +
|
|
125
|
+
`"show_title" boolean not null default true, ` +
|
|
126
|
+
`"show_variant_title" boolean not null default true, ` +
|
|
127
|
+
`"show_sku" boolean not null default true, ` +
|
|
128
|
+
`"show_price" boolean not null default true, ` +
|
|
129
|
+
`"show_compare_at_price" boolean not null default true, ` +
|
|
130
|
+
`"show_brand" boolean not null default false, ` +
|
|
131
|
+
`"show_qr" boolean not null default false, ` +
|
|
132
|
+
`"custom_header" text null, ` +
|
|
133
|
+
`"custom_footer" text null, ` +
|
|
134
|
+
`"dpi" integer not null default 203, ` +
|
|
135
|
+
`"is_default" boolean not null default false, ` +
|
|
136
|
+
`"metadata" jsonb null, ` +
|
|
137
|
+
`"created_at" timestamptz not null default now(), ` +
|
|
138
|
+
`"updated_at" timestamptz not null default now(), ` +
|
|
139
|
+
`"deleted_at" timestamptz null, ` +
|
|
140
|
+
`constraint "barcode_label_template_pkey" primary key ("id")` +
|
|
141
|
+
`);`
|
|
142
|
+
)
|
|
143
|
+
this.addSql(
|
|
144
|
+
`CREATE INDEX IF NOT EXISTS "IDX_barcode_label_template_preset_id" ON "barcode_label_template" ("preset_id") WHERE deleted_at IS NULL;`
|
|
145
|
+
)
|
|
146
|
+
this.addSql(
|
|
147
|
+
`CREATE INDEX IF NOT EXISTS "IDX_barcode_label_template_is_default" ON "barcode_label_template" ("is_default") WHERE deleted_at IS NULL;`
|
|
148
|
+
)
|
|
149
|
+
this.addSql(
|
|
150
|
+
`CREATE INDEX IF NOT EXISTS "IDX_barcode_label_template_deleted_at" ON "barcode_label_template" ("deleted_at") WHERE deleted_at IS NULL;`
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
// 5. barcode_count_session
|
|
154
|
+
this.addSql(
|
|
155
|
+
`create table if not exists "barcode_count_session" (` +
|
|
156
|
+
`"id" text not null, ` +
|
|
157
|
+
`"name" text not null, ` +
|
|
158
|
+
`"location_id" text null, ` +
|
|
159
|
+
`"status" text check ("status" in ('draft', 'in_progress', 'completed', 'cancelled')) not null default 'in_progress', ` +
|
|
160
|
+
`"notes" text null, ` +
|
|
161
|
+
`"total_expected" integer not null default 0, ` +
|
|
162
|
+
`"total_counted" integer not null default 0, ` +
|
|
163
|
+
`"total_variance" integer not null default 0, ` +
|
|
164
|
+
`"total_scans" integer not null default 0, ` +
|
|
165
|
+
`"created_by" text null, ` +
|
|
166
|
+
`"completed_at" timestamptz null, ` +
|
|
167
|
+
`"metadata" jsonb null, ` +
|
|
168
|
+
`"created_at" timestamptz not null default now(), ` +
|
|
169
|
+
`"updated_at" timestamptz not null default now(), ` +
|
|
170
|
+
`"deleted_at" timestamptz null, ` +
|
|
171
|
+
`constraint "barcode_count_session_pkey" primary key ("id")` +
|
|
172
|
+
`);`
|
|
173
|
+
)
|
|
174
|
+
this.addSql(
|
|
175
|
+
`CREATE INDEX IF NOT EXISTS "IDX_barcode_count_session_status" ON "barcode_count_session" ("status") WHERE deleted_at IS NULL;`
|
|
176
|
+
)
|
|
177
|
+
this.addSql(
|
|
178
|
+
`CREATE INDEX IF NOT EXISTS "IDX_barcode_count_session_location_id" ON "barcode_count_session" ("location_id") WHERE deleted_at IS NULL;`
|
|
179
|
+
)
|
|
180
|
+
this.addSql(
|
|
181
|
+
`CREATE INDEX IF NOT EXISTS "IDX_barcode_count_session_deleted_at" ON "barcode_count_session" ("deleted_at") WHERE deleted_at IS NULL;`
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
// 6. barcode_count_item
|
|
185
|
+
this.addSql(
|
|
186
|
+
`create table if not exists "barcode_count_item" (` +
|
|
187
|
+
`"id" text not null, ` +
|
|
188
|
+
`"session_id" text not null, ` +
|
|
189
|
+
`"barcode" text not null, ` +
|
|
190
|
+
`"variant_id" text null, ` +
|
|
191
|
+
`"product_id" text null, ` +
|
|
192
|
+
`"title" text not null, ` +
|
|
193
|
+
`"variant_title" text null, ` +
|
|
194
|
+
`"sku" text null, ` +
|
|
195
|
+
`"expected_quantity" integer not null default 0, ` +
|
|
196
|
+
`"counted_quantity" integer not null default 0, ` +
|
|
197
|
+
`"variance" integer not null default 0, ` +
|
|
198
|
+
`"last_scanned_at" timestamptz null, ` +
|
|
199
|
+
`"metadata" jsonb null, ` +
|
|
200
|
+
`"created_at" timestamptz not null default now(), ` +
|
|
201
|
+
`"updated_at" timestamptz not null default now(), ` +
|
|
202
|
+
`"deleted_at" timestamptz null, ` +
|
|
203
|
+
`constraint "barcode_count_item_pkey" primary key ("id")` +
|
|
204
|
+
`);`
|
|
205
|
+
)
|
|
206
|
+
this.addSql(
|
|
207
|
+
`CREATE INDEX IF NOT EXISTS "IDX_barcode_count_item_session_id" ON "barcode_count_item" ("session_id") WHERE deleted_at IS NULL;`
|
|
208
|
+
)
|
|
209
|
+
this.addSql(
|
|
210
|
+
`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_barcode_count_item_session_barcode_unique" ON "barcode_count_item" ("session_id", "barcode") WHERE deleted_at IS NULL;`
|
|
211
|
+
)
|
|
212
|
+
this.addSql(
|
|
213
|
+
`CREATE INDEX IF NOT EXISTS "IDX_barcode_count_item_deleted_at" ON "barcode_count_item" ("deleted_at") WHERE deleted_at IS NULL;`
|
|
214
|
+
)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
override async down(): Promise<void> {
|
|
218
|
+
this.addSql(`drop table if exists "barcode_count_item" cascade;`)
|
|
219
|
+
this.addSql(`drop table if exists "barcode_count_session" cascade;`)
|
|
220
|
+
this.addSql(`drop table if exists "barcode_label_template" cascade;`)
|
|
221
|
+
this.addSql(`drop table if exists "barcode_tag" cascade;`)
|
|
222
|
+
this.addSql(`drop table if exists "barcode_cache" cascade;`)
|
|
223
|
+
this.addSql(`drop table if exists "barcode_product" cascade;`)
|
|
224
|
+
}
|
|
225
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { model } from "@medusajs/framework/utils"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `BarcodeProduct` links a barcode/GTIN to a Medusa Product and Variant,
|
|
5
|
+
* storing the provider metadata and sync status.
|
|
6
|
+
*/
|
|
7
|
+
export const BarcodeProduct = model
|
|
8
|
+
.define("barcode_product", {
|
|
9
|
+
id: model.id().primaryKey(),
|
|
10
|
+
barcode: model.text().searchable(),
|
|
11
|
+
barcode_type: model.text().default("ean13"),
|
|
12
|
+
product_id: model.text(),
|
|
13
|
+
variant_id: model.text().nullable(),
|
|
14
|
+
provider: model.text(),
|
|
15
|
+
product_data: model.json(),
|
|
16
|
+
last_synced_at: model.dateTime().nullable(),
|
|
17
|
+
status: model.enum(["created", "linked", "updated"]).default("created"),
|
|
18
|
+
metadata: model.json().nullable(),
|
|
19
|
+
})
|
|
20
|
+
.indexes([
|
|
21
|
+
{ on: ["barcode"], unique: true },
|
|
22
|
+
{ on: ["product_id"] },
|
|
23
|
+
{ on: ["variant_id"] },
|
|
24
|
+
])
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* `BarcodeCache` stores external GTIN lookup responses with TTL expiration and hit counters.
|
|
28
|
+
*/
|
|
29
|
+
export const BarcodeCache = model
|
|
30
|
+
.define("barcode_cache", {
|
|
31
|
+
id: model.id().primaryKey(),
|
|
32
|
+
barcode: model.text().searchable(),
|
|
33
|
+
barcode_type: model.text().default("ean13"),
|
|
34
|
+
provider: model.text(),
|
|
35
|
+
found: model.boolean().default(false),
|
|
36
|
+
response_data: model.json(),
|
|
37
|
+
expires_at: model.dateTime(),
|
|
38
|
+
hit_count: model.number().default(1),
|
|
39
|
+
})
|
|
40
|
+
.indexes([{ on: ["barcode"], unique: true }, { on: ["expires_at"] }])
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* `BarcodeTag` represents a generated 1D/2D barcode or dynamic QR token
|
|
44
|
+
* with storefront scan actions, GS1 Digital Link metadata, and HMAC-SHA256 anti-counterfeit seal.
|
|
45
|
+
*/
|
|
46
|
+
export const BarcodeTag = model
|
|
47
|
+
.define("barcode_tag", {
|
|
48
|
+
id: model.id().primaryKey(),
|
|
49
|
+
code: model.text().searchable(),
|
|
50
|
+
target_type: model
|
|
51
|
+
.enum(["product", "variant", "order", "custom"])
|
|
52
|
+
.default("product"),
|
|
53
|
+
target_id: model.text(),
|
|
54
|
+
product_id: model.text().nullable(),
|
|
55
|
+
variant_id: model.text().nullable(),
|
|
56
|
+
label: model.text().nullable(),
|
|
57
|
+
symbology: model.text().default("qr"),
|
|
58
|
+
scan_action: model
|
|
59
|
+
.enum(["view", "add_to_cart", "verify", "gs1_digital_link", "custom"])
|
|
60
|
+
.default("view"),
|
|
61
|
+
gs1_gtin: model.text().nullable(),
|
|
62
|
+
batch_lot: model.text().nullable(),
|
|
63
|
+
serial_number: model.text().nullable(),
|
|
64
|
+
hmac_signature: model.text().nullable(),
|
|
65
|
+
target_url: model.text().nullable(),
|
|
66
|
+
svg_markup: model.text().nullable(),
|
|
67
|
+
svg_url: model.text().nullable(),
|
|
68
|
+
scan_count: model.number().default(0),
|
|
69
|
+
last_scanned_at: model.dateTime().nullable(),
|
|
70
|
+
is_active: model.boolean().default(true),
|
|
71
|
+
metadata: model.json().nullable(),
|
|
72
|
+
})
|
|
73
|
+
.indexes([
|
|
74
|
+
{ on: ["code"], unique: true },
|
|
75
|
+
{ on: ["target_type", "target_id"] },
|
|
76
|
+
{ on: ["product_id"] },
|
|
77
|
+
{ on: ["variant_id"] },
|
|
78
|
+
])
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* `BarcodeLabelTemplate` stores customizable thermal roll and A4 sheet label specifications.
|
|
82
|
+
*/
|
|
83
|
+
export const BarcodeLabelTemplate = model
|
|
84
|
+
.define("barcode_label_template", {
|
|
85
|
+
id: model.id().primaryKey(),
|
|
86
|
+
name: model.text().searchable(),
|
|
87
|
+
preset_id: model.text().default("thermal_50x25"),
|
|
88
|
+
layout_type: model
|
|
89
|
+
.enum(["thermal_roll", "a4_sheet"])
|
|
90
|
+
.default("thermal_roll"),
|
|
91
|
+
width_mm: model.number().default(50),
|
|
92
|
+
height_mm: model.number().default(25),
|
|
93
|
+
columns: model.number().default(1),
|
|
94
|
+
rows: model.number().default(1),
|
|
95
|
+
margin_top_mm: model.number().default(2),
|
|
96
|
+
margin_left_mm: model.number().default(2),
|
|
97
|
+
gap_x_mm: model.number().default(0),
|
|
98
|
+
gap_y_mm: model.number().default(2),
|
|
99
|
+
symbology: model.text().default("ean13"),
|
|
100
|
+
show_title: model.boolean().default(true),
|
|
101
|
+
show_variant_title: model.boolean().default(true),
|
|
102
|
+
show_sku: model.boolean().default(true),
|
|
103
|
+
show_price: model.boolean().default(true),
|
|
104
|
+
show_compare_at_price: model.boolean().default(true),
|
|
105
|
+
show_brand: model.boolean().default(false),
|
|
106
|
+
show_qr: model.boolean().default(false),
|
|
107
|
+
custom_header: model.text().nullable(),
|
|
108
|
+
custom_footer: model.text().nullable(),
|
|
109
|
+
dpi: model.number().default(203),
|
|
110
|
+
is_default: model.boolean().default(false),
|
|
111
|
+
metadata: model.json().nullable(),
|
|
112
|
+
})
|
|
113
|
+
.indexes([{ on: ["preset_id"] }, { on: ["is_default"] }])
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* `BarcodeCountSession` tracks warehouse physical inventory count & variance sessions.
|
|
117
|
+
*/
|
|
118
|
+
export const BarcodeCountSession = model
|
|
119
|
+
.define("barcode_count_session", {
|
|
120
|
+
id: model.id().primaryKey(),
|
|
121
|
+
name: model.text().searchable(),
|
|
122
|
+
location_id: model.text().nullable(),
|
|
123
|
+
status: model
|
|
124
|
+
.enum(["draft", "in_progress", "completed", "cancelled"])
|
|
125
|
+
.default("in_progress"),
|
|
126
|
+
notes: model.text().nullable(),
|
|
127
|
+
total_expected: model.number().default(0),
|
|
128
|
+
total_counted: model.number().default(0),
|
|
129
|
+
total_variance: model.number().default(0),
|
|
130
|
+
total_scans: model.number().default(0),
|
|
131
|
+
created_by: model.text().nullable(),
|
|
132
|
+
completed_at: model.dateTime().nullable(),
|
|
133
|
+
metadata: model.json().nullable(),
|
|
134
|
+
})
|
|
135
|
+
.indexes([{ on: ["status"] }, { on: ["location_id"] }])
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* `BarcodeCountItem` stores per-SKU/barcode expected vs. counted quantities in a count session.
|
|
139
|
+
*/
|
|
140
|
+
export const BarcodeCountItem = model
|
|
141
|
+
.define("barcode_count_item", {
|
|
142
|
+
id: model.id().primaryKey(),
|
|
143
|
+
session_id: model.text(),
|
|
144
|
+
barcode: model.text().searchable(),
|
|
145
|
+
variant_id: model.text().nullable(),
|
|
146
|
+
product_id: model.text().nullable(),
|
|
147
|
+
title: model.text(),
|
|
148
|
+
variant_title: model.text().nullable(),
|
|
149
|
+
sku: model.text().nullable(),
|
|
150
|
+
expected_quantity: model.number().default(0),
|
|
151
|
+
counted_quantity: model.number().default(0),
|
|
152
|
+
variance: model.number().default(0),
|
|
153
|
+
last_scanned_at: model.dateTime().nullable(),
|
|
154
|
+
metadata: model.json().nullable(),
|
|
155
|
+
})
|
|
156
|
+
.indexes([
|
|
157
|
+
{ on: ["session_id"] },
|
|
158
|
+
{ on: ["session_id", "barcode"], unique: true },
|
|
159
|
+
])
|
|
160
|
+
|
|
161
|
+
export const BarcodeModels = {
|
|
162
|
+
BarcodeProduct,
|
|
163
|
+
BarcodeCache,
|
|
164
|
+
BarcodeTag,
|
|
165
|
+
BarcodeLabelTemplate,
|
|
166
|
+
BarcodeCountSession,
|
|
167
|
+
BarcodeCountItem,
|
|
168
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ProviderRegistry } from "../providers"
|
|
2
|
+
import type { BarcodeModuleOptions } from "../types"
|
|
3
|
+
import type { BarcodeDataService } from "./data-service"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Dependencies shared by every barcode operation: the generated CRUD service
|
|
7
|
+
* plus the resolved module options, provider registry and HMAC secret.
|
|
8
|
+
*/
|
|
9
|
+
export type BarcodeOpsContext = {
|
|
10
|
+
svc: BarcodeDataService
|
|
11
|
+
options: BarcodeModuleOptions
|
|
12
|
+
registry: ProviderRegistry
|
|
13
|
+
hmacSecret: string
|
|
14
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { MedusaService } from "@medusajs/framework/utils"
|
|
2
|
+
|
|
3
|
+
import { BarcodeModels } from "../models"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Base service exposing the CRUD methods generated by `MedusaService`
|
|
7
|
+
* (`listBarcodeTags`, `createBarcodeCaches`, ...). Operations depend on this
|
|
8
|
+
* class rather than on `BarcodeModuleService` to avoid circular types.
|
|
9
|
+
*/
|
|
10
|
+
export class BarcodeDataService extends MedusaService(BarcodeModels) {}
|