Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ out/

# Build outputs
dist/
build/

# Environment variables
.env
.env.local
.env.*.local
.env.production

# TypeScript
*.tsbuildinfo
Expand All @@ -25,6 +27,7 @@ playwright-report/
test-results/
junit.xml
*.lcov
.vitest-cache/

# OS files
.DS_Store
Expand All @@ -35,14 +38,19 @@ Thumbs.db
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*

# Editor
# ── Editor ────────────────────────────────────────────────────────────────
.vscode/
.idea/
*.swp
*.swo
*.suo
*.ntvs*
*.njsproj
*.sln

# Misc
# ── Misc ──────────────────────────────────────────────────────────────────
.turbo/
.vercel/
.cache/
Expand Down
99 changes: 99 additions & 0 deletions src/app/api/audit/compliance/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { NextRequest, NextResponse } from 'next/server';
export const dynamic = 'force-dynamic';
import {
filterAuditEntries,
generateComplianceReport,
computeAuditAnalytics,
enforceRetentionPolicy,
searchAuditEntries,
auditEntriesToCsv,
auditEntriesToJson,
type AuditEntry,
type RetentionPolicy,
} from '@/lib/auditLog';

// In-memory audit store for server-side events
const serverAuditLog: AuditEntry[] = [];

export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const action = searchParams.get('action') ?? 'list';

if (action === 'list') {
const q = searchParams.get('q') ?? '';
const category = searchParams.get('category') as AuditEntry['category'] | 'all' | null;
const from = searchParams.get('from') ? Number(searchParams.get('from')) : undefined;
const to = searchParams.get('to') ? Number(searchParams.get('to')) : undefined;
const filtered = filterAuditEntries(serverAuditLog, { query: q, category: category ?? 'all', from, to });
return NextResponse.json({ entries: filtered, total: filtered.length });
}

if (action === 'analytics') {
const analytics = computeAuditAnalytics(serverAuditLog);
return NextResponse.json({ analytics });
}

if (action === 'report') {
const from = Number(searchParams.get('from') ?? Date.now() - 30 * 24 * 60 * 60 * 1000);
const to = Number(searchParams.get('to') ?? Date.now());
const policy = (searchParams.get('retention') ?? '1y') as RetentionPolicy;
const report = generateComplianceReport(serverAuditLog, from, to, policy);
return NextResponse.json({ report });
}

if (action === 'search') {
const q = searchParams.get('q') ?? '';
return NextResponse.json({ entries: searchAuditEntries(serverAuditLog, q) });
}

if (action === 'export') {
const format = searchParams.get('format') ?? 'csv';
const all = filterAuditEntries(serverAuditLog);
if (format === 'json') {
return new Response(auditEntriesToJson(all), {
headers: { 'Content-Type': 'application/json', 'Content-Disposition': 'attachment; filename="audit-log.json"' },
});
}
return new Response(auditEntriesToCsv(all), {
headers: { 'Content-Type': 'text/csv', 'Content-Disposition': 'attachment; filename="audit-log.csv"' },
});
}

return NextResponse.json({ error: 'Unknown action' }, { status: 400 });
}

export async function POST(request: NextRequest) {
const body = await request.json().catch(() => null);
if (!body) return NextResponse.json({ error: 'Invalid JSON' }, { status: 400 });

const { action } = body;

if (action === 'log') {
const entry = body.entry as Partial<AuditEntry> | undefined;
if (!entry?.id || !entry?.actor || !entry?.action || !entry?.category) {
return NextResponse.json({ error: 'entry.id, actor, action and category are required' }, { status: 400 });
}
const full: AuditEntry = {
id: entry.id,
timestamp: entry.timestamp ?? Date.now(),
actor: entry.actor,
action: entry.action,
category: entry.category,
target: entry.target,
details: entry.details,
};
serverAuditLog.push(full);
return NextResponse.json({ logged: true, id: full.id });
}

if (action === 'enforce_retention') {
const policy = (body.policy ?? '1y') as RetentionPolicy;
const before = serverAuditLog.length;
const kept = enforceRetentionPolicy(serverAuditLog, policy);
serverAuditLog.length = 0;
serverAuditLog.push(...kept);
return NextResponse.json({ before, after: serverAuditLog.length, removed: before - serverAuditLog.length });
}

return NextResponse.json({ error: 'Unknown action' }, { status: 400 });
}
85 changes: 85 additions & 0 deletions src/app/api/notifications/mobile/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { NextRequest, NextResponse } from 'next/server';
export const dynamic = 'force-dynamic';
import {
sendMobileNotification,
scheduleNotification,
getPendingSchedules,
getDueSchedules,
getBadgeCount,
updateBadgeCount,
trackNotificationEvent,
getNotificationAnalytics,
getAnalyticsSummary,
type MobileNotificationPayload,
type NotificationSchedule,
} from '@/lib/mobileNotifications';

export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const action = searchParams.get('action');

if (action === 'schedules') {
const type = searchParams.get('type') ?? 'pending';
const data = type === 'due' ? getDueSchedules() : getPendingSchedules();
return NextResponse.json({ schedules: data });
}

if (action === 'badge') {
const userId = searchParams.get('userId');
if (!userId) return NextResponse.json({ error: 'userId required' }, { status: 400 });
return NextResponse.json({ badge: getBadgeCount(userId) });
}

if (action === 'analytics') {
const notificationId = searchParams.get('notificationId') ?? undefined;
if (notificationId) {
return NextResponse.json({ events: getNotificationAnalytics(notificationId) });
}
return NextResponse.json({ summary: getAnalyticsSummary() });
}

return NextResponse.json({ error: 'Unknown action' }, { status: 400 });
}

export async function POST(request: NextRequest) {
const body = await request.json().catch(() => null);
if (!body) return NextResponse.json({ error: 'Invalid JSON' }, { status: 400 });

const { action } = body;

if (action === 'send') {
const payload = body.payload as MobileNotificationPayload | undefined;
if (!payload?.id || !payload?.title || !payload?.body) {
return NextResponse.json({ error: 'payload.id, title and body are required' }, { status: 400 });
}
const result = await sendMobileNotification(payload);
return NextResponse.json(result);
}

if (action === 'schedule') {
const schedule = body.schedule as NotificationSchedule | undefined;
if (!schedule?.id || !schedule?.payload || !schedule?.scheduledAt) {
return NextResponse.json({ error: 'schedule.id, payload and scheduledAt are required' }, { status: 400 });
}
scheduleNotification(schedule);
return NextResponse.json({ scheduled: true });
}

if (action === 'badge') {
const { userId, counts } = body;
if (!userId) return NextResponse.json({ error: 'userId required' }, { status: 400 });
const badge = updateBadgeCount(userId, counts ?? {});
return NextResponse.json({ badge });
}

if (action === 'analytics') {
const { notificationId, event, deepLink } = body;
if (!notificationId || !event) {
return NextResponse.json({ error: 'notificationId and event are required' }, { status: 400 });
}
trackNotificationEvent({ notificationId, event, timestamp: Date.now(), deepLink });
return NextResponse.json({ tracked: true });
}

return NextResponse.json({ error: 'Unknown action' }, { status: 400 });
}
69 changes: 69 additions & 0 deletions src/app/api/security/encryption/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { NextRequest, NextResponse } from 'next/server';
export const dynamic = 'force-dynamic';
import {
encryptData,
decryptData,
hashData,
KeyRotationManager,
EncryptionValidator,
defaultEncryptionConfig,
} from '@/lib/encryption';

const sharedKeyManager = new KeyRotationManager();

export async function POST(request: NextRequest) {
const body = await request.json().catch(() => null);
if (!body) return NextResponse.json({ error: 'Invalid JSON' }, { status: 400 });

const { action, data, password, encryptedData } = body;

if (action === 'encrypt') {
if (typeof data !== 'string' || typeof password !== 'string') {
return NextResponse.json({ error: 'data and password are required strings' }, { status: 400 });
}
if (!EncryptionValidator.validatePassword(password)) {
return NextResponse.json({ error: 'Password must be at least 12 characters' }, { status: 400 });
}
const result = encryptData(data, password);
return NextResponse.json({ encrypted: result });
}

if (action === 'decrypt') {
if (!encryptedData || typeof password !== 'string') {
return NextResponse.json({ error: 'encryptedData and password are required' }, { status: 400 });
}
if (!EncryptionValidator.validateEncryptedData(encryptedData)) {
return NextResponse.json({ error: 'Invalid encrypted data format' }, { status: 400 });
}
try {
const decrypted = decryptData(encryptedData, password);
return NextResponse.json({ decrypted });
} catch {
return NextResponse.json({ error: 'Decryption failed' }, { status: 400 });
}
}

if (action === 'hash') {
if (typeof data !== 'string') {
return NextResponse.json({ error: 'data is required' }, { status: 400 });
}
return NextResponse.json({ hash: hashData(data) });
}

if (action === 'rotate') {
const newKeyId = sharedKeyManager.rotateKey();
return NextResponse.json({ keyId: newKeyId, rotatedAt: Date.now() });
}

if (action === 'audit') {
const config = body.config ?? defaultEncryptionConfig;
const valid = EncryptionValidator.validateEncryptionConfig(config);
return NextResponse.json({
valid,
shouldRotate: sharedKeyManager.shouldRotate(),
currentKeyId: sharedKeyManager.getCurrentKeyId(),
});
}

return NextResponse.json({ error: 'Unknown action' }, { status: 400 });
}
11 changes: 11 additions & 0 deletions src/app/invoice/mobile/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import MobileInvoiceWizard from '@/components/MobileInvoiceWizard';

export const metadata = { title: 'Create Invoice – Mobile' };

export default function MobileInvoicePage() {
return (
<main className="min-h-screen bg-gray-950 text-white py-6">
<MobileInvoiceWizard />
</main>
);
}
Loading