itinerary.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package model
  2. import (
  3. "time"
  4. )
  5. // Leg describes the transportation between two locations on a voyage
  6. type Leg struct {
  7. VoyageNumber VoyageNumber `json:"voyage_number"`
  8. LoadLocation UNLocode `json:"from"`
  9. UnloadLocation UNLocode `json:"to"`
  10. LoadTime time.Time `json:"load_time"`
  11. UnloadTime time.Time `json:"unload_time"`
  12. }
  13. // NewLeg creates a new itinerary leg.
  14. func NewLeg(voyageNumber VoyageNumber, loadLocation, unloadLocation UNLocode, loadTime, unloadTime time.Time) Leg {
  15. return Leg{
  16. VoyageNumber: voyageNumber,
  17. LoadLocation: loadLocation,
  18. UnloadLocation: unloadLocation,
  19. LoadTime: loadTime,
  20. UnloadTime: unloadTime,
  21. }
  22. }
  23. // Itinerary specifies steps required to transport a cargo from its origin to
  24. // destination.
  25. type Itinerary struct {
  26. Legs []Leg `json:"legs"`
  27. }
  28. // InitialDepartureLocation returns the start of the itinerary.
  29. func (i Itinerary) InitialDepartureLocation() UNLocode {
  30. if i.IsEmpty() {
  31. return UNLocode("")
  32. }
  33. return i.Legs[0].LoadLocation
  34. }
  35. // FinalArrivalLocation returns the end of the itinerary.
  36. func (i Itinerary) FinalArrivalLocation() UNLocode {
  37. if i.IsEmpty() {
  38. return UNLocode("")
  39. }
  40. return i.Legs[len(i.Legs)-1].UnloadLocation
  41. }
  42. // FinalArrivalTime returns the expected arrival time at final destination.
  43. func (i Itinerary) FinalArrivalTime() time.Time {
  44. return i.Legs[len(i.Legs)-1].UnloadTime
  45. }
  46. // IsEmpty checks if the itinerary contains at least one leg.
  47. func (i Itinerary) IsEmpty() bool {
  48. return i.Legs == nil || len(i.Legs) == 0
  49. }
  50. // IsExpected checks if the given handling event is expected when executing
  51. // this itinerary.
  52. func (i Itinerary) IsExpected(event HandlingEvent) bool {
  53. if i.IsEmpty() {
  54. return true
  55. }
  56. switch event.Activity.Type {
  57. case Receive:
  58. return i.InitialDepartureLocation() == event.Activity.Location
  59. case Load:
  60. for _, l := range i.Legs {
  61. if l.LoadLocation == event.Activity.Location && l.VoyageNumber == event.Activity.VoyageNumber {
  62. return true
  63. }
  64. }
  65. return false
  66. case Unload:
  67. for _, l := range i.Legs {
  68. if l.UnloadLocation == event.Activity.Location && l.VoyageNumber == event.Activity.VoyageNumber {
  69. return true
  70. }
  71. }
  72. return false
  73. case Claim:
  74. return i.FinalArrivalLocation() == event.Activity.Location
  75. }
  76. return true
  77. }