Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ set -e
npm run db:migrate & PID=$!
wait $PID

npm run local & PID=$!
node ./build/server.js & PID=$!
wait $PID
1 change: 1 addition & 0 deletions src/application/services/ipAddress.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface IPAddressQueryParams {
address?: string,
status?: IPAddressStatus,
poolId?: number,
machineId?: number,
sortBy?: IPAddressSortBy,
sortOrder?: SortOrder
}
Expand Down
8 changes: 5 additions & 3 deletions src/application/services/machine.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down
12 changes: 3 additions & 9 deletions src/persistence/drizzle/ipAddress.persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/persistence/repositories/ipAddress.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { IPAddressQueryParams } from "../../application/services/ipAddress.servi
export interface IIPAddressRepository {
getIPAddresses(ipAddressQueryParams?: IPAddressQueryParams): Promise<IIPAddress[]>
getIPAddressById(id: number): Promise<IIPAddress>
getIPAddressByMachineId(machineId: number): Promise<IIPAddress>
createIPAddress(ipAddress: IIPAddress): Promise<number>
updateIPAddress(id: number, ipAddress: Partial<IIPAddress>): Promise<IIPAddress>
deleteIPAddress(id: number): Promise<number>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 5 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion tests/unit/application/ipAddress.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const mockIpAddressRepo: jest.Mocked<IIPAddressRepository> = {
createIPAddress: jest.fn(),
updateIPAddress: jest.fn(),
deleteIPAddress: jest.fn(),
getIPAddressByMachineId: jest.fn()
}

beforeEach(() => {
Expand Down
10 changes: 8 additions & 2 deletions tests/unit/application/machine.service.test.ts
Original file line number Diff line number Diff line change
@@ -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<IMachineRepository> = {
getMachines: jest.fn(),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down