I would like some insights on how to think when building a Strapi application. I’m using GraphQL and are creating a client/freelancer marketplace app with video calls as the end-product.
I have three content types setup in Strapi:
- Products: Different video sessions that can be scheduled by user for a fixed price
- Orders: Orders made by clients consisting of one (1) product with a scheduled_time
- Transaction: Adds and subtracts clients’ and freelancers’ cash account balance when an order is made.
- Session: Contains video link, created with external API, clients and freelancers can join
I need to write some logic to handle the chain of transactions:
- Place order with desired product linked incl. processing payment
- Create credit transaction on freelancer’s account
- Create debet transaction on client’s account
- Create video session on scheduled_time (cron job?)
- Return order id or appropriate error message
- I need to ensure transactions doesn’t happen individually. This could end up a script adding money to freelancer but failing subtracting from client.
What is your recommended way structuring/handling these chains of events? And how can I keep integrity of transactions table?
Thanks in advance
Can you provide some more context as to what you are trying to ask here, based on the examples provided you are going to want to build some custom controllers (and probably services) to handle this.
As for the cron jobs, if you plan to or feel it’s possible you may need to scale your Strapi backend, I would strongly advise you handle the cron processing outside of Strapi and not with our built in option which does not scale this could be as simple as a cron script that requests a route on the Strapi server which executes a custom controller.
Yeha, that seems like a good idea! I realise my question might be vague.
- How would you handle these “chaining” of the events that needs to happen as a group and fail as a group. It’s mainly to make sure the integrity of the transaction tables is intact = everything adds up correctly.
Is there some way to interact with the database and group the events?
I can get back with more specific questions as soon as I start setting up the project.
Do you expect to make multiple API requests for the chain or a single one?
- Multiple => that gets complicated
- Single => much easier to succeed/fail “as a group” (ugly way would be some if/else statements and a simple
return ctx.badRequest or whatever the error code would be)
I’m not sure which one of the options is viable; I’m only going to make one API call (via GraphQL) but it will use multiple resolvers i guess.
I would basically need that each new order creates 2 transactions: withdrawing from client and adding to talent.
I want there to be no scenario in which an order is placed without transactions, or one of the transaction failing but still creating the other database entries.
What would be the way to approach this?
The best option would probably be a custom controller/GraphQL resolver is my initial thought