132 lines
3.4 KiB
TypeScript
132 lines
3.4 KiB
TypeScript
import assert from 'node:assert/strict'
|
|
import test from 'node:test'
|
|
import prisma from '../../utils/prisma'
|
|
import { completeMaintenance, createMaintenance } from '../maintenanceController'
|
|
|
|
const createMockResponse = () => {
|
|
const responseBody = { statusCode: 200, body: null as any }
|
|
const res = {
|
|
locals: {
|
|
user: { id: 9, username: 'it-user', name: 'IT用户' }
|
|
},
|
|
status(code: number) {
|
|
responseBody.statusCode = code
|
|
return this
|
|
},
|
|
json(payload: any) {
|
|
responseBody.body = payload
|
|
return this
|
|
}
|
|
}
|
|
return { res, responseBody }
|
|
}
|
|
|
|
const equipment = {
|
|
id: 101,
|
|
equipment_code: 'JS-PC-0101',
|
|
equipment_name: '主机',
|
|
parent_id: null,
|
|
department_id: 3,
|
|
storage_location_id: 2,
|
|
status: '可用',
|
|
total_quantity: 1,
|
|
remaining_quantity: 1,
|
|
unit_price: 1000
|
|
}
|
|
|
|
test('并发送修未抢到资产状态时不创建第二条维修单', async () => {
|
|
const originalTransaction = prisma.$transaction
|
|
let maintenanceCreated = false
|
|
let acquireWhere: any = null
|
|
|
|
const tx = {
|
|
equipment: {
|
|
findUnique: async () => equipment,
|
|
findMany: async () => [],
|
|
updateMany: async (args: any) => {
|
|
acquireWhere = args.where
|
|
return { count: 0 }
|
|
}
|
|
},
|
|
borrowRecord: { count: async () => 0 },
|
|
disposalItem: { count: async () => 0 },
|
|
maintenanceRecord: {
|
|
count: async () => 0,
|
|
create: async () => {
|
|
maintenanceCreated = true
|
|
return { id: 1 }
|
|
}
|
|
}
|
|
}
|
|
|
|
;(prisma.$transaction as any) = async (callback: any) => callback(tx)
|
|
try {
|
|
const { res, responseBody } = createMockResponse()
|
|
await createMaintenance(
|
|
{
|
|
body: {
|
|
equipment_id: equipment.id,
|
|
issue_description: '无法开机',
|
|
estimated_cost: 100
|
|
}
|
|
} as any,
|
|
res as any
|
|
)
|
|
|
|
assert.equal(responseBody.statusCode, 409)
|
|
assert.equal(maintenanceCreated, false)
|
|
assert.deepEqual(acquireWhere, {
|
|
id: equipment.id,
|
|
status: '可用',
|
|
parent_id: null,
|
|
remaining_quantity: 1,
|
|
department_id: 3
|
|
})
|
|
} finally {
|
|
;(prisma.$transaction as any) = originalTransaction
|
|
}
|
|
})
|
|
test('重复完成维修时用 in_progress 条件阻止再次覆盖资产', async () => {
|
|
const originalTransaction = prisma.$transaction
|
|
let equipmentUpdated = false
|
|
let maintenanceWhere: any = null
|
|
|
|
const tx = {
|
|
maintenanceRecord: {
|
|
findUnique: async () => ({
|
|
id: 20,
|
|
equipment_id: equipment.id,
|
|
equipment_code: equipment.equipment_code,
|
|
status: 'in_progress',
|
|
estimated_cost: 100
|
|
}),
|
|
updateMany: async (args: any) => {
|
|
maintenanceWhere = args.where
|
|
return { count: 0 }
|
|
}
|
|
},
|
|
equipment: {
|
|
findUnique: async () => ({ ...equipment, status: '维护中' }),
|
|
updateMany: async () => {
|
|
equipmentUpdated = true
|
|
return { count: 1 }
|
|
}
|
|
}
|
|
}
|
|
|
|
;(prisma.$transaction as any) = async (callback: any) => callback(tx)
|
|
try {
|
|
const { res, responseBody } = createMockResponse()
|
|
await completeMaintenance(
|
|
{ params: { id: '20' }, body: { repair_result: '已修复' } } as any,
|
|
res as any
|
|
)
|
|
|
|
assert.equal(responseBody.statusCode, 409)
|
|
assert.deepEqual(maintenanceWhere, { id: 20, status: 'in_progress' })
|
|
assert.equal(equipmentUpdated, false)
|
|
} finally {
|
|
;(prisma.$transaction as any) = originalTransaction
|
|
}
|
|
})
|