MCPcopy Index your code
hub / github.com/53AI/53AIHub / SyncOrganization

Function SyncOrganization

api/controller/sync_organization.go:33–81  ·  view source on GitHub ↗

@Summary Sync organization structure @Description Synchronize enterprise organization structure based on source @Tags Department @Accept json @Produce json @Security BearerAuth @Param from path int true "Source identifier (1=WeCom, 2=DingTalk)" @Param body body service.SyncOrganizationParams true "S

(c *gin.Context)

Source from the content-addressed store, hash-verified

31// @Failure 500 {object} model.CommonResponse "Server error"
32// @Router /api/departments/sync/{from} [post]
33func SyncOrganization(c *gin.Context) {
34 fromStr := c.Param("from")
35 from, err := strconv.Atoi(fromStr)
36 if err != nil {
37 c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(err))
38 return
39 }
40 eid := config.GetEID(c)
41 enterprise, err := model.GetEnterpriseByID(eid)
42 if err != nil {
43 c.JSON(http.StatusInternalServerError, model.DBError.ToResponse(err))
44 return
45 }
46
47 var params service.SyncOrganizationParams
48 if err = c.ShouldBindJSON(&params); err != nil {
49 c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(err))
50 return
51 }
52
53 lockKey := fmt.Sprintf("%s:%s:%d", model.LockOrganizationKeyPre, fromStr, enterprise.Eid)
54 isLock := common.LOCKER.TryLock(lockKey, 60*5*time.Second)
55 if !isLock {
56 c.JSON(http.StatusNotFound, model.OperateTooFast.ToResponse(nil))
57 return
58 }
59
60 switch from {
61 case model.DepartmentFromWecom:
62 go func() {
63 err := service.WeComRunSyncOrganization(enterprise, params)
64 if err != nil {
65 logger.SysErrorf("sync organization from wecom failed: %v", err)
66 }
67 }()
68 case model.DepartmentFromDingtalk:
69 go func() {
70 params.SuiteID = config.GetDingtalkSuiteID()
71 err := service.DingtalkRunSyncOrganization(enterprise, params)
72 if err != nil {
73 // 当同步失败时,删除锁
74 common.LOCKER.Unlock(lockKey)
75 logger.SysErrorf("sync organization from dingtalk failed: %v", err)
76 }
77 }()
78 }
79
80 c.JSON(http.StatusOK, model.Success.ToResponse(nil))
81}
82
83// GetSyncProgress 获取同步进度
84// @Summary Get sync progress

Callers

nothing calls this directly

Calls 4

ToResponseMethod · 0.80
GetEIDMethod · 0.80
TryLockMethod · 0.65
UnlockMethod · 0.65

Tested by

no test coverage detected