@burdenoff/microfe-movethewheels 2026.1108.3 → 2026.1108.4

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,15 @@
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).
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
8
  * with Samsara's own timestamped signature using the secret Samsara issues.
8
- * Every accepted update becomes a tracking event on the matching shipment.
9
+ * Geotab has no push webhooks, so a Geotab feed is pulled: the service signs
10
+ * in to MyGeotab with the credentials entered here and polls its data feeds
11
+ * every few minutes. Every accepted update becomes a tracking event on the
12
+ * matching shipment.
9
13
  *
10
14
  * This module holds the GraphQL documents and the pure helpers the page uses;
11
15
  * the network calls live in `hooks/useCarrierIntegrations`.
@@ -16,9 +20,16 @@ import { resolveGraphqlUrl } from './graphqlClient';
16
20
  * Providers this page can connect. The service also ingests SHIPROCKET, which
17
21
  * is set up from the Shiprocket carrier flow rather than here.
18
22
  */
19
- export const SUPPORTED_PROVIDERS = ['GENERIC_WEBHOOK', 'SAMSARA'] as const;
23
+ export const SUPPORTED_PROVIDERS = ['GENERIC_WEBHOOK', 'SAMSARA', 'GEOTAB'] as const;
20
24
  export type SupportedProvider = (typeof SUPPORTED_PROVIDERS)[number];
21
25
 
26
+ /** Providers the service pulls on a schedule instead of receiving webhooks. */
27
+ export const POLLED_PROVIDERS: ReadonlySet<string> = new Set(['GEOTAB']);
28
+
29
+ export function isPolledProvider(provider: string): boolean {
30
+ return POLLED_PROVIDERS.has(provider);
31
+ }
32
+
22
33
  /** Named networks without an adapter yet; the service refuses the reserved ones. */
23
34
  export const ROADMAP_PROVIDERS = [
24
35
  { id: 'PROJECT44', label: 'project44' },
@@ -26,14 +37,14 @@ export const ROADMAP_PROVIDERS = [
26
37
  { id: 'DESCARTES', label: 'Descartes' },
27
38
  ] as const;
28
39
 
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;
40
+ /** ELD networks planned after Samsara and Geotab (BOFF-10703 follow-ups); not in the schema yet. */
41
+ export const PLANNED_ELD_PROVIDERS = ['Motive'] as const;
31
42
 
32
43
  export const INTEGRATION_STATUSES = ['ACTIVE', 'PAUSED', 'DISABLED'] as const;
33
44
  export type IntegrationStatus = (typeof INTEGRATION_STATUSES)[number];
34
45
 
35
46
  export type IntegrationProvider =
36
- 'GENERIC_WEBHOOK' | 'SAMSARA' | 'SHIPROCKET' | 'PROJECT44' | 'FOURKITES' | 'DESCARTES';
47
+ 'GENERIC_WEBHOOK' | 'SAMSARA' | 'GEOTAB' | 'SHIPROCKET' | 'PROJECT44' | 'FOURKITES' | 'DESCARTES';
37
48
 
38
49
  /** How each provider is named on the page. */
39
50
  export function providerLabel(provider: IntegrationProvider): string {
@@ -42,6 +53,8 @@ export function providerLabel(provider: IntegrationProvider): string {
42
53
  return 'Signed webhook';
43
54
  case 'SAMSARA':
44
55
  return 'Samsara';
56
+ case 'GEOTAB':
57
+ return 'Geotab';
45
58
  case 'SHIPROCKET':
46
59
  return 'Shiprocket';
47
60
  case 'PROJECT44':
@@ -92,6 +105,137 @@ export function externalIdKeyError(key: string): string | null {
92
105
  return null;
93
106
  }
94
107
 
108
+ /** MyGeotab host used when the server field is left blank. */
109
+ export const DEFAULT_GEOTAB_SERVER = 'my.geotab.com';
110
+
111
+ /** How often the service polls each Geotab feed. */
112
+ export const GEOTAB_POLL_INTERVAL_MINUTES = 5;
113
+
114
+ /** Tracking events a Geotab exception rule may be mapped to. */
115
+ export const GEOTAB_RULE_EVENT_TYPES = [
116
+ 'PICKED_UP',
117
+ 'IN_TRANSIT',
118
+ 'ARRIVED',
119
+ 'DEPARTED',
120
+ 'OUT_FOR_DELIVERY',
121
+ 'DELIVERED',
122
+ 'FAILED',
123
+ 'RETURNED',
124
+ ] as const;
125
+ export type GeotabRuleEventType = (typeof GEOTAB_RULE_EVENT_TYPES)[number];
126
+
127
+ /** The service accepts at most this many rule mappings per Geotab feed. */
128
+ export const MAX_GEOTAB_RULE_MAPPINGS = 50;
129
+
130
+ /** The service refuses MyGeotab passwords longer than this. */
131
+ export const MAX_GEOTAB_PASSWORD_LENGTH = 255;
132
+
133
+ export interface GeotabConnectionInput {
134
+ server: string;
135
+ database: string;
136
+ userName: string;
137
+ /** One `<rule id> = <EVENT_TYPE>` per line. */
138
+ ruleMappings: string;
139
+ locationUpdates: boolean;
140
+ }
141
+
142
+ export interface GeotabConfig {
143
+ server: string;
144
+ database: string;
145
+ userName: string;
146
+ ruleEvents: Record<string, GeotabRuleEventType>;
147
+ locationUpdates: boolean;
148
+ }
149
+
150
+ const GEOTAB_HOST = /^(?=.{1,253}$)[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.geotab\.com$/;
151
+ const GEOTAB_RULE_ID = /^[A-Za-z0-9_.-]{1,64}$/;
152
+
153
+ /** The MyGeotab host the service will contact, or null when it is not a geotab.com host. */
154
+ export function normalizeGeotabServer(value: string): string | null {
155
+ const trimmed = value.trim().toLowerCase();
156
+ if (!trimmed) return DEFAULT_GEOTAB_SERVER;
157
+ const host = trimmed.replace(/^https:\/\//, '').replace(/\/(apiv1\/?)?$/, '');
158
+ return GEOTAB_HOST.test(host) ? host : null;
159
+ }
160
+
161
+ /**
162
+ * Parses rule mappings typed one per line as `<rule id> = <EVENT_TYPE>` (a
163
+ * colon works too; blank lines and `#` comments are ignored).
164
+ */
165
+ export function parseGeotabRuleMappings(
166
+ text: string
167
+ ): { ok: true; ruleEvents: Record<string, GeotabRuleEventType> } | { ok: false; error: string } {
168
+ const ruleEvents: Record<string, GeotabRuleEventType> = {};
169
+ const lines = text.split(/\r?\n/);
170
+ for (const [index, raw] of lines.entries()) {
171
+ const line = raw.trim();
172
+ if (!line || line.startsWith('#')) continue;
173
+ const match = /^([^=:]+)[=:](.+)$/.exec(line);
174
+ const ruleId = match?.[1]?.trim() ?? '';
175
+ const eventType = (match?.[2]?.trim() ?? '').toUpperCase().replace(/[\s-]+/g, '_');
176
+ if (!match || !GEOTAB_RULE_ID.test(ruleId)) {
177
+ return {
178
+ ok: false,
179
+ error: `Line ${index + 1}: write each mapping as <rule id> = <event>, e.g. RuleArriveId = ARRIVED.`,
180
+ };
181
+ }
182
+ if (!(GEOTAB_RULE_EVENT_TYPES as readonly string[]).includes(eventType)) {
183
+ return {
184
+ ok: false,
185
+ error: `Line ${index + 1}: use one of ${GEOTAB_RULE_EVENT_TYPES.join(', ')}.`,
186
+ };
187
+ }
188
+ ruleEvents[ruleId] = eventType as GeotabRuleEventType;
189
+ }
190
+ if (Object.keys(ruleEvents).length > MAX_GEOTAB_RULE_MAPPINGS) {
191
+ return { ok: false, error: `Map at most ${MAX_GEOTAB_RULE_MAPPINGS} rules.` };
192
+ }
193
+ return { ok: true, ruleEvents };
194
+ }
195
+
196
+ /** Validates the Geotab connection fields and builds the config the service stores. */
197
+ export function buildGeotabConfig(
198
+ input: GeotabConnectionInput
199
+ ): { ok: true; config: GeotabConfig } | { ok: false; error: string } {
200
+ const server = normalizeGeotabServer(input.server);
201
+ if (!server) return { ok: false, error: 'Use your MyGeotab server, e.g. my.geotab.com.' };
202
+ const database = input.database.trim();
203
+ if (!database || database.length > 255) {
204
+ return { ok: false, error: 'Enter the MyGeotab database name.' };
205
+ }
206
+ const userName = input.userName.trim();
207
+ if (!userName || userName.length > 255) {
208
+ return { ok: false, error: 'Enter the MyGeotab user name.' };
209
+ }
210
+ const rules = parseGeotabRuleMappings(input.ruleMappings);
211
+ if (!rules.ok) return rules;
212
+ if (!input.locationUpdates && Object.keys(rules.ruleEvents).length === 0) {
213
+ return {
214
+ ok: false,
215
+ error: 'Turn on GPS position updates or map at least one exception rule, or nothing is read.',
216
+ };
217
+ }
218
+ return {
219
+ ok: true,
220
+ config: {
221
+ server,
222
+ database,
223
+ userName,
224
+ ruleEvents: rules.ruleEvents,
225
+ locationUpdates: input.locationUpdates,
226
+ },
227
+ };
228
+ }
229
+
230
+ /** Returns an error message for an unusable MyGeotab password, or null. */
231
+ export function geotabPasswordError(password: string): string | null {
232
+ if (!password) return 'Enter the MyGeotab password.';
233
+ if (password.length > MAX_GEOTAB_PASSWORD_LENGTH) {
234
+ return `The password can be at most ${MAX_GEOTAB_PASSWORD_LENGTH} characters.`;
235
+ }
236
+ return null;
237
+ }
238
+
95
239
  /** Headers Samsara signs each delivery with; the workspace gateway forwards both. */
96
240
  export const SAMSARA_SIGNATURE_HEADERS = ['X-Samsara-Signature', 'X-Samsara-Timestamp'] as const;
97
241
 
@@ -120,6 +264,31 @@ export const SIGNATURE_HEADER = 'x-webhook-signature';
120
264
  /** Updates accepted in one delivery (a JSON array of at most this many objects). */
121
265
  export const MAX_UPDATES_PER_DELIVERY = 5;
122
266
 
267
+ export type PollStatus = 'OK' | 'PARTIAL' | 'FAILED';
268
+ export type PollResultStatus = PollStatus | 'SKIPPED';
269
+
270
+ /** Latest poll of a pulled feed (Geotab); null for webhook feeds. */
271
+ export interface IntegrationPollStatus {
272
+ lastPollAt: string | null;
273
+ lastSuccessAt: string | null;
274
+ lastPollStatus: PollStatus | null;
275
+ lastPollError: string | null;
276
+ consecutiveFailures: number;
277
+ lastRecordsFetched: number;
278
+ lastUpdatesAccepted: number;
279
+ lastUpdatesSkipped: number;
280
+ }
281
+
282
+ export interface IntegrationPollResult {
283
+ integrationId: string;
284
+ status: PollResultStatus;
285
+ skippedReason: string | null;
286
+ recordsFetched: number;
287
+ updatesAccepted: number;
288
+ updatesSkipped: number;
289
+ error: string | null;
290
+ }
291
+
123
292
  export interface CarrierIntegration {
124
293
  id: string;
125
294
  workspaceId: string;
@@ -131,6 +300,7 @@ export interface CarrierIntegration {
131
300
  lastErrorMessage: string | null;
132
301
  createdAt: string;
133
302
  updatedAt: string;
303
+ pollStatus: IntegrationPollStatus | null;
134
304
  }
135
305
 
136
306
  export interface CarrierIntegrationEvent {
@@ -156,6 +326,16 @@ const INTEGRATION_FIELDS = `
156
326
  lastErrorMessage
157
327
  createdAt
158
328
  updatedAt
329
+ pollStatus {
330
+ lastPollAt
331
+ lastSuccessAt
332
+ lastPollStatus
333
+ lastPollError
334
+ consecutiveFailures
335
+ lastRecordsFetched
336
+ lastUpdatesAccepted
337
+ lastUpdatesSkipped
338
+ }
159
339
  `;
160
340
 
161
341
  export const LIST_INTEGRATIONS_QUERY = `
@@ -206,6 +386,20 @@ export const UPDATE_INTEGRATION_MUTATION = `
206
386
  }
207
387
  `;
208
388
 
389
+ export const POLL_INTEGRATION_NOW_MUTATION = `
390
+ mutation PollMtwCarrierIntegrationNow($id: ID!) {
391
+ pollIntegrationNow(id: $id) {
392
+ integrationId
393
+ status
394
+ skippedReason
395
+ recordsFetched
396
+ updatesAccepted
397
+ updatesSkipped
398
+ error
399
+ }
400
+ }
401
+ `;
402
+
209
403
  export const DELETE_INTEGRATION_MUTATION = `
210
404
  mutation DeleteMtwCarrierIntegration($id: ID!) {
211
405
  deleteIntegration(id: $id)
@@ -305,14 +499,62 @@ export function integrationStatusTone(status: IntegrationStatus): BadgeTone {
305
499
  return 'outline';
306
500
  }
307
501
 
502
+ export function pollStatusTone(status: PollStatus | null): BadgeTone {
503
+ if (status === 'OK') return 'default';
504
+ if (status === 'PARTIAL') return 'secondary';
505
+ if (status === 'FAILED') return 'destructive';
506
+ return 'outline';
507
+ }
508
+
509
+ /** One line describing the latest poll, for the integration card. */
510
+ export function pollStatusSummary(
511
+ pollStatus: IntegrationPollStatus | null,
512
+ format: (iso: string) => string
513
+ ): string {
514
+ if (!pollStatus?.lastPollAt) return 'Not polled yet; the first poll runs within a few minutes.';
515
+ const when = format(pollStatus.lastPollAt);
516
+ if (pollStatus.lastPollStatus === 'FAILED') {
517
+ const lastGood = pollStatus.lastSuccessAt
518
+ ? ` Last successful poll ${format(pollStatus.lastSuccessAt)}.`
519
+ : '';
520
+ const failures =
521
+ pollStatus.consecutiveFailures > 1
522
+ ? ` ${pollStatus.consecutiveFailures} failures in a row.`
523
+ : '';
524
+ return `Last poll ${when} failed.${failures}${lastGood}`;
525
+ }
526
+ 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).`;
527
+ }
528
+
529
+ /** The notice shown after "Poll now". */
530
+ export function pollResultMessage(name: string, result: IntegrationPollResult): string {
531
+ if (result.status === 'SKIPPED') {
532
+ return result.skippedReason === 'POLL_IN_PROGRESS'
533
+ ? `${name} is being polled right now; refresh in a moment.`
534
+ : `${name} was not polled (${humanize(result.skippedReason ?? 'skipped')}).`;
535
+ }
536
+ if (result.status === 'FAILED')
537
+ return `Polling ${name} failed: ${result.error ?? 'unknown error'}`;
538
+ const partial = result.status === 'PARTIAL' ? ' Some updates were refused; see the log.' : '';
539
+ return `Polled ${name}: ${result.recordsFetched} records read, ${result.updatesAccepted} tracking events added.${partial}`;
540
+ }
541
+
308
542
  export function eventStatusTone(status: IntegrationEventStatus): BadgeTone {
309
543
  if (status === 'ACCEPTED') return 'default';
310
544
  if (status === 'DUPLICATE' || status === 'PARTIAL' || status === 'RECEIVED') return 'secondary';
311
545
  return 'destructive';
312
546
  }
313
547
 
314
- /** What a webhook sender sees for each status, shown next to the status control. */
315
- export function statusExplanation(status: IntegrationStatus): string {
548
+ /** What each status means for a feed, shown next to the status control. */
549
+ export function statusExplanation(
550
+ status: IntegrationStatus,
551
+ provider: IntegrationProvider = 'GENERIC_WEBHOOK'
552
+ ): string {
553
+ if (isPolledProvider(provider)) {
554
+ if (status === 'ACTIVE')
555
+ return `Polled every ${GEOTAB_POLL_INTERVAL_MINUTES} minutes; new records become tracking events.`;
556
+ return 'Not polled. Resume to start reading the feed again from where it stopped.';
557
+ }
316
558
  if (status === 'ACTIVE') return 'Signed deliveries are accepted and become tracking events.';
317
559
  if (status === 'PAUSED')
318
560
  return 'Deliveries are refused with HTTP 423 so the sender can retry after you resume.';