Parcourir la source

🐛 fix(routes): 修复价格范围筛选逻辑错误

- 使用Between操作符优化同时提供minPrice和maxPrice时的价格范围查询
- 修复价格筛选条件判断逻辑,确保单一价格条件正确应用
- 在测试中添加Number转换,确保价格数值比较准确性
yourname il y a 3 mois
Parent
commit
8624692216

+ 5 - 4
src/server/modules/routes/route.service.ts

@@ -1,4 +1,4 @@
-import { DataSource, In, LessThanOrEqual, MoreThanOrEqual } from 'typeorm';
+import { DataSource, In, LessThanOrEqual, MoreThanOrEqual, Between } from 'typeorm';
 import { GenericCrudService } from '@/server/utils/generic-crud.service';
 import { RouteEntity } from './route.entity';
 import { VehicleType, TravelMode } from './route.schema';
@@ -88,10 +88,11 @@ export class RouteService extends GenericCrudService<RouteEntity> {
     }
 
     // 价格筛选
-    if (minPrice !== undefined) {
+    if (minPrice !== undefined && maxPrice !== undefined) {
+      where.price = Between(minPrice, maxPrice);
+    } else if (minPrice !== undefined) {
       where.price = MoreThanOrEqual(minPrice);
-    }
-    if (maxPrice !== undefined) {
+    } else if (maxPrice !== undefined) {
       where.price = LessThanOrEqual(maxPrice);
     }
 

+ 2 - 2
tests/integration/server/routes/search.integration.test.ts

@@ -352,8 +352,8 @@ describe('用户端路线搜索API集成测试', () => {
 
         // 验证价格范围
         responseData.data.routes.forEach((route: any) => {
-          expect(route.price).toBeGreaterThanOrEqual(110);
-          expect(route.price).toBeLessThanOrEqual(130);
+          expect(Number(route.price)).toBeGreaterThanOrEqual(110);
+          expect(Number(route.price)).toBeLessThanOrEqual(130);
         });
       }
     });