@Summary Batch add users and departments to group @Description Add multiple users and departments to a specified group, either by department ID or by directly specifying user IDs @Tags Group @Accept json @Produce json @Security BearerAuth @Param id path int true "Group ID" @Param request body BatchA
(c *gin.Context)
| 888 | // @Success 200 {object} model.CommonResponse "Success" |
| 889 | // @Router /api/groups/{id}/users/batch [post] |
| 890 | func BatchAddUsersToGroup(c *gin.Context) { |
| 891 | // Parse Group ID |
| 892 | groupID, err := strconv.Atoi(c.Param("id")) |
| 893 | if err != nil { |
| 894 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(nil)) |
| 895 | return |
| 896 | } |
| 897 | |
| 898 | // Get Group information, verify its existence |
| 899 | group, err := model.GetGroupByID(int64(groupID)) |
| 900 | if err != nil { |
| 901 | c.JSON(http.StatusNotFound, model.NotFound.ToResponse(err)) |
| 902 | return |
| 903 | } |
| 904 | |
| 905 | // Verify enterprise permissions |
| 906 | if group.Eid != config.GetEID(c) { |
| 907 | c.JSON(http.StatusForbidden, model.NotFound.ToResponse(nil)) |
| 908 | return |
| 909 | } |
| 910 | |
| 911 | // Parse request body |
| 912 | var request BatchAddUsersToGroupRequest |
| 913 | if err := c.ShouldBindJSON(&request); err != nil { |
| 914 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(err)) |
| 915 | return |
| 916 | } |
| 917 | |
| 918 | // Begin transaction |
| 919 | tx := model.DB.Begin() |
| 920 | if tx.Error != nil { |
| 921 | c.JSON(http.StatusInternalServerError, model.DBError.ToResponse(tx.Error)) |
| 922 | return |
| 923 | } |
| 924 | |
| 925 | // Process user resources |
| 926 | if len(request.UserIDs) > 0 { |
| 927 | // Collect all user IDs to be added |
| 928 | allUserIDs := make([]int64, 0) |
| 929 | |
| 930 | // Add directly specified user IDs |
| 931 | if len(request.UserIDs) > 0 { |
| 932 | allUserIDs = append(allUserIDs, request.UserIDs...) |
| 933 | } |
| 934 | |
| 935 | // Remove duplicates |
| 936 | userIDMap := make(map[int64]bool) |
| 937 | uniqueUserIDs := make([]int64, 0) |
| 938 | for _, userID := range allUserIDs { |
| 939 | if !userIDMap[userID] { |
| 940 | uniqueUserIDs = append(uniqueUserIDs, userID) |
| 941 | userIDMap[userID] = true |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | // Batch create ResourcePermission records for users |
| 946 | for _, userID := range uniqueUserIDs { |
| 947 | permission := model.ResourcePermission{ |
nothing calls this directly
no test coverage detected