Skip to content

Instantly share code, notes, and snippets.

@toioski
Last active March 5, 2020 11:02
Show Gist options
  • Save toioski/d6837ce1db239df754e4e66e5f05066f to your computer and use it in GitHub Desktop.
Save toioski/d6837ce1db239df754e4e66e5f05066f to your computer and use it in GitHub Desktop.
Monetate "Discount" JSON response
// Excel reference file: https://docs.google.com/spreadsheets/d/1QqrhoaTxDEbzShiJHBdLbL_zs2dhvN355AQJ2fRbIx4
// ----------------
// Types definition
// ----------------
enum Language {
ar = "ar",
de = "de",
en = "en",
es = "es",
fr = "fr",
it = "it",
zh = "zh",
ko = "ko"
}
// TIP: the question mark means that is optional
interface IMonetateDiscountAction {
rules: [
{
type: MonetateDiscountRuleType
discount: { [K in Language]?: string }
skuList?: string[]
}
]
priority?: number // If there are multiple Discount experiences active, the higher priority will win
}
enum MonetateDiscountRuleType {
ByProduct = "byProduct",
BySale = "bySale"
}
// ----------------------
// Explanation & Examples
// ----------------------
//
// The extraDiscount mechanism works with rules. Each rule has a type which define the logic to apply
// the extraDiscount on the products. Currently we support two types of rules (see MonetateDiscountRuleType).
//
// TYPE 1 - byProduct
//
// We give a list of SKUs in the JSON response and the extraDiscount will be applied only to those SKUs. We can
// pass multiple rules of this type in the JSON response if we want to have a different amount of discount
// for different products. For instance, in the example below, P00265361 will have 20% discount, while P00395361
// will have a 30% discount.
// If we have the same SKU in two different rules because of a mistake, the system will be intelligent enough
// to apply only the first discount has been found.
const response1 = {
extraDiscount: {
rules: [
{
type: "byProduct",
label: {
en: "Extra 20% discount",
de: "Extra 20% rabatt",
it: "Extra 20% di sconto"
},
skuList: ["P00265361", "P00058270", "P00241274"]
},
{
type: "byProduct",
label: {
en: "Extra 30% discount",
de: "Extra 30% rabatt",
it: "Extra 30% di sconto"
},
skuList: ["P00395361", "P00941371"]
}
]
}
}
// TYPE 2 - bySale
//
// We just give the type and the label (i.e. the discount amount) and the extra discount will be automatically
// applied to all the products already in sale
const response2 = {
extraDiscount: {
rules: [
{
type: "bySale",
label: {
en: "Extra 40% discount",
de: "Extra 40% rabatt",
it: "Extra 40% di sconto"
}
}
]
}
}
// EDGE CASES
//
// 1. The system support only one type of rule at the same type. However, the system is smart and if it finds
// multiple "type" of "rules" (like in this case, where we have two "byProduct" and one "bySale"), it will
// apply only the first "type" find in the array. Hence the rules array will be filtered and the last
// rule "bySale" will be deleted. Viceversa, if the rules array was reversed, the two "byProduct" would have
// been deleted and only "bySale" kept
// 2. If one language key is missing inside the "label" object, then that the extra discount for that language
// won't appear. E.g. looking at the example below, if the app is in spanish (i.e. "es") it won't show any
// discount because the key "es" is missing in the "label" object
const response3 = {
extraDiscount: {
rules: [
{
type: "byProduct",
label: {
en: "Extra 20% discount",
de: "Extra 20% rabatt",
it: "Extra 20% di sconto"
},
skuList: ["P00265361", "P00058270", "P00241274"]
},
{
type: "byProduct",
label: {
en: "Extra 30% discount",
de: "Extra 30% rabatt",
it: "Extra 30% di sconto"
},
skuList: ["P00265361"]
},
{
type: "bySale",
label: {
en: "Extra 40% discount",
it: "Extra 40% di sconto"
}
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment