Skip to content

Commit

Permalink
Add tracking for daily edits by user (hackclub#1016)
Browse files Browse the repository at this point in the history
  • Loading branch information
kognise committed May 4, 2023
1 parent 4cb5f14 commit 72d57f3
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/pages/api/games/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,26 @@ export const post: APIRoute = async ({ request, cookies }) => {
const game = await getGame(gameId)
if (!game) return new Response('Game does not exist', { status: 404 })

let trackingId = game.id
let trackingType = 'game'
const trackingDate = new Date().toDateString()

if (!game.unprotected) {
const session = await getSession(cookies)
if (!session) return new Response('Unauthorized', { status: 401 })
if (session.user.id !== game.ownerId) return new Response(`Can't edit a game you don't own`, { status: 403 })
trackingId = session.user.id
trackingType = 'user'
}

await firestore.collection('games').doc(gameId).update({
code,
modifiedAt: Timestamp.now()
})
await firestore.collection('daily-edits').doc(`${trackingId}-${trackingDate}`).set({
type: trackingType,
id: trackingId,
date: Timestamp.now()
})
return new Response(JSON.stringify({}), { status: 200 })
}

0 comments on commit 72d57f3

Please sign in to comment.