Răsfoiți Sursa

fix(order-module): 修复PostgreSQL兼容性问题并配置测试环境

1. 修复DATE_FORMAT函数兼容性:
   - 将MySQL的DATE_FORMAT函数替换为PostgreSQL的TO_CHAR函数
   - 修复getSalaryRecords方法的月份过滤查询
   - 修复getSalaryVideos方法的月份过滤查询
   - 更新SQL语法以符合PostgreSQL标准

2. 配置测试环境:
   - 在vitest.config.ts中设置NODE_ENV=test环境变量
   - 配置TEST_DATABASE_URL指向测试数据库
   - 确保集成测试使用独立的测试数据库

测试结果:
- 所有12个人才就业API集成测试通过 ✅
- 修复前: 2个测试失败(月份过滤500错误)
- 修复后: 12/12测试通过

🤖 Generated with [Claude Code](https://claude.com/claude-code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
yourname 3 săptămâni în urmă
părinte
comite
fc5ed5eeea

+ 6 - 9
allin-packages/order-module/src/services/order.service.ts

@@ -979,8 +979,8 @@ export class OrderService extends GenericCrudService<EmploymentOrder> {
     // 构建查询条件
     const whereConditions = { personId };
 
-    // 月份过滤
-    const dateFilter = month ? `DATE_FORMAT(op.joinDate, '%Y-%m') = '${month}'` : '';
+    // 月份过滤 - PostgreSQL使用TO_CHAR函数
+    const dateFilter = month ? `TO_CHAR(op.joinDate, 'YYYY-MM') = :month` : '';
 
     // 先获取总数
     const countQueryBuilder = this.orderPersonRepository
@@ -988,8 +988,7 @@ export class OrderService extends GenericCrudService<EmploymentOrder> {
       .where('op.personId = :personId', { personId });
 
     if (month) {
-      countQueryBuilder.andWhere('DATE_FORMAT(op.joinDate, :format) = :month', {
-        format: '%Y-%m',
+      countQueryBuilder.andWhere("TO_CHAR(op.joinDate, 'YYYY-MM') = :month", {
         month
       });
     }
@@ -1002,8 +1001,7 @@ export class OrderService extends GenericCrudService<EmploymentOrder> {
       .where('op.personId = :personId', { personId });
 
     if (month) {
-      dataQueryBuilder.andWhere('DATE_FORMAT(op.joinDate, :format) = :month', {
-        format: '%Y-%m',
+      dataQueryBuilder.andWhere("TO_CHAR(op.joinDate, 'YYYY-MM') = :month", {
         month
       });
     }
@@ -1113,10 +1111,9 @@ export class OrderService extends GenericCrudService<EmploymentOrder> {
       queryBuilder.andWhere('asset.assetType = :assetType', { assetType });
     }
 
-    // 月份过滤 - relatedTime是timestamp类型
+    // 月份过滤 - relatedTime是timestamp类型,PostgreSQL使用TO_CHAR函数
     if (month) {
-      queryBuilder.andWhere('DATE_FORMAT(asset.relatedTime, :format) = :month', {
-        format: '%Y-%m',
+      queryBuilder.andWhere("TO_CHAR(asset.relatedTime, 'YYYY-MM') = :month", {
         month
       });
     }

+ 7 - 1
allin-packages/order-module/vitest.config.ts

@@ -16,6 +16,12 @@ export default defineConfig({
       ]
     },
     // 关闭并行测试以避免数据库连接冲突
-    fileParallelism: false
+    fileParallelism: false,
+    // 设置测试环境变量
+    setupFiles: [],
+    env: {
+      NODE_ENV: 'test',
+      TEST_DATABASE_URL: 'postgresql://postgres:test_password@localhost:5432/test_d8dai'
+    }
   }
 });