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 configuration of immutable Custom Fields in Commerce Extensions'
3
+
date: '2024-09-18'
4
+
tags: ['Commerce Extensions']
5
+
hide_blog_post_date: false
6
+
---
7
+
8
+
**MINOR** We have introduced the ability to configure a Custom Field as `immutable`, allowing you to better align your Custom API with your business needs. When set to true, the value of this field can be specified only during POST requests and cannot be modified during PUT requests. For more information, see [Custom Fields](/docs/api/commerce-extensions/custom-fields#immutable).
* Create a Custom API to store your location inventory data.
12
+
* Streamline your data import and sync processes using upserts and accessing entries with well-known identifiers.
12
13
* Ensure the accuracy of your inventory amounts using Conditional Updates to prevent lost updates and other data conistency issues.
13
14
14
15
## Prerequisites
@@ -34,12 +35,13 @@ curl -X POST "https://useast.api.elasticpath.com/v2/settings/extensions/custom-a
34
35
"api_type":"location-inventories_ext",
35
36
"name":"Location Inventories",
36
37
"description":"Inventory entries for all SKUs at all retail locations.",
38
+
"allow_upserts": true,
37
39
"type":"custom_api"
38
40
}
39
41
}
40
42
```
41
43
42
-
Make sure to take note of the Custom API ID [returned](/docs/api/commerce-extensions/create-a-custom-api#responses), you must replace `:customApiId` in the following step with the Custom API ID.
44
+
Make sure to take note of the Custom API ID [returned](/docs/api/commerce-extensions/create-a-custom-api#responses), you must replace `:customApiId` in the following step with the Custom API ID. Additionally, ensure that `data.allow_upserts` is set to `true`, this allows to perform [upserts](/guides/How-To/commerce-extensions/create-a-multilocation-inventories-resource#update-custom-api-entries) for Custom API Entries.
43
45
44
46
## Create Custom Fields
45
47
@@ -167,7 +169,7 @@ Notice the following:
167
169
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:
168
170
169
171
1. Filter for that record.
170
-
2. Save the `data.id` attribute for that record.
172
+
2. Save the `id` attribute for that record.
171
173
3. Update that record using an auto-generated identifier.
172
174
173
175
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:
@@ -186,6 +188,24 @@ curl -X PUT "https://useast.api.elasticpath.com/v2/extensions/location-inventori
186
188
187
189
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
190
191
+
## Upsert Custom API Entries
192
+
193
+
To further simplify and optimize your data import and synchronization processes, you can upsert Custom API Entries. Instead of first checking whether a record exists before deciding to create or update a Custom API Entries, you can upsert a record. If the record exists, it is updated; if it doesn't, it is inserted or created. This is enabled by setting `allow_upserts` when you [created this Custom API](/guides/How-To/commerce-extensions/create-a-multilocation-inventories-resource#create-a-new-custom-api---location-inventories).
194
+
195
+
```sh
196
+
curl -X PUT "https://useast.api.elasticpath.com/v2/extensions/location-inventories/FURN-SOFA-3S-LTH-BLK-0021" \
197
+
-H "Authorization: XXXX" \
198
+
-H "Content-Type: application/json" \
199
+
-d ${
200
+
"data": {
201
+
"sku":"FURN-SOFA-3S-LTH-BLK-0021",
202
+
"type":"location_inventory_ext",
203
+
"amount": 12,
204
+
"location-name":"Paris"
205
+
}
206
+
}
207
+
```
208
+
189
209
## Conditional Updates
190
210
191
211
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.
Copy file name to clipboardExpand all lines: openapispecs/commerceextensions/OpenAPISpec.yaml
+88-2
Original file line number
Diff line number
Diff line change
@@ -63,10 +63,11 @@ tags:
63
63
| Validation: Allow null values | ⛔ | ✅ |
64
64
| Validation: Unique(String) | ⛔ | ✅ |
65
65
| Validation: Unique Case Insensitivity(String) | ⛔ | ✅ |
66
+
| Validation: Immutable | ⛔ | ✅ |
66
67
67
68
## Validation
68
69
69
-
When [creating](/docs/api/commerce-extensions/create-a-custom-field#request) or [updating](/docs/api/commerce-extensions/create-a-custom-field#request) a Custom Field, `validation` can be used to limit the values that may be stored in the corresponding Custom API Entry.
70
+
When [creating](/docs/api/commerce-extensions/create-a-custom-field#request) or [updating](/docs/api/commerce-extensions/update-a-custom-field#request) a Custom Field, `validation` can be used to limit the values that may be stored in the corresponding Custom API Entry.
70
71
71
72
:::note
72
73
@@ -152,7 +153,27 @@ tags:
152
153
}
153
154
}
154
155
}
155
-
```
156
+
```
157
+
158
+
### Immutable
159
+
160
+
When [creating](/docs/api/commerce-extensions/create-a-custom-field#request) a Custom Field, it can be configured to be `immutable`. When set to true, the value of this field can be specified only during POST requests and cannot be modified during PUT requests. By default, this is `false`.
161
+
162
+
sample validation object :
163
+
164
+
```json
165
+
{
166
+
"validation": {
167
+
"boolean": {
168
+
"immutable": false
169
+
}
170
+
}
171
+
}
172
+
```
173
+
174
+
## Presentation
175
+
176
+
When [creating](/docs/api/commerce-extensions/create-a-custom-field#request) or [updating](/docs/api/commerce-extensions/update-a-custom-field#request) a Custom Field, `presentation` can be used to influence the layout of fields within a Custom API in User Interface. It does not order or affect the JSON response, and has no semantics on the backend.
156
177
157
178
## Reserved Slugs
158
179
@@ -1448,6 +1469,53 @@ paths:
1448
1469
}
1449
1470
}
1450
1471
}
1472
+
'201':
1473
+
description: Created
1474
+
headers:
1475
+
ETag:
1476
+
description: "A unique identifier representing the current version of the resource. When the resource changes, the ETag value will also change. The ETag hash will be the same value as `etag_id`, and is marked as a weak entity tag string. For example: etag: W/\"5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9\", etag_id: 5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9"
description: Specifies a unique API type for this Custom API. Entries for this API will use this value for their `type` field. This field must be suffixed with `_ext` to distinguish it from built in APIs.
1796
1864
examples:
1797
1865
- "wishlist_ext"
1866
+
allow_upserts:
1867
+
type: boolean
1868
+
description: Controls whether upsert operations are allowed for Custom API Entries via the `PUT` method. When set to `true`, it allows the creation of new Custom API Entries using `PUT` if the record doesn't exist, and updates the existing record if it does. When `false`, `PUT` requests can only update existing entries.
1869
+
default: false
1798
1870
links:
1799
1871
readOnly: true
1800
1872
$ref: '#/components/schemas/SelfLinkCustomApi'
@@ -1849,6 +1921,20 @@ components:
1849
1921
- "integer"
1850
1922
- "boolean"
1851
1923
- "float"
1924
+
use_as_url_slug:
1925
+
type: boolean
1926
+
description: |
1927
+
Enabling this field will mean Custom API Entries created in this Custom API will use this value in the URL instead of the `id` attribute. In order to set this field, the field must be a string, and unique, not allow null values, no entries have been created yet, and this field cannot be set to true on another custom field. This field cannot be updated. In addition to any validation rules you create, the values must be [Unreserved URL Characters](https://datatracker.ietf.org/doc/html/rfc3986#section-2.3) (i.e., be alpha-numeric, or one of `-`, `.`, `_` or `~`).
1928
+
presentation:
1929
+
type: object
1930
+
properties:
1931
+
sort_order:
1932
+
type: integer
1933
+
description: Specifies the order of the field in the User Interface.
0 commit comments