Переглянути джерело

fix(benteler): enforce department option exclusivity

malk 2 тижнів тому
батько
коміт
49796dd798

+ 2 - 2
mjava-benteler/src/main/java/com/malk/benteler/service/BentelerYidaFormMapper.java

@@ -25,7 +25,7 @@ public class BentelerYidaFormMapper {
 
     private static final String STATUS_SUCCESS = "成功";
     private static final String STATUS_FAILED = "失败";
-    static final String UPDATE_PRIMARY_ORG = "更新部门";
+    static final String REPLACE_PRIMARY_ORG = "替换部门";
     static final String ADD_ORG = "增加部门";
     static final String UPDATE_PHONE = "手机号";
     static final String UPDATE_EMPLOYEE_NUMBER = "工号";
@@ -85,7 +85,7 @@ public class BentelerYidaFormMapper {
             item.setFormInstanceId(formInstanceId);
             item.setUserId(firstString(row.get(conf.getUpdateEmployeeFieldId() + "_id")));
             item.setUpdateContents(updateContents);
-            if (updateContents.contains(UPDATE_PRIMARY_ORG)) {
+            if (updateContents.contains(REPLACE_PRIMARY_ORG)) {
                 item.setPrimaryOrganizationalUnitId(organizationalUnitId);
             }
             if (updateContents.contains(ADD_ORG)) {

+ 11 - 1
mjava-benteler/src/main/java/com/malk/benteler/service/BentelerYidaSyncService.java

@@ -111,6 +111,7 @@ public class BentelerYidaSyncService {
             if (updateContents.isEmpty()) {
                 throw new McException("EIAM_UPDATE_CONTENT_EMPTY", "更新内容不能为空");
             }
+            validateUpdateContents(updateContents);
             String organizationalUnitId = null;
             if (requiresOrganizationalUnit(updateContents)) {
                 organizationalUnitId = firstText(formData.get(
@@ -225,10 +226,19 @@ public class BentelerYidaSyncService {
     }
 
     private boolean requiresOrganizationalUnit(List<String> updateContents) {
-        return updateContents.contains(BentelerYidaFormMapper.UPDATE_PRIMARY_ORG)
+        return updateContents.contains(BentelerYidaFormMapper.REPLACE_PRIMARY_ORG)
                 || updateContents.contains(BentelerYidaFormMapper.ADD_ORG);
     }
 
+    private void validateUpdateContents(List<String> updateContents) {
+        // prd 替换部门和增加部门会产生冲突,即使页面已过滤,后端仍需兜底。
+        if (updateContents.contains(BentelerYidaFormMapper.REPLACE_PRIMARY_ORG)
+                && updateContents.contains(BentelerYidaFormMapper.ADD_ORG)) {
+            throw new McException("EIAM_UPDATE_CONTENT_CONFLICT",
+                    "替换部门和增加部门不能同时选择");
+        }
+    }
+
     private List<String> texts(Object value) {
         List<String> values = new ArrayList<>();
         if (value instanceof Iterable) {

+ 5 - 5
mjava-benteler/src/test/java/com/malk/benteler/service/BentelerYidaFormMapperTest.java

@@ -105,14 +105,14 @@ public class BentelerYidaFormMapperTest {
     }
 
     @Test
-    public void mapUpdateItems_allSelections_mapsOnlyRequestedOperations() {
+    public void mapUpdateItems_addDepartmentAndPatch_mapsOnlyRequestedOperations() {
         Map<String, Object> row = new HashMap<>();
         row.put(conf.getUpdateEmployeeFieldId() + "_id", Collections.singletonList("user_1"));
         row.put(conf.getUpdatePhoneFieldId(), "17612168216");
         row.put(conf.getUpdateEmployeeNumberFieldId(), "EMP-001");
         row.put(conf.getUpdateJobTitleFieldId(), "Software Engineer");
-        List<String> updateContents = Arrays.asList(BentelerYidaFormMapper.UPDATE_PRIMARY_ORG,
-                BentelerYidaFormMapper.ADD_ORG, BentelerYidaFormMapper.UPDATE_PHONE,
+        List<String> updateContents = Arrays.asList(BentelerYidaFormMapper.ADD_ORG,
+                BentelerYidaFormMapper.UPDATE_PHONE,
                 BentelerYidaFormMapper.UPDATE_EMPLOYEE_NUMBER,
                 BentelerYidaFormMapper.UPDATE_JOB_TITLE);
 
@@ -120,7 +120,7 @@ public class BentelerYidaFormMapperTest {
                 Collections.singletonList(row), "ou_target", updateContents).get(0);
 
         assertEquals("user_1", item.getUserId());
-        assertEquals("ou_target", item.getPrimaryOrganizationalUnitId());
+        assertEquals(null, item.getPrimaryOrganizationalUnitId());
         assertEquals("ou_target", item.getAdditionalOrganizationalUnitId());
         assertEquals("17612168216", item.getUsername());
         assertEquals("17612168216", item.getPhoneNumber());
@@ -139,7 +139,7 @@ public class BentelerYidaFormMapperTest {
 
         EiamUpdateUserItem item = mapper.mapUpdateItems("form_update",
                 Collections.singletonList(row), "ou_target",
-                Collections.singletonList(BentelerYidaFormMapper.UPDATE_PRIMARY_ORG)).get(0);
+                Collections.singletonList(BentelerYidaFormMapper.REPLACE_PRIMARY_ORG)).get(0);
 
         assertEquals("ou_target", item.getPrimaryOrganizationalUnitId());
         assertEquals(null, item.getAdditionalOrganizationalUnitId());

+ 27 - 1
mjava-benteler/src/test/java/com/malk/benteler/service/BentelerYidaSyncServiceTest.java

@@ -11,6 +11,7 @@ import com.malk.benteler.dto.EiamUpdateUserItem;
 import com.malk.server.aliwork.YDAuth;
 import com.malk.server.aliwork.YDConf;
 import com.malk.server.aliwork.YDParam;
+import com.malk.server.common.McException;
 import com.malk.service.aliwork.YDClient_Form;
 import com.malk.service.aliwork.YDService;
 import org.junit.Before;
@@ -120,7 +121,7 @@ public class BentelerYidaSyncServiceTest {
                 Collections.singletonList("user_1"));
         Map<String, Object> formData = new HashMap<>();
         formData.put(conf.getUpdateContentFieldId(),
-                Collections.singletonList(BentelerYidaFormMapper.UPDATE_PRIMARY_ORG));
+                Collections.singletonList(BentelerYidaFormMapper.REPLACE_PRIMARY_ORG));
         formData.put(conf.getUpdateOrganizationalUnitIdFieldId(), "ou_target");
         formData.put(conf.getUpdateTableFieldId(), Collections.singletonList(row));
         when(ydClientForm.getForm(any(YDAuth.class), anyString(), isNull()))
@@ -140,6 +141,31 @@ public class BentelerYidaSyncServiceTest {
         assertEquals("ou_target", items.get(0).getPrimaryOrganizationalUnitId());
     }
 
+    @Test
+    public void syncUpdate_replaceAndAddDepartment_rejectsBeforeEiamCall() {
+        Map<String, Object> row = new HashMap<>();
+        row.put(conf.getUpdateEmployeeFieldId() + "_id",
+                Collections.singletonList("user_1"));
+        Map<String, Object> formData = new HashMap<>();
+        formData.put(conf.getUpdateContentFieldId(), Arrays.asList(
+                BentelerYidaFormMapper.REPLACE_PRIMARY_ORG,
+                BentelerYidaFormMapper.ADD_ORG));
+        formData.put(conf.getUpdateOrganizationalUnitIdFieldId(), "ou_target");
+        formData.put(conf.getUpdateTableFieldId(), Collections.singletonList(row));
+        when(ydClientForm.getForm(any(YDAuth.class), anyString(), isNull()))
+                .thenReturn(instance(formData));
+
+        try {
+            service.syncUpdate("form_update_conflict");
+        } catch (McException ex) {
+            assertEquals("EIAM_UPDATE_CONTENT_CONFLICT", ex.getCode());
+            assertEquals("替换部门和增加部门不能同时选择", ex.getMessage());
+            verify(eiamLocalService, never()).batchUpdate(any());
+            return;
+        }
+        throw new AssertionError("替换部门和增加部门同时选择时应拒绝请求");
+    }
+
     @Test
     @SuppressWarnings("unchecked")
     public void syncUpdate_phoneOnly_doesNotRequireOrganizationalUnit() {