diff --git a/src/components/LastCheckedDisplay.tsx b/src/components/LastCheckedDisplay.tsx index df48876b4..0b18a9fd5 100644 --- a/src/components/LastCheckedDisplay.tsx +++ b/src/components/LastCheckedDisplay.tsx @@ -12,9 +12,9 @@ export interface LastCheckedDisplayProps { export const LastCheckedDisplay = ({textDark}: LastCheckedDisplayProps) => { const [i18n] = useI18n(); const [exposureStatus] = useExposureStatus(); - if (!exposureStatus.lastCheckedTimestamp) return null; + if (!exposureStatus.lastChecked?.timestamp) return null; - const lastCheckedDate = new Date(exposureStatus.lastCheckedTimestamp); + const lastCheckedDate = new Date(exposureStatus.lastChecked.timestamp); const daysDiff = daysFromNow(lastCheckedDate); const hoursDiff = hoursFromNow(lastCheckedDate); const minutesDiff = Math.max(minutesFromNow(lastCheckedDate), 1); diff --git a/src/services/ExposureNotificationService/ExposureNotificationService.spec.ts b/src/services/ExposureNotificationService/ExposureNotificationService.spec.ts index 8632b6965..7f1624eaf 100644 --- a/src/services/ExposureNotificationService/ExposureNotificationService.spec.ts +++ b/src/services/ExposureNotificationService/ExposureNotificationService.spec.ts @@ -92,8 +92,10 @@ describe('ExposureNotificationService', () => { }); service.exposureStatus.append({ - lastCheckedTimestamp: new OriginalDate('2020-05-19T06:10:00+0000').getTime(), - lastCheckedPeriod: periodSinceEpoch(new OriginalDate('2020-05-19T06:10:00+0000'), HOURS_PER_PERIOD), + lastChecked: { + timestamp: new OriginalDate('2020-05-19T06:10:00+0000').getTime(), + period: periodSinceEpoch(new OriginalDate('2020-05-19T06:10:00+0000'), HOURS_PER_PERIOD), + }, }); await service.updateExposureStatus(); expect(server.retrieveDiagnosisKeys).toHaveBeenCalledTimes(0); @@ -101,8 +103,10 @@ describe('ExposureNotificationService', () => { server.retrieveDiagnosisKeys.mockClear(); service.exposureStatus.append({ - lastCheckedTimestamp: new OriginalDate('2020-05-18T05:10:00+0000').getTime(), - lastCheckedPeriod: periodSinceEpoch(new OriginalDate('2020-05-18T05:10:00+0000'), HOURS_PER_PERIOD), + lastChecked: { + timestamp: new OriginalDate('2020-05-18T05:10:00+0000').getTime(), + period: periodSinceEpoch(new OriginalDate('2020-05-18T05:10:00+0000'), HOURS_PER_PERIOD), + }, }); await service.updateExposureStatus(); expect(server.retrieveDiagnosisKeys).toHaveBeenCalledTimes(1); @@ -110,8 +114,10 @@ describe('ExposureNotificationService', () => { server.retrieveDiagnosisKeys.mockClear(); service.exposureStatus.append({ - lastCheckedTimestamp: new OriginalDate('2020-05-17T23:10:00+0000').getTime(), - lastCheckedPeriod: periodSinceEpoch(new OriginalDate('2020-05-17T23:10:00+0000'), HOURS_PER_PERIOD), + lastChecked: { + timestamp: new OriginalDate('2020-05-17T23:10:00+0000').getTime(), + period: periodSinceEpoch(new OriginalDate('2020-05-17T23:10:00+0000'), HOURS_PER_PERIOD), + }, }); await service.updateExposureStatus(); expect(server.retrieveDiagnosisKeys).toHaveBeenCalledTimes(2); @@ -132,8 +138,10 @@ describe('ExposureNotificationService', () => { }); service.exposureStatus.append({ - lastCheckedTimestamp: new OriginalDate('2020-05-18T04:10:00+0000').getTime(), - lastCheckedPeriod: periodSinceEpoch(new OriginalDate('2020-05-18T04:10:00+0000'), HOURS_PER_PERIOD), + lastChecked: { + timestamp: new OriginalDate('2020-05-18T04:10:00+0000').getTime(), + period: periodSinceEpoch(new OriginalDate('2020-05-18T04:10:00+0000'), HOURS_PER_PERIOD), + }, }); const currentPeriod = periodSinceEpoch(currentDatetime, HOURS_PER_PERIOD); @@ -146,8 +154,10 @@ describe('ExposureNotificationService', () => { expect(storage.setItem).toHaveBeenCalledWith( EXPOSURE_STATUS, expect.jsonStringContaining({ - lastCheckedTimestamp: currentDatetime.getTime(), - lastCheckedPeriod: currentPeriod - 1, + lastChecked: { + timestamp: currentDatetime.getTime(), + period: currentPeriod - 1, + }, }), ); }); diff --git a/src/services/ExposureNotificationService/ExposureNotificationService.ts b/src/services/ExposureNotificationService/ExposureNotificationService.ts index 990323620..28a0c6577 100644 --- a/src/services/ExposureNotificationService/ExposureNotificationService.ts +++ b/src/services/ExposureNotificationService/ExposureNotificationService.ts @@ -1,8 +1,8 @@ import {Platform} from 'react-native'; import ExposureNotification, { - ExposureConfiguration, ExposureSummary, Status as SystemStatus, + ExposureConfiguration, } from 'bridge/ExposureNotification'; import PushNotification from 'bridge/PushNotification'; import {addDays, daysBetween, periodSinceEpoch} from 'shared/date-fns'; @@ -15,7 +15,7 @@ import {BackendInterface, SubmissionKeySet} from '../BackendService'; import defaultExposureConfiguration from './DefaultExposureConfiguration.json'; const SUBMISSION_AUTH_KEYS = 'submissionAuthKeys'; -const EXPOSURE_CONFIGURATION = 'exposure-configuration'; +const EXPOSURE_CONFIGURATION = 'exposureConfiguration'; const SECURE_OPTIONS = { sharedPreferencesName: 'covidShieldSharedPreferences', @@ -34,14 +34,18 @@ export {SystemStatus}; export type ExposureStatus = | { type: 'monitoring'; - lastCheckedPeriod?: number; - lastCheckedTimestamp?: number; + lastChecked?: { + period: number; + timestamp: number; + }; } | { type: 'exposed'; summary: ExposureSummary; - lastCheckedPeriod?: number; - lastCheckedTimestamp?: number; + lastChecked?: { + period: number; + timestamp: number; + }; } | { type: 'diagnosed'; @@ -49,8 +53,10 @@ export type ExposureStatus = submissionLastCompletedAt?: number; cycleStartsAt: number; cycleEndsAt: number; - lastCheckedPeriod?: number; - lastCheckedTimestamp?: number; + lastChecked?: { + period: number; + timestamp: number; + }; }; export interface PersistencyProvider { @@ -196,7 +202,7 @@ export class ExposureNotificationService { * If the exposureConfiguration is not available from the server for some reason, * try and use a previously stored configuration, or use the default configuration bundled with the app. */ - async getAlternateExposureConfiguration(): Promise { + private async getAlternateExposureConfiguration(): Promise { try { const exposureConfigurationStr = await this.secureStorage.getItem( EXPOSURE_CONFIGURATION, @@ -244,12 +250,8 @@ export class ExposureNotificationService { ): AsyncGenerator<{keysFileUrl: string; period: number} | null> { const runningDate = new Date(); - let lastCheckedPeriod = + const lastCheckedPeriod = _lastCheckedPeriod || periodSinceEpoch(addDays(runningDate, -EXPOSURE_NOTIFICATION_CYCLE), HOURS_PER_PERIOD); - if (TEST_MODE) { - lastCheckedPeriod = periodSinceEpoch(addDays(runningDate, -EXPOSURE_NOTIFICATION_CYCLE), HOURS_PER_PERIOD); - } - let runningPeriod = periodSinceEpoch(runningDate, HOURS_PER_PERIOD); while (runningPeriod > lastCheckedPeriod) { @@ -282,9 +284,15 @@ export class ExposureNotificationService { exposureConfiguration = await this.getAlternateExposureConfiguration(); } - const finalize = async (status: Partial = {}) => { + const finalize = async (status: Partial = {}, lastCheckedPeriod = 0) => { const timestamp = new Date().getTime(); - this.exposureStatus.append({...status, lastCheckedTimestamp: timestamp}); + this.exposureStatus.append({ + ...status, + lastChecked: { + timestamp, + period: lastCheckedPeriod, + }, + }); }; const currentStatus = this.exposureStatus.get(); @@ -306,8 +314,8 @@ export class ExposureNotificationService { } const keysFileUrls: string[] = []; - const generator = this.keysSinceLastFetch(currentStatus.lastCheckedPeriod); - let lastCheckedPeriod = currentStatus.lastCheckedPeriod; + const generator = this.keysSinceLastFetch(currentStatus.lastChecked?.period); + let lastCheckedPeriod = currentStatus.lastChecked?.period; while (true) { const {value, done} = await generator.next(); if (done) break; @@ -315,6 +323,12 @@ export class ExposureNotificationService { const {keysFileUrl, period} = value; keysFileUrls.push(keysFileUrl); + // Temporarily disable lastCheckPeriod + // Ref https://github.com/cds-snc/covid-shield-server/pull/158 + if (TEST_MODE) { + continue; + } + // Temporarily disable persisting lastCheckPeriod on Android // Ref https://github.com/cds-snc/covid-shield-mobile/issues/453 if (Platform.OS !== 'android') { @@ -325,12 +339,18 @@ export class ExposureNotificationService { try { const summary = await this.exposureNotification.detectExposure(exposureConfiguration, keysFileUrls); if (summary.matchedKeyCount > 0) { - return finalize({type: 'exposed', summary, lastCheckedPeriod}); + return finalize( + { + type: 'exposed', + summary, + }, + lastCheckedPeriod, + ); } } catch (error) { - console.log('>>> detectExposure', error); + console.log('>>> DetectExposure', error); } - return finalize({lastCheckedPeriod}); + return finalize({}, lastCheckedPeriod); } }