Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
"expo-task-manager": "~57.0.20",
"expo-video": "~57.0.5",
"expo-web-browser": "~57.0.3",
"firebase": "^12.19.0",
"geojson": "~0.5.0",
"i18next": "~23.14.0",
"livekit-client": "^2.20.1",
Expand Down
7 changes: 7 additions & 0 deletions public/.well-known/apple-app-site-association
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"webcredentials": {
"apps": [
"QKQVAJMTCN.com.wavetech.Resgrid"
]
}
}
14 changes: 14 additions & 0 deletions public/.well-known/assetlinks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"relation": [
"delegate_permission/common.get_login_creds"
],
"target": {
"namespace": "android_app",
"package_name": "wtdt.resgrid.andriod",
"sha256_cert_fingerprints": [
"FF:56:FC:B4:6B:4F:07:41:20:92:00:08:AA:D0:46:E3:C8:7A:B8:95:F1:CF:A6:68:46:F0:D2:55:42:1C:77:2D"
]
}
}
]
113 changes: 113 additions & 0 deletions public/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/* eslint-disable no-undef */
/**
* Service worker for Resgrid Responder web push.
*
* The page mints its FCM web token against this registration (services/web-push.web.ts), but
* Firebase is not loaded here: every push is shown below. Core sends the fields in the FCM webpush
* block (NovuProvider.SendNotification): data carries title, message, eventCode, type and category.
*
* The app does the routing. A click focuses an open window and posts NOTIFICATION_CLICK to it; with no
* window open, the new one is handed the push after it says CLIENT_READY. Every push is also posted as
* PUSH_RECEIVED so a window the person is looking at can show its in-app alert.
*/

const DEFAULT_TITLE = 'Resgrid';

// Clicks waiting for the window they opened to finish loading, by client id.
const pendingClicks = new Map();

function readPush(event) {
let payload = {};
if (event.data) {
try {
payload = event.data.json() || {};
} catch {
payload = { notification: { body: event.data.text() } };
}
}

const notification = payload.notification || {};
const data = payload.data || {};

return {
title: data.title || notification.title || payload.title || DEFAULT_TITLE,
body: data.message || notification.body || data.body || payload.body || payload.message || '',
eventCode: data.eventCode || payload.eventCode || '',
type: data.type || '',
category: data.category || '',
};
}

function isCall(push) {
return push.category === 'calls' || /^c(?!t)/i.test(push.eventCode);
}

self.addEventListener('push', (event) => {
const push = readPush(event);

event.waitUntil(
Promise.all([
self.registration.showNotification(push.title, {
body: push.body,
icon: '/favicon.ico',
tag: push.eventCode || undefined,
renotify: !!push.eventCode,
requireInteraction: isCall(push),
data: push,
}),
self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then((windows) => {
windows.forEach((client) => client.postMessage({ type: 'PUSH_RECEIVED', data: push }));
}),
])
);
});

self.addEventListener('notificationclick', (event) => {
event.notification.close();
const data = event.notification.data || {};

event.waitUntil(
self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then((windows) => {
const client = windows.find((candidate) => 'focus' in candidate);
if (client) {
return client.focus().then((focused) => (focused || client).postMessage({ type: 'NOTIFICATION_CLICK', data }));
}

if (!self.clients.openWindow) {
return undefined;
}

return self.clients.openWindow('/').then((opened) => {
if (opened) {
pendingClicks.set(opened.id, data);
}
});
})
);
});

self.addEventListener('install', () => {
self.skipWaiting();
});

self.addEventListener('activate', (event) => {
event.waitUntil(self.clients.claim());
});

self.addEventListener('message', (event) => {
if (!event.data || !event.source) {
return;
}

if (event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
return;
}

// A window this worker opened has loaded: hand it the click that opened it.
if (event.data.type === 'CLIENT_READY' && pendingClicks.has(event.source.id)) {
const data = pendingClicks.get(event.source.id);
pendingClicks.delete(event.source.id);
event.source.postMessage({ type: 'NOTIFICATION_CLICK', data });
}
});
42 changes: 42 additions & 0 deletions src/api/calls/__tests__/callLocationHistory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { createApiEndpoint } from '../../common/client';
import { getCallLocationHistory } from '../callLocationHistory';

jest.mock('../../common/client', () => {
const get = jest.fn();
return {
createApiEndpoint: jest.fn(() => ({ get })),
__mockGet: get,
};
});

const { __mockGet: mockGet } = jest.requireMock('../../common/client') as { __mockGet: jest.Mock };

// Endpoints are created at module load; capture them before beforeEach clears the mock.
const registeredPaths = (createApiEndpoint as jest.Mock).mock.calls.map((c) => c[0]);

describe('callLocationHistory api', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('targets Calls/GetCallLocationHistory', () => {
expect(registeredPaths).toEqual(['/Calls/GetCallLocationHistory']);
});

it('passes callId as a query parameter and returns the body', async () => {
const payload = { Data: { AddressMatchingAvailable: true, IndexComplete: true, HasMore: false, IsProtected: false, Calls: [] } };
mockGet.mockResolvedValueOnce({ data: payload });

await expect(getCallLocationHistory('42')).resolves.toBe(payload);
expect(mockGet).toHaveBeenCalledWith({ callId: '42' }, undefined);
});

it('forwards the abort signal', async () => {
const controller = new AbortController();
mockGet.mockResolvedValueOnce({ data: { Data: null } });

await getCallLocationHistory('42', controller.signal);

expect(mockGet).toHaveBeenCalledWith({ callId: '42' }, controller.signal);
});
});
15 changes: 15 additions & 0 deletions src/api/calls/callLocationHistory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { type LocationHistoryResult } from '@/models/v4/calls/locationHistoryResult';

import { createApiEndpoint } from '../common/client';

// v4 Calls/GetCallLocationHistory: other calls at this call's location (address however it was typed, or nearby when one
// side has no street address) and calls linked to the same contacts, newest first with their notes. Needs Call_View and is
// limited to the calls the user may see. The client attaches the Protected Data Grant header automatically; without one,
// a protected department's call text and notes come back REDACTED.

const getCallLocationHistoryApi = createApiEndpoint('/Calls/GetCallLocationHistory');

export const getCallLocationHistory = async (callId: string, signal?: AbortSignal) => {
const response = await getCallLocationHistoryApi.get<LocationHistoryResult>({ callId }, signal);
return response.data;
};
33 changes: 33 additions & 0 deletions src/api/contacts/__tests__/contactCallHistory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createApiEndpoint } from '../../common/client';
import { getContactCallHistory } from '../contactCallHistory';

jest.mock('../../common/client', () => {
const get = jest.fn();
return {
createApiEndpoint: jest.fn(() => ({ get })),
__mockGet: get,
};
});

const { __mockGet: mockGet } = jest.requireMock('../../common/client') as { __mockGet: jest.Mock };

// Endpoints are created at module load; capture them before beforeEach clears the mock.
const registeredPaths = (createApiEndpoint as jest.Mock).mock.calls.map((c) => c[0]);

describe('contactCallHistory api', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('targets Contacts/GetContactCallHistory', () => {
expect(registeredPaths).toEqual(['/Contacts/GetContactCallHistory']);
});

it('passes contactId as a query parameter and returns the body', async () => {
const payload = { Data: { AddressMatchingAvailable: true, IndexComplete: true, HasMore: false, IsProtected: false, Calls: [] } };
mockGet.mockResolvedValueOnce({ data: payload });

await expect(getContactCallHistory('c1')).resolves.toBe(payload);
expect(mockGet).toHaveBeenCalledWith({ contactId: 'c1' }, undefined);
});
});
14 changes: 14 additions & 0 deletions src/api/contacts/contactCallHistory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { type LocationHistoryResult } from '@/models/v4/calls/locationHistoryResult';

import { createApiEndpoint } from '../common/client';

// v4 Contacts/GetContactCallHistory: calls linked to a contact plus calls at every occupancy it is linked to (one contact can
// have several, such as a business with more than one location), newest first with their notes. Needs Contacts_View and is
// limited to the calls the user may see.

const getContactCallHistoryApi = createApiEndpoint('/Contacts/GetContactCallHistory');

export const getContactCallHistory = async (contactId: string, signal?: AbortSignal) => {
const response = await getContactCallHistoryApi.get<LocationHistoryResult>({ contactId }, signal);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kody code-review Kody Rules high

Unhandled external API failures from getContactCallHistoryApi.get omit the contactId context in src/api/contacts/contactCallHistory.ts; wrap the call in try/catch and map or rethrow the error with that identifier. Apply the same handling in src/api/calls/callLocationHistory.ts:13-13, src/api/devices/push.ts:27-27, and src/services/__tests__/service-worker.test.ts:12-12.

Kody rule violation: Add try-catch blocks for external calls

let response;
try {
  response = await getContactCallHistoryApi.get<LocationHistoryResult>({ contactId }, signal);
} catch (error) {
  throw new Error(`Failed to get contact call history for contact ${contactId}`, { cause: error });
}
Prompt for LLM

File src/api/contacts/contactCallHistory.ts:

Line 12:

Unhandled external API failures from `getContactCallHistoryApi.get` omit the `contactId` context in `src/api/contacts/contactCallHistory.ts`; wrap the call in `try/catch` and map or rethrow the error with that identifier. Apply the same handling in `src/api/calls/callLocationHistory.ts:13-13`, `src/api/devices/push.ts:27-27`, and `src/services/__tests__/service-worker.test.ts:12-12`.

Suggested Code:

  let response;
  try {
    response = await getContactCallHistoryApi.get<LocationHistoryResult>({ contactId }, signal);
  } catch (error) {
    throw new Error(`Failed to get contact call history for contact ${contactId}`, { cause: error });
  }

Talk to Kody by mentioning @kody

Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.

​

​

return response.data;
};
9 changes: 9 additions & 0 deletions src/api/devices/push.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { type PushRegistrationInput } from '@/models/v4/device/pushRegistrationInput';
import { type PushRegistrationResult } from '@/models/v4/device/pushRegistrationResult';
import { type PushRegistrationUnitInput } from '@/models/v4/device/pushRegistrationUnitInput';
import { type WebPushUnRegistrationInput } from '@/models/v4/device/webPushUnRegistrationInput';

import { createApiEndpoint } from '../common/client';

const registerUnitDeviceApi = createApiEndpoint('/Devices/RegisterUnitDevice');
const registerDeviceApi = createApiEndpoint('/Devices/RegisterDevice');
const unRegisterWebPushApi = createApiEndpoint('/Devices/UnRegisterWebPush');

export const registerUnitDevice = async (data: PushRegistrationUnitInput) => {
const response = await registerUnitDeviceApi.post<PushRegistrationResult>({
Expand All @@ -20,3 +22,10 @@ export const registerDevice = async (data: PushRegistrationInput) => {
});
return response.data;
};

export const unRegisterWebPush = async (data: WebPushUnRegistrationInput) => {
const response = await unRegisterWebPushApi.post<PushRegistrationResult>({
...data,
});
return response.data;
};
3 changes: 3 additions & 0 deletions src/app/(app)/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { locationService } from '@/services/location';
import { offlineEventManager } from '@/services/offline-event-manager.service';
import { offlineQueueService } from '@/services/offline-queue.service';
import { usePushNotifications } from '@/services/push-notification';
import { useWebPushRegistration } from '@/services/web-push';
import { useCoreStore } from '@/stores/app/core-store';
import { useCalendarStore } from '@/stores/calendar/store';
import { useCallsStore } from '@/stores/calls/store';
Expand Down Expand Up @@ -100,6 +101,8 @@ export default function TabLayout() {

// Initialize push notifications
usePushNotifications();
// Browser push on the web edition (a no-op on the phone).
useWebPushRegistration();

const initializeApp = useCallback(async () => {
if (isInitializing.current) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ jest.mock('@/components/calls/call-files-modal', () => () => null);
jest.mock('@/components/calls/call-images-modal', () => () => null);
jest.mock('@/components/calls/call-notes-modal', () => () => null);
jest.mock('@/components/calls/call-site-info-tab-panel', () => ({ CallSiteInfoTabPanel: () => null }));
jest.mock('@/components/calls/location-history-panel', () => ({ LocationHistoryPanel: () => null }));
jest.mock('@/components/calls/close-call-bottom-sheet', () => ({ CloseCallBottomSheet: () => null }));
jest.mock('@/components/check-in/check-in-tab-panel', () => ({ CheckInTabPanel: () => null }));
jest.mock('@/components/common/header-back-button', () => ({ HeaderBackButton: () => null }));
Expand Down
8 changes: 8 additions & 0 deletions src/app/call/[id]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import CallImagesModal from '@/components/calls/call-images-modal';
import CallNotesModal from '@/components/calls/call-notes-modal';
import { CallSiteInfoTabPanel } from '@/components/calls/call-site-info-tab-panel';
import { CloseCallBottomSheet } from '@/components/calls/close-call-bottom-sheet';
import { LocationHistoryPanel } from '@/components/calls/location-history-panel';
import { CheckInTabPanel } from '@/components/check-in/check-in-tab-panel';
import { HeaderBackButton } from '@/components/common/header-back-button';
import { Loading } from '@/components/common/loading';
Expand Down Expand Up @@ -489,6 +490,13 @@ export default function CallDetail() {
content: <CallSiteInfoTabPanel callId={call.CallId} />,
});

// History tab: previous calls at this location (address however it was typed) or with the same contacts.
builtTabs.push({
key: 'history',
title: t('call_detail.tabs.history'),
content: <LocationHistoryPanel source={{ kind: 'call', id: call.CallId }} />,
});

builtTabs.push({
key: 'command',
title: t('call_detail.tabs.command'),
Expand Down
Loading
Loading