updates on the admincontroller and the milercontroller and changes in the creaet single order calculations as well
This commit is contained in:
@@ -1945,17 +1945,51 @@ func CreateMiler(c *fiber.Ctx) error {
|
||||
return utils.BadRequest(c, "invalid request body")
|
||||
}
|
||||
|
||||
tx := db.DB.Begin()
|
||||
// The console form checks these too; the server is what every caller
|
||||
// (imports, scripts, other tools) actually goes through.
|
||||
req.Authname = strings.TrimSpace(req.Authname)
|
||||
req.Displayname = strings.TrimSpace(req.Displayname)
|
||||
req.Email = strings.ToLower(strings.TrimSpace(req.Email))
|
||||
req.Contactno = normalisePhone(req.Contactno)
|
||||
if req.Authname == "" {
|
||||
return utils.BadRequest(c, "enter the rider's login name")
|
||||
}
|
||||
if req.Displayname == "" {
|
||||
req.Displayname = req.Authname
|
||||
}
|
||||
if !indianMobile.MatchString(req.Contactno) {
|
||||
return utils.BadRequest(c, "enter a valid 10-digit Indian mobile number")
|
||||
}
|
||||
if req.Email == "" || !strings.Contains(req.Email, "@") {
|
||||
return utils.BadRequest(c, "enter a valid email address")
|
||||
}
|
||||
vehicle, ok := canonicalVehicleType(req.Defaultvehicletype)
|
||||
if !ok {
|
||||
return utils.BadRequest(c, "vehicle type must be one of "+strings.Join(milerVehicleTypes, ", "))
|
||||
}
|
||||
|
||||
appLocID := req.Applocationid
|
||||
if appLocID == 0 {
|
||||
appLocID = 1
|
||||
}
|
||||
var city models.AppLocation
|
||||
if err := db.DB.Where("applocationid = ?", appLocID).First(&city).Error; err != nil {
|
||||
return utils.BadRequest(c, "that city does not exist")
|
||||
}
|
||||
if msg := checkMilerHub(req.Hubid, appLocID); msg != "" {
|
||||
return utils.BadRequest(c, msg)
|
||||
}
|
||||
|
||||
// A client login may only create riders under its own tenant.
|
||||
tenantID := req.Tenantid
|
||||
if own := consoleTenantID(c); own != 0 {
|
||||
tenantID = own
|
||||
} else if tenantID != 0 {
|
||||
var n int64
|
||||
db.DB.Model(&models.Tenant{}).Where("tenantid = ?", tenantID).Count(&n)
|
||||
if n == 0 {
|
||||
return utils.BadRequest(c, "that client does not exist")
|
||||
}
|
||||
}
|
||||
|
||||
// Configid must match what LoginMiler looks up by — it queries
|
||||
@@ -1967,6 +2001,18 @@ func CreateMiler(c *fiber.Ctx) error {
|
||||
configID = 1001
|
||||
}
|
||||
|
||||
// One rider per phone number: login finds the rider by it.
|
||||
if milerPhoneTaken(req.Contactno, configID, 0) {
|
||||
return utils.Conflict(c, "a rider with this phone number already exists")
|
||||
}
|
||||
var emailUsers int64
|
||||
db.DB.Model(&models.AppUser{}).Where("LOWER(email) = ?", req.Email).Count(&emailUsers)
|
||||
if emailUsers > 0 {
|
||||
return utils.Conflict(c, "this email is already used by another login")
|
||||
}
|
||||
|
||||
tx := db.DB.Begin()
|
||||
|
||||
user := models.AppUser{
|
||||
Authname: req.Authname,
|
||||
Email: req.Email,
|
||||
@@ -1984,6 +2030,9 @@ func CreateMiler(c *fiber.Ctx) error {
|
||||
|
||||
if err := tx.Create(&user).Error; err != nil {
|
||||
tx.Rollback()
|
||||
if isUniqueViolation(err) {
|
||||
return utils.Conflict(c, "this email is already used by another login")
|
||||
}
|
||||
return utils.Internal(c, "failed to create miler account")
|
||||
}
|
||||
|
||||
@@ -1991,7 +2040,7 @@ func CreateMiler(c *fiber.Ctx) error {
|
||||
Userid: user.Userid,
|
||||
Displayname: req.Displayname,
|
||||
Phone: req.Contactno,
|
||||
Defaultvehicletype: req.Defaultvehicletype,
|
||||
Defaultvehicletype: vehicle,
|
||||
Availabilitystatus: constants.MilerOffline,
|
||||
Rating: 5.00,
|
||||
Applocationid: appLocID,
|
||||
@@ -2067,9 +2116,10 @@ func UpdateMiler(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
type MilerUpdate struct {
|
||||
Displayname string `json:"displayname"`
|
||||
Defaultvehicletype string `json:"defaultvehicletype"`
|
||||
Hubid *int `json:"hubid"`
|
||||
Displayname string `json:"displayname"`
|
||||
Defaultvehicletype string `json:"defaultvehicletype"`
|
||||
Hubid *int `json:"hubid"`
|
||||
Contactno *string `json:"contactno"`
|
||||
}
|
||||
|
||||
req := new(MilerUpdate)
|
||||
@@ -2077,18 +2127,53 @@ func UpdateMiler(c *fiber.Ctx) error {
|
||||
return utils.BadRequest(c, "invalid request body")
|
||||
}
|
||||
|
||||
if req.Displayname != "" {
|
||||
profile.Displayname = req.Displayname
|
||||
var user models.AppUser
|
||||
if err := db.DB.Where("userid = ?", profile.Userid).First(&user).Error; err != nil {
|
||||
return utils.NotFound(c, "miler not found")
|
||||
}
|
||||
|
||||
if name := strings.TrimSpace(req.Displayname); name != "" {
|
||||
profile.Displayname = name
|
||||
}
|
||||
if req.Defaultvehicletype != "" {
|
||||
profile.Defaultvehicletype = req.Defaultvehicletype
|
||||
vehicle, ok := canonicalVehicleType(req.Defaultvehicletype)
|
||||
if !ok {
|
||||
return utils.BadRequest(c, "vehicle type must be one of "+strings.Join(milerVehicleTypes, ", "))
|
||||
}
|
||||
profile.Defaultvehicletype = vehicle
|
||||
}
|
||||
if req.Hubid != nil {
|
||||
if req.Hubid != nil && (profile.Hubid == nil || *profile.Hubid != *req.Hubid) {
|
||||
// Only a CHANGED hub is checked: an older rider whose current hub is in
|
||||
// another city (or since deleted) must still be editable.
|
||||
if msg := checkMilerHub(req.Hubid, profile.Applocationid); msg != "" {
|
||||
return utils.BadRequest(c, msg)
|
||||
}
|
||||
profile.Hubid = req.Hubid
|
||||
}
|
||||
// The phone is the rider's login: a wrong or duplicate number could not be
|
||||
// corrected at all before, so the rider could never sign in.
|
||||
phone := user.Contactno
|
||||
if req.Contactno != nil {
|
||||
phone = normalisePhone(*req.Contactno)
|
||||
if !indianMobile.MatchString(phone) {
|
||||
return utils.BadRequest(c, "enter a valid 10-digit Indian mobile number")
|
||||
}
|
||||
if phone != user.Contactno && milerPhoneTaken(phone, user.Configid, user.Userid) {
|
||||
return utils.Conflict(c, "a rider with this phone number already exists")
|
||||
}
|
||||
profile.Phone = phone
|
||||
}
|
||||
profile.Updatedat = time.Now()
|
||||
|
||||
if err := db.DB.Save(profile).Error; err != nil {
|
||||
err := db.DB.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Save(profile).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// appusers carries the login phone and the hub the hub console reads.
|
||||
return tx.Model(&models.AppUser{}).Where("userid = ?", user.Userid).
|
||||
Updates(map[string]interface{}{"contactno": phone, "hubid": profile.Hubid}).Error
|
||||
})
|
||||
if err != nil {
|
||||
return utils.Internal(c, "failed to update miler")
|
||||
}
|
||||
return utils.OK(c, profile)
|
||||
|
||||
Reference in New Issue
Block a user