| 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 bodySchema = z |
| 10 | .object({ |
| 11 | name: z.string |
| 12 | slug: z |
| 13 | .string() |
| 14 | .min(1) |
| 15 | .max(255) |
| 16 | .regex(/^[a-z0-9 |
| 17 | .optional(), |
| 18 | settings: z |
| 19 | .object({ |
| 20 | memberRate: z.number |
| 21 | currency: z.string |
| 22 | timesheetDuration: z.enum |
| 23 | invoiceNumberTemplate: z.string |
| 24 | clientInvolvement: z |
| 25 | .object({ |
| 26 | expenses: z.enum |
| 27 | invoices: z.enum |
| 28 | proposals: z.enum |
| 29 | milestones: z.enum |
| 30 | timesheets: z.enum |
| 31 | requirements: z.enum |
| 32 | }) |
| 33 | .optional(), |
| 34 | }) |
| 35 | .optional(), |
| 36 | }) |
| 37 | .refine( |
| 38 | (data) => |
| 39 | data.name !== |
| 40 | data.slug !== |
| 41 | (data.settings && |
| 42 | (data.settings.memberRate |
| 43 | data.settings.currency |
| 44 | data.settings.timesheetDuration |
| 45 | data.settings.invoiceNumberTemplate |
| 46 | data.settings.clientInvolvement |
| 47 | { message: 'At least one field must be provided' |
| 48 | ) |
| 49 | |
| 50 | const route = createRoute |
| 51 | method: 'patch', |
| 52 | path: '/organization', |
| 53 | tags: ['Organization'], |
| 54 | summary: 'Update organization information', |
| 55 | description: 'Update organization details and default settings', |
| 56 | request: { |
| 57 | headers: HEADERS_SCHEMA, |
| 58 | body: { |
| 59 | content: { |
| 60 | 'application/json': { |
| 61 | schema: bodySchema, |
| 62 | }, |
| 63 | }, |
| 64 | }, |
| 65 | }, |
| 66 | responses: { |
| 67 | 200: { |
| 68 | content: { |
| 69 | 'application/json': { |
| 70 | schema: z.object |
| 71 | id: z.string |
| 72 | name: z.string |
| 73 | slug: z.string |
| 74 | settings: z |
| 75 | .object({ |
| 76 | memberRate: z.number |
| 77 | currency: z.string |
| 78 | timesheetDuration: z |
| 79 | .enum(['weekly', |
| 80 | .nullable(), |
| 81 | invoiceNumberTemplate: z.string |
| 82 | clientInvolvement: z |
| 83 | .object({ |
| 84 | expenses: z.enum |
| 85 | invoices: z.enum |
| 86 | proposals: z.enum |
| 87 | milestones: z.enum |
| 88 | timesheets: z.enum |
| 89 | requirements: z.enum |
| 90 | }) |
| 91 | .nullable(), |
| 92 | }) |
| 93 | .nullable(), |
| 94 | }), |
| 95 | }, |
| 96 | }, |
| 97 | description: 'Updated organization information', |
| 98 | }, |
| 99 | 400: { ...errorResponse |
| 100 | 401: { ...errorResponse |
| 101 | 402: { |
| 102 | ...errorResponse, |
| 103 | description: 'Organization is not on the Pro plan', |
| 104 | }, |
| 105 | 403: { ...errorResponse |
| 106 | 404: { ...errorResponse |
| 107 | 409: { ...errorResponse |
| 108 | 429: { ...errorResponse |
| 109 | }, |
| 110 | }) |
| 111 | |
| 112 | export const handler = |
| 113 | hono.openapi(route |
| 114 | const key = |
| 115 | const body = |
| 116 | |
| 117 | await db.transaction |
| 118 | const orgUpdate: |
| 119 | if (body.name |
| 120 | orgUpdate.name = |
| 121 | } |
| 122 | if (body.slug |
| 123 | orgUpdate.slug = |
| 124 | } |
| 125 | if (Object.keys |
| 126 | if (body.slug |
| 127 | const [existing] |
| 128 | .select({ id: |
| 129 | .from(organizations) |
| 130 | .where(eq(organizations |
| 131 | if (existing && |
| 132 | throwHttp(409, 'Slug is already taken |
| 133 | } |
| 134 | } |
| 135 | await tx |
| 136 | .update(organizations) |
| 137 | .set(orgUpdate) |
| 138 | .where(eq(organizations |
| 139 | } |
| 140 | |
| 141 | if (body.settings |
| 142 | const settingsUpdate: |
| 143 | if (body.settings |
| 144 | settingsUpdate.memberRate = |
| 145 | } |
| 146 | if (body.settings |
| 147 | settingsUpdate.currency = |
| 148 | } |
| 149 | if (body.settings |
| 150 | settingsUpdate.timesheetDuration = |
| 151 | } |
| 152 | if (body.settings |
| 153 | settingsUpdate.invoiceNumberTemplate = |
| 154 | body.settings.invoiceNumberTemplate |
| 155 | } |
| 156 | if (body.settings |
| 157 | settingsUpdate.clientInvolvement = |
| 158 | } |
| 159 | if (Object.keys |
| 160 | const [existing] |
| 161 | .select({ id: |
| 162 | .from(settings) |
| 163 | .where( |
| 164 | and( |
| 165 | eq(settings.organizationId |
| 166 | isNull(settings.projectId |
| 167 | ) |
| 168 | ) |
| 169 | if (existing) { |
| 170 | await tx |
| 171 | .update(settings) |
| 172 | .set(settingsUpdate) |
| 173 | .where(eq(settings |
| 174 | } else { |
| 175 | await tx.insert |
| 176 | organizationId: key.organizationId |
| 177 | ...settingsUpdate, |
| 178 | }) |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | }) |
| 183 | |
| 184 | const [organization] |
| 185 | .select({ |
| 186 | id: organizations.id |
| 187 | name: organizations.name |
| 188 | slug: organizations.slug |
| 189 | defaultMemberRate: settings.memberRate |
| 190 | defaultCurrency: settings.currency |
| 191 | defaultTimesheetDuration: settings.timesheetDuration |
| 192 | invoiceNumberTemplate: settings.invoiceNumberTemplate |
| 193 | clientInvolvement: settings.clientInvolvement |
| 194 | }) |
| 195 | .from(organizations) |
| 196 | .where(eq(organizations |
| 197 | .leftJoin( |
| 198 | settings, |
| 199 | and( |
| 200 | eq(settings.organizationId |
| 201 | isNull(settings.projectId |
| 202 | ) |
| 203 | ) |
| 204 | if (!organization) |
| 205 | throwHttp(404, 'No organization found |
| 206 | } |
| 207 | return c.json |
| 208 | { |
| 209 | id: key.organizationId |
| 210 | name: organization.name |
| 211 | slug: organization.slug |
| 212 | settings: { |
| 213 | memberRate: organization.defaultMemberRate |
| 214 | currency: organization.defaultCurrency |
| 215 | timesheetDuration: organization.defaultTimesheetDuration |
| 216 | invoiceNumberTemplate: organization.invoiceNumberTemplate |
| 217 | clientInvolvement: organization.clientInvolvement |
| 218 | }, |
| 219 | }, |
| 220 | 200 |
| 221 | ) |
| 222 | }) |
| 223 | } |