Initial commit: Port Nimara CRM (Layers 0-4)
Full CRM rebuild with Next.js 15, TypeScript, Tailwind, Drizzle ORM,
PostgreSQL, Redis, BullMQ, MinIO, and Socket.io. Includes 461 source
files covering clients, berths, interests/pipeline, documents/EOI,
expenses/invoices, email, notifications, dashboard, admin, and
client portal. CI/CD via Gitea Actions with Docker builds.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 11:52:51 +01:00
|
|
|
import { NextResponse } from 'next/server';
|
|
|
|
|
|
|
|
|
|
import { withAuth, withPermission } from '@/lib/api/helpers';
|
|
|
|
|
import { errorResponse, ValidationError } from '@/lib/errors';
|
|
|
|
|
import { uploadFile } from '@/lib/services/files';
|
|
|
|
|
import { uploadFileSchema } from '@/lib/validators/files';
|
|
|
|
|
|
|
|
|
|
export const POST = withAuth(
|
2026-05-05 18:33:13 +02:00
|
|
|
withPermission('files', 'upload', async (req, ctx) => {
|
Initial commit: Port Nimara CRM (Layers 0-4)
Full CRM rebuild with Next.js 15, TypeScript, Tailwind, Drizzle ORM,
PostgreSQL, Redis, BullMQ, MinIO, and Socket.io. Includes 461 source
files covering clients, berths, interests/pipeline, documents/EOI,
expenses/invoices, email, notifications, dashboard, admin, and
client portal. CI/CD via Gitea Actions with Docker builds.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 11:52:51 +01:00
|
|
|
try {
|
|
|
|
|
const formData = await req.formData();
|
|
|
|
|
const file = formData.get('file') as File | null;
|
|
|
|
|
|
|
|
|
|
if (!file) {
|
|
|
|
|
throw new ValidationError('No file provided');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const buffer = Buffer.from(await file.arrayBuffer());
|
|
|
|
|
|
2026-05-15 01:12:20 +02:00
|
|
|
// A16: FormData.get returns null when the field is absent, not
|
|
|
|
|
// undefined. Zod's .optional() accepts undefined but rejects null,
|
|
|
|
|
// so the previous `as string | undefined` cast lied and uploads at
|
|
|
|
|
// the hub root (no entity selected) 400'd. Coerce absent / empty
|
|
|
|
|
// values to undefined before parse.
|
|
|
|
|
const formStr = (key: string): string | undefined => {
|
|
|
|
|
const v = formData.get(key);
|
|
|
|
|
return typeof v === 'string' && v.length > 0 ? v : undefined;
|
|
|
|
|
};
|
Initial commit: Port Nimara CRM (Layers 0-4)
Full CRM rebuild with Next.js 15, TypeScript, Tailwind, Drizzle ORM,
PostgreSQL, Redis, BullMQ, MinIO, and Socket.io. Includes 461 source
files covering clients, berths, interests/pipeline, documents/EOI,
expenses/invoices, email, notifications, dashboard, admin, and
client portal. CI/CD via Gitea Actions with Docker builds.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 11:52:51 +01:00
|
|
|
const metadata = uploadFileSchema.parse({
|
2026-05-15 01:12:20 +02:00
|
|
|
filename: formStr('filename') ?? file.name,
|
|
|
|
|
clientId: formStr('clientId'),
|
|
|
|
|
yachtId: formStr('yachtId'),
|
|
|
|
|
companyId: formStr('companyId'),
|
|
|
|
|
category: formStr('category'),
|
|
|
|
|
entityType: formStr('entityType'),
|
|
|
|
|
entityId: formStr('entityId'),
|
|
|
|
|
folderId: formStr('folderId'),
|
Initial commit: Port Nimara CRM (Layers 0-4)
Full CRM rebuild with Next.js 15, TypeScript, Tailwind, Drizzle ORM,
PostgreSQL, Redis, BullMQ, MinIO, and Socket.io. Includes 461 source
files covering clients, berths, interests/pipeline, documents/EOI,
expenses/invoices, email, notifications, dashboard, admin, and
client portal. CI/CD via Gitea Actions with Docker builds.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 11:52:51 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = await uploadFile(
|
|
|
|
|
ctx.portId,
|
|
|
|
|
ctx.portSlug,
|
|
|
|
|
{
|
|
|
|
|
buffer,
|
|
|
|
|
originalName: file.name,
|
|
|
|
|
mimeType: file.type,
|
|
|
|
|
size: file.size,
|
|
|
|
|
},
|
|
|
|
|
metadata,
|
|
|
|
|
{
|
|
|
|
|
userId: ctx.userId,
|
|
|
|
|
portId: ctx.portId,
|
|
|
|
|
ipAddress: ctx.ipAddress,
|
|
|
|
|
userAgent: ctx.userAgent,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return NextResponse.json({ data: result }, { status: 201 });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return errorResponse(error);
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
);
|