trigger-department.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // https://docs.authing.cn/v3/reference/sdk/node/management/%E7%AE%A1%E7%90%86%E7%BB%84%E7%BB%87%E6%9C%BA%E6%9E%84/update-department.html
  2. const { ManagementClient,Models } = require('authing-node-sdk')
  3. const managementClient = new ManagementClient({
  4. accessKeyId: '669b25e1731d50c59f5494d1', // 用户池级别AKSK
  5. accessKeySecret: "4cfc095a72a67e22065c97e90054315c",
  6. host: 'https://textbook.u2-dev.hep.com.cn', // 应用的认证地址
  7. })
  8. /**
  9. * @example
  10. curl -X POST -H "Content-Type: application/json" -H "X-Parse-Application-Id: edu-textbook" -d '{
  11. "name": "测试部门",
  12. "organizationCode": 1000000
  13. }' http://127.0.0.1:61337/parse/classes/Department
  14. */
  15. export function defineDepartmentTrigger(){
  16. Parse.Cloud.beforeSave("Department", async (request) => {
  17. try{
  18. await syncDepartmentInfo(request,request.object)
  19. }catch(err){
  20. console.error(err)
  21. }
  22. });
  23. Parse.Cloud.beforeDelete("Department", async (request) => {
  24. let organizationCode = request?.object?.get("organizationCode")
  25. if(organizationCode) organizationCode = String(organizationCode)
  26. const result = await managementClient.deleteDepartment({
  27. // 替换组织 Code 和部门 ID
  28. organizationCode: organizationCode,
  29. departmentId: request?.object?.id
  30. });
  31. });
  32. }
  33. async function syncDepartmentInfo(request,depart){
  34. let authDepartment = {}
  35. if(depart?.id) authDepartment.departmentId = depart?.id
  36. if(depart?.get("status")) authDepartment.status = depart?.get("status")
  37. if(depart?.get("code")) authDepartment.code = depart?.get("code")
  38. if(depart?.get("name")) authDepartment.name = depart?.get("name")
  39. if(depart?.get("organizationCode")) authDepartment.organizationCode = String(depart?.get("organizationCode"))
  40. if(depart?.get("discription")) authDepartment.discription = depart?.get("discription")
  41. authDepartment.organizationCode = authDepartment.organizationCode || "1000000"
  42. let parentDepartmentId = depart?.get("parent")?.id || depart?.get("parent")?.objectId || "root"
  43. if(parentDepartmentId) authDepartment.parentDepartmentId = parentDepartmentId
  44. let result
  45. if(depart?.id){ // 修改
  46. // INSERT INTO "Department" ("objectId", "organizationCode", "name", "description","branch","parent","code","createdAt","updatedAt","status","type","hasChildren")
  47. // let objectId = depart?.departmentId
  48. // if(!depart?.hasChildren){ // 最后一级,类型为单位
  49. // depart.type = "单位"
  50. // }
  51. // let parentId = depart?.parent?.departmentId // 指针是正常的
  52. // let branchName = depart?.branch?.name // 分值为当前单位最上级分支(表示应用下,分支最高级名称)
  53. // rowData.push([
  54. // // 1-5
  55. // objectId, depart?.organizationCode, depart?.name, depart?.description, branchName,
  56. // // 6-10
  57. // parentId, depart?.code, depart?.createdAt || new Date(), depart?.updatedAt || new Date(), depart?.status,
  58. // // 11
  59. // depart?.type,depart?.hasChildren
  60. // ])
  61. // authDepartment.departmentIdType = Models.UpdateDepartmentReqDto.departmentIdType.DEPARTMENT_ID
  62. console.log("updateDepartment authing")
  63. console.log(authDepartment)
  64. result = await managementClient.updateDepartment(authDepartment);
  65. console.log("update:",result)
  66. if(result?.message?.indexOf('Department id not found')>-1){
  67. result = await managementClient.createDepartment(authDepartment);
  68. console.log("create:",result)
  69. }
  70. }else{ // 新建
  71. // authDepartment.openDepartmentId = depart?.id
  72. // console.log(authDepartment)
  73. console.log("createDepartment authing")
  74. result = await managementClient.createDepartment(authDepartment);
  75. console.log(result)
  76. if(result?.statusCode==200){
  77. request.object.id = result?.data?.departmentId
  78. }
  79. }
  80. // console.log(result)
  81. }