Speed up vote creation endpoint

This commit is contained in:
Max Wofford 2024-08-26 12:24:18 -04:00
parent 91598b19f4
commit acfc365aed

View file

@ -22,66 +22,61 @@ export default async function handler(req, res) {
?.replace('Bearer ', '')
.replace(/[^a-zA-Z0-9-]/g, '')
console.log(req.body)
const { overall, technical, creative } = req.body
const pointsDistribution = [5, 4, 3, 2, 1]
const users = await usersTable.read({
filterByFormula: `{Auth Token} = '${authorization}'`
})
if (users.length === 0) {
return res.status(404).json({ error: 'User not found' })
if (!authorization || authorization.length === 0) {
return res.status(400).json({ error: 'Missing or invalid authorization header' })
}
const userID = users[0].id
const users = await usersTable.read({
filterByFormula: `{Auth Token} = '${authorization}'`,
maxRecords: 1
})
const user = users[0]
const userRecord = await usersTable.find(userID)
if (!user) {
return res.status(400).json({ error: 'Missing or invalid authorization header' })
}
console.log(userRecord)
const voted = userRecord.fields.Voted
if (voted) {
if (user.fields['Voted']) {
return res.status(404).json({ error: 'Already voted' })
}
let jobs = []
const { overall, technical, creative } = req.body
if (!overall || !technical || !creative) {
return res.status(400).json({ error: 'Missing fields' })
}
const pointsDistribution = [5, 4, 3, 2, 1]
let votesToCreate = []
for (let i = 0; i < overall.length; i++) {
const project = overall[i]
const points = pointsDistribution[i]
jobs.push(addVote(project, points, userID, 'Overall'))
votesToCreate.push(addVote(project, points, user.id, 'Overall'))
}
await Promise.all(jobs)
jobs = []
for (let i = 0; i < technical.length; i++) {
const project = technical[i]
const points = pointsDistribution[i]
await addVote(project, points, userID, 'Technical')
votesToCreate.push(addVote(project, points, user.id, 'Technical'))
}
await Promise.all(jobs)
jobs = []
for (let i = 0; i < creative.length; i++) {
const project = creative[i]
const points = pointsDistribution[i]
await addVote(project, points, userID, 'Creative')
votesToCreate.push(addVote(project, points, user.id, 'Creative'))
}
await Promise.all(jobs)
await usersTable.update(userID, {
Voted: true
})
await Promise.all([
batchCreate(votesTable, votesToCreate),
usersTable.update(user.id, {
Voted: true
})
])
return res.status(200).json({ success: true })
} catch (error) {
@ -90,15 +85,26 @@ export default async function handler(req, res) {
}
}
const addVote = async (projectId, points, userID, type) => {
if (!userID || !points || !projectId) {
return res.status(400).json({ error: 'Missing required headers' })
const addVote = (projectId, points, userID, type) => {
return {
fields: {
Points: parseInt(points, 10),
Voter: [userID],
Showcase: [projectId],
Type: type
}
}
}
async function batchCreate(table, records) {
const chunks = []
while (records.length > 0) {
chunks.push(records.splice(0, 10))
}
const vote = await votesTable.create({
Points: parseInt(points, 10),
Voter: [userID],
Showcase: [projectId],
Type: type
})
}
for (const chunk of chunks) {
console.log({ chunk })
await table.create(chunk)
}
}