Skip to content

Commit

Permalink
Merge pull request #79 from fabrix-app/v1.6
Browse files Browse the repository at this point in the history
[fix] virtual calculated_price for products
  • Loading branch information
scott-wyatt authored Mar 20, 2019
2 parents 9df2f0e + 921a4ee commit 440df05
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 57 deletions.
6 changes: 4 additions & 2 deletions lib/api/models/Product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,9 @@ export class Product extends Model {
defaultValue: PRODUCT_DEFAULTS.PRICE
},
// Pricing after
// CHANGED 190320
calculated_price: {
type: Sequelize.INTEGER,
type: Sequelize.VIRTUAL(Sequelize.INTEGER),
defaultValue: PRODUCT_DEFAULTS.CALCULATED_PRICE
},
// Default currency of the product
Expand Down Expand Up @@ -1267,7 +1268,8 @@ Product.prototype.toJSON = function() {
// Make JSON
const resp = this instanceof this.app.models['Product'].instance ? this.get({ plain: true }) : this
// Set Defaults
// resp.calculated_price = resp.price
// CHANGE SBW 190320
resp.calculated_price = resp.price

// Transform Tags to array on toJSON
if (resp.tags) {
Expand Down
2 changes: 1 addition & 1 deletion lib/api/models/ProductVariant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export class ProductVariant extends Model {
},
// The calculated Price of the product
calculated_price: {
type: Sequelize.INTEGER,
type: Sequelize.VIRTUAL(Sequelize.INTEGER),
defaultValue: 0
},
// Competitor price of the variant in cents
Expand Down
3 changes: 3 additions & 0 deletions lib/api/services/ProductService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2068,6 +2068,9 @@ export class ProductService extends Service {
if (_.isNil(product.tax_code)) {
product.tax_code = PRODUCT_DEFAULTS.TAX_CODE
}
if (_.isNumber(product.price) && _.isNil(product.compare_at_price)) {
product.compare_at_price = product.price
}
// If no currency set default currency
if (_.isNil(product.currency)) {
product.currency = PRODUCT_DEFAULTS.CURRENCY
Expand Down
2 changes: 1 addition & 1 deletion lib/api/services/ProxyCartService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ export class ProxyCartService extends Service {
* @param address
* @returns {*}
*/
normalizeAddress(address) {
normalizeAddress(address: {[key: string]: any} = {}) {
const CartCountryService = this.app.services.CartCountryService
const Address = this.app.models['Address']
const countryNorm = address.country_code || address.country || address.country_name
Expand Down
130 changes: 80 additions & 50 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fabrix/spool-cart",
"version": "1.6.11",
"version": "1.6.12",
"description": "Spool - eCommerce Spool for Fabrix",
"homepage": "https://fabrix.app",
"author": {
Expand Down Expand Up @@ -66,10 +66,10 @@
"@fabrix/spool-generics": "^1.5.0",
"@fabrix/spool-i18n": "^1.5.1",
"@fabrix/spool-notifications": "^1.5.0",
"@fabrix/spool-passport": "^1.5.0",
"@fabrix/spool-passport": "^1.6.0",
"@fabrix/spool-permissions": "^1.5.1",
"@fabrix/spool-router": "^1.6.0",
"@fabrix/spool-sequelize": "^1.6.2",
"@fabrix/spool-sequelize": "^1.6.6",
"@types/lodash": "^4.14.109",
"@types/node": "~10.3.4",
"express": "^4.16.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ describe('Admin User ProductController', () => {
assert.equal(res.body.seo_description, 'Chalk Bag')
assert.notEqual(res.body.vendors.indexOf('B.A.G'), -1)
assert.equal(res.body.type, 'Chalk Bag')
assert.equal(res.body.price, 10000)
assert.equal(res.body.compare_at_price, 10000)
assert.equal(res.body.calculated_price, 10000)
assert.notEqual(res.body.options.indexOf('capacity'), -1)
// Metadata
assert.equal(res.body.metadata.test, 'value')
Expand Down Expand Up @@ -200,6 +203,10 @@ describe('Admin User ProductController', () => {
assert.equal(res.body[0].type, 'Snowboard')
assert.notEqual(res.body[0].options.indexOf('size'), -1)
assert.notEqual(res.body[0].options.indexOf('width'), -1)

assert.equal(res.body[0].price, 10000)
assert.equal(res.body[0].compare_at_price, 10000)
assert.equal(res.body[0].calculated_price, 10000)
// Metadata
assert.equal(res.body[0].metadata.test, 'value')
// Collections
Expand Down Expand Up @@ -244,6 +251,8 @@ describe('Admin User ProductController', () => {
assert.equal(res.body[0].variants[0].sku, 'board-m-123')
assert.equal(res.body[0].variants[0].title, res.body[0].title)
assert.equal(res.body[0].variants[0].price, res.body[0].price)
// assert.equal(res.body[0].variants[0].compare_at_price, res.body[0].compare_at_price)
assert.equal(res.body[0].variants[0].calculated_price, res.body[0].calculated_price)
assert.equal(res.body[0].variants[0].weight, res.body[0].weight)
assert.equal(res.body[0].variants[0].weight_unit, res.body[0].weight_unit)
assert.equal(res.body[0].variants[0].option.width, '18in')
Expand Down

0 comments on commit 440df05

Please sign in to comment.