@tallyui/connector-medusa 1.0.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.
@@ -0,0 +1,196 @@
1
+ import type { RxJsonSchema } from 'rxdb';
2
+
3
+ /**
4
+ * MedusaJS v2 Product RxDB schema.
5
+ * Mirrors the Medusa Admin API product shape.
6
+ *
7
+ * Key differences from WooCommerce:
8
+ * - `title` instead of `name`
9
+ * - `handle` instead of `slug`
10
+ * - Pricing lives on variants, not the product itself
11
+ * - Variants have their own SKU, barcode, images
12
+ * - Categories use `name`, but products/collections use `title`
13
+ * - Tags use `value` field
14
+ * - Images have `url` (not `src`)
15
+ */
16
+ export const medusaProductSchema: RxJsonSchema<any> = {
17
+ title: 'Medusa Product',
18
+ version: 0,
19
+ type: 'object',
20
+ primaryKey: 'id',
21
+ properties: {
22
+ id: {
23
+ type: 'string',
24
+ maxLength: 200,
25
+ },
26
+ title: {
27
+ type: 'string',
28
+ },
29
+ handle: {
30
+ type: 'string',
31
+ },
32
+ subtitle: {
33
+ type: ['string', 'null'],
34
+ },
35
+ description: {
36
+ type: ['string', 'null'],
37
+ },
38
+ status: {
39
+ type: 'string',
40
+ enum: ['draft', 'proposed', 'published', 'rejected'],
41
+ default: 'draft',
42
+ },
43
+ thumbnail: {
44
+ type: ['string', 'null'],
45
+ },
46
+ is_giftcard: {
47
+ type: 'boolean',
48
+ default: false,
49
+ },
50
+ discountable: {
51
+ type: 'boolean',
52
+ default: true,
53
+ },
54
+ collection_id: {
55
+ type: ['string', 'null'],
56
+ },
57
+ type_id: {
58
+ type: ['string', 'null'],
59
+ },
60
+ external_id: {
61
+ type: ['string', 'null'],
62
+ },
63
+ weight: {
64
+ type: ['number', 'null'],
65
+ },
66
+ length: {
67
+ type: ['number', 'null'],
68
+ },
69
+ height: {
70
+ type: ['number', 'null'],
71
+ },
72
+ width: {
73
+ type: ['number', 'null'],
74
+ },
75
+ origin_country: {
76
+ type: ['string', 'null'],
77
+ },
78
+ hs_code: {
79
+ type: ['string', 'null'],
80
+ },
81
+ mid_code: {
82
+ type: ['string', 'null'],
83
+ },
84
+ material: {
85
+ type: ['string', 'null'],
86
+ },
87
+ metadata: {
88
+ type: ['object', 'null'],
89
+ },
90
+ categories: {
91
+ type: 'array',
92
+ items: {
93
+ type: 'object',
94
+ properties: {
95
+ id: { type: 'string' },
96
+ name: { type: 'string' },
97
+ handle: { type: 'string' },
98
+ },
99
+ },
100
+ },
101
+ tags: {
102
+ type: 'array',
103
+ items: {
104
+ type: 'object',
105
+ properties: {
106
+ id: { type: 'string' },
107
+ value: { type: 'string' },
108
+ },
109
+ },
110
+ },
111
+ images: {
112
+ type: 'array',
113
+ items: {
114
+ type: 'object',
115
+ properties: {
116
+ id: { type: 'string' },
117
+ url: { type: 'string' },
118
+ rank: { type: 'number' },
119
+ },
120
+ },
121
+ },
122
+ options: {
123
+ type: 'array',
124
+ items: {
125
+ type: 'object',
126
+ properties: {
127
+ id: { type: 'string' },
128
+ title: { type: 'string' },
129
+ values: {
130
+ type: 'array',
131
+ items: {
132
+ type: 'object',
133
+ properties: {
134
+ id: { type: 'string' },
135
+ value: { type: 'string' },
136
+ },
137
+ },
138
+ },
139
+ },
140
+ },
141
+ },
142
+ variants: {
143
+ type: 'array',
144
+ items: {
145
+ type: 'object',
146
+ properties: {
147
+ id: { type: 'string' },
148
+ title: { type: ['string', 'null'] },
149
+ sku: { type: ['string', 'null'] },
150
+ barcode: { type: ['string', 'null'] },
151
+ ean: { type: ['string', 'null'] },
152
+ upc: { type: ['string', 'null'] },
153
+ allow_backorder: { type: ['boolean', 'null'] },
154
+ manage_inventory: { type: ['boolean', 'null'] },
155
+ inventory_quantity: { type: ['number', 'null'] },
156
+ variant_rank: { type: ['number', 'null'] },
157
+ weight: { type: ['number', 'null'] },
158
+ length: { type: ['number', 'null'] },
159
+ height: { type: ['number', 'null'] },
160
+ width: { type: ['number', 'null'] },
161
+ options: {
162
+ type: 'array',
163
+ items: {
164
+ type: 'object',
165
+ properties: {
166
+ id: { type: 'string' },
167
+ value: { type: 'string' },
168
+ },
169
+ },
170
+ },
171
+ prices: {
172
+ type: 'array',
173
+ items: {
174
+ type: 'object',
175
+ properties: {
176
+ id: { type: 'string' },
177
+ currency_code: { type: 'string' },
178
+ amount: { type: 'number' },
179
+ min_quantity: { type: ['number', 'null'] },
180
+ max_quantity: { type: ['number', 'null'] },
181
+ },
182
+ },
183
+ },
184
+ },
185
+ },
186
+ },
187
+ created_at: {
188
+ type: ['string', 'null'],
189
+ },
190
+ updated_at: {
191
+ type: ['string', 'null'],
192
+ },
193
+ },
194
+ indexes: ['handle', 'status'],
195
+ required: ['id'],
196
+ };
@@ -0,0 +1,94 @@
1
+ import type { CollectionSync, SyncContext, RemoteIdEntry } from '@tallyui/core';
2
+
3
+ /**
4
+ * Medusa v2 product sync implementation.
5
+ *
6
+ * Uses the Medusa Admin API to fetch products with variants and pricing.
7
+ * The Admin API gives us raw price data (vs Store API's calculated prices),
8
+ * which is better for a POS that needs to know the actual price entries.
9
+ */
10
+ export const medusaProductSync: CollectionSync = {
11
+ async fetchAllIds(context: SyncContext): Promise<RemoteIdEntry[]> {
12
+ const allIds: RemoteIdEntry[] = [];
13
+ let offset = 0;
14
+ const limit = 100;
15
+
16
+ while (true) {
17
+ const response = await fetch(
18
+ `${context.baseUrl}/admin/products?offset=${offset}&limit=${limit}&fields=id,updated_at`,
19
+ {
20
+ headers: {
21
+ ...context.headers,
22
+ 'Content-Type': 'application/json',
23
+ },
24
+ signal: context.signal,
25
+ }
26
+ );
27
+
28
+ if (!response.ok) {
29
+ throw new Error(`Medusa API error: ${response.status}`);
30
+ }
31
+
32
+ const data = await response.json();
33
+ for (const product of data.products) {
34
+ allIds.push({
35
+ id: product.id,
36
+ dateModified: product.updated_at,
37
+ });
38
+ }
39
+
40
+ if (data.products.length < limit) break;
41
+ offset += limit;
42
+ }
43
+
44
+ return allIds;
45
+ },
46
+
47
+ async fetchByIds(ids: string[], context: SyncContext): Promise<any[]> {
48
+ // Medusa doesn't have a bulk "include" param like WooCommerce,
49
+ // so we fetch individually (or use the list endpoint with id filter)
50
+ const products: any[] = [];
51
+
52
+ for (const id of ids) {
53
+ const response = await fetch(
54
+ `${context.baseUrl}/admin/products/${id}?fields=*variants,*variants.prices,*images,*categories,*tags,*options,*options.values`,
55
+ {
56
+ headers: {
57
+ ...context.headers,
58
+ 'Content-Type': 'application/json',
59
+ },
60
+ signal: context.signal,
61
+ }
62
+ );
63
+
64
+ if (!response.ok) {
65
+ throw new Error(`Medusa API error: ${response.status} for product ${id}`);
66
+ }
67
+
68
+ const data = await response.json();
69
+ products.push(data.product);
70
+ }
71
+
72
+ return products;
73
+ },
74
+
75
+ async fetchModifiedAfter(date: string, context: SyncContext): Promise<any[]> {
76
+ const response = await fetch(
77
+ `${context.baseUrl}/admin/products?updated_at[gte]=${date}&limit=100&fields=*variants,*variants.prices,*images,*categories,*tags,*options,*options.values`,
78
+ {
79
+ headers: {
80
+ ...context.headers,
81
+ 'Content-Type': 'application/json',
82
+ },
83
+ signal: context.signal,
84
+ }
85
+ );
86
+
87
+ if (!response.ok) {
88
+ throw new Error(`Medusa API error: ${response.status}`);
89
+ }
90
+
91
+ const data = await response.json();
92
+ return data.products;
93
+ },
94
+ };
@@ -0,0 +1,101 @@
1
+ import type { ProductTraits } from '@tallyui/core';
2
+
3
+ /**
4
+ * Medusa v2 product trait implementations.
5
+ *
6
+ * Key mapping differences from WooCommerce:
7
+ * - Product name is `title` (not `name`)
8
+ * - Price lives on `variants[0].prices[0].amount` (cents, integer)
9
+ * - Images use `url` (not `src`)
10
+ * - SKU/barcode live on variants, not the product
11
+ * - Categories use `name`, tags use `value`
12
+ * - Stock is per-variant via `inventory_quantity`
13
+ */
14
+ export const medusaProductTraits: ProductTraits = {
15
+ getId: (doc) => doc.id,
16
+
17
+ getName: (doc) => doc.title ?? '',
18
+
19
+ getSku: (doc) => {
20
+ // SKU lives on variants in Medusa
21
+ return doc.variants?.[0]?.sku || undefined;
22
+ },
23
+
24
+ getPrice: (doc) => {
25
+ // Medusa stores prices as integers in smallest currency unit (cents)
26
+ const amount = doc.variants?.[0]?.prices?.[0]?.amount;
27
+ if (amount == null) return undefined;
28
+ // Convert cents to decimal string (assumes 2 decimal places)
29
+ return (amount / 100).toFixed(2);
30
+ },
31
+
32
+ getRegularPrice: (doc) => {
33
+ // Medusa doesn't have a separate regular/sale price on the product level
34
+ // The pricing engine handles this via price lists
35
+ const amount = doc.variants?.[0]?.prices?.[0]?.amount;
36
+ if (amount == null) return undefined;
37
+ return (amount / 100).toFixed(2);
38
+ },
39
+
40
+ getSalePrice: () => {
41
+ // Sale prices in Medusa come from price lists, not a product field
42
+ return undefined;
43
+ },
44
+
45
+ isOnSale: () => {
46
+ // Would need to compare calculated_price vs original_price from Store API
47
+ return false;
48
+ },
49
+
50
+ getImageUrl: (doc) => {
51
+ // Thumbnail first, then first image
52
+ return doc.thumbnail || doc.images?.[0]?.url || undefined;
53
+ },
54
+
55
+ getImageUrls: (doc) => {
56
+ const urls: string[] = [];
57
+ if (doc.thumbnail) urls.push(doc.thumbnail);
58
+ for (const img of doc.images ?? []) {
59
+ if (img.url && img.url !== doc.thumbnail) urls.push(img.url);
60
+ }
61
+ return urls;
62
+ },
63
+
64
+ getDescription: (doc) => doc.description || undefined,
65
+
66
+ getStockStatus: (doc) => {
67
+ const variant = doc.variants?.[0];
68
+ if (!variant) return 'unknown';
69
+
70
+ if (variant.manage_inventory === false) return 'instock';
71
+ if (variant.allow_backorder) return 'onbackorder';
72
+
73
+ const qty = variant.inventory_quantity;
74
+ if (qty == null) return 'unknown';
75
+ return qty > 0 ? 'instock' : 'outofstock';
76
+ },
77
+
78
+ getStockQuantity: (doc) => {
79
+ return doc.variants?.[0]?.inventory_quantity ?? null;
80
+ },
81
+
82
+ hasVariants: (doc) => {
83
+ return (doc.variants?.length ?? 0) > 1;
84
+ },
85
+
86
+ getType: (doc) => {
87
+ // Medusa doesn't have product types like WooCommerce
88
+ // A gift card is the closest equivalent to a "type"
89
+ if (doc.is_giftcard) return 'giftcard';
90
+ if ((doc.variants?.length ?? 0) > 1) return 'variable';
91
+ return 'simple';
92
+ },
93
+
94
+ getBarcode: (doc) => {
95
+ const variant = doc.variants?.[0];
96
+ return variant?.barcode || variant?.ean || variant?.upc || undefined;
97
+ },
98
+
99
+ getCategoryNames: (doc) =>
100
+ (doc.categories ?? []).map((cat: any) => cat.name).filter(Boolean),
101
+ };