diff --git a/repositories/deliveriesRepository.go b/repositories/deliveriesRepository.go index d89158f..f4abb7b 100644 --- a/repositories/deliveriesRepository.go +++ b/repositories/deliveriesRepository.go @@ -197,6 +197,12 @@ func (r *deliveriesRepository) CreateDeliveries(data []models.Deliveries) error return nil } +// stampNow is the one clock these tables are written from: server local time, +// in the layout every lifecycle column already uses. +func stampNow() string { + return time.Now().Format("2006-01-02 15:04:05") +} + func (r *deliveriesRepository) UpdateDelivery(data models.UpdateDeliveryStatus) error { var ord models.Updateorderstatus var cloc models.Customerlocations @@ -210,6 +216,52 @@ func (r *deliveriesRepository) UpdateDelivery(data models.UpdateDeliveryStatus) return tx.Error } + // The lifecycle timestamp on the DELIVERY row, stamped when the caller does + // not send one. + // + // This is why the journey timings were blank. `Updates` with a struct skips + // zero-valued fields, and the rider app sends {deliveryid, orderstatus} and + // nothing else — so starttime, arrivaltime, pickuptime and deliverytime were + // only ever written by a client that volunteered them, and none does. + // Measured on live tenant 1147: every delivery row carries an assigntime + // (written at assign) and an empty string in all five of the others, so the + // step-by-step timings in the console had nothing to show at any stage. + // + // The order side of this was already fixed — see `stamp` below, which does + // exactly this for the mirrored order columns. The delivery's own row was + // left out, which is the half an operator actually looks at. + // + // Same format as the column already holds ("2006-01-02 15:04:05", server + // local): a second convention in one column would be worse than none. + if now := stampNow(); now != "" { + switch data.Orderstatus { + case "pending": + if strings.TrimSpace(data.Assigntime) == "" { + data.Assigntime = now + } + case "accepted": + if strings.TrimSpace(data.Starttime) == "" { + data.Starttime = now + } + case "arrived": + if strings.TrimSpace(data.Arrivaltime) == "" { + data.Arrivaltime = now + } + case "picked": + if strings.TrimSpace(data.Pickuptime) == "" { + data.Pickuptime = now + } + case "delivered": + if strings.TrimSpace(data.Deliverytime) == "" { + data.Deliverytime = now + } + case "cancelled": + if strings.TrimSpace(data.Canceltime) == "" { + data.Canceltime = now + } + } + } + if err := tx.Table("deliveries").Where("deliveryid = ?", data.Deliveryid).Updates(&data).Error; err != nil { tx.Rollback() return err @@ -262,7 +314,7 @@ func (r *deliveriesRepository) UpdateDelivery(data models.UpdateDeliveryStatus) if strings.TrimSpace(supplied) != "" { return supplied } - return time.Now().Format("2006-01-02 15:04:05") + return stampNow() } switch data.Orderstatus {