|
@@ -11,7 +11,6 @@ const managementClient = new ManagementClient({
|
|
|
|
|
|
import { pgClient } from '../../db/pg-instance'
|
|
|
export function defineAuthingDepartSync(){
|
|
|
-// function defineAuthingLogin(){
|
|
|
Parse.Cloud.define("authingDepartSync", async (request) => {
|
|
|
let result = await syncOrganizationsFromAuthing()
|
|
|
return result
|
|
@@ -28,9 +27,7 @@ export function defineAuthingDepartSync(){
|
|
|
*/
|
|
|
async function syncOrganizationsFromAuthing(){
|
|
|
// 通过用户的 id_token 初始化之后获取用户信息
|
|
|
- console.log("syncOrganizationsFromAuthing 2")
|
|
|
|
|
|
- console.log("managementClient")
|
|
|
let orgList = []
|
|
|
try{
|
|
|
|
|
@@ -43,8 +40,9 @@ async function syncOrganizationsFromAuthing(){
|
|
|
if(result?.data?.list?.length){
|
|
|
orgList = orgList.concat(result?.data?.list)
|
|
|
let departList = await getAllChildDepartments(orgList[0])
|
|
|
- console.log(departList)
|
|
|
+ // console.log(departList)
|
|
|
console.log(departList?.length)
|
|
|
+ // return
|
|
|
let insertRes = await InsertAllDepartment(departList)
|
|
|
console.log(insertRes)
|
|
|
return {code:200,count:departList?.length,message:`成功导入${departList?.length}条`}
|
|
@@ -77,16 +75,27 @@ async function InsertAllDepartment(departList){
|
|
|
let rowData = []
|
|
|
departList.forEach(depart=>{
|
|
|
let objectId = depart?.departmentId
|
|
|
+ if(!depart?.hasChildren){ // 最后一级,类型为单位
|
|
|
+ depart.type = "单位"
|
|
|
+ }
|
|
|
+ let parentId = depart?.parent?.departmentId // 指针是正常的
|
|
|
+ let branchName = depart?.branch?.name // 分值为当前单位最上级分支(表示应用下,分支最高级名称)
|
|
|
+
|
|
|
rowData.push([
|
|
|
- objectId,depart?.organizationCode,depart?.name,depart?.description,depart?.parent?.name,
|
|
|
- depart?.parent?.departmentId,depart?.code,depart?.createdAt || new Date(),depart?.updatedAt || new Date(),depart?.status])
|
|
|
+ // 1-5
|
|
|
+ objectId, depart?.organizationCode, depart?.name, depart?.description, branchName,
|
|
|
+ // 6-10
|
|
|
+ parentId, depart?.code, depart?.createdAt || new Date(), depart?.updatedAt || new Date(), depart?.status,
|
|
|
+ // 11
|
|
|
+ depart?.type,depart?.hasChildren
|
|
|
+ ])
|
|
|
})
|
|
|
let flattenedParams = rowData.flat();
|
|
|
|
|
|
let syncDepartSQL = `
|
|
|
- INSERT INTO "Department" ("objectId", "organizationCode", "name", "description","branch","parent","code","createdAt","updatedAt","status")
|
|
|
+ INSERT INTO "Department" ("objectId", "organizationCode", "name", "description","branch","parent","code","createdAt","updatedAt","status","type","hasChildren")
|
|
|
VALUES
|
|
|
- ${rowData.map((_, i) => `($${i * 10 + 1}, $${i * 10 + 2}, $${i * 10 + 3}, $${i * 10 + 4}, $${i * 10 + 5}, $${i * 10 + 6}, $${i * 10 + 7}, $${i * 10 + 8}, $${i * 10 + 9}, $${i * 10 + 10})`).join(',\n')}
|
|
|
+ ${rowData.map((_, i) => `($${i * 12 + 1}, $${i * 12 + 2}, $${i * 12 + 3}, $${i * 12 + 4}, $${i * 12 + 5}, $${i * 12 + 6}, $${i * 12 + 7}, $${i * 12 + 8}, $${i * 12 + 9}, $${i * 12 + 10}, $${i * 12 + 11}, $${i * 12 + 12})`).join(',\n')}
|
|
|
ON CONFLICT ("objectId") DO UPDATE
|
|
|
SET
|
|
|
"organizationCode" = EXCLUDED."organizationCode",
|
|
@@ -97,37 +106,67 @@ async function InsertAllDepartment(departList){
|
|
|
"code" = EXCLUDED."code",
|
|
|
"status" = EXCLUDED."status",
|
|
|
"createdAt" = EXCLUDED."createdAt",
|
|
|
- "updatedAt" = EXCLUDED."updatedAt";
|
|
|
+ "updatedAt" = EXCLUDED."updatedAt",
|
|
|
+ "type" = EXCLUDED."type",
|
|
|
+ "hasChildren" = EXCLUDED."hasChildren";
|
|
|
`
|
|
|
let result = await pgClient().any(syncDepartSQL,flattenedParams);
|
|
|
return result
|
|
|
}
|
|
|
|
|
|
-async function getAllChildDepartments(depart){
|
|
|
- if(!depart?.hasChildren) return []
|
|
|
+async function getAllChildDepartments(organization,depart,parentMap,level){
|
|
|
+ // if(level>3) return []
|
|
|
+ let newParentMap = JSON.parse(JSON.stringify(parentMap||{}))
|
|
|
+ level = level || 1
|
|
|
+ let currentDepart = depart || organization
|
|
|
+ if(level==1) newParentMap["1"] = newParentMap["1"] || depart
|
|
|
+ if(level==2) newParentMap["2"] = newParentMap["2"] || depart
|
|
|
+ if(level==3) newParentMap["3"] = newParentMap["3"] || depart
|
|
|
+ if(level==4) newParentMap["4"] = newParentMap["4"] || depart
|
|
|
+ if(!currentDepart?.hasChildren) return []
|
|
|
let departmentList = []
|
|
|
-
|
|
|
- let childDepartList = await getDepartmentOfOrg(depart?.organizationCode,depart?.departmentId)
|
|
|
+
|
|
|
+ let childDepartList
|
|
|
+ // if(organization){
|
|
|
+ try{
|
|
|
+ childDepartList = await getDepartmentOfOrg(currentDepart?.organizationCode,currentDepart?.departmentId,currentDepart,newParentMap)
|
|
|
+ // childDepartList?.forEach(item=>item.parent = currentDepart)
|
|
|
+ }catch(err){console.error(err)}
|
|
|
+ // }
|
|
|
departmentList = departmentList.concat(childDepartList)
|
|
|
// console.log("childDepartList",childDepartList)
|
|
|
- let pList = childDepartList.map(item=>getAllChildDepartments(item))
|
|
|
+ let pList = childDepartList?.map(item=>getAllChildDepartments(null,item,newParentMap,level+1))
|
|
|
let resList = await Promise.all(pList);
|
|
|
resList.forEach((list,index)=>{
|
|
|
- list = list.map(item=>{item.parent = childDepartList[index];return item})
|
|
|
- departmentList = departmentList.concat(list)
|
|
|
+ list = list?.map(item=>{
|
|
|
+ return item
|
|
|
+ })
|
|
|
+ if(list?.length) departmentList = departmentList.concat(list)
|
|
|
})
|
|
|
return departmentList
|
|
|
}
|
|
|
|
|
|
-async function getDepartmentOfOrg(organizationCode,departmentId){
|
|
|
- let result = await managementClient.listChildrenDepartments({
|
|
|
- // 替换组织 Code 和部门 ID
|
|
|
- organizationCode: organizationCode,
|
|
|
- departmentId: departmentId,
|
|
|
- departmentIdType: 'department_id',
|
|
|
- withCustomData: true
|
|
|
- });
|
|
|
- if(result?.data?.list?.length) return result?.data?.list
|
|
|
+async function getDepartmentOfOrg(organizationCode,departmentId,parent,parentMap){
|
|
|
+ let newParent = JSON.parse(JSON.stringify(parent))
|
|
|
+ let newParentMap = JSON.parse(JSON.stringify(parentMap))
|
|
|
+ let result
|
|
|
+ try{
|
|
|
+ result = await managementClient.listChildrenDepartments({
|
|
|
+ // 替换组织 Code 和部门 ID
|
|
|
+ organizationCode: organizationCode,
|
|
|
+ departmentId: departmentId,
|
|
|
+ departmentIdType: 'department_id',
|
|
|
+ withCustomData: true
|
|
|
+ });
|
|
|
+ }catch(err){}
|
|
|
+ if(result?.data?.list?.length) {
|
|
|
+ let list = result?.data?.list;
|
|
|
+ list.forEach(item=>{
|
|
|
+ item.branch = newParentMap?.["2"]
|
|
|
+ item.parent = newParent
|
|
|
+ })
|
|
|
+ return result?.data?.list
|
|
|
+ }
|
|
|
return []
|
|
|
}
|
|
|
|