| 1 | import { and, asc, desc, eq, gte, inArray, isNull, lte, sum } from 'drizzle-orm' |
| 2 | import type { ReadonlyHeaders |
| 3 | import { getCachedActiveOrgMember } |
| 4 | import { db } |
| 5 | import { |
| 6 | memberRates, |
| 7 | members, |
| 8 | projectBudgets, |
| 9 | requirements, |
| 10 | timeEntries, |
| 11 | timesheetReportEntries, |
| 12 | timesheetReportRecipients, |
| 13 | timesheetReports, |
| 14 | users, |
| 15 | } from '@/server/db/schema' |
| 16 | |
| 17 | interface ListFilters { |
| 18 | billable?: boolean |
| 19 | dateFrom?: string |
| 20 | dateTo?: string |
| 21 | memberId?: string |
| 22 | requirementId?: string |
| 23 | status?: string |
| 24 | } |
| 25 | |
| 26 | const listByProject = async |
| 27 | projectId: string, |
| 28 | headers: ReadonlyHeaders |
| 29 | filters?: ListFilters |
| 30 | ) => { |
| 31 | const activeMember = |
| 32 | const isAdmin = |
| 33 | activeMember?.role === |
| 34 | |
| 35 | const conditions = |
| 36 | |
| 37 | if (!isAdmin && |
| 38 | conditions.push(eq |
| 39 | } |
| 40 | |
| 41 | if (filters?.memberId |
| 42 | conditions.push(eq |
| 43 | } |
| 44 | if (filters?.status |
| 45 | conditions.push( |
| 46 | eq( |
| 47 | timeEntries.status, |
| 48 | filters.status as |
| 49 | ) |
| 50 | ) |
| 51 | } |
| 52 | if (filters?.requirementId |
| 53 | conditions.push(eq |
| 54 | } |
| 55 | if (filters?.billable |
| 56 | conditions.push(eq |
| 57 | } |
| 58 | if (filters?.dateFrom |
| 59 | conditions.push(gte |
| 60 | } |
| 61 | if (filters?.dateTo |
| 62 | conditions.push(lte |
| 63 | } |
| 64 | |
| 65 | const entries = |
| 66 | .select({ |
| 67 | id: timeEntries.id |
| 68 | projectId: timeEntries.projectId |
| 69 | requirementId: timeEntries.requirementId |
| 70 | memberId: timeEntries.memberId |
| 71 | description: timeEntries.description |
| 72 | date: timeEntries.date |
| 73 | durationMinutes: timeEntries.durationMinutes |
| 74 | billable: timeEntries.billable |
| 75 | status: timeEntries.status |
| 76 | rejectReason: timeEntries.rejectReason |
| 77 | invoiceId: timeEntries.invoiceId |
| 78 | createdAt: timeEntries.createdAt |
| 79 | updatedAt: timeEntries.updatedAt |
| 80 | memberName: users.name |
| 81 | memberEmail: users.email |
| 82 | requirementTitle: requirements.title |
| 83 | requirementSlug: requirements.slug |
| 84 | }) |
| 85 | .from(timeEntries) |
| 86 | .innerJoin(members, |
| 87 | .innerJoin(users, |
| 88 | .leftJoin(requirements, |
| 89 | .where(and(... |
| 90 | .orderBy(desc(timeEntries |
| 91 | |
| 92 | return entries as |
| 93 | (typeof entries |
| 94 | 'requirementTitle' | |
| 95 | > & { |
| 96 | } |
| 97 | |
| 98 | const getById = async |
| 99 | const [entry] |
| 100 | .select({ |
| 101 | id: timeEntries.id |
| 102 | projectId: timeEntries.projectId |
| 103 | requirementId: timeEntries.requirementId |
| 104 | memberId: timeEntries.memberId |
| 105 | description: timeEntries.description |
| 106 | date: timeEntries.date |
| 107 | durationMinutes: timeEntries.durationMinutes |
| 108 | billable: timeEntries.billable |
| 109 | status: timeEntries.status |
| 110 | invoiceId: timeEntries.invoiceId |
| 111 | createdAt: timeEntries.createdAt |
| 112 | updatedAt: timeEntries.updatedAt |
| 113 | memberName: users.name |
| 114 | memberEmail: users.email |
| 115 | requirementTitle: requirements.title |
| 116 | }) |
| 117 | .from(timeEntries) |
| 118 | .innerJoin(members, |
| 119 | .innerJoin(users, |
| 120 | .leftJoin(requirements, |
| 121 | .where( |
| 122 | and(eq(timeEntries |
| 123 | ) |
| 124 | |
| 125 | return entry ?? |
| 126 | } |
| 127 | |
| 128 | const getWeeklyTimesheet = async |
| 129 | projectId: string, |
| 130 | memberId: string, |
| 131 | weekStart: string |
| 132 | ) => { |
| 133 | const startDate = |
| 134 | const endDate = |
| 135 | endDate.setDate(endDate |
| 136 | |
| 137 | const entries = |
| 138 | .select({ |
| 139 | id: timeEntries.id |
| 140 | requirementId: timeEntries.requirementId |
| 141 | description: timeEntries.description |
| 142 | date: timeEntries.date |
| 143 | durationMinutes: timeEntries.durationMinutes |
| 144 | billable: timeEntries.billable |
| 145 | status: timeEntries.status |
| 146 | requirementTitle: requirements.title |
| 147 | }) |
| 148 | .from(timeEntries) |
| 149 | .leftJoin(requirements, |
| 150 | .where( |
| 151 | and( |
| 152 | eq(timeEntries.projectId |
| 153 | eq(timeEntries.memberId |
| 154 | gte(timeEntries.date |
| 155 | lte(timeEntries.date |
| 156 | ) |
| 157 | ) |
| 158 | .orderBy(asc(timeEntries |
| 159 | |
| 160 | return entries |
| 161 | } |
| 162 | |
| 163 | const getProjectBudgetStatus = async |
| 164 | const [budget] |
| 165 | .select() |
| 166 | .from(projectBudgets) |
| 167 | .where(eq(projectBudgets |
| 168 | |
| 169 | if (!budget) |
| 170 | return null |
| 171 | } |
| 172 | |
| 173 | const [result] |
| 174 | .select({ |
| 175 | totalMinutes: sum(timeEntries |
| 176 | }) |
| 177 | .from(timeEntries) |
| 178 | .where( |
| 179 | and( |
| 180 | eq(timeEntries.projectId |
| 181 | eq(timeEntries.status |
| 182 | ) |
| 183 | ) |
| 184 | |
| 185 | const totalApprovedMinutes = |
| 186 | const percentageUsed = |
| 187 | (totalApprovedMinutes / budget |
| 188 | ) |
| 189 | |
| 190 | return { |
| 191 | budget, |
| 192 | totalApprovedMinutes, |
| 193 | percentageUsed, |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | const getMemberRate = async |
| 198 | memberId: string, |
| 199 | projectId: string, |
| 200 | date: string |
| 201 | ) => { |
| 202 | const targetDate = |
| 203 | |
| 204 | const [projectRate] |
| 205 | .select() |
| 206 | .from(memberRates) |
| 207 | .where( |
| 208 | and( |
| 209 | eq(memberRates.memberId |
| 210 | eq(memberRates.projectId |
| 211 | lte(memberRates.effectiveFrom |
| 212 | ) |
| 213 | ) |
| 214 | .orderBy(desc(memberRates |
| 215 | .limit(1) |
| 216 | |
| 217 | if (projectRate) { |
| 218 | return projectRate |
| 219 | } |
| 220 | |
| 221 | const [orgRate] |
| 222 | .select() |
| 223 | .from(memberRates) |
| 224 | .where( |
| 225 | and( |
| 226 | eq(memberRates.memberId |
| 227 | isNull(memberRates.projectId |
| 228 | lte(memberRates.effectiveFrom |
| 229 | ) |
| 230 | ) |
| 231 | .orderBy(desc(memberRates |
| 232 | .limit(1) |
| 233 | |
| 234 | return orgRate ?? |
| 235 | } |
| 236 | |
| 237 | const getBillableSummary = async |
| 238 | const entries = |
| 239 | .select({ |
| 240 | id: timeEntries.id |
| 241 | memberId: timeEntries.memberId |
| 242 | requirementId: timeEntries.requirementId |
| 243 | description: timeEntries.description |
| 244 | date: timeEntries.date |
| 245 | durationMinutes: timeEntries.durationMinutes |
| 246 | invoiceId: timeEntries.invoiceId |
| 247 | memberName: users.name |
| 248 | requirementTitle: requirements.title |
| 249 | }) |
| 250 | .from(timeEntries) |
| 251 | .innerJoin(members, |
| 252 | .innerJoin(users, |
| 253 | .leftJoin(requirements, |
| 254 | .where( |
| 255 | and( |
| 256 | eq(timeEntries.projectId |
| 257 | eq(timeEntries.status |
| 258 | eq(timeEntries.billable |
| 259 | isNull(timeEntries.invoiceId |
| 260 | ) |
| 261 | ) |
| 262 | .orderBy(asc(users |
| 263 | |
| 264 | return entries |
| 265 | } |
| 266 | |
| 267 | const getMemberRates = async |
| 268 | const rates = |
| 269 | .select({ |
| 270 | id: memberRates.id |
| 271 | memberId: memberRates.memberId |
| 272 | projectId: memberRates.projectId |
| 273 | hourlyRate: memberRates.hourlyRate |
| 274 | currency: memberRates.currency |
| 275 | effectiveFrom: memberRates.effectiveFrom |
| 276 | createdAt: memberRates.createdAt |
| 277 | memberName: users.name |
| 278 | memberEmail: users.email |
| 279 | }) |
| 280 | .from(memberRates) |
| 281 | .innerJoin(members, |
| 282 | .innerJoin(users, |
| 283 | .where(eq(members |
| 284 | .orderBy(desc(memberRates |
| 285 | |
| 286 | return rates |
| 287 | } |
| 288 | |
| 289 | const getProjectBudget = async |
| 290 | const [budget] |
| 291 | .select() |
| 292 | .from(projectBudgets) |
| 293 | .where(eq(projectBudgets |
| 294 | |
| 295 | return budget ?? |
| 296 | } |
| 297 | |
| 298 | const listReportsByProject = async |
| 299 | const reports = |
| 300 | .select({ |
| 301 | id: timesheetReports.id |
| 302 | projectId: timesheetReports.projectId |
| 303 | title: timesheetReports.title |
| 304 | status: timesheetReports.status |
| 305 | totalMinutes: timesheetReports.totalMinutes |
| 306 | totalAmount: timesheetReports.totalAmount |
| 307 | currency: timesheetReports.currency |
| 308 | disputeReason: timesheetReports.disputeReason |
| 309 | sentAt: timesheetReports.sentAt |
| 310 | respondedAt: timesheetReports.respondedAt |
| 311 | createdAt: timesheetReports.createdAt |
| 312 | updatedAt: timesheetReports.updatedAt |
| 313 | sentByMemberId: timesheetReports.sentByMemberId |
| 314 | }) |
| 315 | .from(timesheetReports) |
| 316 | .where(eq(timesheetReports |
| 317 | .orderBy(desc(timesheetReports |
| 318 | return reports |
| 319 | } |
| 320 | |
| 321 | const listReportsForClient = async |
| 322 | const reports = |
| 323 | .select({ |
| 324 | id: timesheetReports.id |
| 325 | projectId: timesheetReports.projectId |
| 326 | title: timesheetReports.title |
| 327 | status: timesheetReports.status |
| 328 | totalMinutes: timesheetReports.totalMinutes |
| 329 | totalAmount: timesheetReports.totalAmount |
| 330 | currency: timesheetReports.currency |
| 331 | disputeReason: timesheetReports.disputeReason |
| 332 | sentAt: timesheetReports.sentAt |
| 333 | respondedAt: timesheetReports.respondedAt |
| 334 | createdAt: timesheetReports.createdAt |
| 335 | updatedAt: timesheetReports.updatedAt |
| 336 | sentByMemberId: timesheetReports.sentByMemberId |
| 337 | }) |
| 338 | .from(timesheetReportRecipients) |
| 339 | .innerJoin( |
| 340 | timesheetReports, |
| 341 | eq(timesheetReportRecipients.reportId |
| 342 | ) |
| 343 | .where(eq(timesheetReportRecipients |
| 344 | .orderBy(desc(timesheetReports |
| 345 | |
| 346 | return reports |
| 347 | } |
| 348 | |
| 349 | const getReportEntriesBatch = async |
| 350 | interface Row |
| 351 | billable: boolean |
| 352 | createdAt: Date |
| 353 | date: Date |
| 354 | description: string |
| 355 | durationMinutes: number |
| 356 | id: string |
| 357 | invoiceId: string |
| 358 | memberId: string |
| 359 | memberName: string |
| 360 | reportId: string |
| 361 | requirementTitle: string |
| 362 | timeEntryId: string |
| 363 | updatedAt: Date |
| 364 | } |
| 365 | |
| 366 | if (reportIds.length |
| 367 | return {} as Record |
| 368 | } |
| 369 | |
| 370 | const rows = |
| 371 | .select({ |
| 372 | reportId: timesheetReportEntries.reportId |
| 373 | id: timeEntries.id |
| 374 | memberId: timeEntries.memberId |
| 375 | description: timeEntries.description |
| 376 | date: timeEntries.date |
| 377 | durationMinutes: timeEntries.durationMinutes |
| 378 | billable: timeEntries.billable |
| 379 | memberName: users.name |
| 380 | requirementTitle: requirements.title |
| 381 | timeEntryId: timeEntries.id |
| 382 | createdAt: timesheetReportEntries.createdAt |
| 383 | updatedAt: timesheetReportEntries.updatedAt |
| 384 | invoiceId: timeEntries.invoiceId |
| 385 | }) |
| 386 | .from(timesheetReportEntries) |
| 387 | .innerJoin( |
| 388 | timeEntries, |
| 389 | eq(timesheetReportEntries.timeEntryId |
| 390 | ) |
| 391 | .innerJoin(members, |
| 392 | .innerJoin(users, |
| 393 | .leftJoin(requirements, |
| 394 | .where(inArray(timesheetReportEntries |
| 395 | .orderBy(asc(timeEntries |
| 396 | |
| 397 | const grouped = |
| 398 | for (const row |
| 399 | const list = |
| 400 | if (list) { |
| 401 | list.push(row |
| 402 | } else { |
| 403 | grouped.set(row |
| 404 | } |
| 405 | } |
| 406 | return Object.fromEntries |
| 407 | } |
| 408 | |
| 409 | const getReportById = async |
| 410 | const [report] |
| 411 | .select() |
| 412 | .from(timesheetReports) |
| 413 | .where( |
| 414 | and( |
| 415 | eq(timesheetReports.id |
| 416 | eq(timesheetReports.projectId |
| 417 | ) |
| 418 | ) |
| 419 | if (!report) |
| 420 | return null |
| 421 | } |
| 422 | |
| 423 | const entryLinks = |
| 424 | .select({ timeEntryId: |
| 425 | .from(timesheetReportEntries) |
| 426 | .where(eq(timesheetReportEntries |
| 427 | |
| 428 | const entryIds = |
| 429 | if (entryIds.length |
| 430 | return { report, |
| 431 | } |
| 432 | |
| 433 | const entries = |
| 434 | .select({ |
| 435 | id: timeEntries.id |
| 436 | description: timeEntries.description |
| 437 | date: timeEntries.date |
| 438 | durationMinutes: timeEntries.durationMinutes |
| 439 | billable: timeEntries.billable |
| 440 | memberName: users.name |
| 441 | requirementTitle: requirements.title |
| 442 | }) |
| 443 | .from(timeEntries) |
| 444 | .innerJoin(members, |
| 445 | .innerJoin(users, |
| 446 | .leftJoin(requirements, |
| 447 | .where(inArray(timeEntries |
| 448 | .orderBy(asc(timeEntries |
| 449 | |
| 450 | return { report, |
| 451 | } |
| 452 | const getBillableEntriesForReport = async |
| 453 | const entries = |
| 454 | .select({ |
| 455 | id: timeEntries.id |
| 456 | memberId: timeEntries.memberId |
| 457 | requirementId: timeEntries.requirementId |
| 458 | description: timeEntries.description |
| 459 | date: timeEntries.date |
| 460 | durationMinutes: timeEntries.durationMinutes |
| 461 | invoiceId: timeEntries.invoiceId |
| 462 | memberName: users.name |
| 463 | requirementTitle: requirements.title |
| 464 | }) |
| 465 | .from(timesheetReportEntries) |
| 466 | .innerJoin( |
| 467 | timeEntries, |
| 468 | eq(timesheetReportEntries.timeEntryId |
| 469 | ) |
| 470 | .innerJoin(members, |
| 471 | .innerJoin(users, |
| 472 | .leftJoin(requirements, |
| 473 | .where(eq(timesheetReportEntries |
| 474 | .orderBy(asc(users |
| 475 | |
| 476 | return entries |
| 477 | } |
| 478 | |
| 479 | const getReportRecipientsBatch = async |
| 480 | if (reportIds.length |
| 481 | return {} as Record |
| 482 | string, |
| 483 | { |
| 484 | id: string |
| 485 | clientMemberId: string |
| 486 | clientName: string |
| 487 | clientEmail: string |
| 488 | status: 'pending |
| 489 | disputeReason: string |
| 490 | respondedAt: Date |
| 491 | }[] |
| 492 | > |
| 493 | } |
| 494 | |
| 495 | const rows = |
| 496 | .select({ |
| 497 | reportId: timesheetReportRecipients.reportId |
| 498 | id: timesheetReportRecipients.id |
| 499 | clientMemberId: timesheetReportRecipients.clientMemberId |
| 500 | clientName: users.name |
| 501 | clientEmail: users.email |
| 502 | status: timesheetReportRecipients.status |
| 503 | disputeReason: timesheetReportRecipients.disputeReason |
| 504 | respondedAt: timesheetReportRecipients.respondedAt |
| 505 | }) |
| 506 | .from(timesheetReportRecipients) |
| 507 | .innerJoin( |
| 508 | members, |
| 509 | eq(timesheetReportRecipients.clientMemberId |
| 510 | ) |
| 511 | .innerJoin(users, |
| 512 | .where(inArray(timesheetReportRecipients |
| 513 | |
| 514 | const grouped = |
| 515 | for (const row |
| 516 | if (!grouped.has |
| 517 | grouped.set(row |
| 518 | } |
| 519 | grouped.get(row |
| 520 | } |
| 521 | return Object.fromEntries |
| 522 | } |
| 523 | |
| 524 | const listByProjectIdsSince = async |
| 525 | projectIds: string[] |
| 526 | since: Date |
| 527 | memberId?: string |
| 528 | ) => { |
| 529 | if (projectIds.length |
| 530 | return [] |
| 531 | } |
| 532 | |
| 533 | return await db |
| 534 | .select({ |
| 535 | projectId: timeEntries.projectId |
| 536 | durationMinutes: timeEntries.durationMinutes |
| 537 | }) |
| 538 | .from(timeEntries) |
| 539 | .where( |
| 540 | and( |
| 541 | inArray(timeEntries.projectId |
| 542 | gte(timeEntries.date |
| 543 | memberId ? eq( |
| 544 | ) |
| 545 | ) |
| 546 | } |
| 547 | |
| 548 | export const timesheetService = |
| 549 | listByProject, |
| 550 | listByProjectIdsSince, |
| 551 | getById, |
| 552 | getWeeklyTimesheet, |
| 553 | getProjectBudgetStatus, |
| 554 | getMemberRate, |
| 555 | getBillableSummary, |
| 556 | getBillableEntriesForReport, |
| 557 | getMemberRates, |
| 558 | getProjectBudget, |
| 559 | listReportsByProject, |
| 560 | listReportsForClient, |
| 561 | getReportById, |
| 562 | getReportEntriesBatch, |
| 563 | getReportRecipientsBatch, |
| 564 | } |