mirror of
https://github.com/System-End/site.git
synced 2026-04-19 23:22:49 +00:00
Check for duplicates & empty records
This commit is contained in:
parent
1d5556efd1
commit
d99e625034
1 changed files with 29 additions and 7 deletions
|
|
@ -10,13 +10,35 @@ export default async (req, res) => {
|
|||
res.setHeader('Access-Control-Allow-Origin', '*')
|
||||
if (req.method === 'POST') {
|
||||
const data = JSON.parse(req.body)
|
||||
console.log(data)
|
||||
await joinTable.create({
|
||||
'Full Name': data.name,
|
||||
'Email Address': data.email,
|
||||
Student: data.teen,
|
||||
Reason: data.reason
|
||||
})
|
||||
|
||||
const exists = await recordExists(data.name)
|
||||
const empty = await isEmpty(data)
|
||||
|
||||
if (!exists && !empty) {
|
||||
await joinTable.create({
|
||||
'Full Name': data.name,
|
||||
'Email Address': data.email,
|
||||
Student: data.teen,
|
||||
Reason: data.reason
|
||||
})
|
||||
}
|
||||
res.json({ status: 'success' })
|
||||
}
|
||||
}
|
||||
|
||||
async function recordExists(name) {
|
||||
const exists = await joinTable.read({
|
||||
filterByFormula: `{Full Name} = '${name}'`
|
||||
})
|
||||
return typeof exists !== 'undefined'
|
||||
}
|
||||
|
||||
function isEmpty(jsonObject) {
|
||||
let empty = true
|
||||
Object.entries(jsonObject).map(key => {
|
||||
if (key[1] !== '') {
|
||||
empty = false
|
||||
}
|
||||
})
|
||||
return empty
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue