feat(medusa): Adds product variant sale price (#148)
* feat(medusa): Adds product variant sale price * fix(medusa-plugin-content): Always update prices in contentful
This commit is contained in:
1 parent
5ebaf77c7f
commit
451451a38e
8 files changed
+51
-18
No files matched your search
@@ -315,6 +315,11 @@ class ContentfulService extends BaseService {
|
|||||||
return this.createProductVariantInContentful(variant)
|
return this.createProductVariantInContentful(variant)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const cleanPrices = variants.prices.map((price) => ({
|
||||||
|
...price,
|
||||||
|
sale_amount: price.sale_amount || undefined,
|
||||||
|
}))
|
||||||
|
|
||||||
const variantEntryFields = {
|
const variantEntryFields = {
|
||||||
...variantEntry.fields,
|
...variantEntry.fields,
|
||||||
title: {
|
title: {
|
||||||
@@ -327,7 +332,7 @@ class ContentfulService extends BaseService {
|
|||||||
"en-US": variant.options,
|
"en-US": variant.options,
|
||||||
},
|
},
|
||||||
prices: {
|
prices: {
|
||||||
"en-US": variant.prices,
|
"en-US": cleanPrices,
|
||||||
},
|
},
|
||||||
objectId: {
|
objectId: {
|
||||||
"en-US": variant._id,
|
"en-US": variant._id,
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ export default async (req, res) => {
|
|||||||
.items({
|
.items({
|
||||||
currency_code: Validator.string().required(),
|
currency_code: Validator.string().required(),
|
||||||
amount: Validator.number().required(),
|
amount: Validator.number().required(),
|
||||||
|
sale_amount: Validator.number().optional(),
|
||||||
})
|
})
|
||||||
.required(),
|
.required(),
|
||||||
options: Validator.array()
|
options: Validator.array()
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ export default async (req, res) => {
|
|||||||
.items({
|
.items({
|
||||||
currency_code: Validator.string().required(),
|
currency_code: Validator.string().required(),
|
||||||
amount: Validator.number().required(),
|
amount: Validator.number().required(),
|
||||||
|
sale_amount: Validator.number().optional(),
|
||||||
})
|
})
|
||||||
.required(),
|
.required(),
|
||||||
options: Validator.array()
|
options: Validator.array()
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ export default async (req, res) => {
|
|||||||
region_id: Validator.string(),
|
region_id: Validator.string(),
|
||||||
currency_code: Validator.string(),
|
currency_code: Validator.string(),
|
||||||
amount: Validator.number().required(),
|
amount: Validator.number().required(),
|
||||||
|
sale_amount: Validator.number().optional(),
|
||||||
})
|
})
|
||||||
.xor("region_id", "currency_code")
|
.xor("region_id", "currency_code")
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ export default async (req, res) => {
|
|||||||
region_id: Validator.string(),
|
region_id: Validator.string(),
|
||||||
currency_code: Validator.string(),
|
currency_code: Validator.string(),
|
||||||
amount: Validator.number().required(),
|
amount: Validator.number().required(),
|
||||||
|
sale_amount: Validator.number().optional(),
|
||||||
})
|
})
|
||||||
.xor("region_id", "currency_code")
|
.xor("region_id", "currency_code")
|
||||||
),
|
),
|
||||||
@@ -45,13 +46,15 @@ export default async (req, res) => {
|
|||||||
await productVariantService.setRegionPrice(
|
await productVariantService.setRegionPrice(
|
||||||
variant_id,
|
variant_id,
|
||||||
price.region_id,
|
price.region_id,
|
||||||
price.amount
|
price.amount,
|
||||||
|
price.sale_amount || undefined
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
await productVariantService.setCurrencyPrice(
|
await productVariantService.setCurrencyPrice(
|
||||||
variant_id,
|
variant_id,
|
||||||
price.currency_code,
|
price.currency_code,
|
||||||
price.amount
|
price.amount,
|
||||||
|
price.sale_amount || undefined
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,4 +8,5 @@ export default new mongoose.Schema({
|
|||||||
region_id: { type: String },
|
region_id: { type: String },
|
||||||
currency_code: { type: String, required: true },
|
currency_code: { type: String, required: true },
|
||||||
amount: { type: Number, required: true, min: 0 },
|
amount: { type: Number, required: true, min: 0 },
|
||||||
|
sale_amount: { type: Number, min: 0 },
|
||||||
})
|
})
|
||||||
@@ -150,9 +150,10 @@ class ProductVariantService extends BaseService {
|
|||||||
* @param {string} variantId - the id of the variant to set prices for
|
* @param {string} variantId - the id of the variant to set prices for
|
||||||
* @param {string} currencyCode - the currency to set prices for
|
* @param {string} currencyCode - the currency to set prices for
|
||||||
* @param {number} amount - the amount to set the price to
|
* @param {number} amount - the amount to set the price to
|
||||||
|
* @param {number} saleAmount - the sale amount to set the price to
|
||||||
* @return {Promise} the result of the update operation
|
* @return {Promise} the result of the update operation
|
||||||
*/
|
*/
|
||||||
async setCurrencyPrice(variantId, currencyCode, amount) {
|
async setCurrencyPrice(variantId, currencyCode, amount, saleAmount) {
|
||||||
const variant = await this.retrieve(variantId)
|
const variant = await this.retrieve(variantId)
|
||||||
|
|
||||||
// If prices already exist we need to update all prices with the same
|
// If prices already exist we need to update all prices with the same
|
||||||
@@ -162,6 +163,7 @@ class ProductVariantService extends BaseService {
|
|||||||
const newPrices = variant.prices.map(moneyAmount => {
|
const newPrices = variant.prices.map(moneyAmount => {
|
||||||
if (moneyAmount.currency_code === currencyCode) {
|
if (moneyAmount.currency_code === currencyCode) {
|
||||||
moneyAmount.amount = amount
|
moneyAmount.amount = amount
|
||||||
|
moneyAmount.sale_amount = saleAmount
|
||||||
|
|
||||||
if (!moneyAmount.region_id) {
|
if (!moneyAmount.region_id) {
|
||||||
foundDefault = true
|
foundDefault = true
|
||||||
@@ -176,6 +178,7 @@ class ProductVariantService extends BaseService {
|
|||||||
if (!foundDefault) {
|
if (!foundDefault) {
|
||||||
newPrices.push({
|
newPrices.push({
|
||||||
currency_code: currencyCode,
|
currency_code: currencyCode,
|
||||||
|
sale_amount: saleAmount,
|
||||||
amount,
|
amount,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -210,6 +213,7 @@ class ProductVariantService extends BaseService {
|
|||||||
prices: [
|
prices: [
|
||||||
{
|
{
|
||||||
currency_code: currencyCode,
|
currency_code: currencyCode,
|
||||||
|
sale_amount: saleAmount,
|
||||||
amount,
|
amount,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
@@ -238,18 +242,28 @@ class ProductVariantService extends BaseService {
|
|||||||
const region = await this.regionService_.retrieve(regionId)
|
const region = await this.regionService_.retrieve(regionId)
|
||||||
|
|
||||||
let price
|
let price
|
||||||
variant.prices.forEach(({ region_id, amount, currency_code }) => {
|
variant.prices.forEach(
|
||||||
if (!price && !region_id && currency_code === region.currency_code) {
|
({ region_id, amount, sale_amount, currency_code }) => {
|
||||||
// If we haven't yet found a price and the current money amount is
|
if (!price && !region_id && currency_code === region.currency_code) {
|
||||||
// the default money amount for the currency of the region we have found
|
// If we haven't yet found a price and the current money amount is
|
||||||
// a possible price match
|
// the default money amount for the currency of the region we have found
|
||||||
price = amount
|
// a possible price match
|
||||||
} else if (region_id === region._id) {
|
if (sale_amount) {
|
||||||
// If the region matches directly with the money amount this is the best
|
price = sale_amount
|
||||||
// price
|
} else {
|
||||||
price = amount
|
price = amount
|
||||||
|
}
|
||||||
|
} else if (region_id === region._id) {
|
||||||
|
// If the region matches directly with the money amount this is the best
|
||||||
|
// price
|
||||||
|
if (sale_amount) {
|
||||||
|
price = sale_amount
|
||||||
|
} else {
|
||||||
|
price = amount
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
)
|
||||||
|
|
||||||
// Return the price if we found a suitable match
|
// Return the price if we found a suitable match
|
||||||
if (typeof price !== "undefined") {
|
if (typeof price !== "undefined") {
|
||||||
@@ -268,9 +282,10 @@ class ProductVariantService extends BaseService {
|
|||||||
* @param {string} variantId - the id of the variant to update
|
* @param {string} variantId - the id of the variant to update
|
||||||
* @param {string} regionId - the id of the region to set price for
|
* @param {string} regionId - the id of the region to set price for
|
||||||
* @param {number} amount - the amount to set the price to
|
* @param {number} amount - the amount to set the price to
|
||||||
|
* @param {number} saleAmount - the amount to set the price to
|
||||||
* @return {Promise} the result of the update operation
|
* @return {Promise} the result of the update operation
|
||||||
*/
|
*/
|
||||||
async setRegionPrice(variantId, regionId, amount) {
|
async setRegionPrice(variantId, regionId, amount, saleAmount) {
|
||||||
const variant = await this.retrieve(variantId)
|
const variant = await this.retrieve(variantId)
|
||||||
const region = await this.regionService_.retrieve(regionId)
|
const region = await this.regionService_.retrieve(regionId)
|
||||||
|
|
||||||
@@ -280,6 +295,7 @@ class ProductVariantService extends BaseService {
|
|||||||
const newPrices = variant.prices.map(moneyAmount => {
|
const newPrices = variant.prices.map(moneyAmount => {
|
||||||
if (moneyAmount.region_id === region._id) {
|
if (moneyAmount.region_id === region._id) {
|
||||||
moneyAmount.amount = amount
|
moneyAmount.amount = amount
|
||||||
|
moneyAmount.sale_amount = amount
|
||||||
foundRegion = true
|
foundRegion = true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -291,6 +307,7 @@ class ProductVariantService extends BaseService {
|
|||||||
newPrices.push({
|
newPrices.push({
|
||||||
region_id: region._id,
|
region_id: region._id,
|
||||||
currency_code: region.currency_code,
|
currency_code: region.currency_code,
|
||||||
|
sale_amount: saleAmount,
|
||||||
amount,
|
amount,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -327,10 +344,12 @@ class ProductVariantService extends BaseService {
|
|||||||
{
|
{
|
||||||
region_id: region._id,
|
region_id: region._id,
|
||||||
currency_code: region.currency_code,
|
currency_code: region.currency_code,
|
||||||
|
sale_amount: saleAmount,
|
||||||
amount,
|
amount,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
currency_code: region.currency_code,
|
currency_code: region.currency_code,
|
||||||
|
sale_amount: saleAmount,
|
||||||
amount,
|
amount,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -178,13 +178,15 @@ class ProductService extends BaseService {
|
|||||||
await this.productVariantService_.setRegionPrice(
|
await this.productVariantService_.setRegionPrice(
|
||||||
variant._id,
|
variant._id,
|
||||||
price.region_id,
|
price.region_id,
|
||||||
price.amount
|
price.amount,
|
||||||
|
price.sale_amount || undefined
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
await this.productVariantService_.setCurrencyPrice(
|
await this.productVariantService_.setCurrencyPrice(
|
||||||
variant._id,
|
variant._id,
|
||||||
price.currency_code,
|
price.currency_code,
|
||||||
price.amount
|
price.amount,
|
||||||
|
price.sale_amount || undefined
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user