100kmph/app/api/admin/jobs/[id]/actions/route.ts
2025-11-06 01:24:38 +05:30

79 lines
2.2 KiB
TypeScript

import { NextResponse } from 'next/server';
import { supabaseAdmin } from '@/lib/supabase';
// GET /api/admin/jobs/[id]/actions - Get all job actions for a service record
export async function GET(
request: Request,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params;
const serviceRecordId = id;
const { data, error } = await supabaseAdmin
.from('JobActions')
.select('*')
.eq('service_record', serviceRecordId)
.order('created_at', { ascending: false });
if (error) {
console.error('Supabase error:', error);
return NextResponse.json(
{ error: 'Failed to fetch job actions', details: error.message },
{ status: 500 }
);
}
return NextResponse.json(data || []);
} catch (error) {
console.error('Error fetching job actions:', error);
return NextResponse.json(
{ error: 'Failed to fetch job actions' },
{ status: 500 }
);
}
}
// POST /api/admin/jobs/[id]/actions - Create a new job action
export async function POST(
request: Request,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params;
const serviceRecordId = parseInt(id);
const body = await request.json();
const { action_type, part_type, part_id, custom_price, labour_fee } = body;
const { data, error } = await supabaseAdmin
.from('JobActions')
.insert({
service_record: serviceRecordId,
action_type: action_type || '',
part_type: part_type || null,
part_id: part_id || null,
custom_price: custom_price !== undefined ? custom_price : -1,
labour_fee: labour_fee !== undefined ? labour_fee : 0,
})
.select()
.single();
if (error) {
console.error('Supabase error:', error);
return NextResponse.json(
{ error: 'Failed to create job action', details: error.message },
{ status: 500 }
);
}
return NextResponse.json({ message: 'Job action created successfully', data }, { status: 201 });
} catch (error) {
console.error('Error creating job action:', error);
return NextResponse.json(
{ error: 'Failed to create job action' },
{ status: 500 }
);
}
}