Skip to content

Commit 682db06

Browse files
Update commerce extensions guide (#277)
2 parents 7d1391a + 75e5711 commit 682db06

File tree

2 files changed

+60
-33
lines changed

2 files changed

+60
-33
lines changed

Diff for: changelog/2024/2024-09-16-changelog.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: 'Added case-insensitive string uniqueness in Commerce Extensions'
3+
date: '2024-09-16'
4+
tags: ['Commerce Extensions']
5+
hide_blog_post_date: false
6+
---
7+
8+
**MINOR** Previously, we had introduced the ability to model data with uniqueness constraints for string fields in Commerce Extensions. This feature is now further enhanced to be case-insensitive, that is, to take different cases into account for conflicts. For more information, see [Custom Fields](/docs/api/commerce-extensions/custom-fields#string-validation).

Diff for: guides/How-To/commerce-extensions/create-a-multilocation-inventories-resource.md

+52-33
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ nav_position: 3
66

77
Imagine managing inventory across multiple locations effortlessly. Commerce Extensions makes this possible with Custom APIs designed to handle specific data points like SKU, Collection Name, Location Name, and Inventory amount. This functionality allows businesses to quickly provide inventory information to customers, enhancing the shopping experience.
88

9+
In this guide you will learn how to:
10+
11+
* Create a Custom API to store your location inventory data.
12+
* Ensure the accuracy of your inventory amounts using Conditional Updates to prevent lost updates and other data conistency issues.
13+
914
## Prerequisites
1015

1116
If you want to follow along, you need a [Client credential token](/docs/authentication/Tokens/client-credential-token).
@@ -38,25 +43,48 @@ Make sure to take note of the Custom API ID [returned](/docs/api/commerce-extens
3843

3944
## Create Custom Fields
4045

41-
### Create Custom Field - slug
42-
In this step, you will create a Custom Field `slug`, this stores a string to represent this SKU.
46+
### Create Custom Field - sku
47+
48+
In this step, you will create a Custom Field `sku`, this stores a string to represent this SKU.
4349

4450
```sh
4551
curl -X POST "https://useast.api.elasticpath.com/v2/settings/extensions/custom-apis/:customApiId/fields" \
4652
-H "Authorization: XXXX" \
4753
-H "Content-Type: application/json" \
4854
-d $ {
4955
"data": {
50-
"slug": "slug",
56+
"slug": "sku",
5157
"name": "Slug",
5258
"description": "Slug of the entry",
5359
"field_type": "string",
60+
"validation": {
61+
"string": {
62+
"min_length": 12,
63+
"allow_null_values": false,
64+
"unique": "yes",
65+
"unique_case_insensitivity": true
66+
}
67+
},
68+
"use_as_url_slug": true
5469
"type": "custom_field"
5570
}
5671
}
5772
```
5873

74+
Some things to note about `validation` object in the sample request above:
75+
* `min_length`: Ensures that `slug` values have at least 12 characters.
76+
* `allow_null_values`: Will reject any `null` values from being stored as a `sku`.
77+
* `unique`: Blocks any duplicate values from being stored as a `sku`.
78+
* `unique_case_insensitivity`: Controls whether values with different cases (for example, `ABC` and `abc`) should conflict. Only applies when `unique` is set to `yes`.
79+
80+
Each of these can be tailored to fit your business see [Custom Fields Overview](/docs/api/commerce-extensions/custom-fields) for more.
81+
82+
Notice that the attribute `use_as_url_slug` is enabled, this will allow access to your data using well-known identifiers in your domain rather than having to rely on identifiers generated from our platform, we will build on this later. For a field to be configured for `use_as_url_slug`:
83+
1. `allow_null_values` must be set to `false`.
84+
2. `unique` must be set to `yes`.
85+
5986
### Create Custom Field - amount
87+
6088
In this step, you will create a Custom Field `amount`, this stores the amount of inventory of this SKU at this location as an integer.
6189

6290
```sh
@@ -78,9 +106,10 @@ curl -X POST "https://useast.api.elasticpath.com/v2/settings/extensions/custom-a
78106
}
79107
}
80108
```
81-
Take note of `validation` in the step above, this field is restricted to not allow negative values. For more information, see [integer validation](/docs/api/commerce-extensions/custom-fields#integer-validation).
109+
Take note of `min_value` in the step above, this field is restricted to not allow negative values. For more information, see [integer validation](/docs/api/commerce-extensions/custom-fields#integer-validation).
82110

83111
### Create Custom Field - location-name
112+
84113
In this step, you will create a Custom Field `location-name`, this stores a string to represent where a SKU is stored. Additionally, you will restrict this value to specific options using regex.
85114

86115
```sh
@@ -102,31 +131,14 @@ curl -X POST "https://useast.api.elasticpath.com/v2/settings/extensions/custom-a
102131
}
103132
}
104133
```
105-
Take note of `validation` in the step above, this field is restricted to only allow the following values:
134+
135+
Take note of `data.validation.string` in the step above, this field is restricted to only allow the following values:
106136
* Paris
107137
* London
108138
* New York
109139

110140
For more information, see [string validation](/docs/api/commerce-extensions/custom-fields#string-validation).
111141

112-
### Create Custom Field - collection-name
113-
In this step, you will create a Custom Field `collection-name`, this stores a string to represent the collection a SKU is associated with.
114-
115-
```sh
116-
curl -X POST "https://useast.api.elasticpath.com/v2/settings/extensions/custom-apis/:customApiId/fields" \
117-
-H "Authorization: XXXX" \
118-
-H "Content-Type: application/json" \
119-
-d $ {
120-
"data": {
121-
"slug": "collection-name",
122-
"name": "Collection Name",
123-
"description": "Name of the collection",
124-
"field_type": "string",
125-
"type": "custom_field"
126-
}
127-
}
128-
```
129-
130142
## Create Custom API Entries
131143

132144
With your Custom API configured, create Custom API Entries to store you location inventories.
@@ -138,36 +150,43 @@ curl -X POST "https://useast.api.elasticpath.com/v2/extensions/location-inventor
138150
-d ${
139151
"data": {
140152
"type": "location_inventory_ext",
141-
"slug": "LR-SFA-201",
142-
"amount": 4,
153+
"sku": "TBL-DIN-6WD-OAK-0034",
154+
"amount": 40,
143155
"location-name": "Paris"
144156
}
145157
}
146158
```
147159

148-
Notice that the URL you made this request is slightly different, you were able to create a Custom API Entry against the `slug` of the Custom API you created earlier.
160+
Notice the following:
161+
* the URL you made this request is slightly different, you were able to create a Custom API Entry against the `slug` of the Custom API you created earlier.
162+
* the `type` in the request body was the `api_type` of the Custom API you created earlier.
163+
* the keys (`slug`, `amount` and `location-name`) of the attributes used in the request body are the `slug` of the Custom Fields you created earlier.
149164

150-
In addition, the `type` in the request body was the `api_type` of the Custom API you created earlier.
165+
## Update Custom API Entries
151166

152-
Finally, take note that the keys (`slug`, `amount` and `location-name`) of the attributes used in the request body are the `slug` of the Custom Fields you created earlier.
167+
Recall that [earlier](/guides/How-To/commerce-extensions/create-a-multilocation-inventories-resource#create-custom-field---sku) when you created the Custom Field `sku` that you marked it for `use_as_url_slug`. This allows you to access a Custom API Entry by the value that is stored in that Custom Field for that record rather than having to:
153168

154-
## Update Custom API Entries
169+
1. Filter for that record.
170+
2. Save the `data.id` attribute for that record.
171+
3. Update that record using an auto-generated identifier.
155172

156-
With your Custom API Entry created, you can update it to modify inventory `amount`.
173+
Given a Custom API Entry exists with `"sku": "TBL-DIN-6WD-OAK-0034"`, when you want to update the inventory `amount` you make the following request:
157174

158175
```sh
159-
curl -X PUT "https://useast.api.elasticpath.com/v2/extensions/location-inventories/:customApiEntryId" \
176+
curl -X PUT "https://useast.api.elasticpath.com/v2/extensions/location-inventories/TBL-DIN-6WD-OAK-0034" \
160177
-H "Authorization: XXXX" \
161178
-H "Content-Type: application/json" \
162179
-d ${
163180
"data": {
164181
"type": "location_inventory_ext",
165-
"amount": 3
182+
"amount": 43
166183
}
167184
}
168185
```
169186

170-
### Conditional Updates
187+
Notice that the resource identifier in the request above is one that should be well-known to you and your processes and not a random UUID generated by the platform `/v2/extensions/location-inventories/:customApiEntryId`. [Earlier](/guides/How-To/commerce-extensions/create-a-multilocation-inventories-resource#create-custom-field---sku) you made `sku` unique and case-insensive, so requests using a value like `tbl-din-6Wd-oak-0034` and similar varations using different casing will update, retrieve and potentially delete the same resource.
188+
189+
## Conditional Updates
171190

172191
When using Custom API Entries, if multiple independent clients update the same resource, you should have them use the `If-Match` header to prevent lost updates and other data consistency issues in the inventory amounts. For example, if two users simultaneously see an amount of 3 and each allocate 1, both would update the amount to 2. The `If-Match` header ensures that only one of these requests succeeds. It works by comparing the provided ETag value with the current ETag value of the resource. If the resource hasn't changed since you last read it, the ETag will not change, ensuring the update is safe.
173192

0 commit comments

Comments
 (0)