Many Changes for 1.0
This commit is contained in:
90
tools/hardwareGateway/index.mjs
Normal file
90
tools/hardwareGateway/index.mjs
Normal file
@@ -0,0 +1,90 @@
|
||||
import {createClient} from '@supabase/supabase-js'
|
||||
import express from "express"
|
||||
import bodyParser from "body-parser"
|
||||
import dayjs from "dayjs"
|
||||
|
||||
const supabase = createClient(process.env.SUPABASE_URL,process.env.SUPABASE_SERVICE_ROLE_KEY)
|
||||
|
||||
|
||||
const app = express()
|
||||
app.use(bodyParser.json())
|
||||
|
||||
app.get("/", async (req,res) => {
|
||||
|
||||
let data = (await supabase.from("devices").select()).data
|
||||
|
||||
|
||||
res.json(data)
|
||||
})
|
||||
|
||||
app.post("/executeTimeCommand", async (req,res) => {
|
||||
console.log(req.body)
|
||||
let body = req.body
|
||||
let deviceData = (await supabase.from("devices").select().eq("id",body.id).single()).data
|
||||
let currentTenant = deviceData.tenant
|
||||
console.log(deviceData)
|
||||
console.log(currentTenant)
|
||||
|
||||
|
||||
if(deviceData.password === body.password) {
|
||||
let userId = ""
|
||||
if(!body.tokenId && !body.userId) {
|
||||
res.sendStatus(400)
|
||||
}
|
||||
else if(body.tokenId && !body.userId) {
|
||||
userId = ((await supabase.from("profiles").select().eq("tokenId",body.tokenId).single()).data).id
|
||||
console.log(userId)
|
||||
} else {
|
||||
userId = body.userId
|
||||
}
|
||||
|
||||
|
||||
|
||||
let startedWorkingTime = (await supabase.from("workingtimes").select().eq("user",userId).is("end",null).single()).data
|
||||
|
||||
if(startedWorkingTime) {
|
||||
//Stop Time
|
||||
|
||||
const {data,error} = await supabase.from("workingtimes")
|
||||
.update({end: String(dayjs().format("HH:mm:ss"))+ "+01", state: "Am Terminal gestoppt"})
|
||||
.eq("id", startedWorkingTime.id)
|
||||
|
||||
if(error) throw error
|
||||
|
||||
console.log(data)
|
||||
console.log("Time Stopped")
|
||||
|
||||
res.json({
|
||||
message: "Zeit gestoppt",
|
||||
type: "stopped"
|
||||
})
|
||||
|
||||
|
||||
} else {
|
||||
//Start Time
|
||||
|
||||
const {data,error} = await supabase.from("workingtimes")
|
||||
.insert([{
|
||||
user: userId,
|
||||
date: dayjs().format("YYYY-MM-DD"),
|
||||
start: String(dayjs().format("HH:mm:ss"))+ "+01",
|
||||
tenant: currentTenant,
|
||||
state: "Am Terminal gestartet"
|
||||
}])
|
||||
|
||||
if(error) throw error
|
||||
|
||||
console.log(data)
|
||||
console.log("Time Started")
|
||||
|
||||
res.json({
|
||||
message: "Zeit gestartet",
|
||||
type: "started"
|
||||
})
|
||||
}
|
||||
} else {
|
||||
res.sendStatus(401)
|
||||
}
|
||||
})
|
||||
|
||||
app.listen(3001)
|
||||
Reference in New Issue
Block a user