Procházet zdrojové kódy

♻️ refactor(routes): 优化路线相关类型定义和使用

- 将ActivityType枚举从activities模块迁移到routes模块
- 修改route schema中vehicleType和activity.type的验证方式,使用z.enum替代z.nativeEnum
- 将departureTime字段类型从string改为z.coerce.date(),统一日期处理方式
- 更新种子数据脚本,使用VehicleType枚举值替代字符串字面量

✨ feat(routes): 添加活动类型枚举定义

- 新增ActivityType枚举,包含DEPARTURE(去程活动)和RETURN(返程活动)选项
- 在route schema中应用ActivityType枚举进行活动类型验证
yourname před 4 měsíci
rodič
revize
7aa2d7f64d
2 změnil soubory, kde provedl 24 přidání a 18 odebrání
  1. 10 9
      scripts/seed.ts
  2. 14 9
      src/server/modules/routes/route.schema.ts

+ 10 - 9
scripts/seed.ts

@@ -1,6 +1,7 @@
 import { AppDataSource } from '../src/server/data-source.js';
 import { ActivityEntity, ActivityType } from '../src/server/modules/activities/activity.entity.js';
 import { RouteEntity } from '../src/server/modules/routes/route.entity.js';
+import { VehicleType } from '../src/server/modules/routes/route.schema.js';
 
 async function seed() {
   console.log('开始创建种子数据...');
@@ -80,7 +81,7 @@ async function seed() {
         pickupPoint: '北京南站',
         dropoffPoint: '上海虹桥站',
         departureTime: new Date('2025-10-01T08:00:00Z'),
-        vehicleType: 'bus',
+        vehicleType: VehicleType.BUS,
         price: 553.5,
         seatCount: 500,
         availableSeats: 450,
@@ -94,7 +95,7 @@ async function seed() {
         pickupPoint: '北京首都机场',
         dropoffPoint: '广州白云机场',
         departureTime: new Date('2025-10-01T10:30:00Z'),
-        vehicleType: 'minibus',
+        vehicleType: VehicleType.MINIBUS,
         price: 1200,
         seatCount: 200,
         availableSeats: 180,
@@ -108,7 +109,7 @@ async function seed() {
         pickupPoint: '北京西站',
         dropoffPoint: '深圳北站',
         departureTime: new Date('2025-10-01T09:15:00Z'),
-        vehicleType: 'car',
+        vehicleType: VehicleType.CAR,
         price: 756,
         seatCount: 600,
         availableSeats: 550,
@@ -124,7 +125,7 @@ async function seed() {
         pickupPoint: '上海虹桥站',
         dropoffPoint: '北京南站',
         departureTime: new Date('2025-10-07T14:00:00Z'),
-        vehicleType: 'bus',
+        vehicleType: VehicleType.BUS,
         price: 553.5,
         seatCount: 500,
         availableSeats: 400,
@@ -138,7 +139,7 @@ async function seed() {
         pickupPoint: '广州白云机场',
         dropoffPoint: '北京首都机场',
         departureTime: new Date('2025-10-07T16:30:00Z'),
-        vehicleType: 'minibus',
+        vehicleType: VehicleType.MINIBUS,
         price: 1100,
         seatCount: 200,
         availableSeats: 150,
@@ -154,7 +155,7 @@ async function seed() {
         pickupPoint: '北京南站',
         dropoffPoint: '天津站',
         departureTime: new Date('2026-01-01T09:00:00Z'),
-        vehicleType: 'car',
+        vehicleType: VehicleType.CAR,
         price: 54.5,
         seatCount: 600,
         availableSeats: 500,
@@ -170,7 +171,7 @@ async function seed() {
         pickupPoint: '天津站',
         dropoffPoint: '北京南站',
         departureTime: new Date('2026-01-03T18:00:00Z'),
-        vehicleType: 'car',
+        vehicleType: VehicleType.CAR,
         price: 54.5,
         seatCount: 600,
         availableSeats: 450,
@@ -186,7 +187,7 @@ async function seed() {
         pickupPoint: '北京朝阳站',
         dropoffPoint: '哈尔滨西站',
         departureTime: new Date('2026-02-10T07:30:00Z'),
-        vehicleType: 'bus',
+        vehicleType: VehicleType.BUS,
         price: 623.5,
         seatCount: 500,
         availableSeats: 480,
@@ -202,7 +203,7 @@ async function seed() {
         pickupPoint: '哈尔滨西站',
         dropoffPoint: '北京朝阳站',
         departureTime: new Date('2026-02-17T15:00:00Z'),
-        vehicleType: 'bus',
+        vehicleType: VehicleType.BUS,
         price: 623.5,
         seatCount: 500,
         availableSeats: 420,

+ 14 - 9
src/server/modules/routes/route.schema.ts

@@ -1,14 +1,19 @@
 import { z } from 'zod';
 import { DisabledStatus } from '@/share/types';
-import { ActivityType } from '../activities/activity.entity';
 
-// 车型枚举 
+// 车型枚举
 export enum VehicleType {
   BUS = 'bus',        // 大巴
   MINIBUS = 'minibus', // 中巴
   CAR = 'car'         // 小车
 }
 
+// 活动类型枚举
+export enum ActivityType {
+  DEPARTURE = 'departure', // 去程活动
+  RETURN = 'return'        // 返程活动
+}
+
 // 路线创建Schema
 export const createRouteSchema = z.object({
   name: z.string().min(1, '路线名称不能为空').max(255, '路线名称不能超过255个字符'),
@@ -17,8 +22,8 @@ export const createRouteSchema = z.object({
   endPoint: z.string().min(1, '目的地不能为空').max(255, '目的地不能超过255个字符'),
   pickupPoint: z.string().min(1, '上车点不能为空').max(255, '上车点不能超过255个字符'),
   dropoffPoint: z.string().min(1, '下车点不能为空').max(255, '下车点不能超过255个字符'),
-  departureTime: z.string().datetime('出发时间格式不正确'),
-  vehicleType: z.nativeEnum(VehicleType, {
+  departureTime: z.coerce.date(),
+  vehicleType: z.enum([VehicleType.BUS, VehicleType.MINIBUS, VehicleType.CAR], {
     message: '车型必须是有效的类型(bus/minibus/car)'
   }),
   price: z.number().min(0, '价格不能为负数').max(99999999.99, '价格不能超过99999999.99'),
@@ -35,8 +40,8 @@ export const updateRouteSchema = z.object({
   endPoint: z.string().min(1, '目的地不能为空').max(255, '目的地不能超过255个字符').optional(),
   pickupPoint: z.string().min(1, '上车点不能为空').max(255, '上车点不能超过255个字符').optional(),
   dropoffPoint: z.string().min(1, '下车点不能为空').max(255, '下车点不能超过255个字符').optional(),
-  departureTime: z.string().datetime('出发时间格式不正确').optional(),
-  vehicleType: z.nativeEnum(VehicleType, {
+  departureTime: z.coerce.date(),
+  vehicleType: z.enum([VehicleType.BUS, VehicleType.MINIBUS, VehicleType.CAR], {
     message: '车型必须是有效的类型(bus/minibus/car)'
   }).optional(),
   price: z.number().min(0, '价格不能为负数').max(99999999.99, '价格不能超过99999999.99').optional(),
@@ -56,7 +61,7 @@ export const getRouteSchema = z.object({
   pickupPoint: z.string().min(1, '上车点不能为空').max(255, '上车点不能超过255个字符'),
   dropoffPoint: z.string().min(1, '下车点不能为空').max(255, '下车点不能超过255个字符'),
   departureTime: z.coerce.date(),
-  vehicleType: z.nativeEnum(VehicleType, {
+  vehicleType: z.enum([VehicleType.BUS, VehicleType.MINIBUS, VehicleType.CAR], {
     message: '车型必须是有效的类型(bus/minibus/car)'
   }),
   price: z.coerce.number().min(0, '价格不能为负数').max(99999999.99, '价格不能超过99999999.99'),
@@ -94,7 +99,7 @@ export const routeListResponseSchema = z.object({
   pickupPoint: z.string().min(1, '上车点不能为空').max(255, '上车点不能超过255个字符'),
   dropoffPoint: z.string().min(1, '下车点不能为空').max(255, '下车点不能超过255个字符'),
   departureTime: z.coerce.date(),
-  vehicleType: z.nativeEnum(VehicleType, {
+  vehicleType: z.enum([VehicleType.BUS, VehicleType.MINIBUS, VehicleType.CAR], {
     message: '车型必须是有效的类型(bus/minibus/car)'
   }),
   price: z.coerce.number().min(0, '价格不能为负数').max(99999999.99, '价格不能超过99999999.99'),
@@ -109,7 +114,7 @@ export const routeListResponseSchema = z.object({
   activity: z.object({
     id: z.number().int().positive('活动ID必须为正整数'),
     name: z.string().min(1, '活动名称不能为空').max(255, '活动名称不能超过255个字符'),
-    type: z.nativeEnum(ActivityType, {
+    type: z.enum([ActivityType.DEPARTURE, ActivityType.RETURN], {
       message: '活动类型必须是departure(去程)或return(返程)'
     }),
   }).optional(),