@leaddreamer/inventorylogic 0.0.3-beta.86 → 0.0.3-beta.89

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.
@@ -9,9 +9,29 @@
9
9
  //import {SampleJSON, feedSample} from "./SquarespaceSample.js"
10
10
  import sampleJSON from './sampleJSON.json' with { type: 'json' };
11
11
  import feedSample from './feedSample.json' with { type: 'json' };
12
+ import { factoryAPI } from '../APIs/index.js';
12
13
  //////////////////////////////////////////////////////////////////////
13
14
  // Constants
14
15
 
16
+ export const SqsURL = "https://api.squarespace.com/";
17
+ export const SqsProducts = "v2/commerce/products";
18
+ export const listSqsProductsMethod = 'GET'
19
+
20
+ export let SqsAPI = undefined;
21
+
22
+ export function initSqs(authorizationCode) {
23
+ SqsAPI = factoryAPI(SqsURL, authorizationCode)
24
+
25
+ };
26
+
27
+ export async function listProductsSqs() {
28
+ if (SqsAPI) {
29
+ return await SqsAPI.get(SqsProducts).json();
30
+ } else {
31
+ return null;
32
+ }
33
+ }
34
+
15
35
  /**
16
36
  * A list of attributes for a product
17
37
  * @typedef {Array.String} AttributeList
@@ -113,11 +133,12 @@ import feedSample from './feedSample.json' with { type: 'json' };
113
133
 
114
134
  /**
115
135
  * variant record (including inventory and pricing)
116
- * @typedef {object} PhysicalProductVariant
136
+ * @typedef {object} sqsPhysicalProductVariant
117
137
  * @property {Array.AttributeValue} attributes array of attribute/value pairs for a variant
118
138
  * @property {string} gtin Global Trade Item Number
119
139
  * @property {string} id unique ID for product variant
120
140
  * @property {ProductImage} image Image associated with this variant
141
+ * @property {Pricing} pricing
121
142
  * @property {string} mpn manufacturer part number
122
143
  * @property {ProductMeasurements} shippingMeasurements
123
144
  * @property {string} sku
@@ -125,8 +146,14 @@ import feedSample from './feedSample.json' with { type: 'json' };
125
146
  */
126
147
 
127
148
  /**
128
- * a Square product record
129
- * @typedef {object} PhysicalProduct
149
+ * a Squarespace product record
150
+ * Map between GoogleProductAPI and internal Product Data
151
+ * Note that when gg >> internal, index is *discovered* by searching
152
+ * for a matching item_group_id <<>> id. if item_group_id is *not*
153
+ * found add a new top-level product.
154
+ * Then look within varinats for a matching SKU; if found, use that
155
+ * value for the index; if *not* found add the variant.
156
+ * @typedef {object} sqsPhysicalProduct
130
157
  * @property {dateTime} createdOn
131
158
  * @property {?string} id Id of document (redundant for convenience)
132
159
  * @property {dateTime} modifiedOn
@@ -144,81 +171,93 @@ import feedSample from './feedSample.json' with { type: 'json' };
144
171
  * @property {Array.PhysicalProductVariant} variants
145
172
  */
146
173
 
147
- //////////////////////////////////////////////////////////////////////
148
- // business operations
149
-
150
- //////////////////////////////////////////////////////////////////////
151
- // record operations
152
-
153
- //////////////////////////////////////////////////////////////////////
154
- // utilities
155
-
156
- const squarespaceVariantMap = {
157
- "id": {target: "platformIds.SquarespaceId"},
158
- "sku": {target: "sku"},
159
- "image": {target: "image"},
160
- "pricing": {target: "price"},
161
- "stock": {target: "stock"},
162
- "attributes": {target: "attributes"},
163
- "shippingMeasurements": {target: "shippingMeasurements"},
164
- "gtin": {target: "gtin"},
165
- "mpn": {target: "mpn"},
174
+ /**
175
+ * @type {sqsPhysicalProductVariant}
176
+ * @constant
177
+ * @default
178
+ */
179
+ const sqsVariantMap = {
180
+ //all assume productMap.variants[index] prefix
181
+ "id": { target: "platformData.squarespaceData.Id" },
182
+ "sku": { target: "sku" },
183
+ "image": { target: "image" },
184
+ "pricing": { target: "pricing" },
185
+ "stock": { target: "stock" },
186
+ "attributes": { target: "attributes" },
187
+ "shippingMeasurements": { target: "shippingMeasurements" },
188
+ "gtin": { target: "gtin" },
189
+ "mpn": { target: "mpn" },
166
190
  //other fields not used
167
191
  };
168
192
 
169
- const squarespaceProductMap = {
170
- "id": {target: "platformIds.SquarespaceId"},
171
- "type": {target:"product_type"},
172
- "name": {target: "name"},
173
- "description": {target: "description"},
174
- "storePageId": {target: "platformData.squarespaceData.storePageId"},
175
- "url": {target: "URL"},
176
- "urlSlug": {target: "urlSlug"},
177
- "images": {target: "mediaObject.images"},
178
- "tags": {target: "attributes.tags"},
179
- "seoOptions": {target: "seoOptions"},
180
- "createdOn": {target: "recipe.createdOn"},
181
- "modifiedOn": {target: "recipe.modifiedOn"},
193
+ /**
194
+ * @type {sqsPhysicalProduct}
195
+ * @constant
196
+ * @default
197
+ */
198
+ const sqsProductMap = {
199
+ "id": { target: "platformData.squarespaceData.Id" },
200
+ "type": { target: "product_type" },
201
+ "name": { target: "name" },
202
+ "description": { target: "description" },
203
+ "storePageId": { target: "platformData.squarespaceData.storePageId" },
204
+ "url": { target: "platformData.squarespaceData.URL" },
205
+ "urlSlug": { target: "platformData.squarespaceData.urlSlug" },
206
+ "images": { target: "mediaObject.images" },
207
+ "tags": { target: "attributes.tags" },
208
+ "seoOptions": { target: "seoOptions" },
209
+ "createdOn": { target: "recipe.createdOn" },
210
+ "modifiedOn": { target: "recipe.modifiedOn" },
182
211
  "variants": {
183
212
  target: "variants",
184
- formatter: SquarespaceVariantMap
213
+ formatter: sqsVariantMap
185
214
  },
186
- "variantAttributes": {target: "variantAttributes"},
187
- "isVisible": {target: "isVisible"},
215
+ "variantAttributes": { target: "variantAttributes" },
216
+ "isVisible": { target: "isVisible" },
188
217
  };
189
218
 
190
- function jsonToItemMapping(itemJSON, itemMap) {
191
- const incomingItem = JSON.parse(itemJSON);
192
- return itemToItemMapping(incomingItem, itemMap);
193
- }
219
+ //////////////////////////////////////////////////////////////////////
220
+ // business operations
221
+
222
+ //////////////////////////////////////////////////////////////////////
223
+ // record operations
194
224
 
195
- export function jsonProductFeedToItemMapping(feedJSON) {
225
+ //////////////////////////////////////////////////////////////////////
226
+ // utilities
227
+
228
+ /**
229
+ * Converts a JSON encoded Squarespace product feed to an array of internal products.
230
+ * @param {JSON} feedJSON
231
+ * @returns {Array.Products}
232
+ */
233
+ export function sqsProductFeedToProducts(feedJSON) {
196
234
  const incomingFeed = JSON.parse(feedJSON);
235
+ //returns an array
197
236
  return incomingFeed.products.map(product => {
198
- return itemToItemMapping(product, SquarespaceProductMap);
237
+ return sqsProductToProduct(product, sqsProductMap);
199
238
  });
200
239
  }
201
240
 
202
- function itemToItemMapping(incomingItem, itemMap) {
203
-
204
- let properties = Object.getOwnPropertyNames(incomingItem);
205
- let outputItem = properties.reduce((outbound, property) => {
206
- const target = itemMap[property]?.target
241
+ function sqsProductToProduct(sqProduct, itemMap) {
242
+
243
+ const properties = Object.getOwnPropertyNames(sqProduct);
244
+ const product = properties.reduce((outbound, property) => {
245
+ const target = itemMap[property]?.target;
207
246
  const formatter = itemMap[property]?.formatter;
208
- let saveValue = Array.isArray(incomingItem[property])
209
- ? incomingItem[property].map((entry) => {
247
+ const saveValue = Array.isArray(sqProduct[property])
248
+ ? sqProduct[property].map((entry) => {
210
249
  return formatter
211
- ? itemToItemMapping(entry, formatter)
212
- : entry;
250
+ ? sqsProductToProduct(entry, formatter)
251
+ : entry;
213
252
  })
214
- : incomingItem[property];
253
+ : sqProduct[property];
215
254
 
216
255
  const labels = target.split(".");
217
256
  return recursiveMapWalk(outbound, labels, saveValue);
218
257
 
219
- }, {}) ;
258
+ }, {});
220
259
 
221
- return outputItem;
260
+ return product;
222
261
  };
223
262
 
224
263
  /**
@@ -239,6 +278,40 @@ function recursiveMapWalk(thisObject, labels, value) {
239
278
  }
240
279
  };
241
280
 
242
- export function squarespaceFeedTest () {
243
- return jsonProductFeedToItemMapping(feedSample);
244
- }
281
+ /**
282
+ * Takes an array of internal Product Objects and converts it to
283
+ * a JSON feed
284
+ * @param {Array.Product} productsArray
285
+ */
286
+ export function productsToSqsProductFeed(productsArray) {
287
+ const converted = productsArray.map((product) => {
288
+ return productToSqsProduct(product, sqsProductMap);
289
+ });
290
+ return JSON.stringify({ products: converted });
291
+ };
292
+
293
+ export function productToSqsProduct(product, itemMap) {
294
+
295
+ let properties = Object.getOwnPropertyNames(itemMap);
296
+ let outputItem = properties.reduce((outbound, property) => {
297
+ const formatter = itemMap[property]?.formatter;
298
+ const labels = itemMap[property]?.target.split(".");
299
+ const selectValue = labels.reduce((thisLevel, label) => {
300
+ return thisLevel[label];
301
+ }, product);
302
+ const saveValue = Array.isArray(selectValue)
303
+ ? selectValue.map((selectEntry) => {
304
+ return formatter
305
+ ? productToSqsProduct(selectEntry, formatter)
306
+ : selectEntry;
307
+ })
308
+ : selectValue;
309
+ outbound[property] = saveValue;
310
+ return {
311
+ ...outbound,
312
+ [property]: saveValue
313
+ };
314
+ }, {});
315
+ return outputItem;
316
+ };
317
+
package/index.js CHANGED
@@ -49,11 +49,12 @@ import { setLogger } from "@leaddreamer/genericutilities";
49
49
  * export * from "@leaddreamer/firebase-wrapper";
50
50
  * ```
51
51
  */
52
- export default async function databasePromise(firebase, config, thisLogger = null) {
52
+ export async function databasePromise(firebase, config, thisLogger = null) {
53
53
  setLogger(thisLogger);
54
54
  return FirebaseWrapper(firebase, config, thisLogger);
55
55
  }
56
56
 
57
+ export default databasePromise;
57
58
  export * from "./business/index.js";
58
59
  export * from "@leaddreamer/firebase-wrapper";
59
60
  export * from "@leaddreamer/genericutilities";
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "engines": {
4
4
  "node": ">=22"
5
5
  },
6
- "version": "0.0.3-beta.86",
6
+ "version": "0.0.3-beta.89",
7
7
  "description": "Business & Database Logic for Inventory Services",
8
8
  "repository": {
9
9
  "type": "git",
@@ -49,6 +49,7 @@
49
49
  "@leaddreamer/firebase-wrapper": "^2.1.2-beta.0",
50
50
  "@leaddreamer/genericutilities": "^0.2.8",
51
51
  "@leaddreamer/staticforms": "^1.1.0",
52
+ "ky": "2.1.0",
52
53
  "lodash.clonedeep": "4.5.0"
53
54
  },
54
55
  "peerDependencies": {
package/whack.js ADDED
@@ -0,0 +1,18 @@
1
+ import {
2
+ initSqs,
3
+ listProductsSqs,
4
+ sqsProductFeedToProducts,
5
+ productsToSqsProductFeed
6
+ } from "./index.js";
7
+
8
+ const code = '8b962d77-9fd3-49f4-8a56-5d64ba14afa0';
9
+
10
+ await initSqs(code);
11
+
12
+ const productJSON = await listProductsSqs();
13
+
14
+ const items = sqsProductFeedToProducts(JSON.stringify(productJSON));
15
+
16
+ const reverse = productsToSqsProductFeed(items);
17
+
18
+ console.dir(reverse, { depth: null, length: null });