Skip to content

Commit

Permalink
Updated Store Delete Route
Browse files Browse the repository at this point in the history
  • Loading branch information
vraj291 committed Nov 11, 2021
1 parent 59b9e9a commit cbdddea
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
3 changes: 2 additions & 1 deletion server/controllers/store.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Store = require('../models/Store')
const Product = require('../models/Product')
const getError = require('../utils/dbErrorHandler')
const {uploadImages, deleteImages} = require('../utils/multipleImageOperations')
const { deleteStorefromProducts } = require('../utils/relationOperations')
const { deleteStorefromProducts, deleteStorefromUser } = require('../utils/relationOperations')
const {storePopulate} = require('../utils/populateObjects')
const User = require('../models/User')

Expand Down Expand Up @@ -701,6 +701,7 @@ module.exports = {
.then( async(store) => {
if(store){
await deleteStorefromProducts(store)
await deleteStorefromUser(req.user._id,store._id)
await deleteImages(store.images)
await store.deleteOne()
return res.status(200).json({
Expand Down
5 changes: 5 additions & 0 deletions server/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ UserSchema.methods = {
if(!this.hasStore(id)){
this.stores.push(id)
}
},
removeStore : function(id){
if(this.hasStore(id)){
this.stores = this.stores.filter(e => e.toString() !== id.toString())
}
}
}

Expand Down
13 changes: 12 additions & 1 deletion server/utils/populateObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,18 @@ module.exports = {
},
{
path:'stores',
select:'name description addresses rating products '
select:'name description addresses rating products',
populate: {
path: 'products',
populate:{
path:'productId',
select: 'name description images',
populate:{
path:'images',
select:'url'
}
}
}
}

],
Expand Down
12 changes: 11 additions & 1 deletion server/utils/relationOperations.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Product = require('../models/Product')
const Store = require('../models/Store')
const User = require('../models/User')

module.exports = {

Expand Down Expand Up @@ -30,6 +31,15 @@ module.exports = {
}))
}
return Promise.all(promises)
}
},

deleteStorefromUser : async (userId,storeId) => {
User.findById(userId).then(async (user) => {
if(user){
user.removeStore(storeId)
await user.save()
}
})
}

}

0 comments on commit cbdddea

Please sign in to comment.