implement NFCController

This commit is contained in:
SrGooglo 2023-06-19 19:23:40 +00:00
parent 52d6adb326
commit f944cea53a
7 changed files with 163 additions and 1 deletions

View File

@ -0,0 +1,11 @@
export default {
method: "GET",
route: "/execution/:user_id",
fn: async (req, res) => {
let execution = {
}
return res.json(execution)
}
}

View File

@ -0,0 +1,31 @@
import { User, NFCTag } from "@models"
export default {
method: "GET",
route: "/tags/:id",
fn: async (req, res) => {
let tag = await NFCTag.findOne({
_id: req.params.id
})
if (!tag) {
return res.status(404).json({
error: "Cannot find tag"
})
}
tag = tag.toObject()
if (req.user) {
if (tag.user_id.toString() === req.user._id.toString()) {
tag.is_owner = true
}
}
tag.user = await User.findOne({
_id: tag.user_id
})
return res.json(tag)
}
}

View File

@ -0,0 +1,32 @@
import { User, NFCTag } from "@models"
export default {
method: "GET",
route: "/tag/serial/:serial",
middlewares: ["withOptionalAuthentication"],
fn: async (req, res) => {
let tag = await NFCTag.findOne({
serial: req.params.serial
})
if (!tag) {
return res.status(404).json({
error: "Cannot find tag"
})
}
tag = tag.toObject()
if (req.user) {
if (tag.user_id.toString() === req.user._id.toString()) {
tag.is_owner = true
}
}
tag.user = await User.findOne({
_id: tag.user_id
})
return res.json(tag)
}
}

View File

@ -0,0 +1,14 @@
import { NFCTag } from "@models"
export default {
method: "GET",
route: "/tags",
middlewares: ["withAuthentication"],
fn: async (req, res) => {
let tags = await NFCTag.find({
user_id: req.user.id
})
return res.json(tags)
}
}

View File

@ -0,0 +1,63 @@
import { NFCTag } from "@models"
const allowedUpdateFields = [
"user_id",
"alias",
"active",
"behavior",
"icon",
]
export default {
method: "POST",
route: "/tag/:serial",
middlewares: ["withAuthentication"],
fn: async (req, res) => {
let tag = await NFCTag.findOne({
serial: req.params.serial
})
if (!tag) {
tag = new NFCTag({
user_id: req.user._id.toString(),
owner_id: req.user._id.toString(),
serial: req.params.serial,
alias: req.body.alias,
behavior: req.body.behavior,
active: req.body.active,
})
tag.endpoint_url = `${process.env.NFC_TAG_ENDPOINT}/${tag._id.toString()}`
await tag.save()
} else {
tag = tag.toObject()
if (req.user) {
if (tag.user_id !== req.user._id.toString()) {
return res.status(403).json({
error: `You do not own this tag`
})
}
}
let newData = {}
tag.endpoint_url = `${process.env.NFC_TAG_ENDPOINT}/${tag._id.toString()}`
newData.endpoint_url = tag.endpoint_url
for (let field of allowedUpdateFields) {
if (req.body[field]) {
tag[field] = req.body[field]
newData[field] = req.body[field]
}
await NFCTag.findOneAndUpdate({
serial: req.params.serial
}, newData)
}
}
return res.json(tag)
}
}

View File

@ -0,0 +1,9 @@
import { Controller } from "linebridge/dist/server"
import generateEndpointsFromDir from "linebridge/dist/server/lib/generateEndpointsFromDir"
export default class NFCController extends Controller {
static refName = "NFCController"
static useRoute = "/nfc"
httpEndpoints = generateEndpointsFromDir(__dirname + "/endpoints")
}

View File

@ -26,4 +26,6 @@ export { default as AdminController } from "./AdminController"
export { default as SyncController } from "./SyncController" export { default as SyncController } from "./SyncController"
export { default as AutoUpdateController } from "./AutoUpdate" export { default as AutoUpdateController } from "./AutoUpdate"
export { default as NFCController } from "./NFCController"