I m developing an e-commerce project where end users are companies, and the system utilizes coupons to boost sales. I need assistance with implementing the following coupon usage logic:
- Each company has multiple staff members.
- Each coupon can be applied to multiple companies.
- Only one staff member per company can use the coupon once.
Here s the desired flow of the coupon application process:
- On the initial screen, a staff member can input the coupon code and click the "Apply" button.
- Upon clicking "Apply", the system automatically redirects them to the transaction screen.
- On the transaction screen, the staff member has a limited time of 20 minutes to complete the transaction by clicking the "Submit" button. If they fail to do so, the transaction expires, and they are redirected back to the initial screen.
Additionally, I have the following business requirements:
If a staff member (let s say Staff A) from a particular company uses the coupon, no other staff members from the same company should be able to use it temporarily.
If Staff A applies the coupon, submits the transaction, and completes the process, no other staff members from the same company should be able to use the coupon anymore.
If Staff A is on the transaction screen (coupon code applied and "Apply" button clicked), and they encounter any of the following situations:
- Fail to complete the transaction within 20 minutes due to network errors, being AFK, etc.
- Intentionally close the browser or current tab.
- Intentionally click the logout button in the page header.
- Any other method of leaving the page.
=> In such cases, other staff members from the same company should immediately regain the ability to use the coupon.
I have done all other logic implementation (authentication, authorization, coupon release, same company staff detected, price calculating,...) but still can not find out the best way how to handle the coupon usage as the business requirements.
Here are some solution that I can think of:
- Option 1: Utilizing socket technology to continuously update the coupon usage status. However, setting this up seems time-consuming. Is there a more efficient alternative?
- Option 2: Implementing periodic updates of the coupon usage status by calling an update usage status API at regular intervals (e.g., every 30 seconds). However, given that my system may have approximately 1000-2000 companies with numerous staff members, this solution may not be the most efficient.
The technologies used in my project include:
- Front-end: ReactJS
- Back-end: NestJS
- Database: PostgreSQL
I would appreciate any guidance or suggestions on how to implement this coupon management functionality effectively. Thank you!