GetAttendanceItemDetail.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const API = require("../../lib/API");
  2. const { BaseStdResponse } = require("../../BaseStdResponse");
  3. const db = require("../../plugin/DataBase/db");
  4. const AccessControl = require("../../lib/AccessControl");
  5. const UserInfoCache = require("../../lib/UserInfoCache");
  6. class GetAttendanceItemDetail extends API {
  7. constructor() {
  8. super();
  9. this.setPath('/GetAttendanceItemDetail');
  10. this.setMethod('GET');
  11. }
  12. async onRequest(req, res) {
  13. const { uuid, session, project_id } = req.query;
  14. // 检查必需的参数是否缺失
  15. if (!uuid || !session || !project_id) {
  16. return res.json({
  17. ...BaseStdResponse.MISSING_PARAMETER,
  18. endpoint: 1513123
  19. });
  20. }
  21. // 检查 session 是否有效
  22. if (!await AccessControl.checkSession(uuid, session)) {
  23. return res.json({
  24. ...BaseStdResponse.ACCESS_DENIED,
  25. endpoint: 48153145
  26. });
  27. }
  28. try {
  29. // 获取考勤项目详情
  30. const sqlGetProject = 'SELECT * FROM kq_items WHERE id = ?';
  31. let [projectResult] = await db.query(sqlGetProject, [project_id]);
  32. if (!projectResult || projectResult.length === 0) {
  33. return res.json({
  34. ...BaseStdResponse.DATABASE_ERR,
  35. endpoint: 154754511
  36. });
  37. }
  38. // 获取考勤记录
  39. const sqlGetRecords = 'SELECT id, uuid, time, commit FROM kq_records WHERE project_id = ?';
  40. let [records] = await db.query(sqlGetRecords, [project_id]);
  41. // 收集需要查询的用户 UUID
  42. const userUuids = new Set();
  43. const addUserUuid = (uuid) => userUuids.add(uuid);
  44. for (let item of projectResult) {
  45. addUserUuid(item.uuid);
  46. const users = JSON.parse(item.user || '[]');
  47. users.forEach(addUserUuid);
  48. if (item.admin) {
  49. const admins = JSON.parse(item.admin);
  50. admins.forEach(addUserUuid);
  51. }
  52. addUserUuid(item.createUser);
  53. }
  54. // 如果没有需要查询的用户,直接返回结果
  55. if (userUuids.size === 0) {
  56. return res.json({
  57. ...BaseStdResponse.OK,
  58. data: projectResult[0],
  59. userInfo: {},
  60. records
  61. });
  62. }
  63. const userInfo = {};
  64. // 从缓存中获取用户信息
  65. await Promise.all(Array.from(userUuids).map(async (uuid) => {
  66. const userCache = await UserInfoCache.getUserByUuid(uuid);
  67. userInfo[uuid] = {
  68. userCache
  69. };
  70. }));
  71. res.json({
  72. ...BaseStdResponse.OK,
  73. data: projectResult[0],
  74. userInfo,
  75. records
  76. });
  77. } catch (error) {
  78. res.json({
  79. ...BaseStdResponse.DATABASE_ERR,
  80. endpoint: 154754511
  81. });
  82. }
  83. }
  84. }
  85. module.exports.GetAttendanceItemDetail = GetAttendanceItemDetail;