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

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

        this.setPath('/JW/Account')
        this.setMethod('GET')
    }

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

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

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

        let sql = `
                SELECT 
                    a.id,
                    a.username,
                    a.realname,
                    a.deptName,
                    a.className,
                    a.create_time,
                    a.update_time,
                    a.state,
                    u.username AS create_user
                FROM 
                    jw_account a
                LEFT JOIN 
                    users u 
                ON 
                    a.create_user = u.uuid
                WHERE 
                    a.create_user = ?
            `

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

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

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

module.exports.GetAccount = GetAccount;