rfid
This commit is contained in:
@@ -39,6 +39,10 @@ import deviceRoutes from "./routes/internal/devices";
|
|||||||
import tenantRoutesInternal from "./routes/internal/tenant";
|
import tenantRoutesInternal from "./routes/internal/tenant";
|
||||||
import staffTimeRoutesInternal from "./routes/internal/time";
|
import staffTimeRoutesInternal from "./routes/internal/time";
|
||||||
|
|
||||||
|
//Devices
|
||||||
|
import devicesRFIDRoutes from "./routes/devices/rfid";
|
||||||
|
|
||||||
|
|
||||||
import {sendMail} from "./utils/mailer";
|
import {sendMail} from "./utils/mailer";
|
||||||
import {loadSecrets, secrets} from "./utils/secrets";
|
import {loadSecrets, secrets} from "./utils/secrets";
|
||||||
import {initMailer} from "./utils/mailer"
|
import {initMailer} from "./utils/mailer"
|
||||||
@@ -99,6 +103,10 @@ async function main() {
|
|||||||
await m2mApp.register(staffTimeRoutesInternal)
|
await m2mApp.register(staffTimeRoutesInternal)
|
||||||
},{prefix: "/internal"})
|
},{prefix: "/internal"})
|
||||||
|
|
||||||
|
await app.register(async (devicesApp) => {
|
||||||
|
await devicesApp.register(devicesRFIDRoutes)
|
||||||
|
},{prefix: "/devices"})
|
||||||
|
|
||||||
|
|
||||||
//Geschützte Routes
|
//Geschützte Routes
|
||||||
|
|
||||||
|
|||||||
98
src/routes/devices/rfid.ts
Normal file
98
src/routes/devices/rfid.ts
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
import { FastifyInstance } from "fastify";
|
||||||
|
import {and, desc, eq} from "drizzle-orm";
|
||||||
|
import {authProfiles, devices, stafftimeevents} from "../../../db/schema";
|
||||||
|
|
||||||
|
export default async function devicesRFIDRoutes(server: FastifyInstance) {
|
||||||
|
server.post(
|
||||||
|
"/rfid/createevent/:terminal_id",
|
||||||
|
async (req, reply) => {
|
||||||
|
try {
|
||||||
|
|
||||||
|
const {rfid_id} = req.body as {rfid_id: string};
|
||||||
|
const {terminal_id} = req.params as {terminal_id: string};
|
||||||
|
|
||||||
|
if(!rfid_id ||!terminal_id) {
|
||||||
|
console.log(`Missing Params`);
|
||||||
|
return reply.code(400).send(`Missing Params`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const device = await server.db
|
||||||
|
.select()
|
||||||
|
.from(devices)
|
||||||
|
.where(
|
||||||
|
eq(devices.externalId, terminal_id)
|
||||||
|
|
||||||
|
)
|
||||||
|
.limit(1)
|
||||||
|
.then(rows => rows[0]);
|
||||||
|
|
||||||
|
if(!device) {
|
||||||
|
console.log(`Device ${terminal_id} not found`);
|
||||||
|
return reply.code(400).send(`Device ${terminal_id} not found`)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
const profile = await server.db
|
||||||
|
.select()
|
||||||
|
.from(authProfiles)
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(authProfiles.tenant_id, device.tenant),
|
||||||
|
eq(authProfiles.token_id, rfid_id)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.limit(1)
|
||||||
|
.then(rows => rows[0]);
|
||||||
|
|
||||||
|
if(!profile) {
|
||||||
|
console.log(`Profile for Token ${rfid_id} not found`);
|
||||||
|
return reply.code(400).send(`Profile for Token ${rfid_id} not found`)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
const lastEvent = await server.db
|
||||||
|
.select()
|
||||||
|
.from(stafftimeevents)
|
||||||
|
.where(
|
||||||
|
eq(stafftimeevents.user_id, profile.user_id)
|
||||||
|
)
|
||||||
|
.orderBy(desc(stafftimeevents.eventtime)) // <-- Sortierung: Neuestes zuerst
|
||||||
|
.limit(1)
|
||||||
|
.then(rows => rows[0]);
|
||||||
|
|
||||||
|
console.log(lastEvent)
|
||||||
|
|
||||||
|
|
||||||
|
const dataToInsert = {
|
||||||
|
tenant_id: device.tenant,
|
||||||
|
user_id: profile.user_id,
|
||||||
|
actortype: "system",
|
||||||
|
eventtime: new Date(),
|
||||||
|
eventtype: lastEvent.eventtype === "work_start" ? "work_end" : "work_start",
|
||||||
|
source: "WEB"
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(dataToInsert)
|
||||||
|
|
||||||
|
const [created] = await server.db
|
||||||
|
.insert(stafftimeevents)
|
||||||
|
//@ts-ignore
|
||||||
|
.values(dataToInsert)
|
||||||
|
.returning()
|
||||||
|
|
||||||
|
return created
|
||||||
|
} catch (err: any) {
|
||||||
|
console.error(err)
|
||||||
|
return reply.code(400).send({ error: err.message })
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
console.log(req.body)
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user