Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed stale rides in redis #20

Merged
merged 1 commit into from
Nov 3, 2023
Merged
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
20 changes: 17 additions & 3 deletions src/data/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export const addRide = async (ride: Ride): Promise<boolean> => {

export const updateLastRideNotification = async (rideId: string, notificationId: number) => {
try {
const isRideExists = await hasRide(rideId)
if (!isRideExists) {
return false
}

await client.hSet(getKey(rideId), "lastNotificationId", notificationId)

if (notificationId !== 0) {
Expand All @@ -52,10 +57,10 @@ export const updateRideToken = async (rideId: string, token: string) => {
try {
await client.hSet(getKey(rideId), "token", token)

logger.info(logNames.redis.rides.updateNotificationId.success, { rideId, token })
logger.info(logNames.redis.rides.updateToken.success, { rideId, token })
return true
} catch (error) {
logger.error(logNames.redis.rides.updateNotificationId.failed, { error, rideId, token })
logger.error(logNames.redis.rides.updateToken.failed, { error, rideId, token })
return false
}
}
Expand Down Expand Up @@ -93,7 +98,7 @@ export const deleteRide = async (rideId: string) => {
return success
} catch (error) {
try {
const isRideExists = await client.exists(getKey(rideId))
const isRideExists = await hasRide(rideId)
if (!isRideExists) {
return true
} else {
Expand All @@ -107,6 +112,15 @@ export const deleteRide = async (rideId: string) => {
}
}

export const hasRide = async (rideId: string) => {
try {
const result = await client.exists(getKey(rideId))
return Boolean(result)
} catch (error) {
return false
}
}

export const getAllRides = async (): Promise<Ride[] | null> => {
try {
const results = await client.keys("rides:*")
Expand Down
Loading