0

Some employees at my company want an internal tool with a way to approve new purchases before we expense them to the proposed departments. So someone will submit a purchase order for approval, and then anywhere from 1-5 people would have to approve it, depending on the budget accounts involved.

I have a Postgres DB. The users table contains the 5 employees who need to approve orders, the approvals table contains the purchase orders, and the user_approvals table contains which users need to be approve a given proposal.

Let's say that the approval with an id of 1 needs to be approved by employees with ids 3 and 4. The user_approvals table would have two entries--(approval_id: 1, user_id: 3) and (approval_id: 1, user_id: 4).

When user 3 looks at the web page that contains the purchase orders they need to review, I pull the relevant POs:

SELECT * FROM approvals
WHERE id IN (
    SELECT approval_id FROM user_approvals
    WHERE user_id = 3)

That works. It grabs the relevant info from the DB entry. It also ensures that no employee sees any purchase orders that don't require their approval.

However, I would like for user 3 to ALSO be able to see if user 4 has approved the purchase order yet.

I want each user to see the purchase orders that involve them, as well as the responses from anyone else that's involved. I don't mind making two separate queries if needed.

Should I follow up with something like the following?

SELECT status FROM user_approvals
WHERE approval_id IN (
    SELECT id FROM approvals
    WHERE id IN (
        SELECT approval_id FROM user_approvals
        WHERE user_id = 3))

This seems to work in this case, but also seems redundant.

2
  • No, I can't guess what your query might be doing when you don't give any schema. "a better way of handling which users have access to which approvals" -- what tells us how many people and which people are required to approve a purchase? Commented May 29, 2024 at 20:48
  • BTW there are all sorts of software packages for Purchase Ordering and Approvals workflow -- including as part of small business applications such as MYOB. Why are you even starting from scratch? Commented May 29, 2024 at 20:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.