diff --git a/run.sh b/run.sh index 6381b59..613f56e 100644 --- a/run.sh +++ b/run.sh @@ -5,5 +5,5 @@ set -e npm run db:migrate & PID=$! wait $PID -npm run local & PID=$! +node ./build/server.js & PID=$! wait $PID \ No newline at end of file diff --git a/src/application/services/ipAddress.service.ts b/src/application/services/ipAddress.service.ts index f691538..b906846 100644 --- a/src/application/services/ipAddress.service.ts +++ b/src/application/services/ipAddress.service.ts @@ -9,6 +9,7 @@ export interface IPAddressQueryParams { address?: string, status?: IPAddressStatus, poolId?: number, + machineId?: number, sortBy?: IPAddressSortBy, sortOrder?: SortOrder } diff --git a/src/application/services/machine.service.ts b/src/application/services/machine.service.ts index 3e81401..d5515ca 100644 --- a/src/application/services/machine.service.ts +++ b/src/application/services/machine.service.ts @@ -112,9 +112,11 @@ export async function deleteMachine( ipAddressRepo: IIPAddressRepository, id: number ) { - const ipAddress = await ipAddressRepo.getIPAddressByMachineId(id) - if (ipAddress.id) { - await ipAddressRepo.updateIPAddress(ipAddress.id, { + const [ipAddress] = await ipAddressRepo.getIPAddresses({ + machineId: id + }) + if (ipAddress!.id) { + await ipAddressRepo.updateIPAddress(ipAddress!.id, { status: IPAddressStatus.Released, releasedAt: new Date() }) diff --git a/src/persistence/drizzle/ipAddress.persistence.ts b/src/persistence/drizzle/ipAddress.persistence.ts index 1fbda62..ab7129d 100644 --- a/src/persistence/drizzle/ipAddress.persistence.ts +++ b/src/persistence/drizzle/ipAddress.persistence.ts @@ -20,6 +20,9 @@ function buildIPAddressQueryFilters(queryParams?: IPAddressQueryParams): SQL[] { if (queryParams.poolId) { filters.push(eq(ipAddressTable.poolId, queryParams.poolId)) } + if (queryParams.machineId) { + filters.push(eq(ipAddressTable.machineId, queryParams.machineId)) + } return filters } @@ -78,15 +81,6 @@ export class IPAddressDrizzleRepository implements IIPAddressRepository { return ipAddress as IIPAddress } - async getIPAddressByMachineId(machineId: number) { - const [ipAddress] = await db - .select() - .from(ipAddressTable) - .where(eq(ipAddressTable.machineId, machineId)) - - return ipAddress as IIPAddress - } - async createIPAddress(ipAddress: IIPAddress) { const [createdIPAddress] = await db .insert(ipAddressTable) diff --git a/src/persistence/repositories/ipAddress.repository.ts b/src/persistence/repositories/ipAddress.repository.ts index e3523d2..71badb8 100644 --- a/src/persistence/repositories/ipAddress.repository.ts +++ b/src/persistence/repositories/ipAddress.repository.ts @@ -4,7 +4,6 @@ import { IPAddressQueryParams } from "../../application/services/ipAddress.servi export interface IIPAddressRepository { getIPAddresses(ipAddressQueryParams?: IPAddressQueryParams): Promise getIPAddressById(id: number): Promise - getIPAddressByMachineId(machineId: number): Promise createIPAddress(ipAddress: IIPAddress): Promise updateIPAddress(id: number, ipAddress: Partial): Promise deleteIPAddress(id: number): Promise diff --git a/src/presentation/server/controllers/ipAddress.controller.ts b/src/presentation/server/controllers/ipAddress.controller.ts index 8e90d99..37416a3 100644 --- a/src/presentation/server/controllers/ipAddress.controller.ts +++ b/src/presentation/server/controllers/ipAddress.controller.ts @@ -10,6 +10,7 @@ export async function getIPAddresses(req: Request, res: Response) { address: req.query.address as string, status: req.query.status as IPAddressStatus, poolId: Number(req.query.poolId), + machineId: Number(req.query.machineId), sortBy: req.query.sortBy as ipAddressService.IPAddressSortBy, sortOrder: req.query.sortOrder as SortOrder } diff --git a/src/server.ts b/src/server.ts index e569acd..f6e89e8 100644 --- a/src/server.ts +++ b/src/server.ts @@ -29,7 +29,11 @@ const server = express() // Add a list of allowed origins. // If you have more origins you would like to add, you can add them to the array below. -const allowedOrigins = ["http://localhost:3000", "http://127.0.0.1:3000"] +const allowedOrigins = [ + "http://localhost:3000", + "http://127.0.0.1:3000", + "https://dc-manager-frontend-703684363125.asia-east1.run.app" +] const options: cors.CorsOptions = { origin: allowedOrigins, diff --git a/tests/unit/application/ipAddress.service.test.ts b/tests/unit/application/ipAddress.service.test.ts index c490b83..e0dab1d 100644 --- a/tests/unit/application/ipAddress.service.test.ts +++ b/tests/unit/application/ipAddress.service.test.ts @@ -8,7 +8,6 @@ const mockIpAddressRepo: jest.Mocked = { createIPAddress: jest.fn(), updateIPAddress: jest.fn(), deleteIPAddress: jest.fn(), - getIPAddressByMachineId: jest.fn() } beforeEach(() => { diff --git a/tests/unit/application/machine.service.test.ts b/tests/unit/application/machine.service.test.ts index ef9bd1d..016398c 100644 --- a/tests/unit/application/machine.service.test.ts +++ b/tests/unit/application/machine.service.test.ts @@ -1,6 +1,8 @@ import * as machineService from "../../../src/application/services/machine.service" import { IMachineRepository } from "../../../src/persistence/repositories/machine.repository" import { IMachine, MachineStatus } from "../../../src/domain/machine" +import { getIPAddresses } from "../../../src/application/services/ipAddress.service" +import { IPAddressStatus } from "../../../src/domain/ipAddress" const mockMachineRepo: jest.Mocked = { getMachines: jest.fn(), @@ -88,7 +90,7 @@ describe("machineService - createMachine", () => { // Mock dependencies required by createMachine const mockIpAddressRepo = { - getIPAddresses: jest.fn().mockResolvedValue([]), // 新增这一行 + getIPAddresses: jest.fn().mockResolvedValue([]), getIPAddressesByPoolId: jest.fn().mockResolvedValue([]), updateIPAddress: jest.fn() } as any @@ -154,7 +156,11 @@ describe("machineService - deleteMachine", () => { // Mock ipAddressRepo with required methods const mockIpAddressRepo = { - getIPAddressByMachineId: jest.fn().mockResolvedValue({}), + getIPAddresses: jest.fn().mockResolvedValue([{ + id: 123, + machineId: 1, + status: IPAddressStatus.Allocated + }]), updateIPAddress: jest.fn() } as any