You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
Copy file name to clipboardExpand all lines: guides/How-To/commerce-extensions/create-a-multilocation-inventories-resource.md
+52-33
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,11 @@ nav_position: 3
6
6
7
7
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.
8
8
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
+
9
14
## Prerequisites
10
15
11
16
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
38
43
39
44
## Create Custom Fields
40
45
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.
43
49
44
50
```sh
45
51
curl -X POST "https://useast.api.elasticpath.com/v2/settings/extensions/custom-apis/:customApiId/fields" \
46
52
-H "Authorization: XXXX" \
47
53
-H "Content-Type: application/json" \
48
54
-d $ {
49
55
"data": {
50
-
"slug": "slug",
56
+
"slug": "sku",
51
57
"name": "Slug",
52
58
"description": "Slug of the entry",
53
59
"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
54
69
"type": "custom_field"
55
70
}
56
71
}
57
72
```
58
73
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
+
59
86
### Create Custom Field - amount
87
+
60
88
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.
61
89
62
90
```sh
@@ -78,9 +106,10 @@ curl -X POST "https://useast.api.elasticpath.com/v2/settings/extensions/custom-a
78
106
}
79
107
}
80
108
```
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).
82
110
83
111
### Create Custom Field - location-name
112
+
84
113
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.
85
114
86
115
```sh
@@ -102,31 +131,14 @@ curl -X POST "https://useast.api.elasticpath.com/v2/settings/extensions/custom-a
102
131
}
103
132
}
104
133
```
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:
106
136
* Paris
107
137
* London
108
138
* New York
109
139
110
140
For more information, see [string validation](/docs/api/commerce-extensions/custom-fields#string-validation).
111
141
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
-
130
142
## Create Custom API Entries
131
143
132
144
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
138
150
-d ${
139
151
"data": {
140
152
"type":"location_inventory_ext",
141
-
"slug":"LR-SFA-201",
142
-
"amount":4,
153
+
"sku":"TBL-DIN-6WD-OAK-0034",
154
+
"amount":40,
143
155
"location-name":"Paris"
144
156
}
145
157
}
146
158
```
147
159
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.
149
164
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
151
166
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:
153
168
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.
155
172
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:
157
174
158
175
```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" \
160
177
-H "Authorization: XXXX" \
161
178
-H "Content-Type: application/json" \
162
179
-d ${
163
180
"data": {
164
181
"type":"location_inventory_ext",
165
-
"amount":3
182
+
"amount":43
166
183
}
167
184
}
168
185
```
169
186
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
171
190
172
191
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.
0 commit comments