From 483fdeb0d7a427dd2b720c7e3200aec24049650b Mon Sep 17 00:00:00 2001 From: Matan Mashraki <12946462+planecore@users.noreply.github.com> Date: Sat, 9 Sep 2023 15:19:07 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=95=94=20-=20Updated=20wait=20time=20with?= =?UTF-8?q?=20delay=20(#19)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/test.yml | 6 ++++- src/locales/ar.json | 1 + src/locales/en.json | 1 + src/locales/he.json | 1 + src/locales/ru.json | 1 + .../notifications/exchange-trains.test.ts | 11 +++++++-- src/tests/notifications/mocks.ts | 15 +++++++++++- src/utils/date-utils.ts | 4 ++++ src/utils/ride-utils.ts | 23 ++++++++++++++++--- 9 files changed, 56 insertions(+), 7 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 771381c..f2f6fa8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,5 +1,9 @@ name: CI -on: push +on: + push: + branches: + - main + pull_request: jobs: test-server: runs-on: ubuntu-latest diff --git a/src/locales/ar.json b/src/locales/ar.json index 760fa60..746daa5 100644 --- a/src/locales/ar.json +++ b/src/locales/ar.json @@ -14,6 +14,7 @@ }, "exchange": { "waitTime": "انتظر لمدة %{waitTime}.", + "unsafeChange": "هذا التغيير غير مضمون.", "stayOnPlatform": "ابق في المنصة %{platform}.", "changePlatform": "انتقل إلى المنصة %{platform}." } diff --git a/src/locales/en.json b/src/locales/en.json index 39fdc4a..fdad149 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -14,6 +14,7 @@ }, "exchange": { "waitTime": "Wait %{waitTime} for your next train.", + "unsafeChange": "This change isn't guaranteed.", "stayOnPlatform": "Stay on Platform %{platform}.", "changePlatform": "Change to Platform %{platform}." } diff --git a/src/locales/he.json b/src/locales/he.json index b96b888..41f54e8 100644 --- a/src/locales/he.json +++ b/src/locales/he.json @@ -14,6 +14,7 @@ }, "exchange": { "waitTime": "כ-%{waitTime} המתנה לנסיעה הבאה.", + "unsafeChange": "החלפה זו אינה בטוחה.", "stayOnPlatform": "השארו ברציף %{platform}.", "changePlatform": "עברו לרציף %{platform}." } diff --git a/src/locales/ru.json b/src/locales/ru.json index 6537045..22c6a7d 100644 --- a/src/locales/ru.json +++ b/src/locales/ru.json @@ -14,6 +14,7 @@ }, "exchange": { "waitTime": "Подождите %{waitTime}.", + "unsafeChange": "Это изменение не гарантировано.", "stayOnPlatform": "Оставайтесь на платформе %{platform}.", "changePlatform": "Перейдите на платформу %{platform}." } diff --git a/src/tests/notifications/exchange-trains.test.ts b/src/tests/notifications/exchange-trains.test.ts index 74f5c02..0e5ae9a 100644 --- a/src/tests/notifications/exchange-trains.test.ts +++ b/src/tests/notifications/exchange-trains.test.ts @@ -3,7 +3,8 @@ import { keyBy } from "lodash" import { minutesInMs } from "../helpers/utils" import { Status } from "../../types/notification" -import { exchangeRoute, stations, now, exchangeDuration, ride, getOffInExchangeTime } from "./mocks" +import { exchangeTrainPrompt } from "../../utils/ride-utils" +import { exchangeRoute, stations, now, exchangeDuration, ride, getOffInExchangeTime, unsafeChangeTrains } from "./mocks" import { buildGetOnTrainNotifications, buildNextStationNotifications, @@ -136,7 +137,7 @@ test("build get off notifications for exchange train", () => { time: dayjs(now + getOffInExchangeTime - minutesInMs(1)), alert: { title: "Time to get off!", - text: "Change to Platform 4. Wait 5 minutes for your next train.", + text: "Change to Platform 4. Wait 7 minutes for your next train.", }, }, { @@ -204,3 +205,9 @@ test("build get off notifications for exchange train", () => { }, ]) }) + +test("build exchange notification for unsafe change", () => { + const exchangeTrainText = exchangeTrainPrompt(unsafeChangeTrains, 0, ride.locale) + + expect(exchangeTrainText).toBe("This change isn't guaranteed. Change to Platform 4. Wait 1 minute for your next train.") +}) diff --git a/src/tests/notifications/mocks.ts b/src/tests/notifications/mocks.ts index 92b22df..d82433c 100644 --- a/src/tests/notifications/mocks.ts +++ b/src/tests/notifications/mocks.ts @@ -3,7 +3,7 @@ import { minutesInMs } from "../helpers/utils" import { partiallyMock } from "../helpers/types" import { LanguageCode } from "../../locales/i18n" import { Provider } from "../../types/notification" -import { RouteItem, Station } from "../../types/rail" +import { RouteItem, RouteTrain, Station } from "../../types/rail" export const now = Date.now() export const directDuration = minutesInMs(10) @@ -113,3 +113,16 @@ export const exchangeRoute = partiallyMock({ }, ], }) + +export const unsafeChangeTrains = partiallyMock([ + { + arrivalTime: now + minutesInMs(10), + delay: 3, + destinationPlatform: 3, + }, + { + departureTime: now + minutesInMs(12), + delay: 2, + originPlatform: 4, + }, +]) diff --git a/src/utils/date-utils.ts b/src/utils/date-utils.ts index 35dcc2f..c648ab9 100644 --- a/src/utils/date-utils.ts +++ b/src/utils/date-utils.ts @@ -8,5 +8,9 @@ export const routeDurationInMs = (departureTime: number, arrivalTime: number) => } export const localizedDifference = (departureTime: number, arrivalTime: number, locale: LanguageCode) => { + if (departureTime >= arrivalTime) { + return formatDistance(departureTime, departureTime, { locale: dateFnLocales[locale] }) + } + return formatDistance(departureTime, arrivalTime, { locale: dateFnLocales[locale] }) } diff --git a/src/utils/ride-utils.ts b/src/utils/ride-utils.ts index 3e8b744..5205314 100644 --- a/src/utils/ride-utils.ts +++ b/src/utils/ride-utils.ts @@ -1,5 +1,6 @@ import dayjs from "dayjs" import { v4 as uuid } from "uuid" +import { addMinutes, millisecondsToMinutes } from "date-fns" import { isEqual, last, isNumber, head, chunk } from "lodash" import { logNames, logger } from "../logs" @@ -12,6 +13,8 @@ import { LanguageCode, translate } from "../locales/i18n" import { NotificationPayload } from "../types/notification" import { buildGetOnTrainNotifications, buildNextStationNotifications, buildGetOffTrainNotifications } from "./notify-utils" +const SAFE_DURATION_MINS = 3 + export const buildRide = (ride: RideRequest): Ride => { const rideId = uuid() @@ -40,19 +43,33 @@ export const isLastTrain = (trains: RouteTrain[], train: RouteTrain) => { } export const exchangeTrainPrompt = (trains: RouteTrain[], gotOffTrain: number, locale: LanguageCode) => { + const texts: string[] = [] const previous = trains[gotOffTrain] const next = trains[gotOffTrain + 1] - const waitTime = localizedDifference(next.departureTime, previous.arrivalTime, locale) - const waitTimeText = translate("notifications.exchange.waitTime", locale, { waitTime }) + const arrivalTime = addMinutes(previous.arrivalTime, previous.delay).getTime() + const departureTime = addMinutes(next.departureTime, next.delay).getTime() + + const changeDurationInMinutes = arrivalTime >= departureTime ? 0 : millisecondsToMinutes(departureTime - arrivalTime) + const isSafeChange = changeDurationInMinutes >= SAFE_DURATION_MINS + + if (!isSafeChange) { + const unsafeChangeText = translate("notifications.exchange.unsafeChange", locale) + texts.push(unsafeChangeText) + } const platformKey = previous.destinationPlatform === next.originPlatform ? "stayOnPlatform" : "changePlatform" const platformText = translate(platformKey, locale, { scope: "notifications.exchange", platform: next.originPlatform, }) + texts.push(platformText) + + const waitTime = localizedDifference(arrivalTime, departureTime, locale) + const waitTimeText = translate("notifications.exchange.waitTime", locale, { waitTime }) + texts.push(waitTimeText) - return platformText + " " + waitTimeText + return texts.join(" ") } export const scheduleExistingRides = async () => {