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:
Oliver Windall Juhl
2020-12-16 11:54:28 +01:00
committed by GitHub
parent 5ebaf77c7f
commit 451451a38e
8 changed files with 51 additions and 18 deletions

View File

@@ -315,6 +315,11 @@ class ContentfulService extends BaseService {
return this.createProductVariantInContentful(variant)
}
const cleanPrices = variants.prices.map((price) => ({
...price,
sale_amount: price.sale_amount || undefined,
}))
const variantEntryFields = {
...variantEntry.fields,
title: {
@@ -327,7 +332,7 @@ class ContentfulService extends BaseService {
"en-US": variant.options,
},
prices: {
"en-US": variant.prices,
"en-US": cleanPrices,
},
objectId: {
"en-US": variant._id,

View File

@@ -20,6 +20,7 @@ export default async (req, res) => {
.items({
currency_code: Validator.string().required(),
amount: Validator.number().required(),
sale_amount: Validator.number().optional(),
})
.required(),
options: Validator.array()

View File

@@ -10,6 +10,7 @@ export default async (req, res) => {
.items({
currency_code: Validator.string().required(),
amount: Validator.number().required(),
sale_amount: Validator.number().optional(),
})
.required(),
options: Validator.array()

View File

@@ -31,6 +31,7 @@ export default async (req, res) => {
region_id: Validator.string(),
currency_code: Validator.string(),
amount: Validator.number().required(),
sale_amount: Validator.number().optional(),
})
.xor("region_id", "currency_code")
),

View File

@@ -13,6 +13,7 @@ export default async (req, res) => {
region_id: Validator.string(),
currency_code: Validator.string(),
amount: Validator.number().required(),
sale_amount: Validator.number().optional(),
})
.xor("region_id", "currency_code")
),
@@ -45,13 +46,15 @@ export default async (req, res) => {
await productVariantService.setRegionPrice(
variant_id,
price.region_id,
price.amount
price.amount,
price.sale_amount || undefined
)
} else {
await productVariantService.setCurrencyPrice(
variant_id,
price.currency_code,
price.amount
price.amount,
price.sale_amount || undefined
)
}
}

View File

@@ -8,4 +8,5 @@ export default new mongoose.Schema({
region_id: { type: String },
currency_code: { type: String, required: true },
amount: { type: Number, required: true, min: 0 },
sale_amount: { type: Number, min: 0 },
})

View File

@@ -150,9 +150,10 @@ class ProductVariantService extends BaseService {
* @param {string} variantId - the id of the variant 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} saleAmount - the sale amount to set the price to
* @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)
// 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 => {
if (moneyAmount.currency_code === currencyCode) {
moneyAmount.amount = amount
moneyAmount.sale_amount = saleAmount
if (!moneyAmount.region_id) {
foundDefault = true
@@ -176,6 +178,7 @@ class ProductVariantService extends BaseService {
if (!foundDefault) {
newPrices.push({
currency_code: currencyCode,
sale_amount: saleAmount,
amount,
})
}
@@ -210,6 +213,7 @@ class ProductVariantService extends BaseService {
prices: [
{
currency_code: currencyCode,
sale_amount: saleAmount,
amount,
},
],
@@ -238,18 +242,28 @@ class ProductVariantService extends BaseService {
const region = await this.regionService_.retrieve(regionId)
let price
variant.prices.forEach(({ region_id, amount, currency_code }) => {
if (!price && !region_id && currency_code === region.currency_code) {
// If we haven't yet found a price and the current money amount is
// the default money amount for the currency of the region we have found
// a possible price match
price = amount
} else if (region_id === region._id) {
// If the region matches directly with the money amount this is the best
// price
price = amount
variant.prices.forEach(
({ region_id, amount, sale_amount, currency_code }) => {
if (!price && !region_id && currency_code === region.currency_code) {
// If we haven't yet found a price and the current money amount is
// the default money amount for the currency of the region we have found
// a possible price match
if (sale_amount) {
price = sale_amount
} else {
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
if (typeof price !== "undefined") {
@@ -268,9 +282,10 @@ class ProductVariantService extends BaseService {
* @param {string} variantId - the id of the variant to update
* @param {string} regionId - the id of the region to set price for
* @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
*/
async setRegionPrice(variantId, regionId, amount) {
async setRegionPrice(variantId, regionId, amount, saleAmount) {
const variant = await this.retrieve(variantId)
const region = await this.regionService_.retrieve(regionId)
@@ -280,6 +295,7 @@ class ProductVariantService extends BaseService {
const newPrices = variant.prices.map(moneyAmount => {
if (moneyAmount.region_id === region._id) {
moneyAmount.amount = amount
moneyAmount.sale_amount = amount
foundRegion = true
}
@@ -291,6 +307,7 @@ class ProductVariantService extends BaseService {
newPrices.push({
region_id: region._id,
currency_code: region.currency_code,
sale_amount: saleAmount,
amount,
})
}
@@ -327,10 +344,12 @@ class ProductVariantService extends BaseService {
{
region_id: region._id,
currency_code: region.currency_code,
sale_amount: saleAmount,
amount,
},
{
currency_code: region.currency_code,
sale_amount: saleAmount,
amount,
},
],

View File

@@ -178,13 +178,15 @@ class ProductService extends BaseService {
await this.productVariantService_.setRegionPrice(
variant._id,
price.region_id,
price.amount
price.amount,
price.sale_amount || undefined
)
} else {
await this.productVariantService_.setCurrencyPrice(
variant._id,
price.currency_code,
price.amount
price.amount,
price.sale_amount || undefined
)
}
}