| 1 | import { createRoute, z } from '@hono/zod-openapi' |
| 2 | import { and, eq |
| 3 | import type { app |
| 4 | import { requireApiKey, throwHttp |
| 5 | import { errorResponse, HEADERS_SCHEMA |
| 6 | import { db } |
| 7 | import { organizations, settings |
| 8 | |
| 9 | const route = createRoute |
| 10 | method: 'get', |
| 11 | path: '/organization', |
| 12 | tags: ['Organization'], |
| 13 | summary: 'Get organization information', |
| 14 | description: 'Get organization information', |
| 15 | request: { |
| 16 | headers: HEADERS_SCHEMA, |
| 17 | }, |
| 18 | responses: { |
| 19 | 200: { |
| 20 | content: { |
| 21 | 'application/json': { |
| 22 | schema: z.object |
| 23 | id: z.string |
| 24 | name: z.string |
| 25 | slug: z.string |
| 26 | settings: z.object |
| 27 | memberRate: z.number |
| 28 | currency: z.string |
| 29 | timesheetDuration: z |
| 30 | .enum(['weekly', |
| 31 | .nullable(), |
| 32 | invoiceNumberTemplate: z.string |
| 33 | clientInvolvement: z |
| 34 | .object({ |
| 35 | expenses: z.enum |
| 36 | invoices: z.enum |
| 37 | proposals: z.enum |
| 38 | milestones: z.enum |
| 39 | timesheets: z.enum |
| 40 | requirements: z.enum |
| 41 | }) |
| 42 | .nullable(), |
| 43 | }), |
| 44 | }), |
| 45 | }, |
| 46 | }, |
| 47 | description: 'Organization information', |
| 48 | }, |
| 49 | 401: { ...errorResponse |
| 50 | 402: { |
| 51 | ...errorResponse, |
| 52 | description: 'Organization is not on the Pro plan', |
| 53 | }, |
| 54 | 403: { ...errorResponse |
| 55 | 429: { ...errorResponse |
| 56 | }, |
| 57 | }) |
| 58 | |
| 59 | export const handler = |
| 60 | hono.openapi(route |
| 61 | const key = |
| 62 | const [organization] |
| 63 | .select({ |
| 64 | id: organizations.id |
| 65 | name: organizations.name |
| 66 | slug: organizations.slug |
| 67 | memberRate: settings.memberRate |
| 68 | currency: settings.currency |
| 69 | timesheetDuration: settings.timesheetDuration |
| 70 | invoiceNumberTemplate: settings.invoiceNumberTemplate |
| 71 | clientInvolvement: settings.clientInvolvement |
| 72 | }) |
| 73 | .from(organizations) |
| 74 | .where(eq(organizations |
| 75 | .leftJoin( |
| 76 | settings, |
| 77 | and( |
| 78 | eq(settings.organizationId |
| 79 | isNull(settings.projectId |
| 80 | ) |
| 81 | ) |
| 82 | if (!organization) |
| 83 | throwHttp(404, 'No organization found |
| 84 | } |
| 85 | return c.json |
| 86 | { |
| 87 | id: key.organizationId |
| 88 | name: organization.name |
| 89 | slug: organization.slug |
| 90 | settings: { |
| 91 | memberRate: organization?.memberRate |
| 92 | currency: organization?.currency |
| 93 | timesheetDuration: organization?.timesheetDuration |
| 94 | invoiceNumberTemplate: organization?.invoiceNumberTemplate |
| 95 | clientInvolvement: organization?.clientInvolvement |
| 96 | }, |
| 97 | }, |
| 98 | 200 |
| 99 | ) |
| 100 | }) |
| 101 | } |