Skip to content

Commit

Permalink
🕔 - Updated wait time with delay (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
planecore authored Sep 9, 2023
1 parent 0796e90 commit 483fdeb
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 7 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: CI
on: push
on:
push:
branches:
- main
pull_request:
jobs:
test-server:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions src/locales/ar.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"exchange": {
"waitTime": "انتظر لمدة %{waitTime}.",
"unsafeChange": "هذا التغيير غير مضمون.",
"stayOnPlatform": "ابق في المنصة %{platform}.",
"changePlatform": "انتقل إلى المنصة %{platform}."
}
Expand Down
1 change: 1 addition & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}."
}
Expand Down
1 change: 1 addition & 0 deletions src/locales/he.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"exchange": {
"waitTime": "כ-%{waitTime} המתנה לנסיעה הבאה.",
"unsafeChange": "החלפה זו אינה בטוחה.",
"stayOnPlatform": "השארו ברציף %{platform}.",
"changePlatform": "עברו לרציף %{platform}."
}
Expand Down
1 change: 1 addition & 0 deletions src/locales/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"exchange": {
"waitTime": "Подождите %{waitTime}.",
"unsafeChange": "Это изменение не гарантировано.",
"stayOnPlatform": "Оставайтесь на платформе %{platform}.",
"changePlatform": "Перейдите на платформу %{platform}."
}
Expand Down
11 changes: 9 additions & 2 deletions src/tests/notifications/exchange-trains.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.",
},
},
{
Expand Down Expand Up @@ -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.")
})
15 changes: 14 additions & 1 deletion src/tests/notifications/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -113,3 +113,16 @@ export const exchangeRoute = partiallyMock<RouteItem>({
},
],
})

export const unsafeChangeTrains = partiallyMock<RouteTrain[]>([
{
arrivalTime: now + minutesInMs(10),
delay: 3,
destinationPlatform: 3,
},
{
departureTime: now + minutesInMs(12),
delay: 2,
originPlatform: 4,
},
])
4 changes: 4 additions & 0 deletions src/utils/date-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] })
}
23 changes: 20 additions & 3 deletions src/utils/ride-utils.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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()

Expand Down Expand Up @@ -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 () => {
Expand Down

0 comments on commit 483fdeb

Please sign in to comment.