| 1 | 'use server' |
| 2 | |
| 3 | import { and, eq, isNull, sql } from 'drizzle-orm' |
| 4 | import { headers } |
| 5 | import { authedActionClient } |
| 6 | import { auth } |
| 7 | import { db } |
| 8 | import { |
| 9 | customFields, |
| 10 | projects as projectsTable, |
| 11 | settings as settingsTable, |
| 12 | } from '@/server/db/schema' |
| 13 | import { organizations } |
| 14 | import { |
| 15 | createOrgCustomFieldSchema, |
| 16 | deleteOrganizationSchema, |
| 17 | deleteOrgCustomFieldSchema, |
| 18 | renameOrganizationSchema, |
| 19 | updateInvoiceFromDetailsSchema, |
| 20 | updateInvoiceImportDefaultsSchema, |
| 21 | updateInvoiceNumberTemplateSchema, |
| 22 | updateOrgClientInvolvementSchema, |
| 23 | updateOrgCustomFieldSchema, |
| 24 | updateTimesheetDefaultsSchema, |
| 25 | } from './common' |
| 26 | |
| 27 | export const renameOrganizationAction = |
| 28 | .inputSchema(renameOrganizationSchema) |
| 29 | .action( |
| 30 | async ({ |
| 31 | parsedInput: { organizationId, |
| 32 | ctx: { role, |
| 33 | }) => { |
| 34 | if (!role.authorize |
| 35 | throw new Error( |
| 36 | 'You do not have permission to update workspace settings' |
| 37 | ) |
| 38 | } |
| 39 | |
| 40 | if (orgMember.organizationId |
| 41 | throw new Error( |
| 42 | } |
| 43 | |
| 44 | await auth.api |
| 45 | headers: await headers |
| 46 | body: { |
| 47 | data: { name, |
| 48 | organizationId, |
| 49 | }, |
| 50 | }) |
| 51 | |
| 52 | return { success: |
| 53 | } |
| 54 | ) |
| 55 | |
| 56 | export const updateTimesheetDefaultsAction = |
| 57 | .inputSchema(updateTimesheetDefaultsSchema) |
| 58 | .action( |
| 59 | async ({ |
| 60 | parsedInput: { |
| 61 | organizationId, |
| 62 | defaultPayRate, |
| 63 | defaultPayCurrency, |
| 64 | defaultPayFrequency, |
| 65 | defaultBillingRate, |
| 66 | defaultBillingCurrency, |
| 67 | defaultBillingFrequency, |
| 68 | defaultTimesheetDuration, |
| 69 | }, |
| 70 | ctx: { role, |
| 71 | }) => { |
| 72 | if (!role.authorize |
| 73 | throw new Error( |
| 74 | 'You do not have permission to update workspace settings' |
| 75 | ) |
| 76 | } |
| 77 | |
| 78 | if (orgMember.organizationId |
| 79 | throw new Error( |
| 80 | } |
| 81 | // When no billing rate is set, billing mirrors pay entirely — otherwise |
| 82 | // stale billing currency/frequency could leak into "same as pay" mode. |
| 83 | const usePayForBilling = |
| 84 | const defaults = |
| 85 | payRate: defaultPayRate, |
| 86 | payCurrency: defaultPayCurrency, |
| 87 | payFrequency: defaultPayFrequency, |
| 88 | billingRate: usePayForBilling ? |
| 89 | billingCurrency: usePayForBilling |
| 90 | ? defaultPayCurrency |
| 91 | : (defaultBillingCurrency ?? |
| 92 | billingFrequency: usePayForBilling |
| 93 | ? defaultPayFrequency |
| 94 | : (defaultBillingFrequency ?? |
| 95 | timesheetDuration: defaultTimesheetDuration, |
| 96 | } |
| 97 | await db |
| 98 | .insert(settingsTable) |
| 99 | .values({ |
| 100 | organizationId, |
| 101 | ...defaults, |
| 102 | }) |
| 103 | .onConflictDoUpdate({ |
| 104 | target: [settingsTable.organizationId |
| 105 | targetWhere: sql`${settingsTable |
| 106 | set: defaults, |
| 107 | }) |
| 108 | |
| 109 | return { success: |
| 110 | } |
| 111 | ) |
| 112 | |
| 113 | export const updateInvoiceNumberTemplateAction = |
| 114 | .inputSchema(updateInvoiceNumberTemplateSchema) |
| 115 | .action( |
| 116 | async ({ |
| 117 | parsedInput: { organizationId, |
| 118 | ctx: { role, |
| 119 | }) => { |
| 120 | if (!role.authorize |
| 121 | throw new Error( |
| 122 | 'You do not have permission to update workspace settings' |
| 123 | ) |
| 124 | } |
| 125 | |
| 126 | if (orgMember.organizationId |
| 127 | throw new Error( |
| 128 | } |
| 129 | |
| 130 | if (projectId) { |
| 131 | await db |
| 132 | .insert(settingsTable) |
| 133 | .values({ |
| 134 | organizationId, |
| 135 | projectId, |
| 136 | invoiceNumberTemplate, |
| 137 | }) |
| 138 | .onConflictDoUpdate({ |
| 139 | target: [settingsTable.organizationId |
| 140 | set: { invoiceNumberTemplate |
| 141 | }) |
| 142 | } else { |
| 143 | await db |
| 144 | .insert(settingsTable) |
| 145 | .values({ |
| 146 | organizationId, |
| 147 | invoiceNumberTemplate, |
| 148 | }) |
| 149 | .onConflictDoUpdate({ |
| 150 | target: [settingsTable.organizationId |
| 151 | targetWhere: sql`${settingsTable |
| 152 | set: { invoiceNumberTemplate |
| 153 | }) |
| 154 | } |
| 155 | |
| 156 | return { success: |
| 157 | } |
| 158 | ) |
| 159 | |
| 160 | export const updateInvoiceImportDefaultsAction = |
| 161 | .inputSchema(updateInvoiceImportDefaultsSchema) |
| 162 | .action( |
| 163 | async ({ |
| 164 | parsedInput: { organizationId, |
| 165 | ctx: { role, |
| 166 | }) => { |
| 167 | if (!role.authorize |
| 168 | throw new Error( |
| 169 | 'You do not have permission to update workspace settings' |
| 170 | ) |
| 171 | } |
| 172 | |
| 173 | if (orgMember.organizationId |
| 174 | throw new Error( |
| 175 | } |
| 176 | |
| 177 | if (projectId) { |
| 178 | const [project] |
| 179 | .select({ id: |
| 180 | .from(projectsTable) |
| 181 | .where( |
| 182 | and( |
| 183 | eq(projectsTable.id |
| 184 | eq(projectsTable.organizationId |
| 185 | ) |
| 186 | ) |
| 187 | .limit(1) |
| 188 | |
| 189 | if (!project) |
| 190 | throw new Error( |
| 191 | } |
| 192 | |
| 193 | await db |
| 194 | .insert(settingsTable) |
| 195 | .values({ |
| 196 | organizationId, |
| 197 | projectId, |
| 198 | invoiceTimeUnit, |
| 199 | }) |
| 200 | .onConflictDoUpdate({ |
| 201 | target: [settingsTable.organizationId |
| 202 | set: { invoiceTimeUnit |
| 203 | }) |
| 204 | } else { |
| 205 | await db |
| 206 | .insert(settingsTable) |
| 207 | .values({ |
| 208 | organizationId, |
| 209 | invoiceTimeUnit, |
| 210 | }) |
| 211 | .onConflictDoUpdate({ |
| 212 | target: [settingsTable.organizationId |
| 213 | targetWhere: sql`${settingsTable |
| 214 | set: { invoiceTimeUnit |
| 215 | }) |
| 216 | } |
| 217 | |
| 218 | return { success: |
| 219 | } |
| 220 | ) |
| 221 | |
| 222 | export const updateInvoiceFromDetailsAction = |
| 223 | .inputSchema(updateInvoiceFromDetailsSchema) |
| 224 | .action( |
| 225 | async ({ |
| 226 | parsedInput: { organizationId, |
| 227 | ctx: { role, |
| 228 | }) => { |
| 229 | if (!role.authorize |
| 230 | throw new Error( |
| 231 | 'You do not have permission to update workspace settings' |
| 232 | ) |
| 233 | } |
| 234 | |
| 235 | if (orgMember.organizationId |
| 236 | throw new Error( |
| 237 | } |
| 238 | |
| 239 | const fromName = |
| 240 | const fromAddress = |
| 241 | |
| 242 | await db |
| 243 | .insert(settingsTable) |
| 244 | .values({ |
| 245 | organizationId, |
| 246 | invoiceFromName: fromName, |
| 247 | invoiceFromAddress: fromAddress, |
| 248 | }) |
| 249 | .onConflictDoUpdate({ |
| 250 | target: [settingsTable.organizationId |
| 251 | targetWhere: sql`${settingsTable |
| 252 | set: { |
| 253 | invoiceFromName: fromName, |
| 254 | invoiceFromAddress: fromAddress, |
| 255 | }, |
| 256 | }) |
| 257 | |
| 258 | return { success: |
| 259 | } |
| 260 | ) |
| 261 | |
| 262 | export const updateOrgClientInvolvementAction = |
| 263 | .inputSchema(updateOrgClientInvolvementSchema) |
| 264 | .action( |
| 265 | async ({ |
| 266 | parsedInput: { organizationId, |
| 267 | ctx: { role, |
| 268 | }) => { |
| 269 | if (!role.authorize |
| 270 | throw new Error( |
| 271 | 'You do not have permission to update workspace settings' |
| 272 | ) |
| 273 | } |
| 274 | if (orgMember.organizationId |
| 275 | throw new Error( |
| 276 | } |
| 277 | |
| 278 | await db |
| 279 | .insert(settingsTable) |
| 280 | .values({ organizationId |
| 281 | .onConflictDoUpdate({ |
| 282 | target: [settingsTable.organizationId |
| 283 | targetWhere: sql`${settingsTable |
| 284 | set: { clientInvolvement |
| 285 | }) |
| 286 | |
| 287 | return { success: |
| 288 | } |
| 289 | ) |
| 290 | |
| 291 | export const createOrgCustomFieldAction = |
| 292 | .inputSchema(createOrgCustomFieldSchema) |
| 293 | .action( |
| 294 | async ({ |
| 295 | parsedInput: { organizationId, |
| 296 | ctx: { role, |
| 297 | }) => { |
| 298 | if (!role.authorize |
| 299 | throw new Error( |
| 300 | 'You do not have permission to update workspace settings' |
| 301 | ) |
| 302 | } |
| 303 | if (orgMember.organizationId |
| 304 | throw new Error( |
| 305 | } |
| 306 | |
| 307 | const [row] |
| 308 | .insert(customFields) |
| 309 | .values({ |
| 310 | ...definition, |
| 311 | organizationId, |
| 312 | projectId: null, |
| 313 | required: definition.required |
| 314 | visibleToClient: definition.visibleToClient |
| 315 | defaultValue: definition.defaultValue |
| 316 | options: definition.options |
| 317 | config: definition.config |
| 318 | }) |
| 319 | .returning({ id: |
| 320 | |
| 321 | return { success: |
| 322 | } |
| 323 | ) |
| 324 | |
| 325 | export const updateOrgCustomFieldAction = |
| 326 | .inputSchema(updateOrgCustomFieldSchema) |
| 327 | .action( |
| 328 | async ({ |
| 329 | parsedInput: { organizationId, |
| 330 | ctx: { role, |
| 331 | }) => { |
| 332 | if (!role.authorize |
| 333 | throw new Error( |
| 334 | 'You do not have permission to update this custom field' |
| 335 | ) |
| 336 | } |
| 337 | if (orgMember.organizationId |
| 338 | throw new Error( |
| 339 | 'This custom field belongs to a different workspace than the one you have selected' |
| 340 | ) |
| 341 | } |
| 342 | |
| 343 | const [existing] |
| 344 | .select({ type: |
| 345 | .from(customFields) |
| 346 | .where( |
| 347 | and( |
| 348 | eq(customFields.id |
| 349 | eq(customFields.organizationId |
| 350 | isNull(customFields.projectId |
| 351 | ) |
| 352 | ) |
| 353 | .limit(1) |
| 354 | |
| 355 | if (!existing) |
| 356 | throw new Error( |
| 357 | } |
| 358 | // The schema validates config/options/defaultValue against the submitted |
| 359 | // type, but we persist against the stored type. Reject a mismatch so the |
| 360 | // client can't write values incompatible with the actual field type. |
| 361 | if (existing.type |
| 362 | throw new Error( |
| 363 | } |
| 364 | |
| 365 | const { type: |
| 366 | const [updated] |
| 367 | .update(customFields) |
| 368 | .set({ |
| 369 | ...mutable, |
| 370 | defaultValue: definition.defaultValue |
| 371 | options: definition.options |
| 372 | config: definition.config |
| 373 | }) |
| 374 | .where( |
| 375 | and( |
| 376 | eq(customFields.id |
| 377 | eq(customFields.organizationId |
| 378 | isNull(customFields.projectId |
| 379 | ) |
| 380 | ) |
| 381 | .returning({ id: |
| 382 | |
| 383 | if (!updated) |
| 384 | throw new Error( |
| 385 | } |
| 386 | |
| 387 | return { success: |
| 388 | } |
| 389 | ) |
| 390 | |
| 391 | export const deleteOrgCustomFieldAction = |
| 392 | .inputSchema(deleteOrgCustomFieldSchema) |
| 393 | .action( |
| 394 | async ({ |
| 395 | parsedInput: { organizationId, |
| 396 | ctx: { role, |
| 397 | }) => { |
| 398 | if (!role.authorize |
| 399 | throw new Error( |
| 400 | 'You do not have permission to update workspace settings' |
| 401 | ) |
| 402 | } |
| 403 | if (orgMember.organizationId |
| 404 | throw new Error( |
| 405 | } |
| 406 | |
| 407 | const [deleted] |
| 408 | .delete(customFields) |
| 409 | .where( |
| 410 | and( |
| 411 | eq(customFields.id |
| 412 | eq(customFields.organizationId |
| 413 | isNull(customFields.projectId |
| 414 | ) |
| 415 | ) |
| 416 | .returning({ id: |
| 417 | |
| 418 | if (!deleted) |
| 419 | throw new Error( |
| 420 | } |
| 421 | |
| 422 | return { success: |
| 423 | } |
| 424 | ) |
| 425 | |
| 426 | export const deleteOrganizationAction = |
| 427 | .inputSchema(deleteOrganizationSchema) |
| 428 | .action( |
| 429 | async ({ |
| 430 | parsedInput: { organizationId, |
| 431 | ctx: { role, |
| 432 | }) => { |
| 433 | if (!role.authorize |
| 434 | throw new Error( |
| 435 | } |
| 436 | |
| 437 | if (orgMember.organizationId |
| 438 | throw new Error( |
| 439 | } |
| 440 | |
| 441 | const [org] |
| 442 | .select({ name: |
| 443 | .from(organizations) |
| 444 | .where(eq(organizations |
| 445 | |
| 446 | if (!org) |
| 447 | throw new Error( |
| 448 | } |
| 449 | |
| 450 | if (org.name |
| 451 | throw new Error( |
| 452 | } |
| 453 | |
| 454 | await auth.api |
| 455 | headers: await headers |
| 456 | body: { organizationId |
| 457 | }) |
| 458 | |
| 459 | return { success: |
| 460 | } |
| 461 | ) |