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