diff --git a/lib/api/controllers/CollectionController.ts b/lib/api/controllers/CollectionController.ts index 3084129..c8f7e22 100755 --- a/lib/api/controllers/CollectionController.ts +++ b/lib/api/controllers/CollectionController.ts @@ -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 diff --git a/lib/api/services/CollectionService.ts b/lib/api/services/CollectionService.ts index 7e71898..165525c 100755 --- a/lib/api/services/CollectionService.ts +++ b/lib/api/services/CollectionService.ts @@ -281,8 +281,8 @@ export class CollectionService extends Service { * @param options * @returns {Promise.} */ - 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 @@ -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 diff --git a/lib/config/routes.ts b/lib/config/routes.ts index fd5a086..adf920f 100755 --- a/lib/config/routes.ts +++ b/lib/config/routes.ts @@ -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 @@ -1512,7 +1536,7 @@ export const routes = { } }, 'DELETE': { - handler: 'CollectionController.removeCollection', + handler: 'CollectionController.removeCollections', config: { prefix: 'cart.prefix', validate: { @@ -1529,7 +1553,7 @@ export const routes = { }, app: { permissions: { - resource_name: 'apiDeleteCollectionIdCollectionCollectionRoute', + resource_name: 'apiDeleteCollectionIdCollectionsRoute', roles: ['admin'] } } diff --git a/package-lock.json b/package-lock.json index ce3e63e..a34227a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@fabrix/spool-cart", - "version": "1.6.5", + "version": "1.6.6", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 22f12d1..86afcaa 100755 --- a/package.json +++ b/package.json @@ -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": {