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