Conversation
ineiti
left a comment
There was a problem hiding this comment.
There is one part with the then where I'm not sure the code does what you want it to. Else it looks good to me.
web/backend/src/controllers/users.ts
Outdated
| .catch((e) => { | ||
| res.status(400).send(`Error while adding to roles: ${e}`); | ||
| }); | ||
| if (Object.hasOwn(req.body, 'userId')) { |
There was a problem hiding this comment.
For next time: I think 'userId' in req.body would be clearer to me. Same below.
web/backend/src/controllers/users.ts
Outdated
| addPolicy(req.body.userId, req.body.subject, req.body.permission) | ||
| .then(() => { | ||
| res.set(200).send(); | ||
| next(); | ||
| }) | ||
| .catch((e) => { | ||
| res.status(400).send(`Error while adding to roles: ${e}`); | ||
| }); |
There was a problem hiding this comment.
You have to be careful with the then construct here, as it will continue executing the code below, which might not be what you want. If you think this is a problem (I didn't look at it in detail), you should change it to the following. Same for the then just below.
try {
await addPolicy(req.body.userId, req.body.subject, req.body.permission);
res.set(200).send();
next();
} catch(e) {
res.status(400).send(`Error while adding to roles: ${e}`);
};
There was a problem hiding this comment.
I'm not sure I understand what you mean with "ilt will continue executing the code below" - the three cases are mutually exclusive (else if and else) and the function returns after executing either of the three?
There was a problem hiding this comment.
As discussed during coffee - it should be fine. But a good cleanup would be to do a
try {
await ...
} catch (e) {
}
Instead of using then.
ineiti
left a comment
There was a problem hiding this comment.
Now it's important that you add the await, else the catch will never be called:
web/backend/src/controllers/users.ts
Outdated
| // empty then sciper unknown, otherwise add it in userDB | ||
| if ('userId' in req.body) { | ||
| try { | ||
| addPolicy(req.body.userId, req.body.subject, req.body.permission); |
There was a problem hiding this comment.
| addPolicy(req.body.userId, req.body.subject, req.body.permission); | |
| await addPolicy(req.body.userId, req.body.subject, req.body.permission); |
And you'll also need to add an async somewhere in the definition of the callback, IIRC, like that:
usersRouter.post('/add_role', async (req, res, next) => {
There was a problem hiding this comment.
used Promise.resolve because I wasn't sure about the async in the callback, does that make sense?
web/backend/src/controllers/users.ts
Outdated
| next(); | ||
| } else if ('userIds' in req.body) { | ||
| try { | ||
| addListPolicy(req.body.userIds, req.body.subject, req.body.permission); |
There was a problem hiding this comment.
| addListPolicy(req.body.userIds, req.body.subject, req.body.permission); | |
| await addListPolicy(req.body.userIds, req.body.subject, req.body.permission); |
8747b69 to
bc6e987
Compare
ineiti
left a comment
There was a problem hiding this comment.
I'm still surprised it actually works - according to my test in the typescript playground, the async should never be called.
web/backend/src/controllers/users.ts
Outdated
| if ('userId' in req.body) { | ||
| try { | ||
| readSCIPER(req.body.userId); | ||
| Promise.resolve(addPolicy(req.body.userId, req.body.subject, req.body.permission)); |
There was a problem hiding this comment.
The Promise.resolve doesn't change anything - the promise will not be awaited for. Does the following PR break it?
You can run Await Playground and see that goodbye is never called.
There was a problem hiding this comment.
I wasn't sure about the async in the callback 😕 I'll try with async/await
There was a problem hiding this comment.
Here is a thread from stackoverflow: https://stackoverflow.com/questions/44813401/passing-in-async-functions-to-node-js-express-js-router
TLDR:
- express >= 4 handles async callbacks
- express >= 5 handles also errors w/o a try...catch
Edit: the post from 2021 in Stackoverflow made me think that express 5 is already a thing. But it's still in beta...
bc6e987 to
6f748b2
Compare
This PR adds voters in batches instead of one-by-one, allowing to add > 5000 voters at a time. Closes #155 .