feat: codes handling
This commit is contained in:
1 parent
8c6d5d23d0
commit
6273dd4b3b
4 files changed
+145
No files matched your search
@@ -13,6 +13,7 @@ import vote from './routes/vote.js'
|
||||
import login from './routes/login.js'
|
||||
import player from './routes/player.js'
|
||||
import admin from './routes/admin.js'
|
||||
import code from './routes/code.js'
|
||||
|
||||
var app = express(),
|
||||
http = createServer(app)
|
||||
@@ -28,6 +29,7 @@ app.use('/vote', vote)
|
||||
app.use('/login', login)
|
||||
app.use('/player', player)
|
||||
app.use('/admin', admin)
|
||||
app.use('/code', code)
|
||||
|
||||
http.listen(process.env.HTTP_PORT)
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import mongoose from 'mongoose'
|
||||
|
||||
const code = new mongoose.Schema(
|
||||
{
|
||||
value: String,
|
||||
type: String,
|
||||
claimed: Boolean,
|
||||
claimedAt: Date,
|
||||
used: Boolean,
|
||||
usedAt: Date,
|
||||
checked: Number,
|
||||
uid: String,
|
||||
},
|
||||
{
|
||||
collection: 'codes',
|
||||
timestamps: { createdAt: 'createdAt', updatedAt: 'updatedAt' },
|
||||
}
|
||||
)
|
||||
|
||||
const Code = mongoose.model('Code', code)
|
||||
|
||||
export default Code
|
||||
export { Code, code }
|
||||
@@ -0,0 +1,41 @@
|
||||
import Code from '../models/Code.js'
|
||||
|
||||
function getCode(value) {
|
||||
return new Promise((resolve, reject) => {
|
||||
Code.findOne({ value }).then((code) => {
|
||||
resolve(code)
|
||||
if (code) {
|
||||
code.checked += 1
|
||||
code.save()
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function putCode(meta) {
|
||||
return Code.create(meta)
|
||||
.then((ok) => {
|
||||
resolve()
|
||||
})
|
||||
.catch((err) => console.error(err))
|
||||
}
|
||||
|
||||
function claimCode(value, uid) {
|
||||
return new Promise((resolve, reject) => {
|
||||
Code.findOne({ value, claimed: false }).then((code) => {
|
||||
code.claimed = true
|
||||
code.claimedAt = new Date()
|
||||
code.uid = uid
|
||||
|
||||
code.save().then((code) => resolve(code))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function getClaimed(uid) {
|
||||
return Code.find({ claimed: true, uid })
|
||||
}
|
||||
|
||||
function useCode() {}
|
||||
|
||||
export { getCode, getClaimed, putCode, claimCode, useCode }
|
||||
@@ -0,0 +1,79 @@
|
||||
import express from 'express'
|
||||
|
||||
import db from '../db.js'
|
||||
|
||||
import { getCode, claimCode, getClaimed } from '../mongo/codes.js'
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
const descriptions = {
|
||||
top16: `Pozwala na gwarantowane miejsce w Topce dla wybranego przez ciebie utworu.
|
||||
Działa tylko po Zajęciu a następnie Aktywacji.
|
||||
Aktywny kod, działa na ostatni utwór na który zagłosujesz przed 8:00 następnego dnia`,
|
||||
}
|
||||
|
||||
router.get('/info/:c', ({ params: { c } }, res) => {
|
||||
getCode(c).then((code) => {
|
||||
if (code) {
|
||||
res.json({
|
||||
value: code.value,
|
||||
type: code.type,
|
||||
claimed: code.claimed,
|
||||
used: code.used,
|
||||
checked: code.checked,
|
||||
desc: descriptions[code.type],
|
||||
})
|
||||
} else {
|
||||
res.status(404).end()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
router.get('/list/', ({ headers: { authorization } }, res) => {
|
||||
if (authorization === undefined)
|
||||
return res.status(401).end('Access token required')
|
||||
|
||||
if (authorization.length < 1)
|
||||
return res.status(400).end('Access token invalid')
|
||||
|
||||
db.users
|
||||
.verifyJwt(authorization)
|
||||
.then(({ id }) => {
|
||||
getClaimed(id).then((list) => res.json(list))
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err)
|
||||
res.status(403).end('Could not validate token')
|
||||
})
|
||||
})
|
||||
|
||||
router.post(
|
||||
'/claim/:c',
|
||||
({ params: { c }, headers: { authorization } }, res) => {
|
||||
if (authorization === undefined)
|
||||
return res.status(401).end('Access token required')
|
||||
|
||||
if (authorization.length < 1)
|
||||
return res.status(400).end('Access token invalid')
|
||||
|
||||
if (c === undefined) return res.status(400).end('Code required')
|
||||
|
||||
if (c.length < 1) return res.status(400).end('Code invalid')
|
||||
|
||||
db.users
|
||||
.verifyJwt(authorization)
|
||||
.then(({ id }) => {
|
||||
claimCode(c, id).then((code) => {
|
||||
if (code.claimed && code.uid == id) {
|
||||
res.json('true')
|
||||
} else res.json('false')
|
||||
})
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err)
|
||||
res.status(403).end('Could not validate token')
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
export default router
|
||||
Reference in new issue
Block a user