mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 02:54:15 +00:00
implement NFCController
This commit is contained in:
parent
52d6adb326
commit
f944cea53a
@ -0,0 +1,11 @@
|
|||||||
|
export default {
|
||||||
|
method: "GET",
|
||||||
|
route: "/execution/:user_id",
|
||||||
|
fn: async (req, res) => {
|
||||||
|
let execution = {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.json(execution)
|
||||||
|
}
|
||||||
|
}
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
9
packages/server/src/controllers/NFCController/index.js
Normal file
9
packages/server/src/controllers/NFCController/index.js
Normal 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")
|
||||||
|
}
|
@ -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"
|
Loading…
x
Reference in New Issue
Block a user