updates on the order bulk fix
This commit is contained in:
@@ -76,7 +76,7 @@ func tryCustomerAssign(bookingID int) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
miler, agentDecisionID, found := selectMilerWithAI(&booking, nearby)
|
||||
miler, pool, agentDecisionID, found := selectMilerWithAI(&booking, nearby)
|
||||
if !found {
|
||||
return false, nil
|
||||
}
|
||||
@@ -97,13 +97,14 @@ func tryCustomerAssign(bookingID int) (bool, error) {
|
||||
provider = providerResult{company: "Doormile"}
|
||||
}
|
||||
|
||||
// Step 3 — ETA based on miler-to-pickup distance.
|
||||
etaMinutes := calculateETA(miler.distanceKm)
|
||||
|
||||
// Steps 4–6 — Commit to DB and publish to NATS.
|
||||
if err := commitCustomerAssignment(&booking, miler, provider, etaMinutes, agentDecisionID); err != nil {
|
||||
if errors.Is(err, errBookingTaken) {
|
||||
// Steps 3–6 — final balance check, ETA from the chosen rider's distance,
|
||||
// commit to DB and publish to NATS.
|
||||
if err := commitCustomerAssignment(&booking, miler, pool, provider, agentDecisionID); err != nil {
|
||||
switch {
|
||||
case errors.Is(err, errBookingTaken):
|
||||
return true, nil
|
||||
case errors.Is(err, errNoRiderCapacity):
|
||||
return false, nil // everyone filled up meanwhile; retry later
|
||||
}
|
||||
return false, fmt.Errorf("commit: %w", err)
|
||||
}
|
||||
@@ -188,14 +189,25 @@ func calculateETA(distanceKm float64) float64 {
|
||||
func commitCustomerAssignment(
|
||||
booking *models.PickupBooking,
|
||||
miler *milerCandidate,
|
||||
pool []*milerCandidate,
|
||||
provider providerResult,
|
||||
etaMinutes float64,
|
||||
agentDecisionID *uint64,
|
||||
) error {
|
||||
milerUserID := miler.profile.Userid
|
||||
|
||||
tx := db.DB.Begin()
|
||||
|
||||
// Final balance check under the decision lock (finalizeChoice).
|
||||
final, err := finalizeChoice(tx, miler, pool)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
if final != miler {
|
||||
agentDecisionID = nil
|
||||
}
|
||||
miler = final
|
||||
milerUserID := miler.profile.Userid
|
||||
etaMinutes := calculateETA(miler.distanceKm)
|
||||
|
||||
bookingUpdates := map[string]interface{}{
|
||||
"assignedmileruserid": milerUserID,
|
||||
"status": constants.BookingMilerAssigned,
|
||||
@@ -216,6 +228,7 @@ func commitCustomerAssignment(
|
||||
Assignmentstatus: constants.AssignmentAssigned,
|
||||
Assignedat: time.Now(),
|
||||
AgentDecisionID: agentDecisionID,
|
||||
Remarks: autoAssignedRemark,
|
||||
}
|
||||
if err := tx.Create(&ba).Error; err != nil {
|
||||
tx.Rollback()
|
||||
|
||||
Reference in New Issue
Block a user