( input: z.infer<typeof zOnboardingProject>, user: IServiceUser, )
| 11 | import { createTRPCRouter, protectedProcedure, publicProcedure } from '../trpc'; |
| 12 | |
| 13 | async function createOrGetOrganization( |
| 14 | input: z.infer<typeof zOnboardingProject>, |
| 15 | user: IServiceUser, |
| 16 | ) { |
| 17 | if (input.organizationId) { |
| 18 | return await getOrganizationById(input.organizationId); |
| 19 | } |
| 20 | |
| 21 | const TRIAL_DURATION_IN_DAYS = 30; |
| 22 | // Generous trial allowance so trialing orgs never get flagged as |
| 23 | // "limit exceeded" (the limit defaults to 0, which trips on the first event). |
| 24 | const TRIAL_EVENTS_LIMIT = 10_000_000; |
| 25 | |
| 26 | if (input.organization) { |
| 27 | const organizationId = await getId('organization', input.organization); |
| 28 | |
| 29 | // Create the organization and its owner (org:admin member) atomically. The |
| 30 | // `delete` cron treats an organization with no org:admin member as ownerless |
| 31 | // and removes it, so an organization must never exist without one. |
| 32 | const [organization] = await db.$transaction([ |
| 33 | db.organization.create({ |
| 34 | data: { |
| 35 | id: organizationId, |
| 36 | name: input.organization, |
| 37 | createdByUserId: user.id, |
| 38 | subscriptionEndsAt: addDays(new Date(), TRIAL_DURATION_IN_DAYS), |
| 39 | subscriptionStatus: 'trialing', |
| 40 | subscriptionPeriodEventsLimit: TRIAL_EVENTS_LIMIT, |
| 41 | timezone: input.timezone, |
| 42 | onboarding: '', |
| 43 | }, |
| 44 | }), |
| 45 | db.member.create({ |
| 46 | data: { |
| 47 | email: user.email, |
| 48 | organizationId, |
| 49 | role: 'org:admin', |
| 50 | userId: user.id, |
| 51 | }, |
| 52 | }), |
| 53 | ]); |
| 54 | |
| 55 | return organization; |
| 56 | } |
| 57 | |
| 58 | return null; |
| 59 | } |
| 60 | |
| 61 | export const onboardingRouter = createTRPCRouter({ |
| 62 | skipOnboardingCheck: publicProcedure.query(async ({ ctx }) => { |
no test coverage detected