rider creation
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"nearle/models"
|
||||
"nearle/services"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@@ -193,3 +194,119 @@ func (ctl *PartnerController) GetRiderInfo(c *fiber.Ctx) error {
|
||||
"details": result,
|
||||
})
|
||||
}
|
||||
|
||||
// CreateRider hires a rider — three tables in one transaction.
|
||||
//
|
||||
// `tenantid` comes from the query string, which is where this console's other
|
||||
// scoped writes take it from. It is required: a rider with no tenant belongs to
|
||||
// the region and to no merchant, which is the state the 84 existing riders are
|
||||
// already in and not one worth creating more of.
|
||||
func (ctl *PartnerController) CreateRider(c *fiber.Ctx) error {
|
||||
var rider models.NewRider
|
||||
|
||||
if err := c.BodyParser(&rider); err != nil {
|
||||
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
|
||||
"code": http.StatusBadRequest,
|
||||
"status": false,
|
||||
"message": "Invalid request body",
|
||||
})
|
||||
}
|
||||
|
||||
// Taken from the scope rather than trusted from the body. A store admin
|
||||
// must not be able to put a rider on another merchant's books by editing a
|
||||
// payload, and the body is the caller's to edit.
|
||||
if tid, _ := strconv.Atoi(c.Query("tenantid")); tid != 0 {
|
||||
rider.Tenantid = tid
|
||||
}
|
||||
if rider.Tenantid == 0 {
|
||||
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
|
||||
"code": http.StatusBadRequest,
|
||||
"status": false,
|
||||
"message": "tenantid is required",
|
||||
})
|
||||
}
|
||||
|
||||
userid, err := ctl.partnerService.CreateRider(rider)
|
||||
if err != nil {
|
||||
// 400, not 500: every failure this can produce is something the caller
|
||||
// sent — a shift that does not exist, an unconfigured region, a phone
|
||||
// number already in use. Answering 500 would send them to look at the
|
||||
// server for a problem in their own form.
|
||||
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
|
||||
"code": http.StatusBadRequest,
|
||||
"status": false,
|
||||
"message": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.Status(http.StatusCreated).JSON(fiber.Map{
|
||||
"code": http.StatusCreated,
|
||||
"status": true,
|
||||
// Said plainly because it is the single most confusing thing about this
|
||||
// flow: the rider is created and will NOT show in the on-duty fleet
|
||||
// until they open the app and start a shift.
|
||||
"message": "Rider created. They appear in the fleet once they sign in and start a shift.",
|
||||
"details": fiber.Map{"userid": userid},
|
||||
})
|
||||
}
|
||||
|
||||
func (ctl *PartnerController) UpdateRider(c *fiber.Ctx) error {
|
||||
var rider models.NewRider
|
||||
|
||||
if err := c.BodyParser(&rider); err != nil {
|
||||
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
|
||||
"code": http.StatusBadRequest,
|
||||
"status": false,
|
||||
"message": "Invalid request body",
|
||||
})
|
||||
}
|
||||
|
||||
if err := ctl.partnerService.UpdateRider(rider); err != nil {
|
||||
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
|
||||
"code": http.StatusBadRequest,
|
||||
"status": false,
|
||||
"message": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"code": http.StatusOK,
|
||||
"status": true,
|
||||
"message": "Success",
|
||||
})
|
||||
}
|
||||
|
||||
// GetRiderRoster lists riders whether or not they are working today.
|
||||
//
|
||||
// The scope is required. Unscoped this returns every rider on the platform,
|
||||
// which is not a merchant's business — the same guard the other tenant-scoped
|
||||
// reads carry.
|
||||
func (ctl *PartnerController) GetRiderRoster(c *fiber.Ctx) error {
|
||||
tid, _ := strconv.Atoi(c.Query("tenantid"))
|
||||
aid, _ := strconv.Atoi(c.Query("applocationid"))
|
||||
pid, _ := strconv.Atoi(c.Query("partnerid"))
|
||||
|
||||
if tid == 0 && aid == 0 && pid == 0 {
|
||||
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
|
||||
"code": http.StatusBadRequest,
|
||||
"status": false,
|
||||
"message": "One of tenantid, applocationid or partnerid is required",
|
||||
})
|
||||
}
|
||||
|
||||
result, err := ctl.partnerService.GetRiderRoster(tid, aid, pid)
|
||||
if err != nil {
|
||||
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
|
||||
"code": http.StatusInternalServerError,
|
||||
"status": false,
|
||||
"message": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"code": http.StatusOK,
|
||||
"status": true,
|
||||
"message": "Successful",
|
||||
"details": result,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user