improve nfg tag service

This commit is contained in:
SrGooglo 2024-09-16 10:28:54 +00:00
parent 6bd850a316
commit 6ea579fb2c
2 changed files with 47 additions and 2 deletions

View File

@ -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])
}
}
}

View File

@ -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
}
}