Skip to content

Commit 6d9dc45

Browse files
committed
fix: delete will now delete the key-value (412 fix)
The 412 Precondition Failed condition `header` filter now allows client to delete the object irrespective of the conditional delete.
1 parent b361351 commit 6d9dc45

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ module.exports = {
1919
'consistent-return': 'off',
2020
'max-classes-per-file': 'off',
2121
'default-param-last': 'off',
22-
camelcase: 'off',
22+
// camelcase: 'off',
2323
},
2424
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "api",
3-
"version": "1.2.0",
3+
"version": "1.2.2",
44
"description": "RESTful API",
55
"main": "src/server.js",
66
"author": "Siddharth Rawat <rawat.sid@northeastern.edu>",

src/api/controllers/plan.controller.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,31 @@ const deletePlan = async (req, res, next) => {
102102
throw new BadRequestError(`Invalid planId`)
103103
}
104104
const value = await findPlan(planId)
105-
if (value.objectId === planId) {
105+
if (value !== false && value.objectId === planId) {
106106
// conditional delete based on `if-match` header
107-
if (req.headers['if-match'] && value.ETag === req.headers['if-match']) {
108-
const data = {
109-
plan: JSON.parse(value.plan),
110-
}
111-
logger.info(`Item found`, JSON.parse(value.plan))
112-
if (deleteByPlanId(planId)) {
113-
logger.info(`Item deleted`, JSON.parse(value.plan))
114-
noContentHandler(res, data)
107+
const data = {
108+
plan: JSON.parse(value.plan),
109+
}
110+
if (req.headers['if-match']) {
111+
// if ETag matches on conditional delete
112+
if (value.ETag === req.headers['if-match']) {
113+
logger.info(`Item found`, JSON.parse(value.plan))
114+
if (deleteByPlanId(planId)) {
115+
logger.info(`Item deleted`, JSON.parse(value.plan))
116+
noContentHandler(res, data)
117+
} else {
118+
throw new InternalServerError(`Item not deleted`)
119+
}
115120
} else {
116-
throw new InternalServerError(`Item not deleted`)
121+
throw new PreConditionFailedError(
122+
`ETag provided in header is not valid`
123+
)
117124
}
125+
} else if (deleteByPlanId(planId)) {
126+
logger.info(`Item deleted`, JSON.parse(value.plan))
127+
noContentHandler(res, data)
118128
} else {
119-
throw new PreConditionFailedError(
120-
`ETag provided in header is not valid`
121-
)
129+
throw new InternalServerError(`Item not deleted`)
122130
}
123131
} else {
124132
throw new ResourceNotFoundError(`Plan not found`)

0 commit comments

Comments
 (0)