mirror of
https://github.com/johndoe6345789/metabuilder.git
synced 2026-04-25 06:14:59 +00:00
code: dbal,sessions,session (2 files)
This commit is contained in:
@@ -2,33 +2,36 @@
|
||||
* @file extend-session.ts
|
||||
* @description Extend session expiration operation
|
||||
*/
|
||||
import type { Session, Result } from '../types';
|
||||
import type { InMemoryStore } from '../store/in-memory-store';
|
||||
import type { Result, Session } from '../../types'
|
||||
import type { InMemoryStore } from '../../store/in-memory-store'
|
||||
import { validateId } from '../../validation/validate-id'
|
||||
|
||||
/**
|
||||
* Extend a session's expiration time
|
||||
*/
|
||||
export async function extendSession(
|
||||
export const extendSession = async (
|
||||
store: InMemoryStore,
|
||||
id: string,
|
||||
additionalSeconds: number
|
||||
): Promise<Result<Session>> {
|
||||
if (!id) {
|
||||
return { success: false, error: { code: 'VALIDATION_ERROR', message: 'ID required' } };
|
||||
}
|
||||
if (additionalSeconds <= 0) {
|
||||
return { success: false, error: { code: 'VALIDATION_ERROR', message: 'Additional seconds must be positive' } };
|
||||
): Promise<Result<Session>> => {
|
||||
const idErrors = validateId(id)
|
||||
if (idErrors.length > 0) {
|
||||
return { success: false, error: { code: 'VALIDATION_ERROR', message: idErrors[0] } }
|
||||
}
|
||||
|
||||
const session = store.sessions.get(id);
|
||||
if (additionalSeconds <= 0) {
|
||||
return { success: false, error: { code: 'VALIDATION_ERROR', message: 'Additional seconds must be positive' } }
|
||||
}
|
||||
|
||||
const session = store.sessions.get(id)
|
||||
if (!session) {
|
||||
return { success: false, error: { code: 'NOT_FOUND', message: `Session not found: ${id}` } };
|
||||
return { success: false, error: { code: 'NOT_FOUND', message: `Session not found: ${id}` } }
|
||||
}
|
||||
|
||||
if (session.expiresAt < new Date()) {
|
||||
return { success: false, error: { code: 'VALIDATION_ERROR', message: 'Cannot extend expired session' } };
|
||||
return { success: false, error: { code: 'VALIDATION_ERROR', message: 'Cannot extend expired session' } }
|
||||
}
|
||||
|
||||
session.expiresAt = new Date(session.expiresAt.getTime() + additionalSeconds * 1000);
|
||||
return { success: true, data: session };
|
||||
session.expiresAt = new Date(session.expiresAt.getTime() + additionalSeconds * 1000)
|
||||
return { success: true, data: session }
|
||||
}
|
||||
|
||||
@@ -1,36 +1,44 @@
|
||||
/**
|
||||
* @file list-sessions.ts
|
||||
* @description List sessions with filtering
|
||||
* @description List sessions with filtering and pagination
|
||||
*/
|
||||
import type { Session, ListOptions, Result } from '../types';
|
||||
import type { InMemoryStore } from '../store/in-memory-store';
|
||||
import type { ListOptions, Result, Session } from '../../types'
|
||||
import type { InMemoryStore } from '../../store/in-memory-store'
|
||||
import { cleanExpiredSessions } from './clean-expired'
|
||||
|
||||
/**
|
||||
* List sessions with filtering and pagination
|
||||
*/
|
||||
export async function listSessions(
|
||||
export const listSessions = async (
|
||||
store: InMemoryStore,
|
||||
options: ListOptions = {}
|
||||
): Promise<Result<Session[]>> {
|
||||
const { filter = {}, page = 1, limit = 20 } = options;
|
||||
const now = new Date();
|
||||
): Promise<Result<Session[]>> => {
|
||||
await cleanExpiredSessions(store)
|
||||
|
||||
let sessions = Array.from(store.sessions.values());
|
||||
const { filter = {}, sort = {}, page = 1, limit = 20 } = options
|
||||
|
||||
let sessions = Array.from(store.sessions.values())
|
||||
|
||||
// Apply filters
|
||||
if (filter.userId !== undefined) {
|
||||
sessions = sessions.filter((s) => s.userId === filter.userId);
|
||||
}
|
||||
if (filter.activeOnly) {
|
||||
sessions = sessions.filter((s) => s.expiresAt > now);
|
||||
sessions = sessions.filter((session) => session.userId === filter.userId)
|
||||
}
|
||||
|
||||
// Sort by created_at descending (newest first)
|
||||
sessions.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
||||
if (filter.token !== undefined) {
|
||||
sessions = sessions.filter((session) => session.token === filter.token)
|
||||
}
|
||||
|
||||
// Apply pagination
|
||||
const start = (page - 1) * limit;
|
||||
const paginated = sessions.slice(start, start + limit);
|
||||
if (sort.createdAt) {
|
||||
sessions.sort((a, b) =>
|
||||
sort.createdAt === 'asc' ? a.createdAt.getTime() - b.createdAt.getTime() : b.createdAt.getTime() - a.createdAt.getTime()
|
||||
)
|
||||
} else if (sort.expiresAt) {
|
||||
sessions.sort((a, b) =>
|
||||
sort.expiresAt === 'asc' ? a.expiresAt.getTime() - b.expiresAt.getTime() : b.expiresAt.getTime() - a.expiresAt.getTime()
|
||||
)
|
||||
}
|
||||
|
||||
return { success: true, data: paginated };
|
||||
const start = (page - 1) * limit
|
||||
const paginated = sessions.slice(start, start + limit)
|
||||
|
||||
return { success: true, data: paginated }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user