| 1 | 'use server' |
| 2 | |
| 3 | import { eq, sql } from 'drizzle-orm' |
| 4 | import { headers } |
| 5 | import { authedActionClient } |
| 6 | import { auth } |
| 7 | import { db } |
| 8 | import { settings as |
| 9 | import { organizations } |
| 10 | import { |
| 11 | deleteOrganizationSchema, |
| 12 | renameOrganizationSchema, |
| 13 | updateInvoiceNumberTemplateSchema, |
| 14 | updateOrgClientInvolvementSchema, |
| 15 | updateTimesheetDefaultsSchema, |
| 16 | } from './common' |
| 17 | |
| 18 | export const renameOrganizationAction = |
| 19 | .inputSchema(renameOrganizationSchema) |
| 20 | .action( |
| 21 | async ({ |
| 22 | parsedInput: { organizationId, |
| 23 | ctx: { role, |
| 24 | }) => { |
| 25 | if (!role.authorize |
| 26 | throw new Error( |
| 27 | 'You do not have permission to update workspace settings' |
| 28 | ) |
| 29 | } |
| 30 | |
| 31 | if (orgMember.organizationId |
| 32 | throw new Error( |
| 33 | } |
| 34 | |
| 35 | await auth.api |
| 36 | headers: await headers |
| 37 | body: { |
| 38 | data: { name, |
| 39 | organizationId, |
| 40 | }, |
| 41 | }) |
| 42 | |
| 43 | return { success: |
| 44 | } |
| 45 | ) |
| 46 | |
| 47 | export const updateTimesheetDefaultsAction = |
| 48 | .inputSchema(updateTimesheetDefaultsSchema) |
| 49 | .action( |
| 50 | async ({ |
| 51 | parsedInput: { |
| 52 | organizationId, |
| 53 | defaultMemberRate, |
| 54 | defaultCurrency, |
| 55 | defaultTimesheetDuration, |
| 56 | }, |
| 57 | ctx: { role, |
| 58 | }) => { |
| 59 | if (!role.authorize |
| 60 | throw new Error( |
| 61 | 'You do not have permission to update workspace settings' |
| 62 | ) |
| 63 | } |
| 64 | |
| 65 | if (orgMember.organizationId |
| 66 | throw new Error( |
| 67 | } |
| 68 | try { |
| 69 | await db |
| 70 | .insert(settingsTable) |
| 71 | .values({ |
| 72 | organizationId, |
| 73 | memberRate: defaultMemberRate, |
| 74 | currency: defaultCurrency, |
| 75 | timesheetDuration: defaultTimesheetDuration, |
| 76 | }) |
| 77 | .onConflictDoUpdate({ |
| 78 | target: [settingsTable.organizationId |
| 79 | targetWhere: sql`${settingsTable |
| 80 | set: { |
| 81 | memberRate: defaultMemberRate, |
| 82 | currency: defaultCurrency, |
| 83 | timesheetDuration: defaultTimesheetDuration, |
| 84 | }, |
| 85 | }) |
| 86 | |
| 87 | return { success: |
| 88 | } catch (e) |
| 89 | console.error(e |
| 90 | return { success: |
| 91 | } |
| 92 | } |
| 93 | ) |
| 94 | |
| 95 | export const updateInvoiceNumberTemplateAction = |
| 96 | .inputSchema(updateInvoiceNumberTemplateSchema) |
| 97 | .action( |
| 98 | async ({ |
| 99 | parsedInput: { organizationId, |
| 100 | ctx: { role, |
| 101 | }) => { |
| 102 | if (!role.authorize |
| 103 | throw new Error( |
| 104 | 'You do not have permission to update workspace settings' |
| 105 | ) |
| 106 | } |
| 107 | |
| 108 | if (orgMember.organizationId |
| 109 | throw new Error( |
| 110 | } |
| 111 | |
| 112 | if (projectId) { |
| 113 | await db |
| 114 | .insert(settingsTable) |
| 115 | .values({ |
| 116 | organizationId, |
| 117 | projectId, |
| 118 | invoiceNumberTemplate, |
| 119 | }) |
| 120 | .onConflictDoUpdate({ |
| 121 | target: [settingsTable.organizationId |
| 122 | set: { invoiceNumberTemplate |
| 123 | }) |
| 124 | } else { |
| 125 | await db |
| 126 | .insert(settingsTable) |
| 127 | .values({ |
| 128 | organizationId, |
| 129 | invoiceNumberTemplate, |
| 130 | }) |
| 131 | .onConflictDoUpdate({ |
| 132 | target: [settingsTable.organizationId |
| 133 | targetWhere: sql`${settingsTable |
| 134 | set: { invoiceNumberTemplate |
| 135 | }) |
| 136 | } |
| 137 | |
| 138 | return { success: |
| 139 | } |
| 140 | ) |
| 141 | |
| 142 | export const updateOrgClientInvolvementAction = |
| 143 | .inputSchema(updateOrgClientInvolvementSchema) |
| 144 | .action( |
| 145 | async ({ |
| 146 | parsedInput: { organizationId, |
| 147 | ctx: { role, |
| 148 | }) => { |
| 149 | if (!role.authorize |
| 150 | throw new Error( |
| 151 | 'You do not have permission to update workspace settings' |
| 152 | ) |
| 153 | } |
| 154 | if (orgMember.organizationId |
| 155 | throw new Error( |
| 156 | } |
| 157 | |
| 158 | await db |
| 159 | .insert(settingsTable) |
| 160 | .values({ organizationId |
| 161 | .onConflictDoUpdate({ |
| 162 | target: [settingsTable.organizationId |
| 163 | targetWhere: sql`${settingsTable |
| 164 | set: { clientInvolvement |
| 165 | }) |
| 166 | |
| 167 | return { success: |
| 168 | } |
| 169 | ) |
| 170 | |
| 171 | export const deleteOrganizationAction = |
| 172 | .inputSchema(deleteOrganizationSchema) |
| 173 | .action( |
| 174 | async ({ |
| 175 | parsedInput: { organizationId, |
| 176 | ctx: { role, |
| 177 | }) => { |
| 178 | if (!role.authorize |
| 179 | throw new Error( |
| 180 | } |
| 181 | |
| 182 | if (orgMember.organizationId |
| 183 | throw new Error( |
| 184 | } |
| 185 | |
| 186 | const [org] |
| 187 | .select({ name: |
| 188 | .from(organizations) |
| 189 | .where(eq(organizations |
| 190 | |
| 191 | if (!org) |
| 192 | throw new Error( |
| 193 | } |
| 194 | |
| 195 | if (org.name |
| 196 | throw new Error( |
| 197 | } |
| 198 | |
| 199 | await auth.api |
| 200 | headers: await headers |
| 201 | body: { organizationId |
| 202 | }) |
| 203 | |
| 204 | return { success: |
| 205 | } |
| 206 | ) |