@@ -3,8 +3,7 @@ import { query } from "@/lib/db";
33import { getCurrentUserId } from "@/lib/auth/current-user" ;
44import type { VisitPlanList } from "@/lib/types" ;
55import { PLAN_LIST_COLUMNS } from "@/lib/visitPlanListSql" ;
6-
7- const DATE_RE = / ^ \d { 4 } - \d { 2 } - \d { 2 } $ / ;
6+ import { normalizePlanDates } from "@/lib/visitPlanListDates" ;
87
98/**
109 * 現在のユーザーの、指定スポット種別の訪問予定リスト一覧。各リストの経由スポットは
@@ -39,10 +38,11 @@ export async function GET(request: Request) {
3938 group by l.id
4039 order by ${
4140 // アーカイブの一覧は「最近しまったもの」から見たいので archived_at 順。
42- // 通常の一覧は従来どおり訪問予定期間の新しい順
41+ // 通常の一覧はこれから回る順に読めるよう開始日の昇順(開始日を持たない
42+ // リストは末尾へ)。同じ開始日なら作った順
4343 archived
4444 ? "l.archived_at desc"
45- : "l.start_date desc , l.created_at desc "
45+ : "l.start_date asc nulls last , l.created_at asc "
4646 } `,
4747 [ userId , typeKey ]
4848 ) ;
@@ -63,9 +63,8 @@ export async function POST(request: Request) {
6363 typeof body ?. description === "string" && body . description . trim ( )
6464 ? body . description . trim ( )
6565 : null ;
66- const startDate = body ?. start_date ;
67- // 終了日が空なら開始日と同じ(=単日)にする
68- const endDate = body ?. end_date || startDate ;
66+ // 日付は未指定なら「訪問日未定」(両方null)。終了日だけの指定は断る
67+ const dates = normalizePlanDates ( body ?? { } ) ;
6968 const spotIds : string [ ] = Array . isArray ( body ?. spot_ids )
7069 ? body . spot_ids . filter ( ( s : unknown ) : s is string => typeof s === "string" )
7170 : [ ] ;
@@ -76,17 +75,8 @@ export async function POST(request: Request) {
7675 { status : 400 }
7776 ) ;
7877 }
79- if ( ! DATE_RE . test ( startDate ) || ! DATE_RE . test ( endDate ) ) {
80- return NextResponse . json (
81- { error : "訪問予定期間の日付が不正です。" } ,
82- { status : 400 }
83- ) ;
84- }
85- if ( endDate < startDate ) {
86- return NextResponse . json (
87- { error : "終了日は開始日以降にしてください。" } ,
88- { status : 400 }
89- ) ;
78+ if ( ! dates . ok ) {
79+ return NextResponse . json ( { error : dates . error } , { status : 400 } ) ;
9080 }
9181
9282 const typeRow = await query < { id : string } > (
@@ -106,7 +96,7 @@ export async function POST(request: Request) {
10696 (user_id, spot_type_id, title, description, start_date, end_date)
10797 values ($1, $2, $3, $4, $5, $6)
10898 returning id` ,
109- [ userId , spotTypeId , title , description , startDate , endDate ]
99+ [ userId , spotTypeId , title , description , dates . start , dates . end ]
110100 ) ;
111101 const listId = rows [ 0 ] . id ;
112102
0 commit comments