const API = require('../../../../lib/API') const AccessControl = require('../../../../lib/AccessControl') const { BaseStdResponse } = require('../../../../BaseStdResponse') const { getCurrentTerm, getTermRunRecord } = require('../../../../lib/Lepao/lepaoOfficialApi') const { isMissing, loadAdminOfficialAccount, mapOfficialError, syncOfficialTotalNum } = require('../../../../lib/Lepao/officialAccountAccess') class AdminGetOfficialTermRecords extends API { constructor() { super() this.setPath('/Admin/Lepao/OfficialTermRecords') this.setMethod('GET') } async onRequest(req, res) { const { uuid, session, student_num } = req.query const page = Number(req.query.page || 1) if ([uuid, session, student_num].some(isMissing)) { return res.json({ ...BaseStdResponse.MISSING_PARAMETER }) } if (!Number.isFinite(page) || page <= 0) { return res.json({ ...BaseStdResponse.ERR, msg: '参数错误' }) } if (!await AccessControl.checkSession(uuid, session)) { return res.status(401).json({ ...BaseStdResponse.ACCESS_DENIED }) } try { const { account, error } = await loadAdminOfficialAccount(uuid, student_num) if (error) return res.json(error) const currentTerm = await getCurrentTerm(account, { logger: this.logger }) const recordResult = await getTermRunRecord( account, { termId: currentTerm.term_id, page }, { logger: this.logger } ) const recordData = recordResult.data || {} await syncOfficialTotalNum(student_num, recordData, this.logger) return res.json({ ...BaseStdResponse.OK, data: { term_id: currentTerm.term_id, term_name: currentTerm.term_name, page, ...recordData } }) } catch (error) { this.logger.error(`管理员获取官方学期乐跑记录失败 ${student_num}: ${error.stack || error}`) return res.json(await mapOfficialError(error, student_num, this.logger)) } } } module.exports.AdminGetOfficialTermRecords = AdminGetOfficialTermRecords