Skip to content

Commit

Permalink
[fix] collections endpoint of #66
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-wyatt committed Jan 29, 2019
1 parent a5519f4 commit 1461698
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
20 changes: 20 additions & 0 deletions lib/api/controllers/CollectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,26 @@ export class CollectionController extends Controller {
})
}

/**
*
* @param req
* @param res
*/
removeCollections(req, res) {
const CollectionService = this.app.services.CollectionService

CollectionService.removeCollections(req.params.id, req.body)
.then(collection => {
return this.app.services.PermissionsService.sanitizeResult(req, collection)
})
.then(result => {
return res.json(result)
})
.catch(err => {
return res.serverError(err)
})
}

/**
*
* @param req
Expand Down
27 changes: 25 additions & 2 deletions lib/api/services/CollectionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ export class CollectionService extends Service {
* @param options
* @returns {Promise.<T>}
*/
addCollection(collection, subCollection, options) {
options = options || {}
addCollection(collection, subCollection, options: {[key: string]: any} = {}) {

const Collection = this.app.models['Collection']
// const ItemCollection = this.app.models['ItemCollection']
let resCollection, resSubCollection
Expand Down Expand Up @@ -330,6 +330,29 @@ export class CollectionService extends Service {
})
}


/**
* Add Multiple Collections
* @param collection
* @param collections
* @param options
* @returns {Promise.<*>}
*/
removeCollections(collection, collections, options: {[key: string]: any} = {}) {
if (!Array.isArray(collections)) {
collections = [collections]
}
const Sequelize = this.app.models['Collection'].sequelize
// const addedProducts = []
// Setup Transaction
return Sequelize.transaction(t => {
return Sequelize.Promise.mapSeries(collections, subCollection => {
return this.removeCollection(collection, subCollection, {
transaction: t
})
})
})
}
/**
*
* @param collection
Expand Down
28 changes: 26 additions & 2 deletions lib/config/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,30 @@ export const routes = {
}
}
}
},
'DELETE': {
handler: 'CollectionController.removeCollection',
config: {
prefix: 'cart.prefix',
validate: {
params: {
id: joi.alternatives().try(
joi.number(),
joi.string()
).required(),
collection: joi.alternatives().try(
joi.number(),
joi.string()
).required()
}
},
app: {
permissions: {
resource_name: 'apiDeleteCollectionIdCollectionCollectionRoute',
roles: ['admin']
}
}
}
}
},
// TODO Legacy
Expand Down Expand Up @@ -1512,7 +1536,7 @@ export const routes = {
}
},
'DELETE': {
handler: 'CollectionController.removeCollection',
handler: 'CollectionController.removeCollections',
config: {
prefix: 'cart.prefix',
validate: {
Expand All @@ -1529,7 +1553,7 @@ export const routes = {
},
app: {
permissions: {
resource_name: 'apiDeleteCollectionIdCollectionCollectionRoute',
resource_name: 'apiDeleteCollectionIdCollectionsRoute',
roles: ['admin']
}
}
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.5",
"version": "1.6.6",
"description": "Spool - eCommerce Spool for Fabrix",
"homepage": "https://fabrix.app",
"author": {
Expand Down

0 comments on commit 1461698

Please sign in to comment.