| 1 | 'use server' |
| 2 | |
| 3 | import { and, count, eq } from 'drizzle-orm' |
| 4 | // import { PROJECTS_CACHE_TAG } from '@/api/projects/service' |
| 5 | import { getUserBillingStatus } |
| 6 | import { authedActionClient } |
| 7 | import { titleToSlug } |
| 8 | import { FREE_PLAN_LIMITS } |
| 9 | import { db } |
| 10 | import { members, projects |
| 11 | import { createProjectSchema } |
| 12 | |
| 13 | export const createProjectAction = |
| 14 | .inputSchema(createProjectSchema) |
| 15 | .action( |
| 16 | async ({ |
| 17 | parsedInput: { organizationId, |
| 18 | ctx: { orgMember, |
| 19 | }) => { |
| 20 | if (!role.authorize |
| 21 | throw new Error( |
| 22 | } |
| 23 | |
| 24 | if (orgMember.organizationId |
| 25 | throw new Error( |
| 26 | } |
| 27 | |
| 28 | const { slugified, |
| 29 | |
| 30 | const project = |
| 31 | const [ownerRow] |
| 32 | .select({ ownerId: |
| 33 | .from(members) |
| 34 | .where( |
| 35 | and( |
| 36 | eq(members.organizationId |
| 37 | eq(members.role |
| 38 | ) |
| 39 | ) |
| 40 | .for('update') |
| 41 | .limit(1) |
| 42 | |
| 43 | if (!ownerRow) |
| 44 | throw new Error( |
| 45 | } |
| 46 | |
| 47 | const ownerHasPro = |
| 48 | if (!ownerHasPro) |
| 49 | const [projectCountRow] |
| 50 | .select({ value: |
| 51 | .from(projects) |
| 52 | .innerJoin( |
| 53 | members, |
| 54 | eq(members.organizationId |
| 55 | ) |
| 56 | .where( |
| 57 | and( |
| 58 | eq(members.userId |
| 59 | eq(members.role |
| 60 | ) |
| 61 | ) |
| 62 | const projectCount = |
| 63 | if (projectCount >= |
| 64 | throw new Error( |
| 65 | `Free plan is limited to ${FREE_PLAN_LIMITS.PROJECTS |
| 66 | ) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | const [projectWithSlug] |
| 71 | .select({ id: |
| 72 | .from(projects) |
| 73 | .where( |
| 74 | and( |
| 75 | eq(projects.organizationId |
| 76 | eq(projects.slug |
| 77 | ) |
| 78 | ) |
| 79 | .limit(1) |
| 80 | |
| 81 | const [createdProject] |
| 82 | .insert(projects) |
| 83 | .values({ |
| 84 | name, |
| 85 | slug: projectWithSlug ? |
| 86 | description: description || |
| 87 | organizationId, |
| 88 | dueDate: dueDate ?? |
| 89 | }) |
| 90 | .returning() |
| 91 | |
| 92 | if (!createdProject) |
| 93 | throw new Error( |
| 94 | } |
| 95 | |
| 96 | // This is the requirement called "General Work" |
| 97 | await tx.insert |
| 98 | projectId: createdProject.id |
| 99 | slug: 'saturn_default_general_work', |
| 100 | title: 'General Work', |
| 101 | body: '<h1> This is a default requirement generated by Saturn to categorize under General Work <h1>', |
| 102 | status: 'client_accepted', |
| 103 | authorId: orgMember.id |
| 104 | }) |
| 105 | |
| 106 | return createdProject |
| 107 | }) |
| 108 | |
| 109 | // revalidateTag(PROJECTS_CACHE_TAG,"max") |
| 110 | return project |
| 111 | } |
| 112 | ) |