ifms-printer-manager/src/controllers/PrinterStatusController.ts

49 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-06-28 18:34:15 +00:00
import { Router, Request, Response } from 'express'
import { prisma } from '../prisma.js'
import { PrinterStatusService } from '../services/PrinterStatusService.js'
import { hasRolesMiddleware } from '../middlewares/hasRolesMiddleware.js'
2023-07-03 19:49:29 +00:00
import { distributedCopy } from '../utils/distributedCopy.js'
2023-06-20 19:21:28 +00:00
const router = Router()
class PrinterStatusController {
static async update(req: Request, res: Response) {
const printers = await prisma.printer.findMany()
2023-06-28 18:34:15 +00:00
printers.forEach(async printer => {
2023-06-20 19:21:28 +00:00
new PrinterStatusService(printer)
})
2023-06-28 18:34:15 +00:00
res.json({ message: 'Updating printer status' })
2023-06-20 19:21:28 +00:00
}
2023-07-03 19:49:29 +00:00
static async status(req: Request, res: Response) {
const { printerId } = req.params
const { take = 32, days = 60 } = req.query
const gte = new Date(Date.now() - 1000 * 60 * 60 * 24 * Number(days))
const status = await prisma.printerStatus.findMany({
where: {
printerId: Number(printerId),
timestamp: {
gte
}
},
orderBy: { timestamp: 'desc' }
})
const distributedStatus = distributedCopy(status, Number(take))
res.json(distributedStatus)
}
2023-06-20 19:21:28 +00:00
}
2023-06-28 18:34:15 +00:00
router.use(hasRolesMiddleware(['ADMIN', 'INSPECTOR']))
2023-06-20 19:48:25 +00:00
2023-06-28 18:34:15 +00:00
router.post('/update', PrinterStatusController.update)
2023-07-03 19:49:29 +00:00
router.get('/:printerId', PrinterStatusController.status)
2023-06-20 19:45:35 +00:00
2023-06-20 19:21:28 +00:00
export default router