mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 15:25:43 +00:00
FIX[BE]Lock chickins, accumulate pending qty, use qty key
This commit is contained in:
@@ -68,11 +68,17 @@ func (r *ChickinRepositoryImpl) GetByProjectFlockKandangID(ctx context.Context,
|
||||
// GetByProjectFlockKandangIDForUpdate locks chickin rows to prevent race condition
|
||||
func (r *ChickinRepositoryImpl) GetByProjectFlockKandangIDForUpdate(ctx context.Context, projectFlockKandangID uint) ([]entity.ProjectChickin, error) {
|
||||
var chickins []entity.ProjectChickin
|
||||
// CRITICAL: Use FOR UPDATE to lock rows and prevent concurrent chickin requests
|
||||
// This ensures that simultaneous requests wait for each other and read consistent pending_qty
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("project_flock_kandang_id = ?", projectFlockKandangID).
|
||||
Order("created_at DESC").
|
||||
Find(&chickins).
|
||||
Error
|
||||
Raw(`
|
||||
SELECT * FROM project_chickins
|
||||
WHERE project_flock_kandang_id = ?
|
||||
AND deleted_at IS NULL
|
||||
ORDER BY created_at DESC
|
||||
FOR UPDATE
|
||||
`, projectFlockKandangID).
|
||||
Scan(&chickins).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user