| 1 | import { and, asc, eq } from 'drizzle-orm' |
| 2 | import { ArrowLeft } |
| 3 | import type { Metadata |
| 4 | import { headers } |
| 5 | import Link from 'next/link |
| 6 | import { redirect } |
| 7 | import { resolveProjectContext } |
| 8 | import { requirementsService } |
| 9 | import { buttonVariants } |
| 10 | import type { CustomFieldDefinition |
| 11 | import { createMetadata } |
| 12 | import { db } |
| 13 | import { customFields } |
| 14 | import type { RouteImpl |
| 15 | import { TimeEntryFormBody } |
| 16 | |
| 17 | export const metadata: |
| 18 | title: 'Log Time', |
| 19 | description: 'Log a new time entry against this project.', |
| 20 | }) |
| 21 | |
| 22 | const DATE_RE = / |
| 23 | |
| 24 | export default async function |
| 25 | params, |
| 26 | searchParams, |
| 27 | }: PageProps< |
| 28 | const { org, |
| 29 | const { date: |
| 30 | const { organization, |
| 31 | org, |
| 32 | projectSlug |
| 33 | ) |
| 34 | |
| 35 | if (!role.authorize |
| 36 | redirect( |
| 37 | `/error/403?message=${encodeURIComponent('You do not have permission to log time |
| 38 | ) |
| 39 | } |
| 40 | |
| 41 | const h = |
| 42 | const [requirementsList, |
| 43 | requirementsService.listByProject(project |
| 44 | db |
| 45 | .select() |
| 46 | .from(customFields) |
| 47 | .where( |
| 48 | and( |
| 49 | eq(customFields.projectId |
| 50 | eq(customFields.organizationId |
| 51 | ) |
| 52 | ) |
| 53 | .orderBy(asc(customFields |
| 54 | ]) |
| 55 | |
| 56 | const defaultDate = |
| 57 | typeof dateParam === ' |
| 58 | ? (() => |
| 59 | const [y, |
| 60 | number, |
| 61 | number, |
| 62 | number, |
| 63 | ] |
| 64 | const date = |
| 65 | return date.getFullYear |
| 66 | date.getMonth() + |
| 67 | date.getDate() === |
| 68 | ? date |
| 69 | : undefined |
| 70 | })() |
| 71 | : undefined |
| 72 | const defaultDuration = |
| 73 | typeof duration === ' |
| 74 | ? Number(duration) |
| 75 | : undefined |
| 76 | |
| 77 | const backHref = |
| 78 | |
| 79 | return ( |
| 80 | <div className='mx-auto w-full max-w-2xl |
| 81 | <Link |
| 82 | className={buttonVariants({ |
| 83 | href={backHref as |
| 84 | > |
| 85 | <ArrowLeft className=' |
| 86 | Back to timesheets |
| 87 | </Link> |
| 88 | <div className='mb-6 |
| 89 | <h1 className='font-semibold text-2xl |
| 90 | <p className='mt-1 text-muted-foreground text-sm |
| 91 | Record a new time entry for {project.name}. |
| 92 | </p> |
| 93 | </div> |
| 94 | <TimeEntryFormBody |
| 95 | customFields={defs} |
| 96 | defaultDate={defaultDate} |
| 97 | defaultDurationMinutes={defaultDuration} |
| 98 | projectId={project.id |
| 99 | redirectAfterSubmit={backHref} |
| 100 | requirements={requirementsList} |
| 101 | /> |
| 102 | </div> |
| 103 | ) |
| 104 | } |