diff --git a/packages/server/services/main/routes/nfc/tag/id/[id]/execute/get.js b/packages/server/services/main/routes/nfc/tag/id/[id]/execute/get.js new file mode 100644 index 00000000..f8f75b01 --- /dev/null +++ b/packages/server/services/main/routes/nfc/tag/id/[id]/execute/get.js @@ -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]) + } + } +} \ No newline at end of file diff --git a/packages/server/services/main/routes/nfc/tag/register/[serial]/post.js b/packages/server/services/main/routes/nfc/tag/register/[serial]/post.js index 25db4529..4a99c0c3 100644 --- a/packages/server/services/main/routes/nfc/tag/register/[serial]/post.js +++ b/packages/server/services/main/routes/nfc/tag/register/[serial]/post.js @@ -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 } } \ No newline at end of file