person-extension.integration.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import { describe, it, expect, beforeEach } from 'vitest';
  2. import { testClient } from 'hono/testing';
  3. import { IntegrationTestDatabase, setupIntegrationDatabaseHooksWithEntities } from '@d8d/shared-test-util';
  4. import { JWTUtil } from '@d8d/shared-utils';
  5. import { UserEntity, Role } from '@d8d/user-module';
  6. import { File } from '@d8d/file-module';
  7. import { Platform } from '@d8d/allin-platform-module/entities';
  8. import { EmploymentOrder, OrderPerson, OrderPersonAsset } from '@d8d/allin-order-module/entities';
  9. import { BankName } from '@d8d/bank-names-module';
  10. import { DisabledPerson, DisabledBankCard, DisabledPhoto, DisabledRemark, DisabledVisit } from '../../src/entities';
  11. import personExtensionRoutes from '../../src/routes/person-extension.route';
  12. import { Company } from '@d8d/allin-company-module/entities';
  13. // 设置集成测试钩子 - 需要包含所有相关实体
  14. setupIntegrationDatabaseHooksWithEntities([
  15. UserEntity, File, Role, Platform, Company,
  16. EmploymentOrder, OrderPerson, OrderPersonAsset,
  17. DisabledPerson, BankName, DisabledBankCard, DisabledPhoto, DisabledRemark, DisabledVisit
  18. ])
  19. describe('人才扩展API集成测试', () => {
  20. let client: ReturnType<typeof testClient<typeof personExtensionRoutes>>;
  21. let testToken: string;
  22. let testUser: UserEntity;
  23. let testCompany: Company;
  24. let testPlatform: Platform;
  25. let testDisabledPerson: DisabledPerson;
  26. let testOrder: EmploymentOrder;
  27. beforeEach(async () => {
  28. // 创建测试客户端
  29. client = testClient(personExtensionRoutes);
  30. // 获取数据源
  31. const dataSource = await IntegrationTestDatabase.getDataSource();
  32. // 创建测试平台
  33. const platformRepository = dataSource.getRepository(Platform);
  34. testPlatform = platformRepository.create({
  35. platformName: `测试平台_${Date.now()}`,
  36. contactPerson: '平台管理员',
  37. contactPhone: '13800138000',
  38. contactEmail: 'admin@example.com',
  39. address: '测试地址',
  40. status: 1
  41. });
  42. await platformRepository.save(testPlatform);
  43. // 创建测试公司
  44. const companyRepository = dataSource.getRepository(Company);
  45. testCompany = companyRepository.create({
  46. companyName: `测试公司_${Date.now()}`,
  47. contactPerson: '公司联系人',
  48. contactPhone: '13900139000',
  49. contactEmail: 'company@example.com',
  50. address: '公司地址',
  51. platformId: testPlatform.id,
  52. status: 1
  53. });
  54. await companyRepository.save(testCompany);
  55. // 创建测试企业用户
  56. const userRepository = dataSource.getRepository(UserEntity);
  57. testUser = userRepository.create({
  58. username: `enterprise_user_${Date.now()}`,
  59. password: 'test_password',
  60. nickname: '企业测试用户',
  61. registrationSource: 'web',
  62. companyId: testCompany.id
  63. });
  64. await userRepository.save(testUser);
  65. // 生成测试用户的token
  66. testToken = JWTUtil.generateToken({
  67. id: testUser.id,
  68. username: testUser.username,
  69. roles: [{ name: 'enterprise_user' }],
  70. companyId: testCompany.id
  71. });
  72. // 创建测试残疾人
  73. const disabledPersonRepo = dataSource.getRepository(DisabledPerson);
  74. testDisabledPerson = disabledPersonRepo.create({
  75. name: '测试残疾人',
  76. idCard: `110101${Date.now() % 100000000}`,
  77. gender: '男',
  78. birthDate: new Date('1990-01-01'),
  79. disabilityType: '视力残疾',
  80. disabilityLevel: '一级',
  81. disabilityId: `DIS${Date.now() % 100000000}`,
  82. idAddress: '身份证地址',
  83. phone: '13800138000',
  84. province: '北京市',
  85. city: '北京市',
  86. address: '测试地址'
  87. });
  88. await disabledPersonRepo.save(testDisabledPerson);
  89. // 创建测试订单
  90. const orderRepo = dataSource.getRepository(EmploymentOrder);
  91. testOrder = orderRepo.create({
  92. orderName: '测试订单',
  93. platformId: testPlatform.id,
  94. companyId: testCompany.id,
  95. orderStatus: 'confirmed',
  96. workStatus: 'working'
  97. });
  98. await orderRepo.save(testOrder);
  99. // 创建订单人员关联,使人员属于该企业
  100. const orderPersonRepo = dataSource.getRepository(OrderPerson);
  101. const orderPerson = orderPersonRepo.create({
  102. orderId: testOrder.id,
  103. personId: testDisabledPerson.id,
  104. joinDate: new Date('2024-01-01'),
  105. workStatus: 'working',
  106. salaryDetail: 5000.00
  107. });
  108. await orderPersonRepo.save(orderPerson);
  109. });
  110. describe('GET /api/v1/yongren/disability-person/{id}/work-history', () => {
  111. it('应该返回人员工作历史', async () => {
  112. // 准备测试数据:多个订单关联
  113. const dataSource = await IntegrationTestDatabase.getDataSource();
  114. const orderPersonRepo = dataSource.getRepository(OrderPerson);
  115. // 创建另一个订单关联
  116. const orderRepo = dataSource.getRepository(EmploymentOrder);
  117. const anotherOrder = orderRepo.create({
  118. orderName: '另一个测试订单',
  119. platformId: testPlatform.id,
  120. companyId: testCompany.id,
  121. orderStatus: 'completed',
  122. workStatus: 'working'
  123. });
  124. await orderRepo.save(anotherOrder);
  125. const anotherOrderPerson = orderPersonRepo.create({
  126. orderId: anotherOrder.id,
  127. personId: testDisabledPerson.id,
  128. joinDate: new Date('2024-06-01'),
  129. actualStartDate: new Date('2024-06-02'),
  130. leaveDate: new Date('2024-12-31'),
  131. workStatus: 'resigned',
  132. salaryDetail: 6000.00
  133. });
  134. await orderPersonRepo.save(anotherOrderPerson);
  135. // 调用API
  136. const response = await client.api.v1.yongren.disabilityPerson[':id'].workHistory.$get({
  137. param: { id: testDisabledPerson.id },
  138. header: {
  139. Authorization: `Bearer ${testToken}`
  140. }
  141. });
  142. expect(response.status).toBe(200);
  143. const data = await response.json();
  144. // 验证响应结构
  145. expect(data).toHaveProperty('工作历史');
  146. expect(Array.isArray(data.工作历史)).toBe(true);
  147. // 验证数据
  148. expect(data.工作历史.length).toBeGreaterThanOrEqual(1);
  149. const workHistory = data.工作历史[0];
  150. expect(workHistory).toHaveProperty('订单ID');
  151. expect(workHistory).toHaveProperty('订单名称');
  152. expect(workHistory).toHaveProperty('入职日期');
  153. expect(workHistory).toHaveProperty('工作状态');
  154. expect(workHistory).toHaveProperty('个人薪资');
  155. });
  156. it('访问其他企业人员应该返回403', async () => {
  157. // 创建另一个公司的残疾人
  158. const dataSource = await IntegrationTestDatabase.getDataSource();
  159. // 创建另一个公司
  160. const companyRepository = dataSource.getRepository(Company);
  161. const otherCompany = companyRepository.create({
  162. companyName: `其他公司_${Date.now()}`,
  163. contactPerson: '其他联系人',
  164. contactPhone: '13900139001',
  165. contactEmail: 'other@example.com',
  166. address: '其他地址',
  167. platformId: testPlatform.id,
  168. status: 1
  169. });
  170. await companyRepository.save(otherCompany);
  171. // 创建另一个公司的残疾人
  172. const disabledPersonRepo = dataSource.getRepository(DisabledPerson);
  173. const otherDisabledPerson = disabledPersonRepo.create({
  174. name: '其他公司残疾人',
  175. idCard: `110101${Date.now() % 100000000 + 1000}`,
  176. gender: '女',
  177. birthDate: new Date('1995-01-01'),
  178. disabilityType: '听力残疾',
  179. disabilityLevel: '二级',
  180. disabilityId: `DIS${Date.now() % 100000000 + 1000}`,
  181. idAddress: '其他地址',
  182. phone: '13900139000',
  183. province: '上海市',
  184. city: '上海市',
  185. address: '其他地址'
  186. });
  187. await disabledPersonRepo.save(otherDisabledPerson);
  188. // 尝试访问其他公司人员数据
  189. const response = await client.api.v1.yongren.disabilityPerson[':id'].workHistory.$get({
  190. param: { id: otherDisabledPerson.id },
  191. header: {
  192. Authorization: `Bearer ${testToken}`
  193. }
  194. });
  195. expect(response.status).toBe(403);
  196. });
  197. });
  198. describe('GET /api/v1/yongren/disability-person/{id}/salary-history', () => {
  199. it('应该返回人员薪资历史', async () => {
  200. // 注意:薪资历史可能需要从薪资模块获取,这里暂时返回空数组
  201. const response = await client.api.v1.yongren.disabilityPerson[':id'].salaryHistory.$get({
  202. param: { id: testDisabledPerson.id },
  203. header: {
  204. Authorization: `Bearer ${testToken}`
  205. }
  206. });
  207. expect(response.status).toBe(200);
  208. const data = await response.json();
  209. // 验证响应结构
  210. expect(data).toHaveProperty('薪资历史');
  211. expect(Array.isArray(data.薪资历史)).toBe(true);
  212. });
  213. });
  214. describe('GET /api/v1/yongren/disability-person/{id}/credit-info', () => {
  215. it('应该返回人员征信信息', async () => {
  216. // 准备测试数据:银行卡信息
  217. const dataSource = await IntegrationTestDatabase.getDataSource();
  218. // 创建文件
  219. const fileRepository = dataSource.getRepository(File);
  220. const testFile = fileRepository.create({
  221. name: '银行卡照片.jpg',
  222. type: 'image/jpeg',
  223. size: 1024,
  224. path: 'uploads/bank-card.jpg',
  225. description: '银行卡照片',
  226. uploadUserId: testUser.id,
  227. uploadTime: new Date()
  228. });
  229. await fileRepository.save(testFile);
  230. // 创建银行名称记录
  231. const bankNameRepo = dataSource.getRepository(BankName);
  232. const bankName = bankNameRepo.create({
  233. bankName: '测试银行',
  234. bankCode: 'TESTBANK',
  235. status: 1
  236. });
  237. await bankNameRepo.save(bankName);
  238. // 创建银行卡记录
  239. const bankCardRepo = dataSource.getRepository(DisabledBankCard);
  240. const bankCard = bankCardRepo.create({
  241. personId: testDisabledPerson.id,
  242. subBankName: '测试支行',
  243. bankNameId: bankName.id, // 使用实际创建的银行ID
  244. cardNumber: '6228481234567890123',
  245. cardholderName: '测试持卡人',
  246. cardType: '一类卡',
  247. fileId: testFile.id,
  248. isDefault: 1
  249. });
  250. await bankCardRepo.save(bankCard);
  251. // 调用API
  252. const response = await client.api.v1.yongren.disabilityPerson[':id'].creditInfo.$get({
  253. param: { id: testDisabledPerson.id },
  254. header: {
  255. Authorization: `Bearer ${testToken}`
  256. }
  257. });
  258. expect(response.status).toBe(200);
  259. const data = await response.json();
  260. // 验证响应结构
  261. expect(data).toHaveProperty('征信信息');
  262. expect(Array.isArray(data.征信信息)).toBe(true);
  263. if (data.征信信息.length > 0) {
  264. const creditInfo = data.征信信息[0];
  265. expect(creditInfo).toHaveProperty('文件ID');
  266. expect(creditInfo).toHaveProperty('银行卡号');
  267. expect(creditInfo).toHaveProperty('持卡人姓名');
  268. }
  269. });
  270. });
  271. describe('GET /api/v1/yongren/disability-person/{id}/videos', () => {
  272. it('应该返回人员视频关联', async () => {
  273. // 准备测试数据:订单人员资产(视频)
  274. const dataSource = await IntegrationTestDatabase.getDataSource();
  275. // 创建文件
  276. const fileRepository = dataSource.getRepository(File);
  277. const testFile = fileRepository.create({
  278. name: '工作视频.mp4',
  279. type: 'video/mp4',
  280. size: 1024000,
  281. path: 'uploads/work-video.mp4',
  282. description: '工作视频',
  283. uploadUserId: testUser.id,
  284. uploadTime: new Date()
  285. });
  286. await fileRepository.save(testFile);
  287. // 创建订单人员资产记录
  288. const assetRepo = dataSource.getRepository(OrderPersonAsset);
  289. const asset = assetRepo.create({
  290. orderId: testOrder.id,
  291. personId: testDisabledPerson.id,
  292. assetType: 'work_video',
  293. assetFileType: 'video',
  294. fileId: testFile.id,
  295. relatedTime: new Date()
  296. });
  297. await assetRepo.save(asset);
  298. // 调用API
  299. const response = await client.api.v1.yongren.disabilityPerson[':id'].videos.$get({
  300. param: { id: testDisabledPerson.id },
  301. header: {
  302. Authorization: `Bearer ${testToken}`
  303. }
  304. });
  305. expect(response.status).toBe(200);
  306. const data = await response.json();
  307. // 验证响应结构
  308. expect(data).toHaveProperty('视频列表');
  309. expect(Array.isArray(data.视频列表)).toBe(true);
  310. if (data.视频列表.length > 0) {
  311. const video = data.视频列表[0];
  312. expect(video).toHaveProperty('视频类型');
  313. expect(video).toHaveProperty('文件ID');
  314. expect(video).toHaveProperty('关联订单ID');
  315. }
  316. });
  317. });
  318. });