feat: admin controls
This commit is contained in:
+6
-10
@@ -1,5 +1,6 @@
|
||||
import { and, eq, gt, lt, lte, or, sql } from 'drizzle-orm';
|
||||
import { invites, users } from '../../drizzle/schema';
|
||||
import type { Transaction } from '../lib/auth-transaction';
|
||||
import { db } from '../lib/db';
|
||||
|
||||
export const INVITE_TOKEN_HEADER = 'x-invite-token';
|
||||
@@ -9,10 +10,12 @@ const usable = () => and(lt(invites.uses, invites.maxUses), gt(invites.expiresAt
|
||||
|
||||
/**
|
||||
* Takes one use of an invite. It is a single statement, so two people
|
||||
* registering with the same single use link at once cannot both get in.
|
||||
* registering with the same single use link at once cannot both get in. It
|
||||
* runs on the transaction of the registration, a failed one gives the use back
|
||||
* by rolling back.
|
||||
*/
|
||||
export const claimInvite = async (token: string): Promise<boolean> => {
|
||||
const claimed = await db.update(invites)
|
||||
export const claimInvite = async (tx: Transaction, token: string): Promise<boolean> => {
|
||||
const claimed = await tx.update(invites)
|
||||
.set({ uses: sql`${invites.uses} + 1` })
|
||||
.where(and(eq(invites.token, token), usable()))
|
||||
.returning({ id: invites.id });
|
||||
@@ -20,13 +23,6 @@ export const claimInvite = async (token: string): Promise<boolean> => {
|
||||
return claimed.length > 0;
|
||||
};
|
||||
|
||||
/** Gives a use back when the registration it was taken for did not go through. */
|
||||
export const releaseInvite = async (token: string) => {
|
||||
await db.update(invites)
|
||||
.set({ uses: sql`${invites.uses} - 1` })
|
||||
.where(and(eq(invites.token, token), gt(invites.uses, 0)));
|
||||
};
|
||||
|
||||
export const findUsableInvite = async (token: string) => {
|
||||
const rows = await db
|
||||
.select({
|
||||
|
||||
Reference in New Issue
Block a user