| 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 listByOrganization = async |
| 16 | await db |
| 17 | .select() |
| 18 | .from(projects) |
| 19 | .where(eq(projects |
| 20 | |
| 21 | const getBySlug = async |
| 22 | const [project] |
| 23 | .select() |
| 24 | .from(projects) |
| 25 | .where( |
| 26 | and(eq(projects |
| 27 | ) |
| 28 | |
| 29 | return project ?? |
| 30 | } |
| 31 | |
| 32 | const listAccessible = async |
| 33 | organizationId: string, |
| 34 | orgMember: { |
| 35 | ) => { |
| 36 | if (orgMember.role |
| 37 | return db |
| 38 | .select() |
| 39 | .from(projects) |
| 40 | .where(eq(projects |
| 41 | .orderBy(desc(projects |
| 42 | } |
| 43 | |
| 44 | const memberTeams = |
| 45 | .select({ teamId: |
| 46 | .from(teamMembers) |
| 47 | .where(eq(teamMembers |
| 48 | |
| 49 | const teamIds = |
| 50 | |
| 51 | const assignedProjectIds = |
| 52 | .selectDistinct({ projectId: |
| 53 | .from(projectMemberAssignments) |
| 54 | .where(eq(projectMemberAssignments |
| 55 | |
| 56 | const clientProjectIds = |
| 57 | .selectDistinct({ projectId: |
| 58 | .from(projectClientAssignments) |
| 59 | .where(eq(projectClientAssignments |
| 60 | |
| 61 | const teamProjectIds = |
| 62 | teamIds.length > 0 |
| 63 | ? await db |
| 64 | .selectDistinct({ projectId: |
| 65 | .from(projectTeamAssignments) |
| 66 | .where(inArray(projectTeamAssignments |
| 67 | : [] |
| 68 | |
| 69 | const allProjectIds = |
| 70 | ...new Set([ |
| 71 | ...assignedProjectIds.map |
| 72 | ...clientProjectIds.map( |
| 73 | ...teamProjectIds.map( |
| 74 | ]), |
| 75 | ] |
| 76 | |
| 77 | if (allProjectIds.length |
| 78 | return [] |
| 79 | } |
| 80 | |
| 81 | return db |
| 82 | .select() |
| 83 | .from(projects) |
| 84 | .where( |
| 85 | and( |
| 86 | eq(projects.organizationId |
| 87 | inArray(projects.id |
| 88 | ) |
| 89 | ) |
| 90 | .orderBy(desc(projects |
| 91 | } |
| 92 | |
| 93 | async function getProjectDetails( |
| 94 | const [result] |
| 95 | .select({ |
| 96 | projectName: projects.name |
| 97 | projectSlug: projects.slug |
| 98 | orgSlug: organizations.slug |
| 99 | orgName: organizations.name |
| 100 | }) |
| 101 | .from(projects) |
| 102 | .innerJoin(organizations, |
| 103 | .where(eq(projects |
| 104 | |
| 105 | return ( |
| 106 | result ?? { projectName |
| 107 | ) |
| 108 | } |
| 109 | |
| 110 | const getById = async |
| 111 | const [project] |
| 112 | .select() |
| 113 | .from(projects) |
| 114 | .where(eq(projects |
| 115 | |
| 116 | return project ?? |
| 117 | } |
| 118 | const SETTINGS_DEFAULTS = { |
| 119 | memberRate: 0, |
| 120 | currency: 'USD' as |
| 121 | timesheetDuration: 'weekly' as |
| 122 | invoiceNumberTemplate: 'INV-%year(short)%month(num)-%seq(4)', |
| 123 | clientInvolvement: { |
| 124 | proposals: 'on', |
| 125 | requirements: 'on', |
| 126 | milestones: 'on', |
| 127 | timesheets: 'on', |
| 128 | expenses: 'on', |
| 129 | invoices: 'on', |
| 130 | } as const, |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Resolves settings with fallback chain: |
| 135 | * 1. Project-level settings (if projectId provided) |
| 136 | * 2. Organization-level settings |
| 137 | * 3. Hard-coded defaults |
| 138 | */ |
| 139 | const getSettings = async |
| 140 | if (projectId) { |
| 141 | const [projectSettings] |
| 142 | .select() |
| 143 | .from(settingsTable) |
| 144 | .where( |
| 145 | and( |
| 146 | eq(settingsTable.organizationId |
| 147 | eq(settingsTable.projectId |
| 148 | ) |
| 149 | ) |
| 150 | |
| 151 | if (projectSettings) { |
| 152 | return projectSettings |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | const [orgSettings] |
| 157 | .select() |
| 158 | .from(settingsTable) |
| 159 | .where( |
| 160 | and( |
| 161 | eq(settingsTable.organizationId |
| 162 | isNull(settingsTable.projectId |
| 163 | ) |
| 164 | ) |
| 165 | |
| 166 | return orgSettings ?? |
| 167 | } |
| 168 | |
| 169 | export const projectsService = |
| 170 | listByOrganization, |
| 171 | getBySlug, |
| 172 | listAccessible, |
| 173 | getProjectDetails, |
| 174 | getById, |
| 175 | getSettings, |
| 176 | } |