Skip to content

Commit 494a8ce

Browse files
update commerce extensions (#283)
2 parents 54c5652 + 231cf93 commit 494a8ce

File tree

3 files changed

+118
-4
lines changed

3 files changed

+118
-4
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
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).

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

+22-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Imagine managing inventory across multiple locations effortlessly. Commerce Exte
99
In this guide you will learn how to:
1010

1111
* 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.
1213
* Ensure the accuracy of your inventory amounts using Conditional Updates to prevent lost updates and other data conistency issues.
1314

1415
## Prerequisites
@@ -34,12 +35,13 @@ curl -X POST "https://useast.api.elasticpath.com/v2/settings/extensions/custom-a
3435
"api_type": "location-inventories_ext",
3536
"name": "Location Inventories",
3637
"description": "Inventory entries for all SKUs at all retail locations.",
38+
"allow_upserts": true,
3739
"type": "custom_api"
3840
}
3941
}
4042
```
4143

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.
4345

4446
## Create Custom Fields
4547

@@ -167,7 +169,7 @@ Notice the following:
167169
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:
168170

169171
1. Filter for that record.
170-
2. Save the `data.id` attribute for that record.
172+
2. Save the `id` attribute for that record.
171173
3. Update that record using an auto-generated identifier.
172174

173175
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
186188

187189
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.
188190

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+
189209
## Conditional Updates
190210

191211
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.

Diff for: openapispecs/commerceextensions/OpenAPISpec.yaml

+88-2
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ tags:
6363
| Validation: Allow null values | ⛔ | ✅ |
6464
| Validation: Unique(String) | ⛔ | ✅ |
6565
| Validation: Unique Case Insensitivity(String) | ⛔ | ✅ |
66+
| Validation: Immutable | ⛔ | ✅ |
6667
6768
## Validation
6869
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.
7071
7172
:::note
7273
@@ -152,7 +153,27 @@ tags:
152153
}
153154
}
154155
}
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.
156177
157178
## Reserved Slugs
158179
@@ -1448,6 +1469,53 @@ paths:
14481469
}
14491470
}
14501471
}
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"
1477+
schema:
1478+
type: string
1479+
content:
1480+
application/json:
1481+
schema:
1482+
required:
1483+
- data
1484+
properties:
1485+
data:
1486+
type: object
1487+
additionalProperties: true
1488+
required:
1489+
- id
1490+
- type
1491+
- values
1492+
$ref: "#/components/schemas/CustomApiEntryAttributes"
1493+
examples:
1494+
valid_entry:
1495+
summary: "Default Wishlist"
1496+
# language=JSON
1497+
value: |
1498+
{
1499+
"data": {
1500+
"id": "7e067539-6f6c-46e1-8c55-940031b36c6a",
1501+
"type": "wishlist_ext",
1502+
"name": "My Wishlist",
1503+
"items_count": 0,
1504+
"keep_purchased": false,
1505+
"links": {
1506+
"self": "/extensions/wishlists/7e067539-6f6c-46e1-8c55-940031b36c6a"
1507+
},
1508+
"meta": {
1509+
"timestamps": {
1510+
"updated_at": "2017-01-10T11:41:19.244Z",
1511+
"created_at": "2017-01-10T11:41:19.244Z"
1512+
},
1513+
"resource_version": 0,
1514+
"data_size": 6,
1515+
"etag_id": "5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9"
1516+
}
1517+
}
1518+
}
14511519
'400':
14521520
$ref: '#/components/responses/ValidationError'
14531521
'404':
@@ -1795,6 +1863,10 @@ components:
17951863
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.
17961864
examples:
17971865
- "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
17981870
links:
17991871
readOnly: true
18001872
$ref: '#/components/schemas/SelfLinkCustomApi'
@@ -1849,6 +1921,20 @@ components:
18491921
- "integer"
18501922
- "boolean"
18511923
- "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.
1934+
example: 10
1935+
default: 0
1936+
minimum: 0
1937+
maximum: 1000
18521938
links:
18531939
readOnly: true
18541940
$ref: '#/components/schemas/SelfLink'

0 commit comments

Comments
 (0)