diff --git a/apps/web/app/api/timer/timesheet/bulk-delete/route.ts b/apps/web/app/api/timer/timesheet/bulk-delete/route.ts index 792e8e00e..517e1f624 100644 --- a/apps/web/app/api/timer/timesheet/bulk-delete/route.ts +++ b/apps/web/app/api/timer/timesheet/bulk-delete/route.ts @@ -2,8 +2,17 @@ import { deleteTaskTimesheetRequest } from '@/app/services/server/requests'; import { authenticatedGuard } from '@app/services/server/guards/authenticated-guard-app'; import { NextResponse } from "next/server"; -export async function GET(req: Request) { +export async function DELETE(req: Request) { const res = new NextResponse(); + const body = await req.json(); + const { logIds = [] } = body; + + if (!Array.isArray(logIds) || logIds.length === 0) { + return NextResponse.json( + { error: 'logIds must be a non-empty array' }, + { status: 400 } + ); + } const { $res, user, tenantId, organizationId, access_token, } = await authenticatedGuard(req, res); if (!user) return $res('Unauthorized'); @@ -11,7 +20,7 @@ export async function GET(req: Request) { const { data } = await deleteTaskTimesheetRequest({ tenantId, organizationId, - logIds: [], + logIds, }, access_token); if (!data) { diff --git a/apps/web/app/hooks/features/useTimesheet.ts b/apps/web/app/hooks/features/useTimesheet.ts index 63bb39a49..343a45531 100644 --- a/apps/web/app/hooks/features/useTimesheet.ts +++ b/apps/web/app/hooks/features/useTimesheet.ts @@ -16,7 +16,11 @@ export interface GroupedTimesheet { date: string; tasks: ITimeSheet[]; } - +interface DeleteTimesheetParams { + organizationId: string; + tenantId: string; + logIds: string[]; +} const groupByDate = (items: ITimeSheet[]): GroupedTimesheet[] => { if (!items?.length) return []; @@ -49,6 +53,7 @@ export function useTimesheet({ const { loading: loadingTimesheet, queryCall: queryTimesheet } = useQuery(getTaskTimesheetLogsApi); const { loading: loadingDeleteTimesheet, queryCall: queryDeleteTimesheet } = useQuery(deleteTaskTimesheetLogsApi) + const getTaskTimesheet = useCallback( ({ startDate, endDate }: TimesheetParams) => { if (!user) return; @@ -70,15 +75,37 @@ export function useTimesheet({ ); - const deleteTaskTimesheet = useCallback(() => { - if (!user) return; - queryDeleteTimesheet({ - organizationId: user.employee.organizationId, - tenantId: user.tenantId ?? "", - logIds: [] - }) - }, []) + const handleDeleteTimesheet = async (params: DeleteTimesheetParams) => { + try { + return await queryDeleteTimesheet(params); + } catch (error) { + console.error('Error deleting timesheet:', error); + throw error; + } + }; + const deleteTaskTimesheet = useCallback( + async ({ logIds }: DeleteTimesheetParams): Promise => { + if (!user) { + throw new Error('User not authenticated'); + } + if (!logIds.length) { + throw new Error('No timesheet IDs provided for deletion'); + } + + try { + await handleDeleteTimesheet({ + organizationId: user.employee.organizationId, + tenantId: user.tenantId ?? "", + logIds + }); + } catch (error) { + console.error('Failed to delete timesheets:', error); + throw error; + } + }, + [user, queryDeleteTimesheet] + ); useEffect(() => { getTaskTimesheet({ startDate, endDate }); diff --git a/apps/web/app/services/client/api/timer/timer-log.ts b/apps/web/app/services/client/api/timer/timer-log.ts index 1846c48dd..5d5d49c10 100644 --- a/apps/web/app/services/client/api/timer/timer-log.ts +++ b/apps/web/app/services/client/api/timer/timer-log.ts @@ -67,15 +67,31 @@ export async function deleteTaskTimesheetLogsApi({ tenantId: string, logIds: string[] }) { + // Validate required parameters + if (!organizationId || !tenantId || !logIds?.length) { + throw new Error('Required parameters missing: organizationId, tenantId, and logIds are required'); + } + + // Limit bulk deletion size for safety + if (logIds.length > 100) { + throw new Error('Maximum 100 logs can be deleted at once'); + } + const params = new URLSearchParams({ organizationId, tenantId }); logIds.forEach((id, index) => { + if (!id) { + throw new Error(`Invalid logId at index ${index}`); + } params.append(`logIds[${index}]`, id); }); const endPoint = `/timesheet/time-log?${params.toString()}`; - return deleteApi(endPoint, { tenantId }); - + try { + return await deleteApi<{ success: boolean; message: string }>(endPoint, { tenantId }); + } catch (error) { + throw new Error(`Failed to delete timesheet logs`); + } } diff --git a/apps/web/app/services/server/requests/timesheet.ts b/apps/web/app/services/server/requests/timesheet.ts index 105bde9a2..7a6d82d3f 100644 --- a/apps/web/app/services/server/requests/timesheet.ts +++ b/apps/web/app/services/server/requests/timesheet.ts @@ -87,12 +87,11 @@ type IDeleteTimesheetProps = { } export function deleteTaskTimesheetRequest(params: IDeleteTimesheetProps, bearer_token: string) { - const queries = qs.stringify(params); - return serverFetch({ - path: `/timesheet/time-log?${queries.toString()}`, + const { logIds = [] } = params; + return serverFetch({ + path: `/timesheet/time-log/${logIds.join(',')}`, method: 'DELETE', - bearer_token: bearer_token, - tenantId: params.tenantId, - body: params - }) + bearer_token, + tenantId: params.tenantId + }); }