mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 02:24:16 +00:00
improve nfg tag service
This commit is contained in:
parent
6bd850a316
commit
6ea579fb2c
@ -0,0 +1,39 @@
|
||||
import { NFCTag } from "@db_models"
|
||||
|
||||
export default async (req, res) => {
|
||||
let tag = await NFCTag.findOne({
|
||||
_id: req.params.id
|
||||
})
|
||||
|
||||
if (!tag) {
|
||||
return res.status(404).json({
|
||||
error: "Cannot find tag"
|
||||
})
|
||||
}
|
||||
|
||||
switch (tag.behavior.type) {
|
||||
case "url": {
|
||||
if (!tag.behavior.value.startsWith("https://")) {
|
||||
tag.behavior.value = `https://${tag.behavior.value}`
|
||||
}
|
||||
|
||||
return res.redirect(tag.behavior.value)
|
||||
}
|
||||
case "profile": {
|
||||
return new OperationError(501, `Not implemented.`)
|
||||
}
|
||||
case "random_list": {
|
||||
const values = result.behavior.value.split(";")
|
||||
|
||||
const index = Math.floor(Math.random() * values.length)
|
||||
|
||||
let randomURL = values[index]
|
||||
|
||||
if (!randomURL.startsWith("https://")) {
|
||||
randomURL = `https://${randomURL}`
|
||||
}
|
||||
|
||||
return res.redirect(values[index])
|
||||
}
|
||||
}
|
||||
}
|
@ -8,6 +8,10 @@ const allowedUpdateFields = [
|
||||
"icon",
|
||||
]
|
||||
|
||||
function buildEndpoint(id) {
|
||||
return `${process.env.NFC_TAG_ENDPOINT}/${id}/execute`
|
||||
}
|
||||
|
||||
export default {
|
||||
middlewares: ["withAuthentication"],
|
||||
fn: async (req, res) => {
|
||||
@ -27,7 +31,7 @@ export default {
|
||||
active: req.body.active,
|
||||
})
|
||||
|
||||
tag.endpoint_url = `${process.env.NFC_TAG_ENDPOINT}/${tag._id.toString()}`
|
||||
tag.endpoint_url = buildEndpoint(tag._id.toString())
|
||||
|
||||
await tag.save()
|
||||
} else {
|
||||
@ -43,7 +47,7 @@ export default {
|
||||
|
||||
let newData = {}
|
||||
|
||||
tag.endpoint_url = `${process.env.NFC_TAG_ENDPOINT}/${tag._id.toString()}`
|
||||
tag.endpoint_url = buildEndpoint(tag._id.toString())
|
||||
newData.endpoint_url = tag.endpoint_url
|
||||
|
||||
for (let field of allowedUpdateFields) {
|
||||
@ -58,6 +62,8 @@ export default {
|
||||
}
|
||||
}
|
||||
|
||||
console.log(tag)
|
||||
|
||||
return tag
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user