| 1 | 'use server' |
| 2 | |
| 3 | import { and, eq } from 'drizzle-orm' |
| 4 | import { authService } |
| 5 | import { authedActionClient } |
| 6 | import { db } |
| 7 | import { |
| 8 | milestoneRequirements, |
| 9 | milestones, |
| 10 | requirements, |
| 11 | } from '@/server/db/schema' |
| 12 | import { |
| 13 | completeMilestoneSchema, |
| 14 | createMilestoneSchema, |
| 15 | deleteMilestoneSchema, |
| 16 | linkRequirementSchema, |
| 17 | reorderMilestonesSchema, |
| 18 | unlinkRequirementSchema, |
| 19 | updateMilestoneSchema, |
| 20 | } from './common' |
| 21 | |
| 22 | export const createMilestoneAction = |
| 23 | .inputSchema(createMilestoneSchema) |
| 24 | .action( |
| 25 | async ({ |
| 26 | parsedInput: { |
| 27 | projectId, |
| 28 | name, |
| 29 | description, |
| 30 | dueDate, |
| 31 | budgetMinutes, |
| 32 | budgetAmountCents, |
| 33 | currency, |
| 34 | }, |
| 35 | ctx: { role, |
| 36 | }) => { |
| 37 | if (!role.authorize |
| 38 | throw new Error( |
| 39 | } |
| 40 | const hasProjectAccess = |
| 41 | orgMember.organizationId, |
| 42 | projectId, |
| 43 | user.id |
| 44 | ) |
| 45 | if (!hasProjectAccess.success |
| 46 | throw new Error( |
| 47 | } |
| 48 | |
| 49 | // Get the next sort order |
| 50 | const existing = |
| 51 | .select({ sortOrder: |
| 52 | .from(milestones) |
| 53 | .where(eq(milestones |
| 54 | .orderBy(milestones.sortOrder |
| 55 | |
| 56 | const nextSortOrder = |
| 57 | existing.length > 0 |
| 58 | ? Math.max( |
| 59 | : 0 |
| 60 | |
| 61 | const [milestone] |
| 62 | .insert(milestones) |
| 63 | .values({ |
| 64 | projectId, |
| 65 | name, |
| 66 | description, |
| 67 | dueDate, |
| 68 | budgetMinutes, |
| 69 | budgetAmountCents, |
| 70 | sortOrder: nextSortOrder, |
| 71 | currency, |
| 72 | }) |
| 73 | .returning() |
| 74 | |
| 75 | return milestone |
| 76 | } |
| 77 | ) |
| 78 | |
| 79 | export const updateMilestoneAction = |
| 80 | .inputSchema(updateMilestoneSchema) |
| 81 | .action( |
| 82 | async ({ |
| 83 | parsedInput: { |
| 84 | milestoneId, |
| 85 | name, |
| 86 | description, |
| 87 | dueDate, |
| 88 | status, |
| 89 | blockReason, |
| 90 | budgetMinutes, |
| 91 | budgetAmountCents, |
| 92 | }, |
| 93 | ctx: { role, |
| 94 | }) => { |
| 95 | if (!role.authorize |
| 96 | throw new Error( |
| 97 | } |
| 98 | |
| 99 | const existing = |
| 100 | .select({ |
| 101 | id: milestones.id |
| 102 | projectId: milestones.projectId |
| 103 | blockReason: milestones.blockReason |
| 104 | }) |
| 105 | .from(milestones) |
| 106 | .where(eq(milestones |
| 107 | .then((r) |
| 108 | |
| 109 | if (!existing) |
| 110 | throw new Error( |
| 111 | } |
| 112 | const hasProjectAccess = |
| 113 | orgMember.organizationId, |
| 114 | existing.projectId, |
| 115 | user.id |
| 116 | ) |
| 117 | if (!hasProjectAccess.success |
| 118 | throw new Error( |
| 119 | } |
| 120 | |
| 121 | // If setting to blocked, require a block reason |
| 122 | if (status === |
| 123 | throw new Error( |
| 124 | 'Block reason is required when setting status to blocked' |
| 125 | ) |
| 126 | } |
| 127 | |
| 128 | const updates: |
| 129 | if (name !== |
| 130 | updates.name = |
| 131 | } |
| 132 | if (description !== |
| 133 | updates.description = |
| 134 | } |
| 135 | if (dueDate !== |
| 136 | updates.dueDate = |
| 137 | } |
| 138 | if (status !== |
| 139 | updates.status = |
| 140 | } |
| 141 | if (blockReason !== |
| 142 | updates.blockReason = |
| 143 | } |
| 144 | if (budgetMinutes !== |
| 145 | updates.budgetMinutes = |
| 146 | } |
| 147 | if (budgetAmountCents !== |
| 148 | updates.budgetAmountCents = |
| 149 | } |
| 150 | |
| 151 | // Clear block reason when moving away from blocked |
| 152 | if (status && |
| 153 | updates.blockReason = |
| 154 | } |
| 155 | |
| 156 | const [milestone] |
| 157 | .update(milestones) |
| 158 | .set(updates) |
| 159 | .where(eq(milestones |
| 160 | .returning() |
| 161 | |
| 162 | return milestone |
| 163 | } |
| 164 | ) |
| 165 | |
| 166 | export const deleteMilestoneAction = |
| 167 | .inputSchema(deleteMilestoneSchema) |
| 168 | .action( |
| 169 | async ({ |
| 170 | parsedInput: { milestoneId |
| 171 | ctx: { role, |
| 172 | }) => { |
| 173 | if (!role.authorize |
| 174 | throw new Error( |
| 175 | } |
| 176 | |
| 177 | const existing = |
| 178 | .select({ id: |
| 179 | .from(milestones) |
| 180 | .where(eq(milestones |
| 181 | .then((r) |
| 182 | |
| 183 | if (!existing) |
| 184 | throw new Error( |
| 185 | } |
| 186 | const hasProjectAccess = |
| 187 | orgMember.organizationId, |
| 188 | existing.projectId, |
| 189 | user.id |
| 190 | ) |
| 191 | if (!hasProjectAccess.success |
| 192 | throw new Error( |
| 193 | } |
| 194 | |
| 195 | await db.delete |
| 196 | |
| 197 | return { success: |
| 198 | } |
| 199 | ) |
| 200 | |
| 201 | export const completeMilestoneAction = |
| 202 | .inputSchema(completeMilestoneSchema) |
| 203 | .action( |
| 204 | async ({ |
| 205 | parsedInput: { milestoneId |
| 206 | ctx: { role, |
| 207 | }) => { |
| 208 | if (!role.authorize |
| 209 | throw new Error( |
| 210 | } |
| 211 | |
| 212 | const existing = |
| 213 | .select({ |
| 214 | id: milestones.id |
| 215 | projectId: milestones.projectId |
| 216 | status: milestones.status |
| 217 | }) |
| 218 | .from(milestones) |
| 219 | .where(eq(milestones |
| 220 | .then((r) |
| 221 | |
| 222 | if (!existing) |
| 223 | throw new Error( |
| 224 | } |
| 225 | const hasProjectAccess = |
| 226 | orgMember.organizationId, |
| 227 | existing.projectId, |
| 228 | user.id |
| 229 | ) |
| 230 | if (!hasProjectAccess.success |
| 231 | throw new Error( |
| 232 | } |
| 233 | if (existing.status |
| 234 | throw new Error( |
| 235 | } |
| 236 | |
| 237 | const [milestone] |
| 238 | .update(milestones) |
| 239 | .set({ |
| 240 | status: 'completed', |
| 241 | completedAt: new Date |
| 242 | blockReason: null, |
| 243 | }) |
| 244 | .where(eq(milestones |
| 245 | .returning() |
| 246 | |
| 247 | return milestone |
| 248 | } |
| 249 | ) |
| 250 | |
| 251 | export const reorderMilestonesAction = |
| 252 | .inputSchema(reorderMilestonesSchema) |
| 253 | .action( |
| 254 | async ({ |
| 255 | parsedInput: { projectId, |
| 256 | ctx: { role, |
| 257 | }) => { |
| 258 | if (!role.authorize |
| 259 | throw new Error( |
| 260 | } |
| 261 | const hasProjectAccess = |
| 262 | orgMember.organizationId, |
| 263 | projectId, |
| 264 | user.id |
| 265 | ) |
| 266 | if (!hasProjectAccess.success |
| 267 | throw new Error( |
| 268 | } |
| 269 | |
| 270 | await db.transaction |
| 271 | for (let i |
| 272 | await tx |
| 273 | .update(milestones) |
| 274 | .set({ sortOrder: |
| 275 | .where( |
| 276 | and( |
| 277 | eq(milestones.id |
| 278 | eq(milestones.projectId |
| 279 | ) |
| 280 | ) |
| 281 | } |
| 282 | }) |
| 283 | |
| 284 | return { success: |
| 285 | } |
| 286 | ) |
| 287 | |
| 288 | export const linkRequirementAction = |
| 289 | .inputSchema(linkRequirementSchema) |
| 290 | .action( |
| 291 | async ({ |
| 292 | parsedInput: { milestoneId, |
| 293 | ctx: { role, |
| 294 | }) => { |
| 295 | if (!role.authorize |
| 296 | throw new Error( |
| 297 | 'You do not have permission to link requirements to milestones' |
| 298 | ) |
| 299 | } |
| 300 | |
| 301 | const milestone = |
| 302 | .select({ id: |
| 303 | .from(milestones) |
| 304 | .where(eq(milestones |
| 305 | .then((r) |
| 306 | |
| 307 | if (!milestone) |
| 308 | throw new Error( |
| 309 | } |
| 310 | const hasProjectAccess = |
| 311 | orgMember.organizationId, |
| 312 | milestone.projectId, |
| 313 | user.id |
| 314 | ) |
| 315 | if (!hasProjectAccess.success |
| 316 | throw new Error( |
| 317 | } |
| 318 | |
| 319 | // Verify requirement exists and belongs to same project |
| 320 | const requirement = |
| 321 | .select({ id: |
| 322 | .from(requirements) |
| 323 | .where(eq(requirements |
| 324 | .then((r) |
| 325 | |
| 326 | if (!requirement) |
| 327 | throw new Error( |
| 328 | } |
| 329 | if (requirement.projectId |
| 330 | throw new Error( |
| 331 | } |
| 332 | |
| 333 | // Get the next sort order within this milestone |
| 334 | const existingLinks = |
| 335 | .select({ sortOrder: |
| 336 | .from(milestoneRequirements) |
| 337 | .where(eq(milestoneRequirements |
| 338 | |
| 339 | const nextSortOrder = |
| 340 | existingLinks.length > 0 |
| 341 | ? Math.max( |
| 342 | : 0 |
| 343 | |
| 344 | const [link] |
| 345 | .insert(milestoneRequirements) |
| 346 | .values({ |
| 347 | milestoneId, |
| 348 | requirementId, |
| 349 | sortOrder: nextSortOrder, |
| 350 | }) |
| 351 | .returning() |
| 352 | |
| 353 | return link |
| 354 | } |
| 355 | ) |
| 356 | |
| 357 | export const unlinkRequirementAction = |
| 358 | .inputSchema(unlinkRequirementSchema) |
| 359 | .action( |
| 360 | async ({ |
| 361 | parsedInput: { milestoneId, |
| 362 | ctx: { role, |
| 363 | }) => { |
| 364 | if (!role.authorize |
| 365 | throw new Error( |
| 366 | 'You do not have permission to unlink requirements from milestones' |
| 367 | ) |
| 368 | } |
| 369 | |
| 370 | const [milestone] |
| 371 | .select({ projectId: |
| 372 | .from(milestones) |
| 373 | .where(eq(milestones |
| 374 | if (!milestone) |
| 375 | throw new Error( |
| 376 | } |
| 377 | const hasProjectAccess = |
| 378 | orgMember.organizationId, |
| 379 | milestone.projectId, |
| 380 | user.id |
| 381 | ) |
| 382 | if (!hasProjectAccess.success |
| 383 | throw new Error( |
| 384 | } |
| 385 | |
| 386 | await db |
| 387 | .delete(milestoneRequirements) |
| 388 | .where( |
| 389 | and( |
| 390 | eq(milestoneRequirements.milestoneId |
| 391 | eq(milestoneRequirements.requirementId |
| 392 | ) |
| 393 | ) |
| 394 | |
| 395 | return { success: |
| 396 | } |
| 397 | ) |