Skip to content

Commit

Permalink
Allow states other than unassinged at shipment creation (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
harsimranmaan authored Jun 22, 2022
1 parent 8827c49 commit eef0f93
Show file tree
Hide file tree
Showing 17 changed files with 245 additions and 46 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ API_VERSION = 0.1.0
.PHONY: gen
gen:
# Modify the publish action if needed when changing this
cd sdk; rm -rf trober_sdk; dart pub get; dart pub run build_runner clean; dart pub run build_runner build --delete-conflicting-outputs; dart pub run cider --project-root=trober_sdk version $(API_VERSION); dart format .; cp LICENSE trober_sdk/LICENSE; echo "The trober SDK for dart. It provides a dio client for the trober API." > trober_sdk/README.md; echo "See https://github.com/bigpanther/trober/releases/tag/v$(API_VERSION)" > trober_sdk/CHANGELOG.md
cd sdk
npx @openapitools/openapi-generator-cli generate -i trober.yaml -c config.yaml -g dart-dio -o trober_sdk -p pubVersion=$(API_VERSION)
cp LICENSE trober_sdk/LICENSE
echo "See https://github.com/bigpanther/trober/releases/tag/v$(API_VERSION)" > trober_sdk/CHANGELOG.md
cp README.md trober_sdk/README.md

.PHONY: publish
publish: gen
Expand Down
5 changes: 2 additions & 3 deletions actions/carriers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package actions
import (
"fmt"
"net/http"
"strings"
"time"

"github.com/bigpanther/trober/models"
Expand All @@ -21,7 +20,7 @@ import (
// GET /carriers
func carriersList(c buffalo.Context) error {
tx := c.Value("tx").(*pop.Connection)
carrierName := strings.Trim(c.Param("name"), " '")
carrierName := c.Param("name")
carrierType := c.Param("type")
carriers := &models.Carriers{}
// Paginate results. Params "page" and "per_page" control pagination.
Expand All @@ -32,7 +31,7 @@ func carriersList(c buffalo.Context) error {
if len(carrierName) < 2 {
return c.Render(http.StatusOK, r.JSON(carriers))
}
q = q.Where("name ILIKE ?", fmt.Sprintf("%s%%", carrierName))
q = q.Where("name ILIKE ?", fmt.Sprintf("%%%s%%", carrierName))
}
if carrierType != "" {
q = q.Where("type = ?", carrierType)
Expand Down
5 changes: 2 additions & 3 deletions actions/customers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package actions
import (
"fmt"
"net/http"
"strings"
"time"

"github.com/bigpanther/trober/models"
Expand All @@ -22,7 +21,7 @@ import (
// GET /customers
func customersList(c buffalo.Context) error {
tx := c.Value("tx").(*pop.Connection)
customerName := strings.Trim(c.Param("name"), " '")
customerName := c.Param("name")

customers := &models.Customers{}

Expand All @@ -33,7 +32,7 @@ func customersList(c buffalo.Context) error {
if len(customerName) < 2 {
return c.Render(http.StatusOK, r.JSON(customers))
}
q = q.Where("name ILIKE ?", fmt.Sprintf("%s%%", customerName))
q = q.Where("name ILIKE ?", fmt.Sprintf("%%%s%%", customerName))
}
// Retrieve all Customers from the DB
if err := q.Scope(restrictedScope(c)).Order(orderByCreatedAtDesc).All(customers); err != nil {
Expand Down
55 changes: 48 additions & 7 deletions actions/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"errors"
"fmt"
"net/http"
"strings"
"time"

"github.com/bigpanther/trober/models"
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/pop/v6"
"github.com/gofrs/uuid"
)

// Following naming logic is implemented in Buffalo:
Expand All @@ -23,7 +23,7 @@ import (
func ordersList(c buffalo.Context) error {
var loggedInUser = loggedInUser(c)
tx := c.Value("tx").(*pop.Connection)
orderSerialNumber := strings.Trim(c.Param("serial_number"), " '")
orderSerialNumber := c.Param("serial_number")
orderStatus := c.Param("status")
orders := &models.Orders{}

Expand All @@ -35,7 +35,7 @@ func ordersList(c buffalo.Context) error {
if len(orderSerialNumber) < 2 {
return c.Render(http.StatusOK, r.JSON(orders))
}
q = q.Where("serial_number ILIKE ?", fmt.Sprintf("%s%%", orderSerialNumber))
q = q.Where("serial_number ILIKE ?", fmt.Sprintf("%%%s%%", orderSerialNumber))
}
customerID := c.Param("customer_id")
if loggedInUser.IsCustomer() {
Expand Down Expand Up @@ -84,6 +84,11 @@ func ordersShow(c buffalo.Context) error {
return c.Error(http.StatusNotFound, err)
}

shipmentsCount, err := shipmentsCount(c, tx, order.ID)
if err != nil {
return err
}
order.ShipmentCount = shipmentsCount
return c.Render(http.StatusOK, r.JSON(order))

}
Expand All @@ -105,23 +110,37 @@ func ordersCreate(c buffalo.Context) error {
order.Status = models.OrderStatusOpen.String()
order.TenantID = loggedInUser.TenantID
order.CreatedBy = loggedInUser.ID
if order.Type == "" {
order.Type = models.ShipmentTypeInbound.String()
}

tx := c.Value("tx").(*pop.Connection)
if loggedInUser.IsCustomer() {
order.CustomerID = loggedInUser.CustomerID.UUID
} else if err := checkCustomerID(c, tx, loggedInUser, order); err != nil {
return c.Error(http.StatusBadRequest, err)
}

verrs, err := tx.ValidateAndCreate(order)
if err := checkCarrierID(c, tx, loggedInUser, order.CarrierID); err != nil {
return c.Error(http.StatusBadRequest, err)
}
for i := range order.Shipments {
order.Shipments[i].TenantID = order.TenantID
order.Shipments[i].Type = order.Type
order.Shipments[i].CreatedBy = loggedInUser.ID
}
verrs, err := tx.Eager().ValidateAndCreate(order)
if err != nil {
return err
}

if verrs.HasAny() {
return c.Render(http.StatusUnprocessableEntity, r.JSON(verrs))
}

shipmentsCount, err := shipmentsCount(c, tx, order.ID)
if err != nil {
return err
}
order.ShipmentCount = shipmentsCount
return c.Render(http.StatusCreated, r.JSON(order))

}
Expand All @@ -143,8 +162,22 @@ func ordersUpdate(c buffalo.Context) error {

return err
}
if newOrder.SerialNumber != order.SerialNumber || newOrder.Status != order.Status {
if newOrder.SerialNumber != order.SerialNumber || newOrder.Status != order.Status || newOrder.Eta != order.Eta || order.Docco != newOrder.Docco || order.ContainterStatus != newOrder.ContainterStatus || order.CarrierID != newOrder.CarrierID || order.TerminalID != newOrder.TerminalID || order.DropoffCharges != newOrder.DropoffCharges || order.DropoffCost != newOrder.DropoffCost || order.PickupCharges != newOrder.PickupCharges || order.PickupCost != newOrder.PickupCost || order.Rld != newOrder.Rld || order.Shipline != newOrder.Shipline || order.Erd != newOrder.Erd || order.Lfd != newOrder.Lfd || order.SoNumber != newOrder.SoNumber {
order.UpdatedAt = time.Now().UTC()
order.Eta = newOrder.Eta
order.Docco = newOrder.Docco
order.ContainterStatus = newOrder.ContainterStatus
order.CarrierID = newOrder.CarrierID
order.TerminalID = newOrder.TerminalID
order.DropoffCharges = newOrder.DropoffCharges
order.DropoffCost = newOrder.DropoffCost
order.PickupCharges = newOrder.PickupCharges
order.PickupCost = newOrder.PickupCost
order.Rld = newOrder.Rld
order.Shipline = newOrder.Shipline
order.Erd = newOrder.Erd
order.Lfd = newOrder.Lfd
order.SoNumber = newOrder.SoNumber
order.SerialNumber = newOrder.SerialNumber
order.Status = newOrder.Status
} else {
Expand Down Expand Up @@ -189,3 +222,11 @@ func checkCustomerID(c buffalo.Context, tx *pop.Connection, loggedInUser *models
}
return nil
}
func shipmentsCount(c buffalo.Context, tx *pop.Connection, orderID uuid.UUID) (int, error) {
shipmentsCount, err := tx.Scope(restrictedScope(c)).Where("order_id = ?", orderID).Count(&models.Shipments{})
if err != nil {
c.Logger().Errorf("error retrieving shipment count for order: %v\n", err)
return 0, err
}
return shipmentsCount, err
}
28 changes: 26 additions & 2 deletions actions/orders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ func (as *ActionSuite) Test_OrdersList() {
as.Equal(test.orderCount, len(orders))
if test.orderCount > 0 {
as.Equal(newOrder.SerialNumber, orders[0].SerialNumber)
as.Equal("SO123", orders[0].SoNumber.String)
// Issue with golang time precision
//as.Equal(newOrder.Erd.Time.UTC(), orders[0].Erd.Time.UTC())
as.Equal(0, orders[0].ShipmentCount)
as.Equal(10000, orders[0].DropoffCost.Int)
}
}
})
Expand Down Expand Up @@ -149,7 +154,26 @@ func (as *ActionSuite) Test_OrdersShow() {
})
}
}

func (as *ActionSuite) Test_OrdersCreateWithShipments() {
as.LoadFixture("Tenant bootstrap")
var firmino = as.getLoggedInUser("firmino")
efaLiv := as.getCustomer("EFA Liv")
newOrder := models.Order{SerialNumber: "orderWithShipments", Status: models.OrderStatusAccepted.String(), TenantID: firmino.TenantID, CustomerID: efaLiv.ID}
newOrder.Shipments = []models.Shipment{
{
SerialNumber: "123",
TenantID: firmino.TenantID,
Status: string(models.ShipmentStatusUnassigned),
Type: string(models.ShipmentTypeInbound),
},
}
req := as.setupRequest(firmino, "/orders")
res := req.Post(newOrder)
as.Equal(http.StatusCreated, res.Code)
var order = models.Order{}
res.Bind(&order)
as.Equal(1, order.ShipmentCount)
}
func (as *ActionSuite) Test_OrdersCreate() {
as.LoadFixture("Tenant bootstrap")
var tests = []struct {
Expand Down Expand Up @@ -262,7 +286,7 @@ func (as *ActionSuite) Test_OrdersDestroy() {
for _, test := range tests {
as.T().Run(test.username, func(t *testing.T) {
var name = fmt.Sprintf("order%s", test.username)
neworder := &models.Order{SerialNumber: name, Status: models.OrderStatusOpen.String(), TenantID: firmino.TenantID, CustomerID: customer.ID, CreatedBy: firmino.ID}
neworder := &models.Order{SerialNumber: name, Status: models.OrderStatusOpen.String(), TenantID: firmino.TenantID, CustomerID: customer.ID, CreatedBy: firmino.ID, Type: string(models.ShipmentTypeInbound)}
v, err := as.DB.ValidateAndCreate(neworder)
as.Nil(err)
as.Equal(0, len(v.Errors))
Expand Down
12 changes: 8 additions & 4 deletions actions/shipments.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"net/http"
"strings"
"time"

"github.com/bigpanther/trober/firebase"
Expand All @@ -28,7 +27,7 @@ func shipmentsList(c buffalo.Context) error {

tx := c.Value("tx").(*pop.Connection)

shipmentSerialNumber := strings.Trim(c.Param("serial_number"), " '")
shipmentSerialNumber := c.Param("serial_number")
shipmentType := c.Param("type")
shipmentSize := c.Param("size")
shipmentStatus := c.Param("status")
Expand All @@ -43,7 +42,7 @@ func shipmentsList(c buffalo.Context) error {
if len(shipmentSerialNumber) < 2 {
return c.Render(http.StatusOK, r.JSON(shipments))
}
q = q.Where("serial_number ILIKE ?", fmt.Sprintf("%s%%", shipmentSerialNumber))
q = q.Where("serial_number ILIKE ?", fmt.Sprintf("%%%s%%", shipmentSerialNumber))
}
if shipmentType != "" {
q = q.Where("type = ?", shipmentType)
Expand Down Expand Up @@ -117,7 +116,6 @@ func shipmentsCreate(c buffalo.Context) error {
}
tx := c.Value("tx").(*pop.Connection)
var loggedInUser = loggedInUser(c)
shipment.Status = models.ShipmentStatusUnassigned.String()
shipment.TenantID = loggedInUser.TenantID
shipment.CreatedBy = loggedInUser.ID

Expand All @@ -131,6 +129,12 @@ func shipmentsCreate(c buffalo.Context) error {
} else if err := checkDriverID(c, tx, loggedInUser, shipment.DriverID); err != nil {
return c.Error(http.StatusBadRequest, err)
}
if shipment.DriverID.Valid && shipment.Status == models.ShipmentStatusUnassigned.String() {
shipment.Status = models.ShipmentStatusAssigned.String()
}
if shipment.Status == "" {
shipment.Status = models.ShipmentStatusUnassigned.String()
}
if err := checkTerminalID(c, tx, loggedInUser, shipment.TerminalID); err != nil {
return c.Error(http.StatusBadRequest, err)
}
Expand Down
2 changes: 1 addition & 1 deletion actions/shipments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (as *ActionSuite) Test_ShipmentsCreate() {
var shipment = models.Shipment{}
res.Bind(&shipment)
as.Equal(newShipment.SerialNumber, shipment.SerialNumber)
as.Equal(models.ShipmentStatusUnassigned.String(), shipment.Status)
as.Equal(models.ShipmentStatusDelivered.String(), shipment.Status)
as.Equal(models.ShipmentTypeInbound.String(), shipment.Type)
as.Equal(user.TenantID, shipment.TenantID)
as.Equal(order.ID, shipment.OrderID.UUID)
Expand Down
5 changes: 2 additions & 3 deletions actions/tenants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"net/http"
"strings"
"time"

"github.com/bigpanther/trober/models"
Expand All @@ -29,7 +28,7 @@ var errNotFound = errors.New(http.StatusText(http.StatusNotFound))
// GET /tenants
func tenantsList(c buffalo.Context) error {
tx := c.Value("tx").(*pop.Connection)
tenantName := strings.Trim(c.Param("name"), " '")
tenantName := c.Param("name")
tenantType := c.Param("type")
tenants := &models.Tenants{}

Expand All @@ -40,7 +39,7 @@ func tenantsList(c buffalo.Context) error {
if len(tenantName) < 2 {
return c.Render(http.StatusOK, r.JSON(tenants))
}
q = q.Where("name ILIKE ?", fmt.Sprintf("%s%%", tenantName))
q = q.Where("name ILIKE ?", fmt.Sprintf("%%%s%%", tenantName))
}
if tenantType != "" {
q = q.Where("type = ?", tenantType)
Expand Down
5 changes: 2 additions & 3 deletions actions/terminals.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package actions
import (
"fmt"
"net/http"
"strings"
"time"

"github.com/bigpanther/trober/models"
Expand All @@ -22,7 +21,7 @@ import (
func terminalsList(c buffalo.Context) error {

tx := c.Value("tx").(*pop.Connection)
terminalName := strings.Trim(c.Param("name"), " '")
terminalName := c.Param("name")
terminalType := c.Param("type")
terminals := &models.Terminals{}

Expand All @@ -34,7 +33,7 @@ func terminalsList(c buffalo.Context) error {
if len(terminalName) < 2 {
return c.Render(http.StatusOK, r.JSON(terminals))
}
q = q.Where("name ILIKE ?", fmt.Sprintf("%s%%", terminalName))
q = q.Where("name ILIKE ?", fmt.Sprintf("%%%s%%", terminalName))
}
if terminalType != "" {
q = q.Where("type = ?", terminalType)
Expand Down
5 changes: 2 additions & 3 deletions actions/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"math/rand"
"net/http"
"strings"
"time"

"github.com/bigpanther/trober/models"
Expand All @@ -26,7 +25,7 @@ import (
func usersList(c buffalo.Context) error {

tx := c.Value("tx").(*pop.Connection)
userName := strings.Trim(c.Param("name"), " '")
userName := c.Param("name")
userRole := c.Param("role")
users := &models.Users{}

Expand All @@ -38,7 +37,7 @@ func usersList(c buffalo.Context) error {
if len(userName) < 2 {
return c.Render(http.StatusOK, r.JSON(users))
}
q = q.Where("name ILIKE ?", fmt.Sprintf("%s%%", userName))
q = q.Where("name ILIKE ?", fmt.Sprintf("%%%s%%", userName))
}
if userRole != "" {
q = q.Where("role = ?", userRole)
Expand Down
15 changes: 15 additions & 0 deletions actions/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package actions

import (
"time"

"github.com/bigpanther/trober/models"
"github.com/gobuffalo/httptest"
"github.com/gobuffalo/nulls"
Expand Down Expand Up @@ -64,6 +66,19 @@ func (as *ActionSuite) createCustomer(name string, tenantID uuid.UUID, createdBy
}
func (as *ActionSuite) createOrder(serialNumber string, orderStatus models.OrderStatus, tenantID uuid.UUID, createdBy uuid.UUID, customerID uuid.UUID) *models.Order {
newOrder := &models.Order{SerialNumber: serialNumber, Status: orderStatus.String(), TenantID: tenantID, CreatedBy: createdBy, CustomerID: customerID}
newOrder.ContainterStatus = nulls.NewString("Custom Status")
newOrder.Docco = nulls.NewTime(time.Now().Add(time.Duration(time.Hour * time.Duration(1))))
newOrder.Eta = nulls.NewTime(time.Now().Add(time.Duration(time.Hour * time.Duration(3))))
newOrder.Erd = nulls.NewTime(time.Now().Add(time.Duration(time.Hour * time.Duration(4))))
newOrder.Shipline = nulls.NewString("Costco")
newOrder.ShipmentCount = 2 //readonly, not persisted
newOrder.PickupCharges = nulls.NewInt(0)
newOrder.DropoffCharges = nulls.NewInt(1)
newOrder.PickupCost = nulls.NewInt(1000)
newOrder.DropoffCost = nulls.NewInt(10000)
newOrder.SoNumber = nulls.NewString("SO123")
newOrder.Rld = nulls.NewString("RLD")
newOrder.Type = models.ShipmentTypeInbound.String()
v, err := as.DB.ValidateAndCreate(newOrder)
as.Nil(err)
as.Equal(0, len(v.Errors))
Expand Down
Loading

0 comments on commit eef0f93

Please sign in to comment.