fix: prevent race condition on order subtotal calculation

This commit is contained in:
Efril
2026-06-03 23:59:15 +07:00
parent afa1aa5b75
commit ea9dceb333
3 changed files with 44 additions and 5 deletions
+13
View File
@@ -2,6 +2,7 @@ package repository
import (
"context"
"database/sql"
"gorm.io/gorm"
)
@@ -37,3 +38,15 @@ func (m *TxManager) WithTransaction(ctx context.Context, fn func(ctx context.Con
return fn(ctxTx)
})
}
// WithTransactionOptions runs fn inside a DB transaction with custom TxOptions (e.g. isolation level).
func (m *TxManager) WithTransactionOptions(ctx context.Context, opts *sql.TxOptions, fn func(ctx context.Context) error) error {
if m == nil || m.db == nil {
return fn(ctx)
}
return m.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
ctxTx := context.WithValue(ctx, txKey, tx)
return fn(ctxTx)
}, opts)
}