189 lines
5.0 KiB
TypeScript
189 lines
5.0 KiB
TypeScript
import Ticket from '#models/ticket'
|
|
import type { HttpContext } from '@adonisjs/core/http'
|
|
import db from '@adonisjs/lucid/services/db'
|
|
|
|
export default class TicketsController {
|
|
/**
|
|
* List all tickets
|
|
*/
|
|
async get({ request, response, auth }: HttpContext) {
|
|
try {
|
|
const perPage = request.input('per_page', 1)
|
|
const page = request.input('page', 1)
|
|
|
|
const queryLatest = Ticket.query()
|
|
.where('station_id', request.input('station_id'))
|
|
.where('sn', request.input('sn'))
|
|
|
|
const query = Ticket.query()
|
|
.where('station_id', request.input('station_id'))
|
|
.where('sn', request.input('sn'))
|
|
.whereNot('status', 'closed')
|
|
|
|
const tickets = await query.orderBy('tickets.created_at', 'desc').paginate(page, perPage)
|
|
return response.ok({
|
|
status: true,
|
|
data: tickets,
|
|
latest:
|
|
(await queryLatest.orderBy('tickets.created_at', 'desc').paginate(page, perPage)) || null,
|
|
})
|
|
} catch (error) {
|
|
return response.internalServerError({
|
|
status: false,
|
|
message: 'Failed to fetch tickets',
|
|
error,
|
|
})
|
|
}
|
|
}
|
|
|
|
async getAll({ request, response, auth }: HttpContext) {
|
|
try {
|
|
const perPage = request.input('per_page', 20)
|
|
const page = request.input('page', 1)
|
|
|
|
const query = Ticket.query()
|
|
.where('station_id', request.input('station_id'))
|
|
.where('sn', request.input('sn'))
|
|
// .whereNot('status', 'closed')
|
|
|
|
const tickets = await query.orderBy('tickets.created_at', 'desc').paginate(page, perPage)
|
|
return response.ok({
|
|
status: true,
|
|
data: tickets,
|
|
})
|
|
} catch (error) {
|
|
return response.internalServerError({
|
|
status: false,
|
|
message: 'Failed to fetch tickets',
|
|
error,
|
|
})
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create a new ticket
|
|
*/
|
|
async create({ request, response, auth }: HttpContext) {
|
|
try {
|
|
const payload = await request.all()
|
|
|
|
const history = [
|
|
{
|
|
userId: payload.userId,
|
|
userName: payload.userName,
|
|
status: payload.status,
|
|
description: payload.description.trim(),
|
|
time: Date.now(),
|
|
},
|
|
]
|
|
|
|
const trx = await db.transaction()
|
|
try {
|
|
const ticket = await Ticket.create(
|
|
{
|
|
description: payload.description.trim(),
|
|
model: payload.model.trim(),
|
|
sn: payload.sn.trim(),
|
|
stationId: payload.station_id,
|
|
status: 'open',
|
|
history: JSON.stringify(history),
|
|
},
|
|
{ client: trx }
|
|
)
|
|
|
|
await trx.commit()
|
|
|
|
return response.ok({
|
|
status: true,
|
|
message: 'Ticket created successfully',
|
|
data: ticket,
|
|
})
|
|
} catch (error) {
|
|
await trx.rollback()
|
|
|
|
return response.internalServerError({
|
|
status: false,
|
|
message: 'Failed to create ticket, please try again!',
|
|
error,
|
|
})
|
|
}
|
|
} catch (error) {
|
|
return response.internalServerError({
|
|
status: false,
|
|
message: 'Failed to create ticket',
|
|
error,
|
|
})
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get a single ticket by ID
|
|
*/
|
|
async show({ params, response }: HttpContext) {
|
|
try {
|
|
const ticket = await Ticket.findOrFail(params.id)
|
|
return response.ok({ status: true, data: ticket })
|
|
} catch (error) {
|
|
return response.notFound({ status: false, message: 'Ticket not found' })
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update a ticket
|
|
*/
|
|
async update({ request, response, auth }: HttpContext) {
|
|
try {
|
|
const ticketId = request.param('id')
|
|
const payload = await request.all()
|
|
const ticket = await Ticket.findOrFail(ticketId)
|
|
const history = {
|
|
userId: payload.userId,
|
|
userName: payload.userName,
|
|
status: payload.status,
|
|
description: payload.description.trim(),
|
|
time: Date.now(),
|
|
}
|
|
if (!ticket) {
|
|
return response.notFound({ message: 'Ticket not found' })
|
|
}
|
|
const listHistory = ticket.history ? JSON.parse(ticket.history) : []
|
|
listHistory.unshift(history)
|
|
payload.history = JSON.stringify(listHistory)
|
|
ticket.merge(payload)
|
|
await ticket.save()
|
|
|
|
return response.ok({ status: true, message: 'Ticket updated successfully', data: ticket })
|
|
} catch (error) {
|
|
return response.internalServerError({
|
|
status: false,
|
|
message: 'Failed to update ticket',
|
|
error,
|
|
})
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Delete a ticket
|
|
*/
|
|
async delete({ request, response }: HttpContext) {
|
|
try {
|
|
const ticketId = request.param('id')
|
|
const ticket = await Ticket.findOrFail(ticketId)
|
|
|
|
if (!ticket) {
|
|
return response.notFound({ message: 'Ticket not found' })
|
|
}
|
|
|
|
await ticket.delete()
|
|
|
|
return response.ok({ status: true, message: 'Ticket deleted successfully' })
|
|
} catch (error) {
|
|
return response.internalServerError({
|
|
status: false,
|
|
message: 'Failed to delete ticket',
|
|
error,
|
|
})
|
|
}
|
|
}
|
|
}
|