Skip to content

Commit

Permalink
fix(api-server): invalidate cache on change
Browse files Browse the repository at this point in the history
  • Loading branch information
chenhongqiao committed Jan 25, 2024
1 parent 8edbdfb commit 25373e6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
23 changes: 19 additions & 4 deletions packages/api-server/src/services/team.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type TeamInvitation, type NewTeam, type Team, type TeamMembers } from '
import { ConflictError, MethodNotAllowedError, NotFoundError } from 'http-errors-enhanced'
import { nanoid } from 'nanoid'
import gravatarUrl from 'gravatar-url'
import { USER_CACHE_KEY, deleteCache } from './cache.services.js'

export async function createTeam ({ newTeam, contestId, userId }: { newTeam: NewTeam, contestId: string, userId: string }): Promise<{ teamId: string }> {
const id = nanoid()
Expand Down Expand Up @@ -31,10 +32,12 @@ export async function createTeam ({ newTeam, contestId, userId }: { newTeam: New

await teamScoreCollection.insertOne({ id, contestId, scores: {}, time: {}, lastTime: 0, totalScore: 0 })
})
return { teamId: id }
} finally {
await session.endSession()
}

await deleteCache({ key: `${USER_CACHE_KEY}:${userId}` })
return { teamId: id }
}

export async function fetchTeam ({ teamId, contestId }: { teamId: string, contestId: string }): Promise<Team> {
Expand Down Expand Up @@ -97,8 +100,8 @@ export async function deleteTeamInvitation ({ teamId, contestId, invitationId }:

export async function completeTeamInvitation ({ invitationId, userId }: { invitationId: string, userId: string }): Promise<{ modified: boolean }> {
const session = mongoClient.startSession()
let modifiedCount = 0
try {
let modifiedCount = 0
await session.withTransaction(async () => {
const invitation = await teamInvitationCollection.findOne({ id: invitationId }, { session })
if (invitation == null) {
Expand All @@ -123,10 +126,14 @@ export async function completeTeamInvitation ({ invitationId, userId }: { invita
const { modifiedCount: modifiedTeam } = await teamCollection.updateOne({ id: teamId, contestId }, { $addToSet: { members: userId } }, { session })
modifiedCount += Math.floor(modifiedTeam)
})
return { modified: modifiedCount > 0 }
} finally {
await session.endSession()
}
const modified = modifiedCount > 0
if (modified) {
await deleteCache({ key: `${USER_CACHE_KEY}:${userId}` })
}
return { modified }
}

export async function makeTeamCaptain ({ teamId, contestId, userId }: { teamId: string, contestId: string, userId: string }): Promise<{ modified: boolean }> {
Expand Down Expand Up @@ -173,14 +180,19 @@ export async function removeTeamMember ({ teamId, contestId, userId }: { teamId:
}
modifiedCount += Math.floor(modifiedUser)
})
return { modified: modifiedCount > 0 }
} finally {
await session.endSession()
}
const modified = modifiedCount > 0
if (modified) {
await deleteCache({ key: `${USER_CACHE_KEY}:${userId}` })
}
return { modified }
}

export async function deleteTeam ({ teamId, contestId }: { teamId: string, contestId: string }): Promise<void> {
const session = mongoClient.startSession()
let userId = ''
try {
await session.withTransaction(async () => {
const team = await teamCollection.findOne({ id: teamId, contestId }, { session })
Expand All @@ -190,6 +202,7 @@ export async function deleteTeam ({ teamId, contestId }: { teamId: string, conte
if (team.members.length > 1) {
throw new MethodNotAllowedError('Cannot disband a team when there are more than one member')
}
userId = team.members[0]

await userCollection.updateOne({ id: team.members[0] }, { $unset: { [`teams.${team.contestId}`]: '' } }, { session })

Expand All @@ -202,4 +215,6 @@ export async function deleteTeam ({ teamId, contestId }: { teamId: string, conte
} finally {
await session.endSession()
}

await deleteCache({ key: `${USER_CACHE_KEY}:${userId}` })
}
7 changes: 6 additions & 1 deletion packages/api-server/src/services/user.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,12 @@ export async function completeVerification ({ verificationId }: { verificationId
throw new NotFoundError('User not found')
}

return { modified: modifiedCount > 0 }
const modified = modifiedCount > 0
if (modified) {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
await deleteCache({ key: `${USER_CACHE_KEY}:${userId}` })
}
return { modified }
}

export async function queryUsers ({ query }: { query: string }): Promise<User[]> {
Expand Down

0 comments on commit 25373e6

Please sign in to comment.