@burdenoff/microfe-movethewheels 2026.1108.3 → 2026.1108.5

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.
@@ -1,11 +1,17 @@
1
1
  /**
2
- * Carrier and telematics integrations (BOFF-4404, Samsara in BOFF-10703).
2
+ * Carrier and telematics integrations (BOFF-4404, Samsara in BOFF-10703,
3
+ * Geotab in BOFF-10922, Motive in BOFF-10921).
3
4
  *
4
- * A workspace connects an external carrier, ELD or GPS feed to MoveTheWheels
5
- * as a signed webhook. A generic feed is verified with HMAC-SHA256 over the
5
+ * A workspace connects an external carrier, ELD or GPS feed to MoveTheWheels.
6
+ * Webhook feeds push: a generic feed is verified with HMAC-SHA256 over the
6
7
  * raw request body using a secret generated here; a Samsara feed is verified
7
- * with Samsara's own timestamped signature using the secret Samsara issues.
8
- * Every accepted update becomes a tracking event on the matching shipment.
8
+ * with Samsara's own timestamped signature using the secret Samsara issues; a
9
+ * Motive feed is verified with Motive's HMAC-SHA1 signature using a secret
10
+ * generated here and entered on the Motive webhook.
11
+ * Geotab has no push webhooks, so a Geotab feed is pulled: the service signs
12
+ * in to MyGeotab with the credentials entered here and polls its data feeds
13
+ * every few minutes. Every accepted update becomes a tracking event on the
14
+ * matching shipment.
9
15
  *
10
16
  * This module holds the GraphQL documents and the pure helpers the page uses;
11
17
  * the network calls live in `hooks/useCarrierIntegrations`.
@@ -16,9 +22,16 @@ import { resolveGraphqlUrl } from './graphqlClient';
16
22
  * Providers this page can connect. The service also ingests SHIPROCKET, which
17
23
  * is set up from the Shiprocket carrier flow rather than here.
18
24
  */
19
- export const SUPPORTED_PROVIDERS = ['GENERIC_WEBHOOK', 'SAMSARA'] as const;
25
+ export const SUPPORTED_PROVIDERS = ['GENERIC_WEBHOOK', 'SAMSARA', 'MOTIVE', 'GEOTAB'] as const;
20
26
  export type SupportedProvider = (typeof SUPPORTED_PROVIDERS)[number];
21
27
 
28
+ /** Providers the service pulls on a schedule instead of receiving webhooks. */
29
+ export const POLLED_PROVIDERS: ReadonlySet<string> = new Set(['GEOTAB']);
30
+
31
+ export function isPolledProvider(provider: string): boolean {
32
+ return POLLED_PROVIDERS.has(provider);
33
+ }
34
+
22
35
  /** Named networks without an adapter yet; the service refuses the reserved ones. */
23
36
  export const ROADMAP_PROVIDERS = [
24
37
  { id: 'PROJECT44', label: 'project44' },
@@ -26,14 +39,18 @@ export const ROADMAP_PROVIDERS = [
26
39
  { id: 'DESCARTES', label: 'Descartes' },
27
40
  ] as const;
28
41
 
29
- /** ELD networks planned after Samsara (BOFF-10703 follow-ups); not in the schema yet. */
30
- export const PLANNED_ELD_PROVIDERS = ['Motive', 'Geotab'] as const;
31
-
32
42
  export const INTEGRATION_STATUSES = ['ACTIVE', 'PAUSED', 'DISABLED'] as const;
33
43
  export type IntegrationStatus = (typeof INTEGRATION_STATUSES)[number];
34
44
 
35
45
  export type IntegrationProvider =
36
- 'GENERIC_WEBHOOK' | 'SAMSARA' | 'SHIPROCKET' | 'PROJECT44' | 'FOURKITES' | 'DESCARTES';
46
+ | 'GENERIC_WEBHOOK'
47
+ | 'SAMSARA'
48
+ | 'MOTIVE'
49
+ | 'GEOTAB'
50
+ | 'SHIPROCKET'
51
+ | 'PROJECT44'
52
+ | 'FOURKITES'
53
+ | 'DESCARTES';
37
54
 
38
55
  /** How each provider is named on the page. */
39
56
  export function providerLabel(provider: IntegrationProvider): string {
@@ -42,6 +59,10 @@ export function providerLabel(provider: IntegrationProvider): string {
42
59
  return 'Signed webhook';
43
60
  case 'SAMSARA':
44
61
  return 'Samsara';
62
+ case 'MOTIVE':
63
+ return 'Motive';
64
+ case 'GEOTAB':
65
+ return 'Geotab';
45
66
  case 'SHIPROCKET':
46
67
  return 'Shiprocket';
47
68
  case 'PROJECT44':
@@ -61,6 +82,15 @@ export const SAMSARA_EVENT_TYPES = [
61
82
  { type: 'GeofenceExit', becomes: 'DEPARTED' },
62
83
  ] as const;
63
84
 
85
+ /** Motive webhook actions the service turns into tracking events. */
86
+ export const MOTIVE_ACTIONS = [
87
+ { action: 'vehicle_geofence_event', becomes: 'Arrived on geofence entry, Departed on exit' },
88
+ { action: 'vehicle_location_updated', becomes: 'In transit, with the reported position' },
89
+ ] as const;
90
+
91
+ /** Header Motive signs each delivery with (hex HMAC-SHA1 of the raw body); the gateway forwards it. */
92
+ export const MOTIVE_SIGNATURE_HEADER = 'X-KT-Webhook-Signature';
93
+
64
94
  /** Route or stop external id key the service reads the shipment reference from by default. */
65
95
  export const DEFAULT_SAMSARA_SHIPMENT_KEY = 'mtwShipment';
66
96
 
@@ -92,6 +122,137 @@ export function externalIdKeyError(key: string): string | null {
92
122
  return null;
93
123
  }
94
124
 
125
+ /** MyGeotab host used when the server field is left blank. */
126
+ export const DEFAULT_GEOTAB_SERVER = 'my.geotab.com';
127
+
128
+ /** How often the service polls each Geotab feed. */
129
+ export const GEOTAB_POLL_INTERVAL_MINUTES = 5;
130
+
131
+ /** Tracking events a Geotab exception rule may be mapped to. */
132
+ export const GEOTAB_RULE_EVENT_TYPES = [
133
+ 'PICKED_UP',
134
+ 'IN_TRANSIT',
135
+ 'ARRIVED',
136
+ 'DEPARTED',
137
+ 'OUT_FOR_DELIVERY',
138
+ 'DELIVERED',
139
+ 'FAILED',
140
+ 'RETURNED',
141
+ ] as const;
142
+ export type GeotabRuleEventType = (typeof GEOTAB_RULE_EVENT_TYPES)[number];
143
+
144
+ /** The service accepts at most this many rule mappings per Geotab feed. */
145
+ export const MAX_GEOTAB_RULE_MAPPINGS = 50;
146
+
147
+ /** The service refuses MyGeotab passwords longer than this. */
148
+ export const MAX_GEOTAB_PASSWORD_LENGTH = 255;
149
+
150
+ export interface GeotabConnectionInput {
151
+ server: string;
152
+ database: string;
153
+ userName: string;
154
+ /** One `<rule id> = <EVENT_TYPE>` per line. */
155
+ ruleMappings: string;
156
+ locationUpdates: boolean;
157
+ }
158
+
159
+ export interface GeotabConfig {
160
+ server: string;
161
+ database: string;
162
+ userName: string;
163
+ ruleEvents: Record<string, GeotabRuleEventType>;
164
+ locationUpdates: boolean;
165
+ }
166
+
167
+ const GEOTAB_HOST = /^(?=.{1,253}$)[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.geotab\.com$/;
168
+ const GEOTAB_RULE_ID = /^[A-Za-z0-9_.-]{1,64}$/;
169
+
170
+ /** The MyGeotab host the service will contact, or null when it is not a geotab.com host. */
171
+ export function normalizeGeotabServer(value: string): string | null {
172
+ const trimmed = value.trim().toLowerCase();
173
+ if (!trimmed) return DEFAULT_GEOTAB_SERVER;
174
+ const host = trimmed.replace(/^https:\/\//, '').replace(/\/(apiv1\/?)?$/, '');
175
+ return GEOTAB_HOST.test(host) ? host : null;
176
+ }
177
+
178
+ /**
179
+ * Parses rule mappings typed one per line as `<rule id> = <EVENT_TYPE>` (a
180
+ * colon works too; blank lines and `#` comments are ignored).
181
+ */
182
+ export function parseGeotabRuleMappings(
183
+ text: string
184
+ ): { ok: true; ruleEvents: Record<string, GeotabRuleEventType> } | { ok: false; error: string } {
185
+ const ruleEvents: Record<string, GeotabRuleEventType> = {};
186
+ const lines = text.split(/\r?\n/);
187
+ for (const [index, raw] of lines.entries()) {
188
+ const line = raw.trim();
189
+ if (!line || line.startsWith('#')) continue;
190
+ const match = /^([^=:]+)[=:](.+)$/.exec(line);
191
+ const ruleId = match?.[1]?.trim() ?? '';
192
+ const eventType = (match?.[2]?.trim() ?? '').toUpperCase().replace(/[\s-]+/g, '_');
193
+ if (!match || !GEOTAB_RULE_ID.test(ruleId)) {
194
+ return {
195
+ ok: false,
196
+ error: `Line ${index + 1}: write each mapping as <rule id> = <event>, e.g. RuleArriveId = ARRIVED.`,
197
+ };
198
+ }
199
+ if (!(GEOTAB_RULE_EVENT_TYPES as readonly string[]).includes(eventType)) {
200
+ return {
201
+ ok: false,
202
+ error: `Line ${index + 1}: use one of ${GEOTAB_RULE_EVENT_TYPES.join(', ')}.`,
203
+ };
204
+ }
205
+ ruleEvents[ruleId] = eventType as GeotabRuleEventType;
206
+ }
207
+ if (Object.keys(ruleEvents).length > MAX_GEOTAB_RULE_MAPPINGS) {
208
+ return { ok: false, error: `Map at most ${MAX_GEOTAB_RULE_MAPPINGS} rules.` };
209
+ }
210
+ return { ok: true, ruleEvents };
211
+ }
212
+
213
+ /** Validates the Geotab connection fields and builds the config the service stores. */
214
+ export function buildGeotabConfig(
215
+ input: GeotabConnectionInput
216
+ ): { ok: true; config: GeotabConfig } | { ok: false; error: string } {
217
+ const server = normalizeGeotabServer(input.server);
218
+ if (!server) return { ok: false, error: 'Use your MyGeotab server, e.g. my.geotab.com.' };
219
+ const database = input.database.trim();
220
+ if (!database || database.length > 255) {
221
+ return { ok: false, error: 'Enter the MyGeotab database name.' };
222
+ }
223
+ const userName = input.userName.trim();
224
+ if (!userName || userName.length > 255) {
225
+ return { ok: false, error: 'Enter the MyGeotab user name.' };
226
+ }
227
+ const rules = parseGeotabRuleMappings(input.ruleMappings);
228
+ if (!rules.ok) return rules;
229
+ if (!input.locationUpdates && Object.keys(rules.ruleEvents).length === 0) {
230
+ return {
231
+ ok: false,
232
+ error: 'Turn on GPS position updates or map at least one exception rule, or nothing is read.',
233
+ };
234
+ }
235
+ return {
236
+ ok: true,
237
+ config: {
238
+ server,
239
+ database,
240
+ userName,
241
+ ruleEvents: rules.ruleEvents,
242
+ locationUpdates: input.locationUpdates,
243
+ },
244
+ };
245
+ }
246
+
247
+ /** Returns an error message for an unusable MyGeotab password, or null. */
248
+ export function geotabPasswordError(password: string): string | null {
249
+ if (!password) return 'Enter the MyGeotab password.';
250
+ if (password.length > MAX_GEOTAB_PASSWORD_LENGTH) {
251
+ return `The password can be at most ${MAX_GEOTAB_PASSWORD_LENGTH} characters.`;
252
+ }
253
+ return null;
254
+ }
255
+
95
256
  /** Headers Samsara signs each delivery with; the workspace gateway forwards both. */
96
257
  export const SAMSARA_SIGNATURE_HEADERS = ['X-Samsara-Signature', 'X-Samsara-Timestamp'] as const;
97
258
 
@@ -120,6 +281,31 @@ export const SIGNATURE_HEADER = 'x-webhook-signature';
120
281
  /** Updates accepted in one delivery (a JSON array of at most this many objects). */
121
282
  export const MAX_UPDATES_PER_DELIVERY = 5;
122
283
 
284
+ export type PollStatus = 'OK' | 'PARTIAL' | 'FAILED';
285
+ export type PollResultStatus = PollStatus | 'SKIPPED';
286
+
287
+ /** Latest poll of a pulled feed (Geotab); null for webhook feeds. */
288
+ export interface IntegrationPollStatus {
289
+ lastPollAt: string | null;
290
+ lastSuccessAt: string | null;
291
+ lastPollStatus: PollStatus | null;
292
+ lastPollError: string | null;
293
+ consecutiveFailures: number;
294
+ lastRecordsFetched: number;
295
+ lastUpdatesAccepted: number;
296
+ lastUpdatesSkipped: number;
297
+ }
298
+
299
+ export interface IntegrationPollResult {
300
+ integrationId: string;
301
+ status: PollResultStatus;
302
+ skippedReason: string | null;
303
+ recordsFetched: number;
304
+ updatesAccepted: number;
305
+ updatesSkipped: number;
306
+ error: string | null;
307
+ }
308
+
123
309
  export interface CarrierIntegration {
124
310
  id: string;
125
311
  workspaceId: string;
@@ -131,6 +317,7 @@ export interface CarrierIntegration {
131
317
  lastErrorMessage: string | null;
132
318
  createdAt: string;
133
319
  updatedAt: string;
320
+ pollStatus: IntegrationPollStatus | null;
134
321
  }
135
322
 
136
323
  export interface CarrierIntegrationEvent {
@@ -156,6 +343,16 @@ const INTEGRATION_FIELDS = `
156
343
  lastErrorMessage
157
344
  createdAt
158
345
  updatedAt
346
+ pollStatus {
347
+ lastPollAt
348
+ lastSuccessAt
349
+ lastPollStatus
350
+ lastPollError
351
+ consecutiveFailures
352
+ lastRecordsFetched
353
+ lastUpdatesAccepted
354
+ lastUpdatesSkipped
355
+ }
159
356
  `;
160
357
 
161
358
  export const LIST_INTEGRATIONS_QUERY = `
@@ -206,6 +403,20 @@ export const UPDATE_INTEGRATION_MUTATION = `
206
403
  }
207
404
  `;
208
405
 
406
+ export const POLL_INTEGRATION_NOW_MUTATION = `
407
+ mutation PollMtwCarrierIntegrationNow($id: ID!) {
408
+ pollIntegrationNow(id: $id) {
409
+ integrationId
410
+ status
411
+ skippedReason
412
+ recordsFetched
413
+ updatesAccepted
414
+ updatesSkipped
415
+ error
416
+ }
417
+ }
418
+ `;
419
+
209
420
  export const DELETE_INTEGRATION_MUTATION = `
210
421
  mutation DeleteMtwCarrierIntegration($id: ID!) {
211
422
  deleteIntegration(id: $id)
@@ -305,14 +516,62 @@ export function integrationStatusTone(status: IntegrationStatus): BadgeTone {
305
516
  return 'outline';
306
517
  }
307
518
 
519
+ export function pollStatusTone(status: PollStatus | null): BadgeTone {
520
+ if (status === 'OK') return 'default';
521
+ if (status === 'PARTIAL') return 'secondary';
522
+ if (status === 'FAILED') return 'destructive';
523
+ return 'outline';
524
+ }
525
+
526
+ /** One line describing the latest poll, for the integration card. */
527
+ export function pollStatusSummary(
528
+ pollStatus: IntegrationPollStatus | null,
529
+ format: (iso: string) => string
530
+ ): string {
531
+ if (!pollStatus?.lastPollAt) return 'Not polled yet; the first poll runs within a few minutes.';
532
+ const when = format(pollStatus.lastPollAt);
533
+ if (pollStatus.lastPollStatus === 'FAILED') {
534
+ const lastGood = pollStatus.lastSuccessAt
535
+ ? ` Last successful poll ${format(pollStatus.lastSuccessAt)}.`
536
+ : '';
537
+ const failures =
538
+ pollStatus.consecutiveFailures > 1
539
+ ? ` ${pollStatus.consecutiveFailures} failures in a row.`
540
+ : '';
541
+ return `Last poll ${when} failed.${failures}${lastGood}`;
542
+ }
543
+ return `Last poll ${when}: ${pollStatus.lastRecordsFetched} records read, ${pollStatus.lastUpdatesAccepted} tracking events added, ${pollStatus.lastUpdatesSkipped} skipped (vehicle not matched or not on an in-progress route).`;
544
+ }
545
+
546
+ /** The notice shown after "Poll now". */
547
+ export function pollResultMessage(name: string, result: IntegrationPollResult): string {
548
+ if (result.status === 'SKIPPED') {
549
+ return result.skippedReason === 'POLL_IN_PROGRESS'
550
+ ? `${name} is being polled right now; refresh in a moment.`
551
+ : `${name} was not polled (${humanize(result.skippedReason ?? 'skipped')}).`;
552
+ }
553
+ if (result.status === 'FAILED')
554
+ return `Polling ${name} failed: ${result.error ?? 'unknown error'}`;
555
+ const partial = result.status === 'PARTIAL' ? ' Some updates were refused; see the log.' : '';
556
+ return `Polled ${name}: ${result.recordsFetched} records read, ${result.updatesAccepted} tracking events added.${partial}`;
557
+ }
558
+
308
559
  export function eventStatusTone(status: IntegrationEventStatus): BadgeTone {
309
560
  if (status === 'ACCEPTED') return 'default';
310
561
  if (status === 'DUPLICATE' || status === 'PARTIAL' || status === 'RECEIVED') return 'secondary';
311
562
  return 'destructive';
312
563
  }
313
564
 
314
- /** What a webhook sender sees for each status, shown next to the status control. */
315
- export function statusExplanation(status: IntegrationStatus): string {
565
+ /** What each status means for a feed, shown next to the status control. */
566
+ export function statusExplanation(
567
+ status: IntegrationStatus,
568
+ provider: IntegrationProvider = 'GENERIC_WEBHOOK'
569
+ ): string {
570
+ if (isPolledProvider(provider)) {
571
+ if (status === 'ACTIVE')
572
+ return `Polled every ${GEOTAB_POLL_INTERVAL_MINUTES} minutes; new records become tracking events.`;
573
+ return 'Not polled. Resume to start reading the feed again from where it stopped.';
574
+ }
316
575
  if (status === 'ACTIVE') return 'Signed deliveries are accepted and become tracking events.';
317
576
  if (status === 'PAUSED')
318
577
  return 'Deliveries are refused with HTTP 423 so the sender can retry after you resume.';