trusted-setup-server/server/models/contributions.model.js

56 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-01-29 12:30:50 +01:00
const crypto = require('crypto')
2020-01-31 18:03:09 +01:00
const fs = require('fs')
2020-01-30 15:00:34 +01:00
const db = require('./db.js')
2020-01-29 12:30:50 +01:00
let sql
2020-01-30 15:00:34 +01:00
const Contributions = {}
2020-01-29 12:30:50 +01:00
Contributions.currentContributionIndex = async function() {
2020-01-30 15:00:34 +01:00
const [rows] = await sql.query('select max(id) as max from contributions')
2020-01-29 12:30:50 +01:00
return (rows[0].max || 0) + 1
}
2020-02-05 16:02:34 +01:00
Contributions.insertContributionInfo = async function({ name, company, handle, socialType }) {
2020-01-29 12:30:50 +01:00
const token = crypto.randomBytes(32).toString('hex')
2020-02-05 16:02:34 +01:00
await sql.execute(
'insert into contributions(token, name, company, handle, socialType) values(?, ?, ?, ?, ?)',
[token, name, company, handle, socialType]
)
2020-01-29 12:30:50 +01:00
}
2020-01-30 15:00:34 +01:00
2020-02-05 16:02:34 +01:00
Contributions.updateContributionInfo = async function({ token, name, company }) {
2020-01-30 15:00:34 +01:00
await sql.execute('insert into contributions(token, name, company) values(?, ?, ?)', [
token,
name,
company
])
2020-01-29 12:30:50 +01:00
}
Contributions.getContributions = async function() {
2020-02-05 16:02:34 +01:00
const [rows] = await sql.execute(
'select id, name, company, handle, socialType from contributions'
)
2020-01-29 12:30:50 +01:00
return rows
}
2020-01-30 15:00:34 +01:00
async function main() {
;({ sql } = await db())
2020-01-31 18:03:09 +01:00
const [rows] = await sql.query("show tables like 'contributions'")
if (rows.length === 0) {
console.log('Database appears to be empty, creating tables')
const sqlFile = await fs.readFileSync('seed.sql')
for (const s of sqlFile.toString().split(';')) {
if (s.trim().length > 0) {
await sql.query(s)
}
}
}
2020-01-29 12:30:50 +01:00
const contribitionIndex = await Contributions.currentContributionIndex()
console.log('Next contribution index is', contribitionIndex)
}
main()
2020-01-30 15:00:34 +01:00
module.exports = Contributions