Hello guys,
The title may be cloudy but here the scenario:
- Coupons are pre-saved
- Coupons are unique
- 1 coupon is only for 1 user
Here is the code for redeeming:
...
withTransaction(async (session) => {
...
const coupon = await this.storage.coupons
.findOneAndUpdate(
{
referralId: { $exists: false },
},
{ $set: { referralId: referralDoc.id } },
{ session, new: true },
)
.lean();
if (!coupon || !coupon.referralId) {
throw newError(ErrorCode.INVALID_REQUEST);
}
return { code: coupon.code, ok: true };
})
My problem: I am not sure whether that coupon does not conflict if there are many requests? What is the better approach if I dont want to use retry
?
Thank you all for reading. I’m looking forward to your question and response.