Skip to content

Commit

Permalink
Merge pull request #71 from fabrix-app/v1.6
Browse files Browse the repository at this point in the history
[fix] Customer's Customers listing
  • Loading branch information
scott-wyatt authored Jan 29, 2019
2 parents d72fe39 + a5519f4 commit 5a99419
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 87 deletions.
74 changes: 57 additions & 17 deletions lib/api/controllers/CustomerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2266,7 +2266,7 @@ export class CustomerController extends Controller {
}

/**
* retuns a list of customers
* retuns a list of customers of a customer
* @param req
* @param res
*/
Expand All @@ -2287,32 +2287,72 @@ export class CustomerController extends Controller {
const limit = Math.max(0, req.query.limit || 10)
const offset = Math.max(0, req.query.offset || 0)
const sort = req.query.sort || [['created_at', 'DESC']]
Customer.findAndCountAll({
// TODO fix for sqlite
include: [{
model: this.app.models['Customer'].instance,
as: 'customers',
attributes: ['id'],
where: {
id: customerId
}
}],
offset: offset,
// TODO sequelize breaks if limit is set here
limit: limit,
order: sort

const ItemCustomer = this.app.models['ItemCustomer']

let count = 0

Customer.resolve(customerId, {
attributes: ['id']
})
.then(_customer => {

return ItemCustomer.findAndCountAll({
where: {
customer_id: _customer.id,
model: 'customer'
},
attributes: ['model_id'],
limit: limit,
offset: offset
})
})
.then(arr => {
count = arr.count
const customerIds = arr.rows.map(model => model.model_id)
return Customer.findAll({
where: {
id: customerIds
}
})
})
.then(customers => {
// Paginate
res.paginate(customers.count, limit, offset, sort)
return this.app.services.PermissionsService.sanitizeResult(req, customers.rows)
res.paginate(count, limit, offset, sort)
return this.app.services.PermissionsService.sanitizeResult(req, customers)
})
.then(result => {
return res.json(result)
})
.catch(err => {
return res.serverError(err)
})
// Customer.findAndCountAll({
// // TODO fix for sqlite
// include: [{
// model: this.app.models['Customer'].instance,
// as: 'customers',
// attributes: ['id'],
// where: {
// id: customerId
// }
// }],
// offset: offset,
// // TODO sequelize breaks if limit is set here
// limit: limit,
// order: sort
// })
// .then(customers => {
// // Paginate
// res.paginate(customers.count, limit, offset, sort)
// return this.app.services.PermissionsService.sanitizeResult(req, customers.rows)
// })
// .then(result => {
// return res.json(result)
// })
// .catch(err => {
// return res.serverError(err)
// })
}
/**
* remove a customer from a customer
Expand Down
25 changes: 14 additions & 11 deletions lib/api/services/CustomerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export class CustomerService extends Service {
tax_exempt: customer.tax_exempt,
verified_email: customer.verified_email,
metadata: Metadata.transform(customer.metadata || {}),
account_balance: customer.account_balance
account_balance: customer.account_balance,
type: customer.type
}, {
include: [
{
Expand Down Expand Up @@ -824,14 +825,15 @@ export class CustomerService extends Service {
* @returns {Promise.<TResult>}
*/
addCustomer(customer, subcustomer, options: {[key: string]: any} = {}) {
const Customer = this.app.models['Customer']
let resCustomer, resSubcustomer
return this.app.models['Customer'].resolve(customer, {transaction: options.transaction || null})
return Customer.resolve(customer, {transaction: options.transaction || null})
.then(_customer => {
if (!_customer) {
throw new Error('Customer Could Not Be Resolved')
}
resCustomer = _customer
return this.app.models['Customer'].resolve(subcustomer, {transaction: options.transaction || null})
return Customer.resolve(subcustomer, {transaction: options.transaction || null})
})
.then(_customer => {
if (!_customer) {
Expand All @@ -852,8 +854,8 @@ export class CustomerService extends Service {
object_id: resCustomer.id,
object: 'customer',
objects: [{
customer: resCustomer.id,
}, {
// customer: resCustomer.id,
// }, {
customer: resSubcustomer.id,
}],
type: 'customer.customer.added',
Expand Down Expand Up @@ -907,14 +909,15 @@ export class CustomerService extends Service {
*/
removeCustomer(customer, subcustomer, options: {[key: string]: any } = {}) {

const Customer = this.app.models['Customer']
let resCustomer, resSubcustomer
return this.app.models['Customer'].resolve(customer, {transaction: options.transaction || null})
return Customer.resolve(customer, {transaction: options.transaction || null})
.then(_customer => {
if (!_customer) {
throw new Error('Customer Could Not Be Resolved')
}
resCustomer = _customer
return this.app.models['Customer'].resolve(subcustomer, {transaction: options.transaction || null})
return Customer.resolve(subcustomer, {transaction: options.transaction || null})
})
.then(_customer => {
if (!_customer) {
Expand All @@ -925,9 +928,9 @@ export class CustomerService extends Service {
})
.then(hasCustomer => {
if (hasCustomer) {
return
return resCustomer.removeCustomer(resSubcustomer.id, {transaction: options.transaction || null})
}
return resCustomer.removeCustomer(resSubcustomer.id, {transaction: options.transaction || null})
return
})
.then((removed) => {
// console.log('BROKE REMOVED', removed)
Expand All @@ -936,8 +939,8 @@ export class CustomerService extends Service {
object_id: resCustomer.id,
object: 'customer',
objects: [{
customer: resCustomer.id,
}, {
// customer: resCustomer.id,
// }, {
customer: resSubcustomer.id,
}],
type: 'customer.customer.removed',
Expand Down
128 changes: 74 additions & 54 deletions lib/config/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2327,46 +2327,6 @@ export const routes = {
}
}
}
}
},
// TODO Deprecate
'/collection/handle/:handle': {
'GET': {
handler: 'CollectionController.findByHandle',
config: {
prefix: 'cart.prefix',
validate: {
params: {
handle: joi.string().required()
}
},
app: {
permissions: {
resource_name: 'apiGetCollectionHandleRoute',
roles: ['public', 'registered', 'admin']
}
}
}
},
'POST': {
handler: 'CollectionController.addCustomers',
config: {
prefix: 'cart.prefix',
validate: {
params: {
id: joi.alternatives().try(
joi.number(),
joi.string()
).required()
}
},
app: {
permissions: {
resource_name: 'apiPostCollectionIdCustomersRoute',
roles: ['admin']
}
}
}
},
'PUT': {
handler: 'CollectionController.addCustomers',
Expand All @@ -2387,31 +2347,91 @@ export const routes = {
}
}
}
},
'DELETE': {
handler: 'CollectionController.removeCustomer',
}
},
// TODO Deprecate
'/collection/handle/:handle': {
'GET': {
handler: 'CollectionController.findByHandle',
config: {
prefix: 'cart.prefix',
validate: {
params: {
id: joi.alternatives().try(
joi.number(),
joi.string()
).required(),
customer: joi.alternatives().try(
joi.number(),
joi.string()
).required()
handle: joi.string().required()
}
},
app: {
permissions: {
resource_name: 'apiDeleteCollectionIdCustomerCustomerRoute',
roles: ['admin']
resource_name: 'apiGetCollectionHandleRoute',
roles: ['public', 'registered', 'admin']
}
}
}
}
},
// 'POST': {
// handler: 'CollectionController.addCustomers',
// config: {
// prefix: 'cart.prefix',
// validate: {
// params: {
// id: joi.alternatives().try(
// joi.number(),
// joi.string()
// ).required()
// }
// },
// app: {
// permissions: {
// resource_name: 'apiPostCollectionIdCustomersRoute',
// roles: ['admin']
// }
// }
// }
// },
// 'PUT': {
// handler: 'CollectionController.addCustomers',
// config: {
// prefix: 'cart.prefix',
// validate: {
// params: {
// id: joi.alternatives().try(
// joi.number(),
// joi.string()
// ).required()
// }
// },
// app: {
// permissions: {
// resource_name: 'apiPutCollectionIdCustomersRoute',
// roles: ['admin']
// }
// }
// }
// },
// 'DELETE': {
// handler: 'CollectionController.removeCustomer',
// config: {
// prefix: 'cart.prefix',
// validate: {
// params: {
// id: joi.alternatives().try(
// joi.number(),
// joi.string()
// ).required(),
// customer: joi.alternatives().try(
// joi.number(),
// joi.string()
// ).required()
// }
// },
// app: {
// permissions: {
// resource_name: 'apiDeleteCollectionIdCustomerCustomerRoute',
// roles: ['admin']
// }
// }
// }
// }
},
// TODO legacy
'/collection/:id/removeCustomer/:customer': {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fabrix/spool-cart",
"version": "1.6.4",
"version": "1.6.5",
"description": "Spool - eCommerce Spool for Fabrix",
"homepage": "https://fabrix.app",
"author": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ describe('Admin User CustomerController', () => {
assert.equal(_.isNumber(parseInt(res.headers['x-pagination-page'])), true)
assert.equal(_.isNumber(parseInt(res.headers['x-pagination-pages'])), true)

console.log(res.body)
assert.equal(res.body.length > 0, true)
done(err)
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ describe('Admin User ProductController', () => {
}))
.expect(200)
.end((err, res) => {
console.log('FIX BROKE ASSOCs', res.body)
// console.log('FIX BROKE ASSOCs', res.body)
// assert.equal(res.body.length, 2)
assert.ok(res.body.length)
done(err)
Expand Down Expand Up @@ -1102,7 +1102,7 @@ describe('Admin User ProductController', () => {
}))
.expect(200)
.end((err, res) => {
console.log('BROKE ASSOC', res.body)
// console.log('BROKE ASSOC', res.body)
assert.ok(res.body.length)
done(err)
})
Expand Down

0 comments on commit 5a99419

Please sign in to comment.