Implement status toggle functionality for contact requests, updating the status display and adding error handling. Enhance the UI with a new button for marking requests as completed or reopening them.
Some checks failed
Code Analysis (JS/Vue) / analyze (push) Failing after 56s
Some checks failed
Code Analysis (JS/Vue) / analyze (push) Failing after 56s
This commit is contained in:
33
server/api/cms/contact-requests/[id]/toggle-status.patch.js
Normal file
33
server/api/cms/contact-requests/[id]/toggle-status.patch.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import { getUserFromToken, hasAnyRole } from '../../../../utils/auth.js'
|
||||
import { readContactRequests, updateContactRequestStatus } from '../../../../utils/contact-requests.js'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const token = getCookie(event, 'auth_token')
|
||||
const currentUser = token ? await getUserFromToken(token) : null
|
||||
|
||||
if (!currentUser || !hasAnyRole(currentUser, 'admin', 'vorstand', 'trainer')) {
|
||||
throw createError({
|
||||
statusCode: 403,
|
||||
statusMessage: 'Zugriff verweigert'
|
||||
})
|
||||
}
|
||||
|
||||
const requestId = getRouterParam(event, 'id')
|
||||
if (!requestId) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Anfrage-ID fehlt' })
|
||||
}
|
||||
|
||||
const all = await readContactRequests()
|
||||
const target = all.find((r) => r.id === requestId)
|
||||
if (!target) {
|
||||
throw createError({ statusCode: 404, statusMessage: 'Anfrage nicht gefunden' })
|
||||
}
|
||||
|
||||
const newStatus = target.status === 'beantwortet' ? 'offen' : 'beantwortet'
|
||||
const updated = await updateContactRequestStatus(requestId, newStatus)
|
||||
|
||||
return {
|
||||
success: true,
|
||||
request: updated
|
||||
}
|
||||
})
|
||||
@@ -77,3 +77,22 @@ export async function addContactReply({ requestId, replyText, responderEmail })
|
||||
await writeContactRequests(current)
|
||||
return current[index]
|
||||
}
|
||||
|
||||
export async function updateContactRequestStatus(requestId, newStatus) {
|
||||
const validStatuses = ['offen', 'beantwortet']
|
||||
if (!validStatuses.includes(newStatus)) return null
|
||||
|
||||
const current = await readContactRequests()
|
||||
const index = current.findIndex((r) => r.id === requestId)
|
||||
if (index === -1) return null
|
||||
|
||||
const now = new Date().toISOString()
|
||||
current[index] = {
|
||||
...current[index],
|
||||
status: newStatus,
|
||||
updatedAt: now
|
||||
}
|
||||
|
||||
await writeContactRequests(current)
|
||||
return current[index]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user