diff --git a/controllers/milerController.go b/controllers/milerController.go index 6e1f11e..0c745d6 100644 --- a/controllers/milerController.go +++ b/controllers/milerController.go @@ -68,6 +68,23 @@ func LoginMiler(cfg *config.Config) fiber.Handler { } } +// resolveTenantName returns the tenant's display name for a tenantid, or "" +// when the id is unset/unknown. The miler app resolves a rider's service +// profile (hyperlocal vs logistics) from this name in preference to the raw +// tenantid, so it must be present on the login and profile responses — a rider +// whose tenant we don't name falls back to id-matching, and any unmapped id +// falls back to logistics (no Start-delivery button). Cheap single-row lookup. +func resolveTenantName(tenantID int) string { + if tenantID == 0 { + return "" + } + var t models.Tenant + if err := db.DB.Select("tenantname").Where("tenantid = ?", tenantID).First(&t).Error; err != nil { + return "" + } + return t.Tenantname +} + func VerifyMilerPin(cfg *config.Config) fiber.Handler { return func(c *fiber.Ctx) error { req := new(dto.MilerPinVerifyRequest) @@ -123,15 +140,24 @@ func VerifyMilerPin(cfg *config.Config) fiber.Handler { db.DB.Model(&profile).Update("device_token", req.DeviceToken) } + // tenantname drives the app's service-profile resolution (hyperlocal vs + // logistics), checked ahead of the raw tenantid. Persisted client-side at + // verify-pin, so a rider picks up a changed tenant name on next login. + tenantName := resolveTenantName(user.Tenantid) + return c.JSON(fiber.Map{ - "success": true, - "token": token, + "success": true, + "token": token, + "tenantid": user.Tenantid, + "tenantname": tenantName, "user": fiber.Map{ - "userid": user.Userid, - "authname": user.Authname, - "email": user.Email, - "contactno": user.Contactno, - "profile": profile, + "userid": user.Userid, + "authname": user.Authname, + "email": user.Email, + "contactno": user.Contactno, + "tenantid": user.Tenantid, + "tenantname": tenantName, + "profile": profile, }, }) } @@ -192,12 +218,16 @@ func GetMilerProfile(c *fiber.Ctx) error { return utils.NotFound(c, "miler profile not found") } + // tenantname is included here too so the app can refresh it at launch via + // GET /miler/profile without forcing a re-login after the tenantname rollout. return utils.OK(c, fiber.Map{ - "userid": user.Userid, - "authname": user.Authname, - "email": user.Email, - "contactno": user.Contactno, - "profile": profile, + "userid": user.Userid, + "authname": user.Authname, + "email": user.Email, + "contactno": user.Contactno, + "tenantid": user.Tenantid, + "tenantname": resolveTenantName(user.Tenantid), + "profile": profile, }) }