| 1 | import { and, desc, eq, inArray, isNull } from 'drizzle-orm' |
| 2 | import { db } |
| 3 | import { |
| 4 | organizations, |
| 5 | projectClientAssignments, |
| 6 | projectMemberAssignments, |
| 7 | projects, |
| 8 | projectTeamAssignments, |
| 9 | settings as settingsTable, |
| 10 | teamMembers, |
| 11 | } from '@/server/db/schema' |
| 12 | |
| 13 | export const PROJECTS_CACHE_TAG = |
| 14 | |
| 15 | const SETTINGS_DEFAULTS = { |
| 16 | currency: 'USD' as |
| 17 | billingRate: null as number |
| 18 | billingCurrency: 'USD' as |
| 19 | billingFrequency: 'hourly' as |
| 20 | payRate: 0, |
| 21 | payCurrency: 'USD' as |
| 22 | payFrequency: 'hourly' as |
| 23 | timesheetDuration: 'weekly' as |
| 24 | invoiceTimeUnit: 'hours' as |
| 25 | invoiceNumberTemplate: 'INV-%year(short)%month(num)-%seq(4)', |
| 26 | clientInvolvement: { |
| 27 | proposals: 'on', |
| 28 | requirements: 'on', |
| 29 | milestones: 'on', |
| 30 | timesheets: 'on', |
| 31 | expenses: 'on', |
| 32 | invoices: 'on', |
| 33 | } as const, |
| 34 | invoiceFromName: null as string |
| 35 | invoiceFromAddress: null as string |
| 36 | invoiceToName: null as string |
| 37 | invoiceToAddress: null as string |
| 38 | } |
| 39 | |
| 40 | const listByOrganization = async |
| 41 | await db |
| 42 | .select() |
| 43 | .from(projects) |
| 44 | .where(eq(projects |
| 45 | |
| 46 | const getBySlug = async |
| 47 | const [project] |
| 48 | .select() |
| 49 | .from(projects) |
| 50 | .where( |
| 51 | and(eq(projects |
| 52 | ) |
| 53 | |
| 54 | return project ?? |
| 55 | } |
| 56 | |
| 57 | const listAccessible = async |
| 58 | organizationId: string, |
| 59 | orgMember: { |
| 60 | ) => { |
| 61 | if (orgMember.role |
| 62 | return db |
| 63 | .select() |
| 64 | .from(projects) |
| 65 | .where(eq(projects |
| 66 | .orderBy(desc(projects |
| 67 | } |
| 68 | |
| 69 | const memberTeams = |
| 70 | .select({ teamId: |
| 71 | .from(teamMembers) |
| 72 | .where(eq(teamMembers |
| 73 | |
| 74 | const teamIds = |
| 75 | |
| 76 | const assignedProjectIds = |
| 77 | .selectDistinct({ projectId: |
| 78 | .from(projectMemberAssignments) |
| 79 | .where(eq(projectMemberAssignments |
| 80 | |
| 81 | const clientProjectIds = |
| 82 | .selectDistinct({ projectId: |
| 83 | .from(projectClientAssignments) |
| 84 | .where(eq(projectClientAssignments |
| 85 | |
| 86 | const teamProjectIds = |
| 87 | teamIds.length > 0 |
| 88 | ? await db |
| 89 | .selectDistinct({ projectId: |
| 90 | .from(projectTeamAssignments) |
| 91 | .where(inArray(projectTeamAssignments |
| 92 | : [] |
| 93 | |
| 94 | const allProjectIds = |
| 95 | ...new Set([ |
| 96 | ...assignedProjectIds.map |
| 97 | ...clientProjectIds.map( |
| 98 | ...teamProjectIds.map( |
| 99 | ]), |
| 100 | ] |
| 101 | |
| 102 | if (allProjectIds.length |
| 103 | return [] |
| 104 | } |
| 105 | |
| 106 | return db |
| 107 | .select() |
| 108 | .from(projects) |
| 109 | .where( |
| 110 | and( |
| 111 | eq(projects.organizationId |
| 112 | inArray(projects.id |
| 113 | ) |
| 114 | ) |
| 115 | .orderBy(desc(projects |
| 116 | } |
| 117 | |
| 118 | async function getProjectDetails( |
| 119 | const [result] |
| 120 | .select({ |
| 121 | projectName: projects.name |
| 122 | projectSlug: projects.slug |
| 123 | orgSlug: organizations.slug |
| 124 | orgName: organizations.name |
| 125 | }) |
| 126 | .from(projects) |
| 127 | .innerJoin(organizations, |
| 128 | .where(eq(projects |
| 129 | |
| 130 | return ( |
| 131 | result ?? { projectName |
| 132 | ) |
| 133 | } |
| 134 | |
| 135 | const getById = async |
| 136 | const [project] |
| 137 | .select() |
| 138 | .from(projects) |
| 139 | .where(eq(projects |
| 140 | |
| 141 | return project ?? |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Resolves settings with fallback chain: |
| 146 | * 1. Project-level settings (if projectId provided) |
| 147 | * 2. Organization-level settings |
| 148 | * 3. Hard-coded defaults |
| 149 | */ |
| 150 | const getSettings = async |
| 151 | const [orgSettings] |
| 152 | .select() |
| 153 | .from(settingsTable) |
| 154 | .where( |
| 155 | and( |
| 156 | eq(settingsTable.organizationId |
| 157 | isNull(settingsTable.projectId |
| 158 | ) |
| 159 | ) |
| 160 | |
| 161 | if (projectId) { |
| 162 | const [projectSettings] |
| 163 | .select() |
| 164 | .from(settingsTable) |
| 165 | .where( |
| 166 | and( |
| 167 | eq(settingsTable.organizationId |
| 168 | eq(settingsTable.projectId |
| 169 | ) |
| 170 | ) |
| 171 | |
| 172 | if (projectSettings) { |
| 173 | const fallback = |
| 174 | // A project settings row may exist purely for an invoice-contact override. |
| 175 | // Treat an unset pay rate (0) / billing rate (null) as "no rate override" |
| 176 | // and inherit the org-level rate triple so the project row can't silently |
| 177 | // zero out the organization's configured rates. |
| 178 | const hasPayOverride = |
| 179 | const hasBillingOverride = |
| 180 | return { |
| 181 | ...projectSettings, |
| 182 | invoiceFromName: |
| 183 | projectSettings.invoiceFromName || |
| 184 | invoiceFromAddress: |
| 185 | projectSettings.invoiceFromAddress || |
| 186 | invoiceToName: projectSettings.invoiceToName |
| 187 | invoiceToAddress: |
| 188 | projectSettings.invoiceToAddress || |
| 189 | payRate: hasPayOverride ? |
| 190 | payCurrency: hasPayOverride |
| 191 | ? projectSettings.payCurrency |
| 192 | : fallback.payCurrency, |
| 193 | payFrequency: hasPayOverride |
| 194 | ? projectSettings.payFrequency |
| 195 | : fallback.payFrequency, |
| 196 | billingRate: hasBillingOverride |
| 197 | ? projectSettings.billingRate |
| 198 | : fallback.billingRate, |
| 199 | billingCurrency: hasBillingOverride |
| 200 | ? projectSettings.billingCurrency |
| 201 | : fallback.billingCurrency, |
| 202 | billingFrequency: hasBillingOverride |
| 203 | ? projectSettings.billingFrequency |
| 204 | : fallback.billingFrequency, |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | return orgSettings ?? |
| 210 | } |
| 211 | |
| 212 | export const projectsService = |
| 213 | listByOrganization, |
| 214 | getBySlug, |
| 215 | listAccessible, |
| 216 | getProjectDetails, |
| 217 | getById, |
| 218 | getSettings, |
| 219 | } |