| 1 | 'use server' |
| 2 | |
| 3 | import { and, asc, count, eq, isNull } from 'drizzle-orm' |
| 4 | // import { PROJECTS_CACHE_TAG } from '@/api/projects/service' |
| 5 | import { getUserBillingStatus } |
| 6 | import { formatLocalDateOnly } |
| 7 | import { authedActionClient } |
| 8 | import { titleToSlug } |
| 9 | import { FREE_PLAN_LIMITS } |
| 10 | import { db } |
| 11 | import { |
| 12 | customFields, |
| 13 | members, |
| 14 | projects, |
| 15 | requirements, |
| 16 | settings as settingsTable, |
| 17 | } from '@/server/db/schema' |
| 18 | import { createProjectSchema } |
| 19 | |
| 20 | export const createProjectAction = |
| 21 | .inputSchema(createProjectSchema) |
| 22 | .action( |
| 23 | async ({ |
| 24 | parsedInput: { |
| 25 | organizationId, |
| 26 | name, |
| 27 | description, |
| 28 | dueDate, |
| 29 | invoiceFromName, |
| 30 | invoiceFromAddress, |
| 31 | invoiceToName, |
| 32 | invoiceToAddress, |
| 33 | }, |
| 34 | ctx: { orgMember, |
| 35 | }) => { |
| 36 | if (!role.authorize |
| 37 | throw new Error( |
| 38 | } |
| 39 | |
| 40 | if (orgMember.organizationId |
| 41 | throw new Error( |
| 42 | } |
| 43 | |
| 44 | const { slugified, |
| 45 | |
| 46 | const project = |
| 47 | const [ownerRow] |
| 48 | .select({ ownerId: |
| 49 | .from(members) |
| 50 | .where( |
| 51 | and( |
| 52 | eq(members.organizationId |
| 53 | eq(members.role |
| 54 | ) |
| 55 | ) |
| 56 | .for('update') |
| 57 | .limit(1) |
| 58 | |
| 59 | if (!ownerRow) |
| 60 | throw new Error( |
| 61 | } |
| 62 | |
| 63 | const ownerHasPro = |
| 64 | if (!ownerHasPro) |
| 65 | const [projectCountRow] |
| 66 | .select({ value: |
| 67 | .from(projects) |
| 68 | .innerJoin( |
| 69 | members, |
| 70 | eq(members.organizationId |
| 71 | ) |
| 72 | .where( |
| 73 | and( |
| 74 | eq(members.userId |
| 75 | eq(members.role |
| 76 | ) |
| 77 | ) |
| 78 | const projectCount = |
| 79 | if (projectCount >= |
| 80 | throw new Error( |
| 81 | `Free plan is limited to ${FREE_PLAN_LIMITS.PROJECTS |
| 82 | ) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | const [projectWithSlug] |
| 87 | .select({ id: |
| 88 | .from(projects) |
| 89 | .where( |
| 90 | and( |
| 91 | eq(projects.organizationId |
| 92 | eq(projects.slug |
| 93 | ) |
| 94 | ) |
| 95 | .limit(1) |
| 96 | |
| 97 | const [createdProject] |
| 98 | .insert(projects) |
| 99 | .values({ |
| 100 | name, |
| 101 | slug: projectWithSlug ? |
| 102 | description: description || |
| 103 | organizationId, |
| 104 | dueDate: dueDate ? |
| 105 | }) |
| 106 | .returning() |
| 107 | |
| 108 | if (!createdProject) |
| 109 | throw new Error( |
| 110 | } |
| 111 | |
| 112 | // This is the requirement called "General Work" |
| 113 | await tx.insert |
| 114 | projectId: createdProject.id |
| 115 | slug: 'saturn_default_general_work', |
| 116 | title: 'General Work', |
| 117 | body: '<h1> This is a default requirement generated by Saturn to categorize under General Work <h1>', |
| 118 | status: 'client_accepted', |
| 119 | authorId: orgMember.id |
| 120 | }) |
| 121 | |
| 122 | const orgTemplates = |
| 123 | .select({ |
| 124 | label: customFields.label |
| 125 | type: customFields.type |
| 126 | required: customFields.required |
| 127 | visibleToClient: customFields.visibleToClient |
| 128 | defaultValue: customFields.defaultValue |
| 129 | options: customFields.options |
| 130 | config: customFields.config |
| 131 | }) |
| 132 | .from(customFields) |
| 133 | .where( |
| 134 | and( |
| 135 | eq(customFields.organizationId |
| 136 | isNull(customFields.projectId |
| 137 | ) |
| 138 | ) |
| 139 | .orderBy(asc(customFields |
| 140 | |
| 141 | if (orgTemplates.length |
| 142 | await tx.insert |
| 143 | orgTemplates.map((t |
| 144 | ...t, |
| 145 | organizationId, |
| 146 | projectId: createdProject.id |
| 147 | })) |
| 148 | ) |
| 149 | } |
| 150 | |
| 151 | const fromName = |
| 152 | const fromAddress = |
| 153 | const toName = |
| 154 | const toAddress = |
| 155 | if (fromName || |
| 156 | await tx.insert |
| 157 | organizationId, |
| 158 | projectId: createdProject.id |
| 159 | invoiceFromName: fromName, |
| 160 | invoiceFromAddress: fromAddress, |
| 161 | invoiceToName: toName, |
| 162 | invoiceToAddress: toAddress, |
| 163 | }) |
| 164 | } |
| 165 | |
| 166 | return createdProject |
| 167 | }) |
| 168 | |
| 169 | // revalidateTag(PROJECTS_CACHE_TAG,"max") |
| 170 | return project |
| 171 | } |
| 172 | ) |