优化
This commit is contained in:
+51
@@ -0,0 +1,51 @@
|
||||
package com.baiyue.warehouse.controller;
|
||||
|
||||
import com.baiyue.common.core.api.ApiResponse;
|
||||
import com.baiyue.common.core.api.PageResult;
|
||||
import com.baiyue.common.core.api.ResultCode;
|
||||
import com.baiyue.common.core.exception.BizException;
|
||||
import com.baiyue.common.security.AuthenticatedUser;
|
||||
import com.baiyue.warehouse.service.WarehouseApplicationService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** Read-only resource options, without granting warehouse master maintenance permissions. */
|
||||
@RestController
|
||||
public class PlatformOverseasReceivingOptionsController {
|
||||
private final WarehouseApplicationService service;
|
||||
|
||||
public PlatformOverseasReceivingOptionsController(WarehouseApplicationService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@GetMapping("/api/platform/v1/warehouse-admin/overseas-receiving-options")
|
||||
@PreAuthorize("hasAuthority('platform:overseas-warehouse:warehouse:list')")
|
||||
@Operation(summary = "Page active shared overseas receiving warehouse options",
|
||||
description = "Requires platform:overseas-warehouse:warehouse:list; only public OVERSEAS/ACTIVE/RECEIVING warehouses.")
|
||||
public ApiResponse<PageResult<WarehouseOption>> warehouses(
|
||||
@AuthenticationPrincipal AuthenticatedUser user,
|
||||
@RequestParam(defaultValue = "1") long page,
|
||||
@RequestParam(defaultValue = "200") long size) {
|
||||
if (user == null || !"PLATFORM".equals(user.appCode())
|
||||
|| user.tenantId() == null || user.tenantId() != 0L) {
|
||||
throw new BizException(ResultCode.FORBIDDEN, "仅允许平台端访问");
|
||||
}
|
||||
if (page < 1 || size < 1 || size > 200) throw new BizException(ResultCode.BAD_REQUEST, "分页参数无效");
|
||||
List<WarehouseOption> all = service.activeSharedOverseasWarehouses().stream()
|
||||
.map(value -> new WarehouseOption(value.warehouseId(), 0L, value.warehouseCode(),
|
||||
value.warehouseName(), "OVERSEAS", value.status())).toList();
|
||||
long pages = (all.size() + size - 1) / size;
|
||||
long from = page > pages ? all.size() : (page - 1) * size;
|
||||
long to = Math.min(from + size, all.size());
|
||||
return ApiResponse.success(new PageResult<>(all.subList((int) from, (int) to), page, size, all.size(), pages));
|
||||
}
|
||||
|
||||
public record WarehouseOption(long id, long customerId, String warehouseCode,
|
||||
String warehouseName, String warehouseType, String status) { }
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baiyue.warehouse.controller;
|
||||
|
||||
import com.baiyue.common.security.AuthenticatedUser;
|
||||
import com.baiyue.common.core.exception.BizException;
|
||||
import com.baiyue.warehouse.dto.SharedOverseasWarehouseResponse;
|
||||
import com.baiyue.warehouse.service.WarehouseApplicationService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class PlatformOverseasReceivingOptionsControllerTest {
|
||||
private final WarehouseApplicationService service = mock(WarehouseApplicationService.class);
|
||||
private final PlatformOverseasReceivingOptionsController controller = new PlatformOverseasReceivingOptionsController(service);
|
||||
private final AuthenticatedUser platform = new AuthenticatedUser(1L, 0L, 0L, 2L,
|
||||
"tester", "PLATFORM", "PLATFORM", Set.of());
|
||||
|
||||
@Test
|
||||
void pagesAuthoritativeSharedOptionsWithoutExposingMasterWrites() throws Exception {
|
||||
when(service.activeSharedOverseasWarehouses()).thenReturn(List.of(
|
||||
new SharedOverseasWarehouseResponse(21L, "US-LAX", "洛杉矶仓", "US", "ACTIVE")));
|
||||
var page = controller.warehouses(platform, 1, 200).data();
|
||||
assertEquals(1, page.total());
|
||||
assertEquals(21L, page.records().get(0).id());
|
||||
assertEquals(0L, page.records().get(0).customerId());
|
||||
assertEquals("OVERSEAS", page.records().get(0).warehouseType());
|
||||
assertEquals("hasAuthority('platform:overseas-warehouse:warehouse:list')",
|
||||
controller.getClass().getMethod("warehouses", AuthenticatedUser.class, long.class, long.class)
|
||||
.getAnnotation(PreAuthorize.class).value());
|
||||
assertTrue(controller.warehouses(platform, Long.MAX_VALUE, 200).data().records().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsInvalidScopeAndPaginationBeforeReadingDirectory() {
|
||||
var merchant = new AuthenticatedUser(2L, 31L, 31L, 3L, "client", "MERCHANT", "MERCHANT", Set.of());
|
||||
assertThrows(BizException.class, () -> controller.warehouses(merchant, 1, 200));
|
||||
assertThrows(BizException.class, () -> controller.warehouses(null, 1, 200));
|
||||
assertThrows(BizException.class, () -> controller.warehouses(platform, 0, 200));
|
||||
assertThrows(BizException.class, () -> controller.warehouses(platform, 1, 201));
|
||||
verifyNoInteractions(service);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user