GetOfficialTermRecords.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. const API = require('../../../../lib/API')
  2. const AccessControl = require('../../../../lib/AccessControl')
  3. const { BaseStdResponse } = require('../../../../BaseStdResponse')
  4. const { getCurrentTerm, getTermRunRecord } = require('../../../../lib/Lepao/lepaoOfficialApi')
  5. const {
  6. isMissing,
  7. loadAdminOfficialAccount,
  8. mapOfficialError
  9. } = require('../../../../lib/Lepao/officialAccountAccess')
  10. class AdminGetOfficialTermRecords extends API {
  11. constructor() {
  12. super()
  13. this.setPath('/Admin/Lepao/OfficialTermRecords')
  14. this.setMethod('GET')
  15. }
  16. async onRequest(req, res) {
  17. const { uuid, session, student_num } = req.query
  18. const page = Number(req.query.page || 1)
  19. if ([uuid, session, student_num].some(isMissing)) {
  20. return res.json({
  21. ...BaseStdResponse.MISSING_PARAMETER
  22. })
  23. }
  24. if (!Number.isFinite(page) || page <= 0) {
  25. return res.json({
  26. ...BaseStdResponse.ERR,
  27. msg: '参数错误'
  28. })
  29. }
  30. if (!await AccessControl.checkSession(uuid, session)) {
  31. return res.status(401).json({
  32. ...BaseStdResponse.ACCESS_DENIED
  33. })
  34. }
  35. try {
  36. const { account, error } = await loadAdminOfficialAccount(uuid, student_num)
  37. if (error) return res.json(error)
  38. const currentTerm = await getCurrentTerm(account, { logger: this.logger })
  39. const recordResult = await getTermRunRecord(
  40. account,
  41. { termId: currentTerm.term_id, page },
  42. { logger: this.logger }
  43. )
  44. return res.json({
  45. ...BaseStdResponse.OK,
  46. data: {
  47. term_id: currentTerm.term_id,
  48. term_name: currentTerm.term_name,
  49. page,
  50. ...(recordResult.data || {})
  51. }
  52. })
  53. } catch (error) {
  54. this.logger.error(`管理员获取官方学期乐跑记录失败 ${student_num}: ${error.stack || error}`)
  55. return res.json(await mapOfficialError(error, student_num, this.logger))
  56. }
  57. }
  58. }
  59. module.exports.AdminGetOfficialTermRecords = AdminGetOfficialTermRecords