mirror of
https://github.com/System-End/site.git
synced 2026-04-20 00:25:19 +00:00
Speed up vote creation endpoint
This commit is contained in:
parent
91598b19f4
commit
acfc365aed
1 changed files with 50 additions and 44 deletions
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue