import CommandModel from '#models/command' import { type CommandRecord, CommandStatus, type CommandType } from '#domain/commands/command' import { ModeType } from '#domain/shared/mode' import { Priority } from '#domain/commands/command' export default class CommandService { async getCommands() { const collection = CommandModel.getCollection() return collection.find({}).sort({ createdAt: -1 }).toArray() } async getCommandsByDevice(deviceIdentifier: string) { return CommandModel.findByDevice(deviceIdentifier) } async createCommand(data: { deviceIdentifier: string action: CommandType parameters: Record mode?: ModeType priority?: number }): Promise { return CommandModel.createCommand({ deviceIdentifier: data.deviceIdentifier, action: data.action, parameters: data.parameters, mode: data.mode || ModeType.NI, status: CommandStatus.PENDING, priority: data.priority ?? Priority.NORMAL, createdAt: new Date(), updatedAt: new Date(), }) } async getICommandsForDeviceWithStatus( deviceIdentifier: string, status: CommandStatus ): Promise { const collection = CommandModel.getCollection() return collection .find({ deviceIdentifier, mode: ModeType.I, status }) .sort({ priority: 1 }) .toArray() as unknown as Promise } async getNICommandsForDeviceWithStatus( deviceIdentifier: string, status: CommandStatus ): Promise { const collection = CommandModel.getCollection() return collection .find({ deviceIdentifier, mode: ModeType.NI, status }) .sort({ priority: 1 }) .toArray() as unknown as Promise } async updateCommandStatus(id: any, update: Partial): Promise { await CommandModel.updateCommand(id, update) } async deleteCommand(id: string): Promise { return CommandModel.deleteById(id) } }