Should I Handle Approval/Rejection in One Database Function or Split It Up?

0
7
Asked By CleverSprout92 On

I'm curious about how to best manage an approval or rejection status update in my application. I have an endpoint that receives a status and I need to update it accordingly. Should I check the status in the application layer and call separate database functions for approving or rejecting, or is it better to handle everything in the database layer with a single function? Here's how I've outlined the two options:

**Option A – Application Layer Checks:**
- If the status is "approve", then call `db.approve(id)`.
- If it's "reject", call `db.reject(id)`.
- If the status is neither, return an error with a 422 status code.

**Option B – Single Database Function:**
- Check if the status is either "approve" or "reject" and call `db.resolveStatus({ id, decision: status })` for both.
- For any other status, return a 422 error.

Which approach do you guys think is more common or better?

2 Answers

Answered By CodeWhisperer88 On

Personally, I’d go with Option B but maybe rename it to `setStatus()`. It clearly conveys what the function does, encapsulating the logic for both approving and rejecting in one neat package!

Answered By SyntaxSorcerer45 On

Consider what you're trying to achieve with atomicity. If your operation is changing lots of things at once, it might need to happen in a transaction. Ultimately, think about what’s simpler and easier to maintain. Sometimes, breaking it out into separate functions for readability can be a great approach.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.