@fleetbase/fleetops-engine 0.4.16 → 0.4.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (225) hide show
  1. package/.php-cs-fixer.php +2 -0
  2. package/addon/components/activity-event-selector.hbs +35 -0
  3. package/addon/components/activity-event-selector.js +85 -0
  4. package/addon/components/activity-form-panel.hbs +23 -0
  5. package/addon/components/activity-form-panel.js +90 -0
  6. package/addon/components/activity-logic-builder.hbs +80 -0
  7. package/addon/components/activity-logic-builder.js +260 -0
  8. package/addon/components/contact-form-panel.hbs +1 -1
  9. package/addon/components/contact-panel.hbs +1 -1
  10. package/addon/components/custom-entity-form-panel.hbs +43 -0
  11. package/addon/components/custom-entity-form-panel.js +129 -0
  12. package/addon/components/custom-field-form-panel/options-input.hbs +12 -0
  13. package/addon/components/custom-field-form-panel/options-input.js +139 -0
  14. package/addon/components/custom-field-form-panel.hbs +77 -0
  15. package/addon/components/custom-field-form-panel.js +144 -0
  16. package/addon/components/custom-field.hbs +63 -41
  17. package/addon/components/custom-field.js +123 -25
  18. package/addon/components/driver-form-panel.hbs +1 -1
  19. package/addon/components/driver-panel.hbs +1 -1
  20. package/addon/components/edit-order-route-panel.hbs +1 -1
  21. package/addon/components/fleet-form-panel.hbs +1 -1
  22. package/addon/components/fleet-panel.hbs +1 -1
  23. package/addon/components/fuel-report-form-panel.hbs +1 -1
  24. package/addon/components/fuel-report-panel.hbs +1 -1
  25. package/addon/components/issue-form-panel.hbs +1 -1
  26. package/addon/components/issue-panel.hbs +1 -1
  27. package/addon/components/joint-graph.hbs +1 -0
  28. package/addon/components/joint-graph.js +103 -0
  29. package/addon/components/layout/fleet-ops-sidebar.hbs +3 -3
  30. package/addon/components/layout/fleet-ops-sidebar.js +1 -0
  31. package/addon/components/live-map.js +71 -28
  32. package/addon/components/modals/new-custom-field-group.hbs +5 -0
  33. package/addon/components/modals/new-order-config.hbs +3 -3
  34. package/addon/components/modals/order-new-activity.hbs +1 -1
  35. package/addon/components/order-config-manager/activity-flow.hbs +11 -0
  36. package/addon/components/order-config-manager/activity-flow.js +964 -0
  37. package/addon/components/order-config-manager/custom-fields.hbs +50 -0
  38. package/addon/components/order-config-manager/custom-fields.js +276 -0
  39. package/addon/components/order-config-manager/details.hbs +23 -0
  40. package/addon/components/order-config-manager/details.js +106 -0
  41. package/addon/components/order-config-manager/entities.hbs +54 -0
  42. package/addon/components/order-config-manager/entities.js +227 -0
  43. package/addon/components/order-config-manager-overlay.hbs +26 -0
  44. package/addon/components/order-config-manager-overlay.js +60 -0
  45. package/addon/components/order-config-manager.hbs +15 -0
  46. package/addon/components/order-config-manager.js +298 -0
  47. package/addon/components/order-flow.hbs +1 -0
  48. package/addon/components/order-flow.js +3 -0
  49. package/addon/components/place-form-panel.hbs +1 -1
  50. package/addon/components/place-panel.hbs +1 -1
  51. package/addon/components/vehicle-form-panel.hbs +1 -1
  52. package/addon/components/vehicle-panel.hbs +1 -1
  53. package/addon/components/vendor-form-panel.hbs +1 -1
  54. package/addon/components/vendor-panel.hbs +1 -1
  55. package/addon/controllers/application.js +5 -1
  56. package/addon/controllers/operations/order-config.js +43 -0
  57. package/addon/controllers/operations/orders/index/new.js +125 -21
  58. package/addon/controllers/operations/orders/index/view.js +87 -4
  59. package/addon/controllers/operations/orders/index.js +2 -2
  60. package/addon/engine.js +10 -1
  61. package/addon/initializers/load-jointjs.js +16 -0
  62. package/addon/routes/application.js +1 -1
  63. package/addon/routes/operations/order-config.js +28 -0
  64. package/addon/routes/operations/orders/index/new.js +2 -3
  65. package/addon/routes/operations/orders/index/view.js +1 -1
  66. package/addon/routes.js +1 -0
  67. package/addon/services/context-panel.js +65 -3
  68. package/addon/styles/fleetops-engine.css +268 -0
  69. package/addon/templates/application.hbs +1 -1
  70. package/addon/templates/operations/order-config.hbs +55 -0
  71. package/addon/templates/operations/orders/index/new.hbs +98 -339
  72. package/addon/templates/operations/orders/index/view.hbs +36 -96
  73. package/addon/templates/operations/orders/index.hbs +0 -1
  74. package/addon/utils/apply-context-component-arguments.js +4 -2
  75. package/addon/utils/create-custom-entity.js +22 -0
  76. package/addon/utils/create-flow-activity.js +31 -0
  77. package/addon/utils/get-custom-field-type-map.js +34 -0
  78. package/app/components/activity-event-selector.js +1 -0
  79. package/app/components/activity-form-panel.js +1 -0
  80. package/app/components/activity-logic-builder.js +1 -0
  81. package/app/components/custom-entity-form-panel.js +1 -0
  82. package/app/components/custom-field-form-panel/options-input.js +1 -0
  83. package/app/components/custom-field-form-panel.js +1 -0
  84. package/app/components/joint-graph.js +1 -0
  85. package/app/components/modals/new-custom-field-group.js +1 -0
  86. package/app/components/order-config-manager/activity-flow.js +1 -0
  87. package/app/components/order-config-manager/custom-fields.js +1 -0
  88. package/app/components/order-config-manager/details.js +1 -0
  89. package/app/components/order-config-manager/entities.js +1 -0
  90. package/app/components/order-config-manager-overlay.js +1 -0
  91. package/app/components/order-config-manager.js +1 -0
  92. package/app/components/order-flow.js +1 -0
  93. package/app/controllers/operations/order-config.js +1 -0
  94. package/app/routes/operations/order-config.js +1 -0
  95. package/app/templates/operations/order-config.js +1 -0
  96. package/app/utils/create-custom-entity.js +1 -0
  97. package/app/utils/create-flow-activity.js +1 -0
  98. package/app/utils/get-custom-field-type-map.js +1 -0
  99. package/composer.json +4 -3
  100. package/config/environment.js +1 -0
  101. package/extension.json +1 -1
  102. package/index.js +20 -0
  103. package/package.json +7 -5
  104. package/server/config/api.php +3 -650
  105. package/server/migrations/2023_04_27_053456_create_contacts_table.php +1 -2
  106. package/server/migrations/2023_04_27_053456_create_drivers_table.php +1 -2
  107. package/server/migrations/2023_04_27_053456_create_entities_table.php +1 -2
  108. package/server/migrations/2023_04_27_053456_create_fleet_drivers_table.php +1 -2
  109. package/server/migrations/2023_04_27_053456_create_fleets_table.php +1 -2
  110. package/server/migrations/2023_04_27_053456_create_fuel_reports_table.php +1 -2
  111. package/server/migrations/2023_04_27_053456_create_integrated_vendors_table.php +1 -2
  112. package/server/migrations/2023_04_27_053456_create_issues_table.php +1 -2
  113. package/server/migrations/2023_04_27_053456_create_orders_table.php +1 -2
  114. package/server/migrations/2023_04_27_053456_create_payloads_table.php +1 -2
  115. package/server/migrations/2023_04_27_053456_create_places_table.php +1 -2
  116. package/server/migrations/2023_04_27_053456_create_positions_table.php +1 -2
  117. package/server/migrations/2023_04_27_053456_create_proofs_table.php +1 -2
  118. package/server/migrations/2023_04_27_053456_create_purchase_rates_table.php +1 -2
  119. package/server/migrations/2023_04_27_053456_create_routes_table.php +1 -2
  120. package/server/migrations/2023_04_27_053456_create_service_areas_table.php +1 -2
  121. package/server/migrations/2023_04_27_053456_create_service_quote_items_table.php +1 -2
  122. package/server/migrations/2023_04_27_053456_create_service_quotes_table.php +1 -2
  123. package/server/migrations/2023_04_27_053456_create_service_rate_fees_table.php +1 -2
  124. package/server/migrations/2023_04_27_053456_create_service_rate_parcel_fees_table.php +1 -2
  125. package/server/migrations/2023_04_27_053456_create_service_rates_table.php +1 -2
  126. package/server/migrations/2023_04_27_053456_create_tracking_numbers_table.php +1 -2
  127. package/server/migrations/2023_04_27_053456_create_tracking_statuses_table.php +1 -2
  128. package/server/migrations/2023_04_27_053456_create_vehicles_table.php +1 -2
  129. package/server/migrations/2023_04_27_053456_create_vendors_table.php +1 -2
  130. package/server/migrations/2023_04_27_053456_create_waypoints_table.php +1 -2
  131. package/server/migrations/2023_04_27_053456_create_zones_table.php +1 -2
  132. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_companies_table.php +1 -2
  133. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_contacts_table.php +1 -2
  134. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_drivers_table.php +1 -2
  135. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_entities_table.php +1 -2
  136. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_fleet_drivers_table.php +1 -2
  137. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_fleets_table.php +1 -2
  138. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_fuel_reports_table.php +1 -2
  139. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_integrated_vendors_table.php +1 -2
  140. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_issues_table.php +1 -2
  141. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_orders_table.php +1 -2
  142. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_payloads_table.php +1 -2
  143. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_places_table.php +1 -2
  144. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_positions_table.php +1 -2
  145. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_proofs_table.php +1 -2
  146. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_purchase_rates_table.php +1 -2
  147. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_routes_table.php +1 -2
  148. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_service_areas_table.php +1 -2
  149. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_service_quote_items_table.php +1 -2
  150. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_service_quotes_table.php +1 -2
  151. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_service_rate_fees_table.php +1 -2
  152. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_service_rate_parcel_fees_table.php +1 -2
  153. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_service_rates_table.php +1 -2
  154. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_tracking_numbers_table.php +1 -2
  155. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_tracking_statuses_table.php +1 -2
  156. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_vehicles_table.php +1 -2
  157. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_vendors_table.php +1 -2
  158. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_waypoints_table.php +1 -2
  159. package/server/migrations/2023_04_27_053459_add_foreign_keys_to_zones_table.php +1 -2
  160. package/server/migrations/2023_09_14_074512_update_vehicles_table.php +1 -2
  161. package/server/migrations/2023_09_14_074619_create_vehicle_devices_table.php +35 -36
  162. package/server/migrations/2023_09_26_071205_add_online_column_to_vehicles_table.php +1 -2
  163. package/server/migrations/2023_09_26_083957_create_fleet_vehicles_table.php +1 -2
  164. package/server/migrations/2023_09_27_023757_add_column_to_fleets_table.php +1 -2
  165. package/server/migrations/2023_10_05_074811_update_vehicle.php +1 -2
  166. package/server/migrations/2023_10_25_093014_create_vehicle_device_events_table.php +1 -2
  167. package/server/migrations/2023_11_03_072503_refactor_issues_table_columns.php +1 -2
  168. package/server/migrations/2023_11_06_040645_refactor_fuel_reports_table.php +1 -2
  169. package/server/migrations/2023_11_09_041807_add_status_column_to_fuel_reports_table.php +1 -2
  170. package/server/migrations/2023_11_15_062814_add_comments_column_and_resolved_at_column_to_vehicle_device_events_table.php +1 -2
  171. package/server/migrations/2023_11_17_053001_add_category_uuid_to_entities_table.php +1 -2
  172. package/server/migrations/2023_11_28_074244_add_supplier_uuid_column_to_entities_table.php +1 -2
  173. package/server/migrations/2023_12_14_100209_add_meta_column_on_driver_table copy.php +1 -2
  174. package/server/migrations/2024_02_18_100209_add_avatar_url_columns_table.php +3 -4
  175. package/server/migrations/2024_02_27_093648_create_order_configs_table.php +43 -0
  176. package/server/migrations/2024_03_07_075508_add_order_config_uuid_column_to_orders_table.php +28 -0
  177. package/server/migrations/2024_03_12_033527_add_public_id_column_to_order_configs_table.php +34 -0
  178. package/server/seeders/OrderConfigSeeder.php +23 -0
  179. package/server/src/Casts/OrderConfigEntities.php +53 -0
  180. package/server/src/Console/Commands/FixLegacyOrderConfigs.php +66 -0
  181. package/server/src/Events/DriverLocationChanged.php +1 -1
  182. package/server/src/Events/DriverSimulatedLocationChanged.php +1 -1
  183. package/server/src/Flow/Activity.php +232 -6
  184. package/server/src/Flow/Condition.php +84 -0
  185. package/server/src/Flow/Event.php +88 -0
  186. package/server/src/Flow/Flow.php +50 -0
  187. package/server/src/Flow/FlowResource.php +143 -0
  188. package/server/src/Flow/Logic.php +57 -1
  189. package/server/src/Http/Controllers/Api/v1/DriverController.php +2 -2
  190. package/server/src/Http/Controllers/Api/v1/IssueController.php +132 -0
  191. package/server/src/Http/Controllers/Api/v1/OrderController.php +152 -57
  192. package/server/src/Http/Controllers/Api/v1/OrganizationController.php +27 -0
  193. package/server/src/Http/Controllers/Api/v1/PlaceController.php +2 -3
  194. package/server/src/Http/Controllers/Controller.php +9 -0
  195. package/server/src/Http/Controllers/FleetOpsController.php +7 -0
  196. package/server/src/Http/Controllers/Internal/v1/OrderConfigController.php +40 -233
  197. package/server/src/Http/Controllers/Internal/v1/OrderController.php +58 -28
  198. package/server/src/Http/Controllers/Internal/v1/SettingController.php +20 -0
  199. package/server/src/Http/Filter/OrderConfigFilter.php +13 -0
  200. package/server/src/Http/Requests/CreateIssueRequest.php +19 -0
  201. package/server/src/Http/Requests/Internal/CreateOrderConfigRequest.php +40 -0
  202. package/server/src/Http/Requests/Internal/CreateOrderRequest.php +66 -0
  203. package/server/src/Http/Requests/Internal/DeleteOrderConfigRequest.php +0 -0
  204. package/server/src/Http/Requests/UpdateIssueRequest.php +7 -0
  205. package/server/src/Http/Resources/v1/Order.php +3 -0
  206. package/server/src/Listeners/HandleOrderCanceled.php +8 -4
  207. package/server/src/Listeners/HandleOrderDispatched.php +14 -12
  208. package/server/src/Models/Order.php +527 -31
  209. package/server/src/Models/OrderConfig.php +467 -0
  210. package/server/src/Models/Payload.php +6 -7
  211. package/server/src/Notifications/OrderAssigned.php +1 -1
  212. package/server/src/Notifications/OrderCanceled.php +1 -1
  213. package/server/src/Notifications/OrderDispatchFailed.php +1 -1
  214. package/server/src/Notifications/OrderDispatched.php +1 -1
  215. package/server/src/Notifications/OrderPing.php +1 -1
  216. package/server/src/Observers/CompanyObserver.php +20 -0
  217. package/server/src/Providers/FleetOpsServiceProvider.php +4 -1
  218. package/server/src/Providers/NotificationServiceProvider.php +2 -1
  219. package/server/src/Support/FleetOps.php +137 -0
  220. package/server/src/Support/Utils.php +17 -0
  221. package/server/src/Traits/HasTrackingNumber.php +9 -9
  222. package/server/src/routes.php +19 -11
  223. package/translations/en-us.yaml +137 -0
  224. package/server/src/Flow/Config.php +0 -7
  225. package/server/src/Support/Flow.php +0 -653
@@ -2,12 +2,238 @@
2
2
 
3
3
  namespace Fleetbase\FleetOps\Flow;
4
4
 
5
- use Fleetbase\Models\Model;
5
+ use Fleetbase\FleetOps\Models\Order;
6
+ use Fleetbase\FleetOps\Support\Utils;
6
7
 
7
- class Activity
8
+ class Activity extends FlowResource
8
9
  {
9
- protected string $status;
10
- protected string $code;
11
- protected string $details;
12
- protected Model $owner;
10
+ /**
11
+ * The Flow context for the activity.
12
+ */
13
+ public Flow $flow;
14
+
15
+ /**
16
+ * Create a new Activity instance.
17
+ *
18
+ * @param array $attributes initial attributes of the activity
19
+ * @param array $flow the flow configuration for this activity
20
+ */
21
+ public function __construct(array $attributes = [], array $flow = [])
22
+ {
23
+ parent::__construct($attributes);
24
+ $this->flow = new Flow($flow);
25
+ }
26
+
27
+ /**
28
+ * Retrieves the logic attribute of the Activity.
29
+ *
30
+ * @return array an array of Logic instances associated with the Activity
31
+ */
32
+ public function getLogicAttribute(): array
33
+ {
34
+ return array_map(
35
+ function ($logic) {
36
+ return new Logic($logic);
37
+ },
38
+ $this->get('logic', [])
39
+ );
40
+ }
41
+
42
+ /**
43
+ * Retrieves the events attribute of the Activity.
44
+ *
45
+ * @return array an array of Event instances associated with the Activity
46
+ */
47
+ public function getEventsAttribute(): array
48
+ {
49
+ return array_map(
50
+ function ($eventName) {
51
+ return new Event($eventName);
52
+ },
53
+ $this->get('events', [])
54
+ );
55
+ }
56
+
57
+ /**
58
+ * Determines if the Activity passes based on the provided Order.
59
+ *
60
+ * @param Order $order the order to evaluate against the Activity's logic
61
+ *
62
+ * @return bool true if all the logic associated with the Activity passes; False otherwise
63
+ */
64
+ public function passes(Order $order)
65
+ {
66
+ $logicCollection = collect($this->logic);
67
+
68
+ return $logicCollection->every(function ($logic) use ($order) {
69
+ $conditionCollection = collect($logic->conditions);
70
+
71
+ switch ($logic->type) {
72
+ case 'and':
73
+ // All conditions must be true
74
+ return $conditionCollection->every(fn ($condition) => $condition->eval($order));
75
+
76
+ case 'or':
77
+ // At least one condition must be true
78
+ return $conditionCollection->contains(fn ($condition) => $condition->eval($order));
79
+
80
+ case 'not':
81
+ // None of the conditions must be true (assuming 'not' can have multiple conditions)
82
+ return !$conditionCollection->contains(fn ($condition) => $condition->eval($order));
83
+
84
+ case 'if':
85
+ // Only one condition in 'if', and it must be true (typically, 'if' has only one condition)
86
+ return $conditionCollection->first()->eval($order);
87
+
88
+ default:
89
+ // Unrecognized logic type
90
+ throw new \Exception("Unsupported logic type '{$logic->type}' provided.");
91
+ }
92
+ });
93
+ }
94
+
95
+ /**
96
+ * Fires a series of events associated with an order.
97
+ *
98
+ * Iterates over the events array and triggers each event, passing the order as a parameter.
99
+ * This function is typically used to execute a sequence of events as part of a workflow process.
100
+ *
101
+ * @param Order $order the order object to be passed to each event
102
+ */
103
+ public function fireEvents(Order $order)
104
+ {
105
+ foreach ($this->events as $event) {
106
+ $event->fire($order);
107
+ }
108
+ }
109
+
110
+ /**
111
+ * Retrieves the child activities of this Activity.
112
+ *
113
+ * @return \Illuminate\Support\Collection a collection of child activities
114
+ */
115
+ public function getChildActivities()
116
+ {
117
+ $children = collect();
118
+ if (is_array($this->activities)) {
119
+ foreach ($this->activities as $childActivityCode) {
120
+ $childActivity = $this->flow->getActivity($childActivityCode);
121
+ if ($childActivity) {
122
+ $children->push($childActivity);
123
+ }
124
+ }
125
+ }
126
+
127
+ return $children;
128
+ }
129
+
130
+ /**
131
+ * Retrieves the children activities of this Activity.
132
+ *
133
+ * This is a shorthand method for `getChildActivities`.
134
+ *
135
+ * @return \Illuminate\Support\Collection the collection of child activities
136
+ */
137
+ public function children()
138
+ {
139
+ return $this->getChildActivities();
140
+ }
141
+
142
+ /**
143
+ * Checks whether this Activity has a child activity with a specified code.
144
+ *
145
+ * @param string $code the unique code identifier for the child activity
146
+ *
147
+ * @return bool true if the child activity is found; otherwise, False
148
+ */
149
+ public function hasChildActivity(string $code)
150
+ {
151
+ return $this->children()->contains(
152
+ function ($activity) use ($code) {
153
+ return $activity->code === $code;
154
+ }
155
+ );
156
+ }
157
+
158
+ /**
159
+ * Determines the next set of activities based on the provided Order.
160
+ *
161
+ * @param Order $order the order to determine the next activities for
162
+ *
163
+ * @return \Illuminate\Support\Collection a collection of the next activities
164
+ */
165
+ public function getNext(Order $order)
166
+ {
167
+ $children = $this->getChildActivities();
168
+ $nextActivities = collect();
169
+
170
+ foreach ($children as $childActivity) {
171
+ if ($childActivity->passes($order)) {
172
+ $nextActivities->push($childActivity);
173
+ }
174
+ }
175
+
176
+ return $nextActivities;
177
+ }
178
+
179
+ /**
180
+ * Retrieves the previous activities of this Activity in the Flow.
181
+ *
182
+ * This method iterates over the flow to find activities that list this Activity as a child.
183
+ *
184
+ * @return \Illuminate\Support\Collection a collection of previous activities
185
+ */
186
+ public function getPrevious()
187
+ {
188
+ $previous = collect();
189
+ foreach ($this->flow as $activity) {
190
+ if ($activity->hasChildActivity($this->code)) {
191
+ $previous->push($activity);
192
+ }
193
+ }
194
+
195
+ return $previous;
196
+ }
197
+
198
+ /**
199
+ * Checks if the activity's code matches a specified code.
200
+ *
201
+ * This method is used to determine if the current activity instance
202
+ * has a specific code, which is useful for identifying the activity.
203
+ *
204
+ * @param string $code the code to compare against the activity's code
205
+ *
206
+ * @return bool returns true if the activity's code matches the specified code, false otherwise
207
+ */
208
+ public function is(string $code)
209
+ {
210
+ return $this->code === $code;
211
+ }
212
+
213
+ /**
214
+ * Determines if the activity is marked as complete.
215
+ *
216
+ * This method checks the 'complete' property of the activity. It's typically used
217
+ * to verify if the activity has been completed as part of the workflow process.
218
+ *
219
+ * @return bool returns true if the activity is marked as complete, false otherwise
220
+ */
221
+ public function complete(): bool
222
+ {
223
+ return Utils::isTrue($this->complete);
224
+ }
225
+
226
+ /**
227
+ * Checks if completing this activity leads to the completion of the order.
228
+ *
229
+ * This method uses the `complete` method to determine if the completion of
230
+ * this activity signifies the completion of the associated order. It is useful
231
+ * for workflows where the finalization of an activity concludes the entire order process.
232
+ *
233
+ * @return bool returns true if completing this activity completes the order, false otherwise
234
+ */
235
+ public function completesOrder(): bool
236
+ {
237
+ return $this->complete();
238
+ }
13
239
  }
@@ -0,0 +1,84 @@
1
+ <?php
2
+
3
+ namespace Fleetbase\FleetOps\Flow;
4
+
5
+ use Fleetbase\FleetOps\Models\Order;
6
+ use Illuminate\Support\Str;
7
+
8
+ class Condition extends FlowResource
9
+ {
10
+ /**
11
+ * Evaluate the condition against an order.
12
+ *
13
+ * @param Order $order the order to evaluate the condition against
14
+ *
15
+ * @return bool the result of the condition evaluation
16
+ *
17
+ * @throws \Exception if the operator is unknown
18
+ */
19
+ public function eval(Order $order)
20
+ {
21
+ $field = $this->field;
22
+ $operator = $this->operator;
23
+ $value = $this->value;
24
+ $actualValue = strtolower($order->resolveDynamicValue($field));
25
+
26
+ // if value is string convert to lowercase
27
+ if (is_string($value)) {
28
+ $value = strtolower($value);
29
+ }
30
+
31
+ switch ($operator) {
32
+ case 'exists':
33
+ return isset($actualValue);
34
+
35
+ case 'in':
36
+ case 'has':
37
+ return is_array($actualValue) && in_array($actualValue, $value);
38
+
39
+ case 'notIn':
40
+ case 'doesntHave':
41
+ return is_array($actualValue) && !in_array($actualValue, $value);
42
+
43
+ case 'contains':
44
+ return is_string($value) && is_string($actualValue) && Str::contains($value, $actualValue);
45
+
46
+ case 'beginsWith':
47
+ return is_string($value) && is_string($actualValue) && Str::startsWith($value, $actualValue);
48
+
49
+ case 'endsWith':
50
+ return is_string($value) && is_string($actualValue) && Str::endsWith($value, $actualValue);
51
+
52
+ case 'equal':
53
+ return $actualValue == $value;
54
+
55
+ case 'and':
56
+ return $actualValue && $value;
57
+
58
+ case 'or':
59
+ return $actualValue || $value;
60
+
61
+ case 'not':
62
+ return !$actualValue;
63
+
64
+ case 'notEqual':
65
+ return $actualValue != $value;
66
+
67
+ case 'greaterThan':
68
+ return $actualValue > $value;
69
+
70
+ case 'greaterThanOrEqual':
71
+ return $actualValue >= $value;
72
+
73
+ case 'lessThan':
74
+ return $actualValue < $value;
75
+
76
+ case 'lessThanOrEqual':
77
+ return $actualValue <= $value;
78
+
79
+ // ... and so on for the other operators
80
+ default:
81
+ throw new \Exception("Unknown operator: {$operator}");
82
+ }
83
+ }
84
+ }
@@ -0,0 +1,88 @@
1
+ <?php
2
+
3
+ namespace Fleetbase\FleetOps\Flow;
4
+
5
+ use Fleetbase\FleetOps\Models\Order;
6
+ use Fleetbase\FleetOps\Support\Utils;
7
+ use Illuminate\Support\Str;
8
+
9
+ class Event
10
+ {
11
+ /**
12
+ * The name of the event.
13
+ */
14
+ public string $name;
15
+
16
+ /**
17
+ * The order associated with the event, if any.
18
+ *
19
+ * @var ?Order
20
+ */
21
+ public ?Order $order = null;
22
+
23
+ /**
24
+ * Constructs a new Event instance.
25
+ *
26
+ * @param string $name the name of the event
27
+ * @param ?Order $order the associated order, if any
28
+ */
29
+ public function __construct(string $name, ?Order $order = null)
30
+ {
31
+ $this->name = $name;
32
+ $this->order = $order;
33
+ }
34
+
35
+ /**
36
+ * Sets the order for the event.
37
+ *
38
+ * @param Order $order the order to associate with this event
39
+ */
40
+ public function setOrder(Order $order)
41
+ {
42
+ $this->order = $order;
43
+ }
44
+
45
+ /**
46
+ * Triggers the event, optionally setting the order if not already set.
47
+ *
48
+ * Resolves the event class based on the event name and fires it using Laravel's event system.
49
+ * If the order is not set in the event, and an Order object is provided, it sets the order before firing.
50
+ *
51
+ * @param ?Order $order optional order to associate with the event
52
+ */
53
+ public function fire(?Order $order = null)
54
+ {
55
+ if (!$this->order && $order instanceof Order) {
56
+ $this->setOrder($order);
57
+ }
58
+
59
+ $eventClass = $this->resolve();
60
+ if ($eventClass) {
61
+ event(new $eventClass($this->order));
62
+ }
63
+ }
64
+
65
+ /**
66
+ * Resolves the class name of the event based on the event name.
67
+ *
68
+ * Tries to locate the class in predefined namespaces and returns the fully qualified name if found.
69
+ *
70
+ * @return string|null the resolved class name or null if not found
71
+ */
72
+ public function resolve()
73
+ {
74
+ $eventClassName = Str::studly(preg_replace('/[^a-zA-Z0-9]/', ' ', $this->name));
75
+ $eventClasses = [
76
+ '\\Fleetbase\\FleetOps\Events\\' . $eventClassName,
77
+ '\\Fleetbase\\Events\\' . $eventClassName,
78
+ ];
79
+
80
+ foreach ($eventClasses as $eventClass) {
81
+ if (Utils::classExists($eventClass)) {
82
+ return $eventClass;
83
+ }
84
+ }
85
+
86
+ return null;
87
+ }
88
+ }
@@ -0,0 +1,50 @@
1
+ <?php
2
+
3
+ namespace Fleetbase\FleetOps\Flow;
4
+
5
+ class Flow extends FlowResource implements \IteratorAggregate
6
+ {
7
+ /**
8
+ * Returns an iterator for the flow.
9
+ *
10
+ * @return \Traversable an iterator for the flow's activities
11
+ */
12
+ public function getIterator(): \Traversable
13
+ {
14
+ foreach ($this->attributes['activities'] as $activityAttributes) {
15
+ yield new Activity($activityAttributes, $this->serialize());
16
+ }
17
+ }
18
+
19
+ /**
20
+ * Retrieve the activity for a given code.
21
+ *
22
+ * @param string $code the unique code identifier for the activity
23
+ *
24
+ * @return Activity|null the Activity object if found, or null otherwise
25
+ */
26
+ public function getActivity(string $code): ?Activity
27
+ {
28
+ if (isset($this->{$code})) {
29
+ return new Activity($this->{$code}, $this->serialize());
30
+ }
31
+
32
+ return null;
33
+ }
34
+
35
+ /**
36
+ * Determines if a given object is an instance of Activity.
37
+ *
38
+ * This static method is used to check if a provided object is an
39
+ * instance of the Activity class. This can be useful in contexts where
40
+ * there is a need to verify the type of a given resource or object.
41
+ *
42
+ * @param mixed $activity the object to check
43
+ *
44
+ * @return bool returns true if the provided object is an instance of Activity, false otherwise
45
+ */
46
+ public static function isActivity($activity)
47
+ {
48
+ return $activity instanceof Activity;
49
+ }
50
+ }
@@ -0,0 +1,143 @@
1
+ <?php
2
+
3
+ namespace Fleetbase\FleetOps\Flow;
4
+
5
+ use Illuminate\Support\Str;
6
+
7
+ /**
8
+ * Base class for flow resources which can serialize to JSON.
9
+ */
10
+ class FlowResource implements \JsonSerializable
11
+ {
12
+ /**
13
+ * The array of attributes for the flow resource.
14
+ */
15
+ public array $attributes = [];
16
+
17
+ /**
18
+ * Create a new flow resource instance.
19
+ *
20
+ * @param array $attributes attributes to set upon construction
21
+ */
22
+ public function __construct(array $attributes = [])
23
+ {
24
+ $this->attributes = $attributes;
25
+ }
26
+
27
+ /**
28
+ * Magic method to retrieve the value of a property.
29
+ * If a getter method exists for the property, it will be used; otherwise, it will access the property directly.
30
+ *
31
+ * @param string $name the name of the property to access
32
+ *
33
+ * @return mixed the value of the property
34
+ */
35
+ public function __get($name)
36
+ {
37
+ // if has special attribute `get_Attribute()` call first
38
+ $mutatorMethod = 'get' . Str::studly($name) . 'Attribute';
39
+ if (method_exists($this, $mutatorMethod)) {
40
+ return $this->{$mutatorMethod}();
41
+ }
42
+
43
+ if (array_key_exists($name, $this->attributes)) {
44
+ return $this->attributes[$name];
45
+ }
46
+
47
+ return $this->{$name};
48
+ }
49
+
50
+ /**
51
+ * Magic method to check if a property is set.
52
+ * This will return true if the property exists and is not null.
53
+ *
54
+ * @param string $name the name of the property to check
55
+ *
56
+ * @return bool true if the property is set, false otherwise
57
+ */
58
+ public function __isset($name)
59
+ {
60
+ if (array_key_exists($name, $this->attributes)) {
61
+ return isset($this->attributes[$name]);
62
+ }
63
+
64
+ return isset($this->{$name});
65
+ }
66
+
67
+ /**
68
+ * Get the value of a given key from the attributes.
69
+ * If the key does not exist, the default value will be returned.
70
+ *
71
+ * @param string $key the key to retrieve
72
+ * @param mixed $defaultValue the default value to return if the key does not exist
73
+ *
74
+ * @return mixed the value from the attributes or the default value
75
+ */
76
+ public function get($key, $defaultValue = null)
77
+ {
78
+ if (array_key_exists($key, $this->attributes)) {
79
+ return $this->attributes[$key];
80
+ }
81
+
82
+ if (isset($this->{$key})) {
83
+ return $this->{$key};
84
+ }
85
+
86
+ return data_get($this, $key, $defaultValue);
87
+ }
88
+
89
+ /**
90
+ * Set a given attribute on the resource.
91
+ *
92
+ * @param string $key the key of the attribute to set
93
+ * @param mixed $value the value to set for the attribute
94
+ *
95
+ * @return self returns instance of FlowResource for chaining
96
+ */
97
+ public function set(string $key, $value): self
98
+ {
99
+ $this->attributes[$key] = $value;
100
+
101
+ return $this;
102
+ }
103
+
104
+ /**
105
+ * Serialize the attributes to an array.
106
+ *
107
+ * @return array the array representation of the attributes
108
+ */
109
+ public function serialize(): array
110
+ {
111
+ return $this->attributes;
112
+ }
113
+
114
+ /**
115
+ * Convert the object into an array.
116
+ *
117
+ * @return array the array representation of the object
118
+ */
119
+ public function toArray(): array
120
+ {
121
+ return $this->serialize();
122
+ }
123
+
124
+ /**
125
+ * Convert the object into a JSON string.
126
+ *
127
+ * @return string the JSON representation of the object
128
+ */
129
+ public function toJson()
130
+ {
131
+ return json_encode($this->serialize());
132
+ }
133
+
134
+ /**
135
+ * Specify data which should be serialized to JSON.
136
+ *
137
+ * @return mixed data which can be serialized by json_encode, which is a value of any type other than a resource
138
+ */
139
+ public function jsonSerialize()
140
+ {
141
+ return $this->toArray();
142
+ }
143
+ }
@@ -2,6 +2,62 @@
2
2
 
3
3
  namespace Fleetbase\FleetOps\Flow;
4
4
 
5
- class Logic
5
+ use Fleetbase\FleetOps\Models\Order;
6
+
7
+ class Logic extends FlowResource
6
8
  {
9
+ /**
10
+ * Accessor method to retrieve the 'conditions' attribute.
11
+ *
12
+ * This method maps each condition in the 'conditions' attribute to a new Condition object.
13
+ * It utilizes the array_map function to apply this transformation, ensuring each entry
14
+ * in the conditions array is converted to a Condition object.
15
+ *
16
+ * @return array an array of Condition objects derived from the 'conditions' attribute
17
+ */
18
+ public function getConditionsAttribute(): array
19
+ {
20
+ return array_map(
21
+ function ($condition) {
22
+ return new Condition($condition);
23
+ },
24
+ $this->get('conditions', [])
25
+ );
26
+ }
27
+
28
+ /**
29
+ * Evaluates the logic conditions against an order.
30
+ *
31
+ * @param Order $order the order to evaluate the logic against
32
+ *
33
+ * @return bool whether the logic passes based on the order's attributes
34
+ *
35
+ * @throws \Exception if the logic type is unknown
36
+ */
37
+ public function passes(Order $order)
38
+ {
39
+ $logicType = $this->type;
40
+ $conditions = collect($this->conditions);
41
+
42
+ switch ($logicType) {
43
+ case 'and':
44
+ // All conditions must be true
45
+ return $conditions->every(fn ($condition) => $condition->eval($order));
46
+
47
+ case 'or':
48
+ // At least one condition must be true
49
+ return $conditions->contains(fn ($condition) => $condition->eval($order));
50
+
51
+ case 'not':
52
+ // None of the conditions must be true
53
+ return !$conditions->contains(fn ($condition) => $condition->eval($order));
54
+
55
+ case 'if':
56
+ // 'if' type logic can have multiple conditions; all must be true
57
+ return $conditions->every(fn ($condition) => $condition->eval($order));
58
+
59
+ default:
60
+ throw new \Exception("Unknown logic type: {$logicType}");
61
+ }
62
+ }
7
63
  }
@@ -527,7 +527,7 @@ class DriverController extends Controller
527
527
  }
528
528
 
529
529
  // Get the user account company
530
- $company = Flow::getCompanySessionForUser($user);
530
+ $company = Auth::getCompanySessionForUser($user);
531
531
  if (!$company) {
532
532
  return response()->apiError('No company found for this driver.');
533
533
  }
@@ -752,7 +752,7 @@ class DriverController extends Controller
752
752
 
753
753
  // If unable to find company from driver profile, fallback to session flow
754
754
  if (!$company) {
755
- $company = Flow::getCompanySessionForUser($user);
755
+ $company = Auth::getCompanySessionForUser($user);
756
756
  }
757
757
 
758
758
  return $company;