const baseModel = require("../../middleware/baseModel");
const { build_search_where } = require("../utils/query_helpers");

module.exports = {
  "POST /biz_audit_log/page": async (ctx) => {
    const body = ctx.getBody();
    const param = body.param || body;
    const page_option = param.pageOption || {};
    const seach_option = param.seachOption || {};
    const page_num = parseInt(page_option.page, 10) || 1;
    const page_size = parseInt(page_option.pageSize, 10) || 20;
    const offset = (page_num - 1) * page_size;
    const biz_audit_log = baseModel.biz_audit_log;
    const where = build_search_where(biz_audit_log, seach_option);
    const tn = biz_audit_log.tableName;
    const { count, rows } = await biz_audit_log.findAndCountAll({
      where,
      offset,
      limit: page_size,
      order: [["id", "DESC"]],
      attributes: {
        include: [[biz_audit_log.sequelize.col(`${tn}.created_at`), "created_at"]],
      },
    });
    ctx.success({ rows, count });
  },
  "POST /biz_audit_log/export": async (ctx) => {
    const body = ctx.getBody();
    const param = body.param || body;
    const biz_audit_log = baseModel.biz_audit_log;
    const where = build_search_where(biz_audit_log, param.seachOption || {});
    const tn = biz_audit_log.tableName;
    const rows = await biz_audit_log.findAll({
      where,
      limit: 10000,
      order: [["id", "DESC"]],
      attributes: {
        include: [[biz_audit_log.sequelize.col(`${tn}.created_at`), "created_at"]],
      },
    });
    ctx.success({ rows });
  },
};
