-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add superadmin trial controls #422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
72a3d7f
da3d505
9d02efa
1bf7dd7
bdcaaff
599d1c4
3a1ba5b
ebff613
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
| import { type Kysely, sql } from "kysely"; | ||
|
|
||
| export async function up(db: Kysely<any>): Promise<void> { | ||
| await db.schema | ||
| .alterTable("invitation") | ||
| .addColumn("trialDays", "integer") | ||
| .execute(); | ||
|
|
||
| // Backfill existing trial invitations with the default trial period | ||
| await db | ||
| .updateTable("invitation") | ||
| .where("isTrial", "=", true) | ||
| .set({ trialDays: 30 }) | ||
| .execute(); | ||
|
|
||
| await db.schema.alterTable("invitation").dropColumn("isTrial").execute(); | ||
| } | ||
|
|
||
| export async function down(db: Kysely<any>): Promise<void> { | ||
| await db.schema | ||
| .alterTable("invitation") | ||
| .addColumn("isTrial", "boolean", (col) => | ||
| col.notNull().defaultTo(sql`false`), | ||
| ) | ||
| .execute(); | ||
|
|
||
| // Restore isTrial from trialDays | ||
| await db | ||
| .updateTable("invitation") | ||
| .where("trialDays", "is not", null) | ||
| .set({ isTrial: true }) | ||
| .execute(); | ||
|
|
||
| await db.schema.alterTable("invitation").dropColumn("trialDays").execute(); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -92,6 +92,15 @@ export async function updateUserTrialEndsAt(id: string, trialEndsAt: Date) { | |||||
| .executeTakeFirstOrThrow(); | ||||||
| } | ||||||
|
|
||||||
| export async function clearUserTrial(id: string) { | ||||||
| return db | ||||||
| .updateTable("user") | ||||||
| .where("id", "=", id) | ||||||
| .set({ trialEndsAt: null }) | ||||||
| .returningAll() | ||||||
|
||||||
| .returningAll() | |
| .returning(["id", "trialEndsAt"]) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import { TRPCError } from "@trpc/server"; | ||
| import { SignJWT } from "jose"; | ||
| import z from "zod"; | ||
| import { DEFAULT_TRIAL_PERIOD_DAYS } from "@/constants"; | ||
| import { UserRole } from "@/models/User"; | ||
| import copyMapsToOrganisation from "@/server/commands/copyMapsToOrganisation"; | ||
| import ensureOrganisationMap from "@/server/commands/ensureOrganisationMap"; | ||
|
|
@@ -37,6 +38,8 @@ export const invitationRouter = router({ | |
| }), | ||
| ) | ||
| .optional(), | ||
| isTrial: z.boolean().optional(), | ||
| trialDays: z.number().int().min(1).optional(), | ||
| }) | ||
|
Comment on lines
+41
to
43
|
||
| .refine((data) => data.organisationId || data.organisationName, { | ||
| message: "Either organisationId or organisationName must be provided", | ||
|
|
@@ -76,12 +79,21 @@ export const invitationRouter = router({ | |
| await ensureOrganisationMap(org.id); | ||
| } | ||
|
|
||
| const isSuperadmin = ctx.user.role === UserRole.Superadmin; | ||
| const isTrial = isSuperadmin ? Boolean(input.isTrial) : true; | ||
| const requestedTrialDays = isSuperadmin | ||
| ? input.trialDays | ||
| : DEFAULT_TRIAL_PERIOD_DAYS; | ||
| const trialDays = isTrial | ||
| ? (requestedTrialDays ?? DEFAULT_TRIAL_PERIOD_DAYS) | ||
| : null; | ||
|
joaquimds marked this conversation as resolved.
|
||
|
|
||
| const invitation = await createInvitation({ | ||
| email: input.email.toLowerCase().trim(), | ||
| name: input.name, | ||
| organisationId: org.id, | ||
| senderOrganisationId: senderOrg.id, | ||
| isTrial: ctx.user.role !== UserRole.Superadmin, | ||
| trialDays, | ||
| }); | ||
|
|
||
| const secret = new TextEncoder().encode(process.env.JWT_SECRET || ""); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ import { TRPCError } from "@trpc/server"; | |||||
| import z from "zod"; | ||||||
| import { UserRole, passwordSchema, userSchema } from "@/models/User"; | ||||||
| import { | ||||||
| clearUserTrial, | ||||||
| listUsers, | ||||||
| updateUser, | ||||||
| updateUserRole, | ||||||
|
|
@@ -11,6 +12,11 @@ import { protectedProcedure, router, superadminProcedure } from "../index"; | |||||
|
|
||||||
| export const userRouter = router({ | ||||||
| list: superadminProcedure.query(() => listUsers()), | ||||||
| clearTrial: superadminProcedure | ||||||
| .input(z.object({ userId: z.string().uuid() })) | ||||||
| .mutation(async ({ input }) => { | ||||||
| return clearUserTrial(input.userId); | ||||||
|
||||||
| return clearUserTrial(input.userId); | |
| await clearUserTrial(input.userId); |
Uh oh!
There was an error while loading. Please reload this page.