GetAttendanceItemDetail.js 3.2 KB

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