feat: refine admin access management

This commit is contained in:
Esdras Renan 2025-10-18 01:32:19 -03:00
parent dded6d1927
commit a69d37a672
9 changed files with 265 additions and 83 deletions

View file

@ -0,0 +1,27 @@
export const INVITE_REACTIVATION_WINDOW_DAYS = 7
export const INVITE_REACTIVATION_WINDOW_MS = INVITE_REACTIVATION_WINDOW_DAYS * 24 * 60 * 60 * 1000
function normalizeRevokedAt(value: Date | string | null | undefined) {
if (!value) return null
if (value instanceof Date) return value
const parsed = new Date(value)
return Number.isNaN(parsed.getTime()) ? null : parsed
}
export function isWithinInviteReactivationWindow(
revokedAt: Date | string | null | undefined,
reference: Date = new Date()
) {
const timestamp = normalizeRevokedAt(revokedAt)
if (!timestamp) return false
return reference.getTime() - timestamp.getTime() <= INVITE_REACTIVATION_WINDOW_MS
}
export function canReactivateInvite(
invite: { status: string | null | undefined; revokedAt: Date | string | null | undefined },
reference: Date = new Date()
) {
if ((invite.status ?? "").toLowerCase() !== "revoked") return false
return isWithinInviteReactivationWindow(invite.revokedAt, reference)
}