Skip to content

Commit

Permalink
fix: coderabbitai
Browse files Browse the repository at this point in the history
  • Loading branch information
Innocent-Akim committed Nov 14, 2024
1 parent 038ae57 commit 320ce1b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 20 deletions.
13 changes: 11 additions & 2 deletions apps/web/app/api/timer/timesheet/bulk-delete/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@ 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');
try {
const { data } = await deleteTaskTimesheetRequest({
tenantId,
organizationId,
logIds: [],
logIds,
}, access_token);

if (!data) {
Expand Down
45 changes: 36 additions & 9 deletions apps/web/app/hooks/features/useTimesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
Expand Down Expand Up @@ -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;
Expand All @@ -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<void> => {
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 });
Expand Down
20 changes: 18 additions & 2 deletions apps/web/app/services/client/api/timer/timer-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ITimeSheet[]>(endPoint, { tenantId });

try {
return await deleteApi<{ success: boolean; message: string }>(endPoint, { tenantId });
} catch (error) {
throw new Error(`Failed to delete timesheet logs`);
}
}
13 changes: 6 additions & 7 deletions apps/web/app/services/server/requests/timesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,11 @@ type IDeleteTimesheetProps = {
}

export function deleteTaskTimesheetRequest(params: IDeleteTimesheetProps, bearer_token: string) {
const queries = qs.stringify(params);
return serverFetch<ITimeSheet[]>({
path: `/timesheet/time-log?${queries.toString()}`,
const { logIds = [] } = params;
return serverFetch<void>({
path: `/timesheet/time-log/${logIds.join(',')}`,
method: 'DELETE',
bearer_token: bearer_token,
tenantId: params.tenantId,
body: params
})
bearer_token,
tenantId: params.tenantId
});
}

0 comments on commit 320ce1b

Please sign in to comment.