Skip to content

Commit

Permalink
feat: update billable from api
Browse files Browse the repository at this point in the history
  • Loading branch information
Innocent-Akim committed Nov 27, 2024
1 parent c3c2a26 commit 60ab188
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 7 deletions.
33 changes: 33 additions & 0 deletions apps/web/app/api/timer/timesheet/time-log/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { UpdateTimesheet } from "@/app/interfaces";
import { authenticatedGuard } from "@/app/services/server/guards/authenticated-guard-app";
import { updateTimesheetRequest } from "@/app/services/server/requests";
import { NextResponse } from "next/server";

export async function PUT(req: Request) {
const res = new NextResponse();
const {
$res,
user,
tenantId,
organizationId,
access_token
} = await authenticatedGuard(req, res);
if (!user) return $res('Unauthorized');

try {
const { searchParams } = new URL(req.url);
const id = searchParams.get('id') as string;
const body = (await req.json()) as UpdateTimesheet;
const { data } = await updateTimesheetRequest(
{ ...body, tenantId, organizationId, id },
access_token
);
return $res(data);
} catch (error) {
console.error('Error updating timesheet status:', error);
return $res({
success: false,
message: 'Failed to update timesheet status'
});
}
}
18 changes: 15 additions & 3 deletions apps/web/app/hooks/features/useTimesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useAtom } from 'jotai';
import { timesheetRapportState } from '@/app/stores/time-logs';
import { useQuery } from '../useQuery';
import { useCallback, useEffect, useMemo } from 'react';
import { deleteTaskTimesheetLogsApi, getTaskTimesheetLogsApi, updateStatusTimesheetFromApi, createTimesheetFromApi } from '@/app/services/client/api/timer/timer-log';
import { deleteTaskTimesheetLogsApi, getTaskTimesheetLogsApi, updateStatusTimesheetFromApi, createTimesheetFromApi, updateTimesheetFromAPi } from '@/app/services/client/api/timer/timer-log';
import moment from 'moment';
import { ID, TimesheetLog, TimesheetStatus, UpdateTimesheet } from '@/app/interfaces';
import { useTimelogFilterOptions } from './useTimelogFilterOptions';
Expand Down Expand Up @@ -97,7 +97,8 @@ export function useTimesheet({
const { loading: loadingTimesheet, queryCall: queryTimesheet } = useQuery(getTaskTimesheetLogsApi);
const { loading: loadingDeleteTimesheet, queryCall: queryDeleteTimesheet } = useQuery(deleteTaskTimesheetLogsApi);
const { loading: loadingUpdateTimesheetStatus, queryCall: queryUpdateTimesheetStatus } = useQuery(updateStatusTimesheetFromApi)
const { loading: loadingCreateTimesheet, queryCall: queryCreateTimesheet } = useQuery(createTimesheetFromApi)
const { loading: loadingCreateTimesheet, queryCall: queryCreateTimesheet } = useQuery(createTimesheetFromApi);
const { loading: loadingUpdateTimesheet, queryCall: queryUpdateTimesheet } = useQuery(updateTimesheetFromAPi);


const getTaskTimesheet = useCallback(
Expand Down Expand Up @@ -152,6 +153,15 @@ export function useTimesheet({



const updateTimesheet = useCallback(async ({ ...timesheet }: UpdateTimesheet) => {
if (!user) return;
const response = await queryUpdateTimesheet(timesheet);
setTimesheet(prevTimesheet => [
response.data,
...prevTimesheet,
])
}, [queryUpdateTimesheet, setTimesheet, user])


const updateTimesheetStatus = useCallback(
async ({ status, ids }: { status: TimesheetStatus; ids: ID[] | ID }) => {
Expand Down Expand Up @@ -268,6 +278,8 @@ export function useTimesheet({
loadingUpdateTimesheetStatus,
puTimesheetStatus,
createTimesheet,
loadingCreateTimesheet
loadingCreateTimesheet,
updateTimesheet,
loadingUpdateTimesheet
};
}
1 change: 1 addition & 0 deletions apps/web/app/interfaces/timer/ITimerLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export interface UpdateTimesheet extends Pick<
>,
Pick<
TimesheetLog,
| 'id'
| 'startedAt'
| 'stoppedAt'
| 'tenantId'
Expand Down
14 changes: 14 additions & 0 deletions apps/web/app/services/client/api/timer/timer-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,17 @@ export function createTimesheetFromApi(data: UpdateTimesheet) {
throw new Error('Failed to create timesheet log');
}
}

export function updateTimesheetFromAPi(params: UpdateTimesheet) {
const { id, ...data } = params
const organizationId = getOrganizationIdCookie();
const tenantId = getTenantIdCookie();
if (!organizationId || !tenantId) {
throw new Error('Required parameters missing: organizationId and tenantId are required');
}
try {
return put<TimesheetLog>(`/timesheet/time-log/${params.id}`, { ...data, organizationId }, { tenantId })
} catch (error) {
throw new Error('Failed to create timesheet log');
}
}
10 changes: 10 additions & 0 deletions apps/web/app/services/server/requests/timesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,13 @@ export function createTimesheetRequest(params: UpdateTimesheet, bearer_token: st
tenantId: params.tenantId
})
}

export function updateTimesheetRequest(params: UpdateTimesheet, bearer_token: string) {
return serverFetch<TimesheetLog>({
path: `/timesheet/time-log/${params.id}`,
method: 'PUT',
body: { ...params },
bearer_token,
tenantId: params.tenantId
});
}
28 changes: 24 additions & 4 deletions apps/web/lib/features/integrations/calendar/table-time-sheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import { useTranslations } from 'next-intl';
import { formatDate } from '@/app/helpers';
import { GroupedTimesheet, useTimesheet } from '@/app/hooks/features/useTimesheet';
import { DisplayTimeForTimesheet, TaskNameInfoDisplay, TotalDurationByDate, TotalTimeDisplay } from '../../task/task-displays';
import { TimesheetLog, TimesheetStatus } from '@/app/interfaces';
import { TimeLogType, TimesheetLog, TimesheetStatus } from '@/app/interfaces';

export const columns: ColumnDef<TimeSheet>[] = [
{
Expand Down Expand Up @@ -498,7 +498,23 @@ const TaskDetails = ({ description, name }: { description: string; name: string

export const StatusTask = ({ timesheet }: { timesheet: TimesheetLog }) => {
const t = useTranslations();
const { updateTimesheetStatus } = useTimesheet({});
const { updateTimesheetStatus, updateTimesheet } = useTimesheet({});
const handleUpdateTimesheet = async (isBillable: boolean) => {
await updateTimesheet({
id: timesheet.timesheetId,
isBillable: isBillable,
employeeId: timesheet.employeeId,
logType: timesheet.logType,
source: timesheet.source,
stoppedAt: timesheet.stoppedAt,
startedAt: timesheet.startedAt,
tenantId: timesheet.tenantId,
organizationId: timesheet.organizationId,
description: timesheet.description,
projectId: timesheet.projectId,
reason: timesheet.reason,
});
};

return (
<>
Expand Down Expand Up @@ -534,12 +550,16 @@ export const StatusTask = ({ timesheet }: { timesheet: TimesheetLog }) => {
</DropdownMenuSubTrigger>
<DropdownMenuPortal>
<DropdownMenuSubContent>
<DropdownMenuItem textValue={'Yes'} className="cursor-pointer">
<DropdownMenuItem onClick={async () => {
await handleUpdateTimesheet(true)
}} textValue={'Yes'} className="cursor-pointer">
<div className="flex items-center gap-3">
<span>{t('pages.timesheet.BILLABLE.YES')}</span>
</div>
</DropdownMenuItem>
<DropdownMenuItem textValue={'No'} className="cursor-pointer">
<DropdownMenuItem onClick={async () => {
await handleUpdateTimesheet(false)
}} textValue={'No'} className="cursor-pointer">
<div className="flex items-center gap-3">
<span>{t('pages.timesheet.BILLABLE.NO')}</span>
</div>
Expand Down

0 comments on commit 60ab188

Please sign in to comment.