-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0cce9bd
commit a2fc1d8
Showing
7 changed files
with
145 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { PayloadHandler } from 'payload/config' | ||
import { z, ZodError } from 'zod' | ||
|
||
const bodySchema = z.object({ | ||
buildId: z.number(), | ||
}) | ||
|
||
export const toggleVote: PayloadHandler = async (req, res): Promise<void> => { | ||
const { user, payload } = req | ||
|
||
if (!user) { | ||
res.status(401).json({ message: 'Unauthorized' }) | ||
return | ||
} | ||
|
||
try { | ||
const { buildId } = bodySchema.parse(req.body) | ||
|
||
const build = await payload.findByID({ | ||
collection: 'er-builds', | ||
id: buildId, | ||
depth: 0, | ||
}) | ||
|
||
const indexOfVote = build.votes.indexOf(user.id) | ||
|
||
if (indexOfVote > -1) { | ||
build.votes.splice(indexOfVote, 1) | ||
} else { | ||
build.votes.push(user.id) | ||
} | ||
|
||
const result = await payload.update({ | ||
collection: 'er-builds', | ||
id:buildId, | ||
depth: 0, | ||
data: { | ||
votes: build.votes, | ||
votes_count: build.votes.length | ||
} | ||
}) | ||
|
||
res.json(result) | ||
} catch (error) { | ||
if (error instanceof ZodError) { | ||
payload.logger.error(`[${req.body.email}] ${error.message} ${error.issues.map((e) => e.message).join(', ')}`) | ||
|
||
res.status(400).json({ | ||
...error | ||
}) | ||
return | ||
} | ||
|
||
if (error instanceof Error) { | ||
payload.logger.error(`[${req.body.email}] ${error.message}`) | ||
res.status(400).json({ ...error }) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters