-
owners collection
-
sellers collection
-
countries collection
-
shipping_rates collection
-
Fields:
-
seller_id: Many-to-One Relationship Field -> Sellers (key = seller_id)
-
country_id: Many-to-One Relationship Field -> Countries (key = country_id)
-
shipping_rate: Input Field (for shipping rate)
-
-
Sellers are only assigned one shipping rate per country.
For example:
-
Assume there are currently two sellers in the sellers collection; Seller A and Seller B.
-
Assume two countries are currently available in countries collection; Egypt and United States.
-
Assume an owner can add shipping_rates to sellers.
-
Owner creates two shipping rates. One rate is Seller A, Egypt, $5 and another rate of Seller A, United States, $6 (two items now created in shipping_rates collection for Seller A.)
-
Once both Shipping Rates are created for the two Countries, owner should try to add a third shipping rate to Seller A, for either Egypt or United States, resulting in an error being thown and error message displayed in a modal.
-
Same scenario for owner creating shipping rates for Seller B and so on.
Basically, only one shipping rate, per country AND per seller is allowed.
Note: do not select a Response Body, leave it blank.
{
"filter": {
"seller_id": {
"_eq": "{{ $trigger.payload.seller_id }}"
},
"country_id": {
"_eq": "{{ $trigger.payload.country_id }}"
}
}
}
Note: we use this query to check if seller_id
and country_id
both already exists.
module.exports = async function (data) {
return { output: data.unique_indexes.length === 0 ? true : false };
};
Note: checking unique_indexes
array if it's empty return { output: true }
else return { output: false }
unique_indexes
comes from previous Read Data operation's key.
Update: the following steps can be removed if you do the following in the Run Script Operation^: https://gist.github.com/Natetronn/9bafd1722a2b67b9bf4528e45992baa2?permalink_comment_id=4762238#gistcomment-4762238
{
"script": {
"output": {
"_eq": true
}
}
}
Note: script
is the key from the previous operation's and output
is the object key from the returned value of its code.
If condition is true
create item (don't block).
If condition is false
run script to throw error; payload isn't passed and item isn't created; instead an error modal is displayed with the throw new error message.
module.exports = async function (data) {
throw new Error("This country already exists, add another one!");
return {};
};
This is in response to no composite key support currently in Directus.
We're using a Flow to avoid the creation of an extension; as cube-dan suggested in the same discussion here.
The following discussion assisted in checking for empty payload array.