const API = require("../../lib/API.js");
const db = require("../../plugin/DataBase/db.js");
const { BaseStdResponse } = require("../../BaseStdResponse.js");
const AccessControl = require("../../lib/AccessControl.js");

class GetAccount extends API {
    constructor() {
        super();

        this.setPath('/Power/GetChangeRecord')
        this.setMethod('POST')
    }

    async onRequest(req, res) {
        let { uuid, session, id } = req.body

        if ([uuid, session, id].some(value => value === '' || value === null || value === undefined))
            return res.json({
                ...BaseStdResponse.MISSING_PARAMETER
            })

        if (!await AccessControl.checkSession(uuid, session))
            return res.status(401).json({
                ...BaseStdResponse.ACCESS_DENIED
            })

        let sql = `
                SELECT 
                    *
                FROM 
                    power_change_record
                WHERE 
                    task_id = ?
                ORDER BY id DESC
            `

        let rows = await db.query(sql, [id])

        if (!rows)
            return res.json({
                ...BaseStdResponse.DATABASE_ERR
            })

        res.json({
            ...BaseStdResponse.OK,
            data: rows
        })
    }
}

module.exports.GetAccount = GetAccount