Redone
This commit is contained in:
@@ -18,19 +18,29 @@ export default async function staffTimeRoutes(server: FastifyInstance) {
|
|||||||
// -------------------------------------------------------------
|
// -------------------------------------------------------------
|
||||||
server.post("/staff/time", async (req, reply) => {
|
server.post("/staff/time", async (req, reply) => {
|
||||||
try {
|
try {
|
||||||
const { user_id, ...rest } = req.body as any
|
|
||||||
const userId = req.user.user_id
|
const userId = req.user.user_id
|
||||||
const tenantId = req.user.tenant_id
|
const tenantId = req.user.tenant_id
|
||||||
|
|
||||||
const newEntry = {
|
const body = req.body as any
|
||||||
|
|
||||||
|
const normalizeDate = (val: any) => {
|
||||||
|
if (!val) return null
|
||||||
|
const d = new Date(val)
|
||||||
|
return isNaN(d.getTime()) ? null : d
|
||||||
|
}
|
||||||
|
|
||||||
|
const dataToInsert = {
|
||||||
tenant_id: tenantId,
|
tenant_id: tenantId,
|
||||||
user_id: user_id || userId,
|
user_id: body.user_id || userId,
|
||||||
...rest
|
type: body.type || "work",
|
||||||
|
description: body.description || null,
|
||||||
|
started_at: normalizeDate(body.started_at),
|
||||||
|
stopped_at: normalizeDate(body.stopped_at),
|
||||||
}
|
}
|
||||||
|
|
||||||
const [created] = await server.db
|
const [created] = await server.db
|
||||||
.insert(stafftimeentries)
|
.insert(stafftimeentries)
|
||||||
.values(newEntry)
|
.values(dataToInsert)
|
||||||
.returning()
|
.returning()
|
||||||
|
|
||||||
return created
|
return created
|
||||||
@@ -43,23 +53,42 @@ export default async function staffTimeRoutes(server: FastifyInstance) {
|
|||||||
// -------------------------------------------------------------
|
// -------------------------------------------------------------
|
||||||
// ▶ Zeit stoppen
|
// ▶ Zeit stoppen
|
||||||
// -------------------------------------------------------------
|
// -------------------------------------------------------------
|
||||||
server.put("/staff/time/:id/stop", async (req, reply) => {
|
server.put<{
|
||||||
|
Params: { id: string },
|
||||||
|
Body: { stopped_at: string }
|
||||||
|
}>("/staff/time/:id/stop", async (req, reply) => {
|
||||||
try {
|
try {
|
||||||
const { id } = req.params as any
|
const { id } = req.params
|
||||||
const { stopped_at } = req.body as any
|
const { stopped_at } = req.body
|
||||||
|
|
||||||
|
// Normalize timestamp
|
||||||
|
const normalizeDate = (val: any) => {
|
||||||
|
const d = new Date(val)
|
||||||
|
return isNaN(d.getTime()) ? null : d
|
||||||
|
}
|
||||||
|
|
||||||
|
const stopTime = normalizeDate(stopped_at)
|
||||||
|
if (!stopTime) {
|
||||||
|
return reply.code(400).send({ error: "Invalid stopped_at timestamp" })
|
||||||
|
}
|
||||||
|
|
||||||
const [updated] = await server.db
|
const [updated] = await server.db
|
||||||
.update(stafftimeentries)
|
.update(stafftimeentries)
|
||||||
.set({
|
.set({
|
||||||
stopped_at,
|
stopped_at: stopTime,
|
||||||
updated_at: new Date()
|
updated_at: new Date(),
|
||||||
})
|
})
|
||||||
.where(eq(stafftimeentries.id, id))
|
.where(eq(stafftimeentries.id, id))
|
||||||
.returning()
|
.returning()
|
||||||
|
|
||||||
return updated
|
if (!updated) {
|
||||||
} catch (err) {
|
return reply.code(404).send({ error: "Time entry not found" })
|
||||||
return reply.code(400).send({ error: (err as Error).message })
|
}
|
||||||
|
|
||||||
|
return reply.send(updated)
|
||||||
|
} catch (err: any) {
|
||||||
|
console.error("STOP ERROR:", err)
|
||||||
|
return reply.code(500).send({ error: err.message || "Internal server error" })
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -131,15 +160,43 @@ export default async function staffTimeRoutes(server: FastifyInstance) {
|
|||||||
// -------------------------------------------------------------
|
// -------------------------------------------------------------
|
||||||
// ▶ Zeit bearbeiten
|
// ▶ Zeit bearbeiten
|
||||||
// -------------------------------------------------------------
|
// -------------------------------------------------------------
|
||||||
server.put("/staff/time/:id", async (req, reply) => {
|
// ▶ Zeit bearbeiten
|
||||||
|
server.put<{
|
||||||
|
Params: { id: string },
|
||||||
|
}>("/staff/time/:id", async (req, reply) => {
|
||||||
try {
|
try {
|
||||||
const { id } = req.params as any
|
const { id } = req.params
|
||||||
|
const body = req.body
|
||||||
|
|
||||||
|
// Normalize all timestamp fields
|
||||||
|
const normalizeDate = (val: any) => {
|
||||||
|
if (!val) return null
|
||||||
|
const d = new Date(val)
|
||||||
|
return isNaN(d.getTime()) ? null : d
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
const updateData = {
|
const updateData: any = {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
...req.body,
|
...body,
|
||||||
updated_at: new Date()
|
updated_at: new Date(),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only convert if present — avoid overriding with null unless sent
|
||||||
|
// @ts-ignore
|
||||||
|
if (body.started_at !== undefined) {
|
||||||
|
// @ts-ignore
|
||||||
|
updateData.started_at = normalizeDate(body.started_at)
|
||||||
|
}
|
||||||
|
// @ts-ignore
|
||||||
|
if (body.stopped_at !== undefined) {
|
||||||
|
// @ts-ignore
|
||||||
|
updateData.stopped_at = normalizeDate(body.stopped_at)
|
||||||
|
}
|
||||||
|
// @ts-ignore
|
||||||
|
if (body.approved_at !== undefined) {
|
||||||
|
// @ts-ignore
|
||||||
|
updateData.approved_at = normalizeDate(body.approved_at)
|
||||||
}
|
}
|
||||||
|
|
||||||
const [updated] = await server.db
|
const [updated] = await server.db
|
||||||
@@ -148,12 +205,18 @@ export default async function staffTimeRoutes(server: FastifyInstance) {
|
|||||||
.where(eq(stafftimeentries.id, id))
|
.where(eq(stafftimeentries.id, id))
|
||||||
.returning()
|
.returning()
|
||||||
|
|
||||||
return updated
|
if (!updated) {
|
||||||
} catch (err) {
|
return reply.code(404).send({ error: "Time entry not found" })
|
||||||
return reply.code(400).send({ error: (err as Error).message })
|
}
|
||||||
|
|
||||||
|
return reply.send(updated)
|
||||||
|
} catch (err: any) {
|
||||||
|
console.error("UPDATE ERROR:", err)
|
||||||
|
return reply.code(500).send({ error: err.message || "Internal server error" })
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
// -------------------------------------------------------------
|
// -------------------------------------------------------------
|
||||||
// ▶ Zeit löschen
|
// ▶ Zeit löschen
|
||||||
// -------------------------------------------------------------
|
// -------------------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user