I liked your solution however, I mainly struggle with the queue. Our users can bill their clients on subscription payments (think Spotify, Netflix, etc.)
The problem comes when users create a subscription. For context, the invoice for next month is created at the moment the current one is paid, the workflow checks if it’s from a subscription, then it creates the invoice due next month and schedules the next payment to charge on the due date of that invoice which is next month.
All of those invoices get scheduled at 12am, the problem happens when occasionally one of our users signs more than 1 client on the same day of the month. Which I found is not that uncommon and when it does happen 1/3 times there will be a duplicate invoice number
How do I add those to a queue since all the invoices execute independently?
The solution that I implemented in the meantime is I just schedule out for 30 days later PLUS a random number of seconds between 1-30. It’s a solution for now but it doesn’t follow good design exactly.