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