Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4029447

Browse files
authored
Merge pull request #11 from Foxy/feat/default-collection-id
feat(webflow): adds configurable default webflow collection
2 parents 1fd6a4d + ee559a0 commit 4029447

File tree

4 files changed

+51
-20
lines changed

4 files changed

+51
-20
lines changed

config.js

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const config = {
2727
storeId: env("FOXY_ORDERDESK_STORE_ID"),
2828
},
2929
webflow: {
30+
collection: env('FOXY_WEBFLOW_COLLECTION'),
3031
token: env('FOXY_WEBFLOW_TOKEN') || env('WEBFLOW_TOKEN'),
3132
}
3233
},

src/functions/datastore-integration-webflow/README.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ When adding your items to the cart, beyond `price` and `quantity` that are neede
5050

5151
| Parameter | Description | Example |
5252
| ----------------- | -------------------------------------------------------------------------------------------------------------------- | --------------------------------------- |
53-
| `collection_id` | **Required** The id of the item's collection. | `collectionId=5f74f169fbbb4b118497207a` |
53+
| `collection_id` | **Optional** The id of the item's collection. If a default collection_id is provided, this field may be ignored. If both are provided, this field overrides the default collection_id. | `collectionId=5f74f169fbbb4b118497207a` |
5454
| `code` | **Required** The item's code. Must be unique. | `code=896EYSA678` |
5555

5656

@@ -72,11 +72,12 @@ You may have some items you don't want to be subject to price verification. This
7272

7373
| Variable | Default Value | Description |
7474
| ------------------------------- | ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
75-
| `FOXY_SKIP_PRICE_CODES` | "" | A comma separated list of code values (this is the value set in your 'code' field in Webflow or in the field you set with `code_field` parameter. **The items with these codes will skip price verification**. |
76-
| `FOXY_SKIP_INVENTORY_CODES` | "" | A comma separated list of code values (this is the value set in your 'code' field in Webflow or in the field you set with `code_field` parameter. **The items with these codes will skip inventory verification**. |
77-
| `FOXY_FIELD_CODE` | "code" | The name of the field that stores the code in the webflow collection. |
78-
| `FOXY_FIELD_PRICE` | "price" | The name of the field that stores the price in the webflow collection. |
79-
| `FOXY_FIELD_INVENTORY` | "inventory" | The name of the field that stores the inventory in the webflow collection. Set this variable to "false" (without the quotes) to disable inventory verification for all items. |
75+
| `FOXY_SKIP_PRICE_CODES` | "" | A comma separated list of code values (this is the value set in your 'code' field in Webflow or in the field you set with `code_field` parameter. **The items with these codes will skip price verification**. |
76+
| `FOXY_SKIP_INVENTORY_CODES` | "" | A comma separated list of code values (this is the value set in your 'code' field in Webflow or in the field you set with `code_field` parameter. **The items with these codes will skip inventory verification**. |
77+
| `FOXY_FIELD_CODE` | "code" | The name of the field that stores the code in the webflow collection. |
78+
| `FOXY_FIELD_PRICE` | "price" | The name of the field that stores the price in the webflow collection. |
79+
| `FOXY_FIELD_INVENTORY` | "inventory" | The name of the field that stores the inventory in the webflow collection. Set this variable to "false" (without the quotes) to disable inventory verification for all items. |
80+
| `FOXY_WEBFLOW_COLLECTION` | "" | The id of the collection that contains the products. If this is set, there is no need to set `collection_id` in the HTML. |
8081

8182
##### Error messages
8283

src/functions/datastore-integration-webflow/index.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function customOptions() {
1717
fields: {
1818
code: config.datastore.field.code || 'code',
1919
inventory: config.datastore.field.inventory || 'inventory',
20-
price: config.datastore.field.price || 'price'
20+
price: config.datastore.field.price || 'price',
2121
},
2222
skip: {
2323
inventory: (config.datastore.skipValidation.inventory || '').split(',').map(e => e.trim()).filter(e => !!e) || [],
@@ -140,8 +140,8 @@ function getCustomKey(default_key) {
140140
/**
141141
* Retrieve an option from an item using it's custom key, if set, or the default key
142142
*
143-
* @param item the item to retrieve the custom option from
144-
* @param option the option to retrieve
143+
* @param {Object} item the item to retrieve the custom option from
144+
* @param {string} option the option to retrieve
145145
* @returns {{}|{name: string, value: string|number}} the retrieved option
146146
*/
147147
function getCustomizableOption(item, option) {
@@ -199,7 +199,7 @@ function extractItems(body) {
199199
/**
200200
* Checks if item is valid
201201
*
202-
* @param item to be validated
202+
* @param {Object} item to be validated
203203
* @returns {boolean} valid
204204
*/
205205
function validItem(item) {
@@ -213,8 +213,7 @@ function validItem(item) {
213213
if (!(item.code || parseInt(item.code, 10) === 0)) {
214214
errors.push(`${item.name} has no code.`)
215215
}
216-
const collection = getOption(item, 'collection_id').value;
217-
if (!collection) {
216+
if (!getCollectionId(item)) {
218217
errors.push(`${item.name} has no collection_id.`)
219218
}
220219
if (errors.length) {
@@ -341,6 +340,18 @@ function getWebflow() {
341340
return webflowApi;
342341
}
343342

343+
/**
344+
* Retrieve the collection id to be used for this item.
345+
*
346+
* When not set for the specific item, the collection id set for the whole environment is used.
347+
*
348+
* @param {Object} item the item received from Foxy
349+
* @returns {string|number} the collection item to be used to fetch data on this item.
350+
*/
351+
function getCollectionId(item) {
352+
return getOption(item, 'collection_id').value || config.datastore.provider.webflow.collection;
353+
}
354+
344355
/**
345356
* Stores a reference to the matched item in the item itself.
346357
* returns a pair of matched items that can be easily validated.
@@ -376,7 +387,7 @@ function fetchItem(cache, foxyItem, offset = 0) {
376387
if (offset) {
377388
console.log(" ... couldn't find the item in first", offset, "items.");
378389
}
379-
const collectionId = getCustomizableOption(foxyItem, 'collection_id').value;
390+
const collectionId = getCollectionId(foxyItem);
380391
const webflow = getWebflow();
381392
const found = cache.findItem(collectionId, foxyItem);
382393
if (found) {

test/functions/datastore-integration-webflow/index.test.js

+25-7
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ describe("Verifies the price of an item in a Webflow collection", () => {
111111
});
112112

113113
/**
114-
* Creates a request for a subscrition with a given start date
114+
* Creates a request for a subscription with a given start date
115115
*
116-
* @param startDate the start date of the subscription
117-
* @returns request event
116+
* @param {Date} startDate the start date of the subscription
117+
* @returns {Object} request event
118118
*/
119119
function subscriptionsWithStartDate(startDate) {
120120
const nextMonth = ((d) => new Date(d.setDate(d.getMonth() + 1)))(
@@ -247,7 +247,7 @@ describe("Verifies the price of an item in a Webflow collection", () => {
247247
expect(JSON.parse(response.body)).to.deep.equal({ ok: true, details: "" });
248248
});
249249

250-
it("Rejects invalid items.", async () => {
250+
it("rejects invalid items.", async () => {
251251
const invalidItemOptions = {
252252
'code': /has no code\.$/,
253253
'collection_id': /has no collection_id\.$/,
@@ -277,20 +277,37 @@ describe("Verifies the price of an item in a Webflow collection", () => {
277277
}
278278
});
279279

280+
it("uses collection_id set in config", async () => {
281+
let response;
282+
config.datastore.provider.webflow.collection = 'COLLECTIONID';
283+
const event = mockFoxyCart.foxyRequest({ options: { collection_id: ''}});
284+
const items = JSON.parse(event.body)._embedded["fx:items"];
285+
injectedWebflow.items = function () {
286+
return Promise.resolve(
287+
mockWebflow.arbitrary(items)({}, {}, [])
288+
);
289+
};
290+
response = await prePayment.handler(event);
291+
expect(response.statusCode).to.exist.and.to.equal(200);
292+
expect(JSON.parse(response.body)).to.deep.equal({ ok: true, details: "" });
293+
config.datastore.provider.webflow.collection = '';
294+
response = await prePayment.handler(event);
295+
expect(response.statusCode).to.exist.and.to.equal(200);
296+
expect(JSON.parse(response.body).details).to.match(/Invalid items/);
297+
});
298+
280299
it("Approves when all items are correct", async () => {
281300
let response;
282301
const event = mockFoxyCart.foxyRequest({ price: 11, quantity: 1 });
283302
const items = JSON.parse(event.body)._embedded["fx:items"];
284-
// Make sure the response values matches
285303
injectedWebflow.items = function () {
286304
return Promise.resolve(
287305
mockWebflow.arbitrary(items)({}, {}, [
288306
"category",
289307
])
290308
);
291309
};
292-
const context = {};
293-
response = await prePayment.handler(event, context);
310+
response = await prePayment.handler(event);
294311
expect(response.statusCode).to.exist.and.to.equal(200);
295312
expect(JSON.parse(response.body)).to.deep.equal({ ok: true, details: "" });
296313
});
@@ -475,6 +492,7 @@ describe("Verifies the price of an item in a Webflow collection", () => {
475492
/**
476493
* Returns a response for a request with insufficient inventory.
477494
*
495+
* @param {object} event
478496
* @returns {object} the insufficient inventory response
479497
*/
480498
async function insufficientInventoryRequest(event = null) {

0 commit comments

Comments
 (0)