Skip to content

Commit 0e8cea9

Browse files
authored
feature: post middleware for add and remove (#313)
1 parent 67cec41 commit 0e8cea9

File tree

1 file changed

+93
-1
lines changed

1 file changed

+93
-1
lines changed

utilities/handler-helper.js

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,29 @@ async function _addOneHandler(
12361236
Log
12371237
)
12381238
}
1239+
1240+
try {
1241+
if (
1242+
ownerModel.routeOptions &&
1243+
ownerModel.routeOptions.add &&
1244+
ownerModel.routeOptions.add[associationName] &&
1245+
ownerModel.routeOptions.add[associationName].post
1246+
) {
1247+
await ownerModel.routeOptions.add[associationName].post(
1248+
payload,
1249+
request,
1250+
Log
1251+
)
1252+
}
1253+
} catch (err) {
1254+
handleError(
1255+
err,
1256+
'There was a postprocessing error after setting the association.',
1257+
Boom.badRequest,
1258+
Log
1259+
)
1260+
}
1261+
12391262
return true
12401263
} else {
12411264
throw Boom.notFound('No resource was found with that id.')
@@ -1432,6 +1455,29 @@ async function _removeOneHandler(
14321455
Log
14331456
)
14341457
}
1458+
1459+
try {
1460+
if (
1461+
ownerModel.routeOptions &&
1462+
ownerModel.routeOptions.remove &&
1463+
ownerModel.routeOptions.remove[associationName] &&
1464+
ownerModel.routeOptions.remove[associationName].post
1465+
) {
1466+
await ownerModel.routeOptions.remove[associationName].post(
1467+
{},
1468+
request,
1469+
Log
1470+
)
1471+
}
1472+
} catch (err) {
1473+
handleError(
1474+
err,
1475+
'There was a postprocessing error after removing the association.',
1476+
Boom.badRequest,
1477+
Log
1478+
)
1479+
}
1480+
14351481
return true
14361482
} else {
14371483
throw Boom.notFound('No resource was found with that id.')
@@ -1643,6 +1689,29 @@ async function _addManyHandler(
16431689
)
16441690
}
16451691
}
1692+
1693+
try {
1694+
if (
1695+
ownerModel.routeOptions &&
1696+
ownerModel.routeOptions.add &&
1697+
ownerModel.routeOptions.add[associationName] &&
1698+
ownerModel.routeOptions.add[associationName].post
1699+
) {
1700+
await ownerModel.routeOptions.add[associationName].post(
1701+
payload,
1702+
request,
1703+
Log
1704+
)
1705+
}
1706+
} catch (err) {
1707+
handleError(
1708+
err,
1709+
'There was a postprocessing error after setting the associations.',
1710+
Boom.badRequest,
1711+
Log
1712+
)
1713+
}
1714+
16461715
return true
16471716
} else {
16481717
throw Boom.notFound('No owner resource was found with that id.')
@@ -1812,7 +1881,7 @@ async function _removeManyHandler(
18121881
} catch (err) {
18131882
handleError(
18141883
err,
1815-
'There was a preprocessing error while removing the association.',
1884+
'There was a preprocessing error while removing the associations.',
18161885
Boom.badRequest,
18171886
Log
18181887
)
@@ -1837,6 +1906,29 @@ async function _removeManyHandler(
18371906
)
18381907
}
18391908
}
1909+
1910+
try {
1911+
if (
1912+
ownerModel.routeOptions &&
1913+
ownerModel.routeOptions.remove &&
1914+
ownerModel.routeOptions.remove[associationName] &&
1915+
ownerModel.routeOptions.remove[associationName].post
1916+
) {
1917+
await ownerModel.routeOptions.remove[associationName].post(
1918+
payload,
1919+
request,
1920+
Log
1921+
)
1922+
}
1923+
} catch (err) {
1924+
handleError(
1925+
err,
1926+
'There was a postprocessing error after removing the associations.',
1927+
Boom.badRequest,
1928+
Log
1929+
)
1930+
}
1931+
18401932
return true
18411933
} else {
18421934
throw Boom.notFound('No owner resource was found with that id.')

0 commit comments

Comments
 (0)