Skip to content

Commit

Permalink
init: sequelize hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
ShubhamPalriwala committed Mar 19, 2022
1 parent 9afb972 commit c3736d0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 9 additions & 0 deletions models/relations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ const relationsInit = (_sequelize: Sequelize) => {
name: 'UserId'
}
})

BasketItemModel.addHook('beforeUpdate', 'dontUpdateId', (basketItem:any, options) => { // TODO: any
if(basketItem.dataValues.BasketId!=basketItem._previousDataValues.BasketId){
return Promise.reject(new Error("null: `BasketId` cannot be updated due `dontUpdateId` constraint"));
}
if(basketItem.dataValues.ProductId!=basketItem._previousDataValues.ProductId){
return Promise.reject(new Error("null: `ProductId` cannot be updated due `dontUpdateId` constraint"));
}
});
}

export { relationsInit }
8 changes: 4 additions & 4 deletions test/api/basketItemApiSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ describe('/api/BasketItems/:id', () => {
BasketId: 42
}
})
.expect('status', 400)
.expect('json', { message: 'null: `BasketId` cannot be updated due `noUpdate` constraint', errors: [{ field: 'BasketId', message: '`BasketId` cannot be updated due `noUpdate` constraint' }] })
.expect('status', 500)
.expect('json', { message: 'internal error', errors: ["null: `BasketId` cannot be updated due `dontUpdateId` constraint"] })
})
})

Expand Down Expand Up @@ -201,8 +201,8 @@ describe('/api/BasketItems/:id', () => {
ProductId: 42
}
})
.expect('status', 400)
.expect('json', { message: 'null: `ProductId` cannot be updated due `noUpdate` constraint', errors: [{ field: 'ProductId', message: '`ProductId` cannot be updated due `noUpdate` constraint' }] })
.expect('status', 500)
.expect('json', { message: 'internal error', errors: ["null: `ProductId` cannot be updated due `dontUpdateId` constraint"] })
})
})

Expand Down

0 comments on commit c3736d0

Please sign in to comment.