New Staff Functions

This commit is contained in:
2025-11-08 19:01:22 +01:00
parent ee8e17d80e
commit d7939a9ca4
4 changed files with 267 additions and 37 deletions

135
src/routes/staff/time.ts Normal file
View File

@@ -0,0 +1,135 @@
import { FastifyInstance } from 'fastify'
import { StaffTimeEntry } from '../../types/staff'
export default async function staffTimeRoutes(server: FastifyInstance) {
// ▶ Neue Zeit starten
server.post<{ Body: Pick<StaffTimeEntry, 'started_at' | 'stopped_at' | 'type' | 'description'> }>(
'/staff/time',
async (req, reply) => {
const { started_at, stopped_at, type = 'work', description } = req.body
const userId = req.user.user_id
const tenantId = req.user.tenant_id
const { data, error } = await server.supabase
.from('staff_time_entries')
.insert([{ tenant_id: tenantId, user_id: userId, started_at, stopped_at, type, description }])
.select()
.maybeSingle()
if (error) return reply.code(400).send({ error: error.message })
return reply.send(data)
}
)
// ▶ Zeit stoppen
server.put<{ Params: { id: string }, Body: { stopped_at: string } }>(
'/staff/time/:id/stop',
async (req, reply) => {
const { id } = req.params
const { stopped_at } = req.body
const { data, error } = await server.supabase
.from('staff_time_entries')
.update({ stopped_at, updated_at: new Date().toISOString() })
.eq('id', id)
.select()
.maybeSingle()
if (error) return reply.code(400).send({ error: error.message })
return reply.send(data)
}
)
// ▶ Liste aller Zeiten
server.get<{
Querystring: {
from?: string
to?: string
type?: string
user_id?: string
}
}>('/staff/time', async (req, reply) => {
const { from, to, type, user_id } = req.query
const { user_id: currentUserId, tenant_id } = req.user
// 🧩 Basis-Query für den Tenant
let query = server.supabase
.from('staff_time_entries')
.select('*')
.eq('tenant_id', tenant_id)
.order('started_at', { ascending: false })
// 🔒 Zugriffsbeschränkung: nur eigene Zeiten, außer Berechtigung erlaubt mehr
if (!req.hasPermission('staff.time.read_all')) {
query = query.eq('user_id', currentUserId)
} else if (user_id) {
// falls explizit user_id angegeben wurde
query = query.eq('user_id', user_id)
}
// 📅 Zeitfilter
if (from) query = query.gte('started_at', from)
if (to) query = query.lte('started_at', to)
if (type) query = query.eq('type', type)
const { data, error } = await query
if (error) return reply.code(400).send({ error: error.message })
return reply.send(data)
})
// ▶ Einzelne Zeit abrufen (inkl. Connects)
server.get<{ Params: { id: string } }>(
'/staff/time/:id',
async (req, reply) => {
const { id } = req.params
const { data, error } = await server.supabase
.from('staff_time_entries')
.select(`
*,
staff_time_entry_connects(*)
`)
.eq('id', id)
.maybeSingle()
if (error) return reply.code(400).send({ error: error.message })
return reply.send(data)
}
)
// ▶ Zeit bearbeiten
server.put<{ Params: { id: string }, Body: Partial<StaffTimeEntry> }>(
'/staff/time/:id',
async (req, reply) => {
const { id } = req.params
const { data, error } = await server.supabase
.from('staff_time_entries')
.update({ ...req.body, updated_at: new Date().toISOString() })
.eq('id', id)
.select()
.maybeSingle()
if (error) return reply.code(400).send({ error: error.message })
return reply.send(data)
}
)
// ▶ Zeit löschen
server.delete<{ Params: { id: string } }>(
'/staff/time/:id',
async (req, reply) => {
const { id } = req.params
const { error } = await server.supabase
.from('staff_time_entries')
.delete()
.eq('id', id)
if (error) return reply.code(400).send({ error: error.message })
return reply.send({ success: true })
}
)
}