Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/app/api/assignment/auto/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
const yearMonth = searchParams.get('yearMonth');
const validateRules = searchParams.get('validateRules') === 'true';
const validateHardRule2 = searchParams.get('validateHardRule2') === 'true';
const autoAssign = searchParams.get('autoAssign') === 'true';

if (!yearMonth) {
return NextResponse.json({ error: 'yearMonth is required' }, { status: 400 });
Expand Down Expand Up @@ -79,6 +80,54 @@
return NextResponse.json({ error: 'No roster found' }, { status: 404 });
}

// Handle auto-assignment via GET request (for Cron jobs)
if (autoAssign) {
try {
// If no yearMonth provided, generate for next month
let targetYearMonth = yearMonth;
if (!targetYearMonth) {
const now = new Date();
const nextMonth = new Date(now.getFullYear(), now.getMonth() + 1, 1);
targetYearMonth = `${nextMonth.getFullYear()}-${String(nextMonth.getMonth() + 1).padStart(2, '0')}`;
}

console.log(`🚀 Starting auto-assignment for ${targetYearMonth} via Cron`);

// Create or get roster for the target month
const { data: targetRoster, error: targetRosterError } = await supabase
.from('crew_roster')
.upsert(
{ year_month: targetYearMonth, status: 'generated' },
{ onConflict: 'year_month' }
)
.select()
.single();

if (targetRosterError) {
console.error('Failed to create/get target roster:', targetRosterError);
return NextResponse.json({ error: 'Failed to create target roster' }, { status: 500 });
}

const result = await autoAssignCrew(targetYearMonth, false, targetRoster.id);
console.log(`✅ Auto-assignment completed for ${targetYearMonth}:`, result);
return NextResponse.json({
success: true,
message: `Auto-assignment completed for ${targetYearMonth}`,
result,
});
} catch (error) {
console.error(`❌ Auto-assignment failed:`, error);
return NextResponse.json(
{
success: false,
error: `Auto-assignment failed`,
details: error instanceof Error ? error.message : 'Unknown error',
},
{ status: 500 }
);
}
}

// Get assignments with all related data for validation (with pagination)
const allAssignments: any[] = [];
let from = 0;
Expand Down Expand Up @@ -238,7 +287,7 @@

return {
type: 'TIME_OVERLAP',
crewId: parseInt(crewId),

Check notice on line 290 in src/app/api/assignment/auto/route.ts

View workflow job for this annotation

GitHub Actions / Lint (Biome)

lint/correctness/useParseIntRadix

Missing radix parameter
crewName: assignment1.crew_member.name,
flight1: assignment1.flight.flight_code,
flight2: assignment2.flight.flight_code,
Expand Down Expand Up @@ -424,7 +473,7 @@
if (totalShiftDutyTime > VALIDATION_CONSTANTS.MAX_DAILY_DUTY_TIME) {
violations.push(
createDailyDutyTimeViolation(
parseInt(crewId),

Check notice on line 476 in src/app/api/assignment/auto/route.ts

View workflow job for this annotation

GitHub Actions / Lint (Biome)

lint/correctness/useParseIntRadix

Missing radix parameter
crewName,
day,
totalShiftDutyTime,
Expand All @@ -451,7 +500,7 @@

return {
type: 'REST_TIME_INSUFFICIENT',
crewId: parseInt(crewId),

Check notice on line 503 in src/app/api/assignment/auto/route.ts

View workflow job for this annotation

GitHub Actions / Lint (Biome)

lint/correctness/useParseIntRadix

Missing radix parameter
crewName,
day1,
day2,
Expand All @@ -464,7 +513,7 @@
}

// Helper function to validate rest time between different work days
function validateRestTimeLimit(assignments: any[]): any[] {

Check warning on line 516 in src/app/api/assignment/auto/route.ts

View workflow job for this annotation

GitHub Actions / Lint (Biome)

lint/complexity/noExcessiveCognitiveComplexity

Excessive complexity of 22 detected (max: 15).
const violations: any[] = [];

// Group assignments by crew member
Expand Down
8 changes: 8 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"crons": [
{
"path": "/api/assignment/auto?autoAssign=true",
"schedule": "15 1 * * *"
}
]
}
Loading