- Add #domain, #protocol, #utils aliases to package.json imports map - Replace all relative imports (./, ../, ../..) in app/ and database/mongo_migrations/ with the corresponding #alias imports, eliminating relative path traversal across the codebase - Remove dead code: ConfigurationVersionModel.findByConfigurationIdentifier (unused, superseded by paginateVersions) and unused exception classes (NotFoundException, UnprocessableEntityException, DatabaseException) from app/exceptions/index.ts, keeping only BadRequestException - Fix pre-existing syntax error in configuration_version.ts (mismatched parenthesis) that broke tsc - Fix eqeqeq violation in configuration_service.ts and isNaN -> Number.isNaN in configuration_engine.ts / cwmp-1-0.ts / exchange_controller.ts - Fix no-shadow violation in cwmp-1-0.ts EventStruct constructor - Prettier formatting pass on touched files Validated: tsc --noEmit clean, eslint app/ clean, full test suite green.
99 lines
3.4 KiB
TypeScript
99 lines
3.4 KiB
TypeScript
import type { HttpContext } from '@adonisjs/core/http'
|
|
import DeviceService from '#services/device_service'
|
|
import { type PaginationData } from '#services/device_service'
|
|
import { type CommandType } from '#domain/commands/command'
|
|
import DeviceModel from '#models/device'
|
|
|
|
export default class DevicesController {
|
|
private deviceService = new DeviceService()
|
|
|
|
async criteriaValues({ response }: HttpContext) {
|
|
const values = await DeviceModel.distinctCriteriaValues()
|
|
return response.json(values)
|
|
}
|
|
|
|
async index({ request, response }: HttpContext) {
|
|
const offset = Number(request.qs().offset) || 0
|
|
const limit = Number(request.qs().limit) || 100
|
|
|
|
const { data, nbDocuments, pager } = await this.deviceService.getDevices(offset, limit)
|
|
|
|
response.header('x-total-count', nbDocuments.toString())
|
|
response.header('link', this.generateLinkHeader(request, pager))
|
|
return response.json(data)
|
|
}
|
|
|
|
async show({ params, response }: HttpContext) {
|
|
const device = await this.deviceService.getDeviceByIdentifier(params.identifier)
|
|
if (!device) {
|
|
return response.status(404).json({ message: 'Device not found' })
|
|
}
|
|
return response.json(device)
|
|
}
|
|
|
|
async destroy({ params, response }: HttpContext) {
|
|
const deleted = await this.deviceService.deleteByIdentifier(params.identifier)
|
|
if (!deleted) {
|
|
return response.status(404).json({ message: 'Device not found' })
|
|
}
|
|
return response.json({ message: 'Device deleted successfully' })
|
|
}
|
|
|
|
async executeCommand({ params, request, response }: HttpContext) {
|
|
try {
|
|
const body = request.body() as {
|
|
action: CommandType
|
|
parameters?: Record<string, any>
|
|
mode?: string
|
|
priority?: number
|
|
}
|
|
await this.deviceService.executeCommand(params.identifier, body)
|
|
return response.status(201).json({ message: 'Command created' })
|
|
} catch (error: any) {
|
|
if (error.message?.includes('not found')) {
|
|
return response.status(404).json({ message: error.message })
|
|
}
|
|
if (error.message?.includes('Duplicate')) {
|
|
return response.status(409).json({ message: error.message })
|
|
}
|
|
return response.status(400).json({ message: error.message })
|
|
}
|
|
}
|
|
|
|
async refresh({ params, response }: HttpContext) {
|
|
try {
|
|
await this.deviceService.refreshDevice(params.identifier)
|
|
return response.status(200).json({ message: 'Refresh command sent' })
|
|
} catch (error: any) {
|
|
if (error.message?.includes('not found')) {
|
|
return response.status(404).json({ message: error.message })
|
|
}
|
|
return response.status(500).json({ message: error.message })
|
|
}
|
|
}
|
|
|
|
private generateLinkHeader(request: any, pager: PaginationData): string {
|
|
const baseUrl = `${request.protocol()}://${request.host()}${request.url().split('?')[0]}`
|
|
const parts: string[] = []
|
|
|
|
if (pager.first) {
|
|
parts.push(
|
|
`<${baseUrl}?offset=${pager.first.offset}&limit=${pager.first.limit}>; rel="first"`
|
|
)
|
|
}
|
|
if (pager.previous) {
|
|
parts.push(
|
|
`<${baseUrl}?offset=${pager.previous.offset}&limit=${pager.previous.limit}>; rel="prev"`
|
|
)
|
|
}
|
|
if (pager.next) {
|
|
parts.push(`<${baseUrl}?offset=${pager.next.offset}&limit=${pager.next.limit}>; rel="next"`)
|
|
}
|
|
if (pager.last) {
|
|
parts.push(`<${baseUrl}?offset=${pager.last.offset}&limit=${pager.last.limit}>; rel="last"`)
|
|
}
|
|
|
|
return parts.join(',')
|
|
}
|
|
}
|