task-p.6 migrate
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { downloadActiveDocumentLibraryVersion } from '@/features/foundation/document-library/server/service';
|
||||
import { PERMISSIONS } from '@/lib/auth/rbac';
|
||||
import { AuthError, requireOrganizationAccess } from '@/lib/auth/session';
|
||||
|
||||
type Params = { params: Promise<{ id: string }> };
|
||||
|
||||
export async function GET(_request: NextRequest, { params }: Params) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const { organization, session } = await requireOrganizationAccess({
|
||||
permission: PERMISSIONS.crmDocumentLibraryDownload
|
||||
});
|
||||
|
||||
const { object } = await downloadActiveDocumentLibraryVersion({
|
||||
libraryId: id,
|
||||
organizationId: organization.id,
|
||||
userId: session.user.id
|
||||
});
|
||||
|
||||
return new NextResponse(object.body, {
|
||||
headers: {
|
||||
'Content-Type': object.contentType,
|
||||
'Content-Disposition': `attachment; filename="${object.fileName}"`
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
return NextResponse.json({ message: error.message }, { status: error.status });
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ message: 'Unable to download active document version' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { previewActiveDocumentLibraryVersion } from '@/features/foundation/document-library/server/service';
|
||||
import { PERMISSIONS } from '@/lib/auth/rbac';
|
||||
import { AuthError, requireOrganizationAccess } from '@/lib/auth/session';
|
||||
|
||||
type Params = { params: Promise<{ id: string }> };
|
||||
|
||||
export async function GET(_request: NextRequest, { params }: Params) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const { organization, session } = await requireOrganizationAccess({
|
||||
permission: PERMISSIONS.crmDocumentLibraryRead
|
||||
});
|
||||
|
||||
const { object } = await previewActiveDocumentLibraryVersion({
|
||||
libraryId: id,
|
||||
organizationId: organization.id,
|
||||
userId: session.user.id
|
||||
});
|
||||
|
||||
return new NextResponse(object.body, {
|
||||
headers: {
|
||||
'Content-Type': object.contentType,
|
||||
'Content-Disposition': `inline; filename="${object.fileName}"`
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
return NextResponse.json({ message: error.message }, { status: error.status });
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ message: 'Unable to preview active document version' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
92
src/app/api/crm/settings/document-library/[id]/route.ts
Normal file
92
src/app/api/crm/settings/document-library/[id]/route.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { z } from 'zod';
|
||||
import {
|
||||
getDocumentLibraryById,
|
||||
updateDocumentLibrary
|
||||
} from '@/features/foundation/document-library/server/service';
|
||||
import {
|
||||
DOCUMENT_LIBRARY_BRANDS,
|
||||
DOCUMENT_LIBRARY_LANGUAGES,
|
||||
DOCUMENT_LIBRARY_PRODUCT_TYPES,
|
||||
DOCUMENT_LIBRARY_TYPES
|
||||
} from '@/features/foundation/document-library/types';
|
||||
import { PERMISSIONS } from '@/lib/auth/rbac';
|
||||
import { AuthError, requireOrganizationAccess } from '@/lib/auth/session';
|
||||
|
||||
type Params = { params: Promise<{ id: string }> };
|
||||
|
||||
const updateDocumentLibrarySchema = z.object({
|
||||
code: z.string().min(1).optional(),
|
||||
name: z.string().min(1).optional(),
|
||||
documentType: z.enum(DOCUMENT_LIBRARY_TYPES).optional(),
|
||||
description: z.string().optional().nullable(),
|
||||
brand: z.enum(DOCUMENT_LIBRARY_BRANDS).optional(),
|
||||
language: z.enum(DOCUMENT_LIBRARY_LANGUAGES).optional(),
|
||||
productType: z.enum(DOCUMENT_LIBRARY_PRODUCT_TYPES).optional(),
|
||||
isActive: z.boolean().optional()
|
||||
});
|
||||
|
||||
export async function GET(_request: NextRequest, { params }: Params) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const { organization } = await requireOrganizationAccess({
|
||||
permission: PERMISSIONS.crmDocumentLibraryRead
|
||||
});
|
||||
const library = await getDocumentLibraryById(id, organization.id);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
time: new Date().toISOString(),
|
||||
message: 'Document library item loaded successfully',
|
||||
library
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
return NextResponse.json({ message: error.message }, { status: error.status });
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
return NextResponse.json({ message: error.message }, { status: 400 });
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ message: 'Unable to load document library item' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function PATCH(request: NextRequest, { params }: Params) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const { organization, session } = await requireOrganizationAccess({
|
||||
permission: PERMISSIONS.crmDocumentLibraryUpdate
|
||||
});
|
||||
const payload = updateDocumentLibrarySchema.parse(await request.json());
|
||||
const library = await updateDocumentLibrary({
|
||||
id,
|
||||
organizationId: organization.id,
|
||||
userId: session.user.id,
|
||||
values: payload
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Document library item updated successfully',
|
||||
library
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
return NextResponse.json({ message: error.message }, { status: error.status });
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
return NextResponse.json({ message: error.message }, { status: 400 });
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ message: 'Unable to update document library item' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { uploadDocumentLibraryVersion } from '@/features/foundation/document-library/server/service';
|
||||
import { PERMISSIONS } from '@/lib/auth/rbac';
|
||||
import { AuthError, requireOrganizationAccess } from '@/lib/auth/session';
|
||||
|
||||
type Params = { params: Promise<{ id: string }> };
|
||||
|
||||
export async function POST(request: NextRequest, { params }: Params) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const { organization, session } = await requireOrganizationAccess({
|
||||
permission: PERMISSIONS.crmDocumentLibraryUpload
|
||||
});
|
||||
|
||||
const formData = await request.formData();
|
||||
const version = formData.get('version');
|
||||
const file = formData.get('file');
|
||||
|
||||
if (typeof version !== 'string' || !version.trim()) {
|
||||
return NextResponse.json({ message: 'Version is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
if (!(file instanceof File)) {
|
||||
return NextResponse.json({ message: 'PDF file is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
const created = await uploadDocumentLibraryVersion({
|
||||
libraryId: id,
|
||||
organizationId: organization.id,
|
||||
userId: session.user.id,
|
||||
version,
|
||||
file: {
|
||||
name: file.name,
|
||||
type: file.type,
|
||||
size: file.size,
|
||||
buffer: Buffer.from(await file.arrayBuffer())
|
||||
}
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Document library version uploaded successfully',
|
||||
version: created
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
return NextResponse.json({ message: error.message }, { status: error.status });
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
return NextResponse.json({ message: error.message }, { status: 400 });
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ message: 'Unable to upload document library version' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
102
src/app/api/crm/settings/document-library/route.ts
Normal file
102
src/app/api/crm/settings/document-library/route.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { z } from 'zod';
|
||||
import {
|
||||
createDocumentLibrary,
|
||||
listDocumentLibraries
|
||||
} from '@/features/foundation/document-library/server/service';
|
||||
import {
|
||||
DOCUMENT_LIBRARY_BRANDS,
|
||||
DOCUMENT_LIBRARY_LANGUAGES,
|
||||
DOCUMENT_LIBRARY_PRODUCT_TYPES,
|
||||
DOCUMENT_LIBRARY_TYPES
|
||||
} from '@/features/foundation/document-library/types';
|
||||
import { PERMISSIONS } from '@/lib/auth/rbac';
|
||||
import { AuthError, requireOrganizationAccess } from '@/lib/auth/session';
|
||||
|
||||
const documentLibrarySchema = z.object({
|
||||
code: z.string().min(1),
|
||||
name: z.string().min(1),
|
||||
documentType: z.enum(DOCUMENT_LIBRARY_TYPES),
|
||||
description: z.string().optional().nullable(),
|
||||
brand: z.enum(DOCUMENT_LIBRARY_BRANDS),
|
||||
language: z.enum(DOCUMENT_LIBRARY_LANGUAGES),
|
||||
productType: z.enum(DOCUMENT_LIBRARY_PRODUCT_TYPES),
|
||||
isActive: z.boolean().optional()
|
||||
});
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { organization } = await requireOrganizationAccess({
|
||||
permission: PERMISSIONS.crmDocumentLibraryRead
|
||||
});
|
||||
|
||||
const { searchParams } = request.nextUrl;
|
||||
const isActiveParam = searchParams.get('isActive');
|
||||
const items = await listDocumentLibraries(organization.id, {
|
||||
documentType:
|
||||
(searchParams.get('documentType') as (typeof DOCUMENT_LIBRARY_TYPES)[number] | null) ??
|
||||
undefined,
|
||||
brand:
|
||||
(searchParams.get('brand') as (typeof DOCUMENT_LIBRARY_BRANDS)[number] | null) ??
|
||||
undefined,
|
||||
language:
|
||||
(searchParams.get('language') as (typeof DOCUMENT_LIBRARY_LANGUAGES)[number] | null) ??
|
||||
undefined,
|
||||
productType:
|
||||
(searchParams.get('productType') as
|
||||
| (typeof DOCUMENT_LIBRARY_PRODUCT_TYPES)[number]
|
||||
| null) ?? undefined,
|
||||
isActive: isActiveParam === 'true' || isActiveParam === 'false' ? isActiveParam : undefined
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
time: new Date().toISOString(),
|
||||
message: 'Document library loaded successfully',
|
||||
items
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
return NextResponse.json({ message: error.message }, { status: error.status });
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
return NextResponse.json({ message: error.message }, { status: 400 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ message: 'Unable to load document library' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const { organization, session } = await requireOrganizationAccess({
|
||||
permission: PERMISSIONS.crmDocumentLibraryCreate
|
||||
});
|
||||
const payload = documentLibrarySchema.parse(await request.json());
|
||||
const library = await createDocumentLibrary({
|
||||
organizationId: organization.id,
|
||||
userId: session.user.id,
|
||||
values: payload
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Document library item created successfully',
|
||||
library
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
return NextResponse.json({ message: error.message }, { status: error.status });
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
return NextResponse.json({ message: error.message }, { status: 400 });
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ message: 'Unable to create document library item' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { activateDocumentLibraryVersion } from '@/features/foundation/document-library/server/service';
|
||||
import { PERMISSIONS } from '@/lib/auth/rbac';
|
||||
import { AuthError, requireOrganizationAccess } from '@/lib/auth/session';
|
||||
|
||||
type Params = { params: Promise<{ id: string }> };
|
||||
|
||||
export async function POST(_request: NextRequest, { params }: Params) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const { organization, session } = await requireOrganizationAccess({
|
||||
permission: PERMISSIONS.crmDocumentLibraryActivate
|
||||
});
|
||||
const version = await activateDocumentLibraryVersion({
|
||||
versionId: id,
|
||||
organizationId: organization.id,
|
||||
userId: session.user.id
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Document library version activated successfully',
|
||||
version
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
return NextResponse.json({ message: error.message }, { status: error.status });
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
return NextResponse.json({ message: error.message }, { status: 400 });
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ message: 'Unable to activate document library version' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { archiveDocumentLibraryVersion } from '@/features/foundation/document-library/server/service';
|
||||
import { PERMISSIONS } from '@/lib/auth/rbac';
|
||||
import { AuthError, requireOrganizationAccess } from '@/lib/auth/session';
|
||||
|
||||
type Params = { params: Promise<{ id: string }> };
|
||||
|
||||
export async function POST(_request: NextRequest, { params }: Params) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const { organization, session } = await requireOrganizationAccess({
|
||||
permission: PERMISSIONS.crmDocumentLibraryArchive
|
||||
});
|
||||
const version = await archiveDocumentLibraryVersion({
|
||||
versionId: id,
|
||||
organizationId: organization.id,
|
||||
userId: session.user.id
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Document library version archived successfully',
|
||||
version
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
return NextResponse.json({ message: error.message }, { status: error.status });
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
return NextResponse.json({ message: error.message }, { status: 400 });
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ message: 'Unable to archive document library version' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { downloadDocumentLibraryVersion } from '@/features/foundation/document-library/server/service';
|
||||
import { PERMISSIONS } from '@/lib/auth/rbac';
|
||||
import { AuthError, requireOrganizationAccess } from '@/lib/auth/session';
|
||||
|
||||
type Params = { params: Promise<{ id: string }> };
|
||||
|
||||
export async function GET(_request: NextRequest, { params }: Params) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const { organization, session } = await requireOrganizationAccess({
|
||||
permission: PERMISSIONS.crmDocumentLibraryDownload
|
||||
});
|
||||
const { object } = await downloadDocumentLibraryVersion({
|
||||
versionId: id,
|
||||
organizationId: organization.id,
|
||||
userId: session.user.id
|
||||
});
|
||||
|
||||
return new NextResponse(object.body, {
|
||||
headers: {
|
||||
'Content-Type': object.contentType,
|
||||
'Content-Disposition': `attachment; filename="${object.fileName}"`
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
return NextResponse.json({ message: error.message }, { status: error.status });
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ message: 'Unable to download document library version' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { previewDocumentLibraryVersion } from '@/features/foundation/document-library/server/service';
|
||||
import { PERMISSIONS } from '@/lib/auth/rbac';
|
||||
import { AuthError, requireOrganizationAccess } from '@/lib/auth/session';
|
||||
|
||||
type Params = { params: Promise<{ id: string }> };
|
||||
|
||||
export async function GET(_request: NextRequest, { params }: Params) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const { organization, session } = await requireOrganizationAccess({
|
||||
permission: PERMISSIONS.crmDocumentLibraryRead
|
||||
});
|
||||
const { object } = await previewDocumentLibraryVersion({
|
||||
versionId: id,
|
||||
organizationId: organization.id,
|
||||
userId: session.user.id
|
||||
});
|
||||
|
||||
return new NextResponse(object.body, {
|
||||
headers: {
|
||||
'Content-Type': object.contentType,
|
||||
'Content-Disposition': `inline; filename="${object.fileName}"`
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
return NextResponse.json({ message: error.message }, { status: error.status });
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ message: 'Unable to preview document library version' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { publishDocumentLibraryVersion } from '@/features/foundation/document-library/server/service';
|
||||
import { PERMISSIONS } from '@/lib/auth/rbac';
|
||||
import { AuthError, requireOrganizationAccess } from '@/lib/auth/session';
|
||||
|
||||
type Params = { params: Promise<{ id: string }> };
|
||||
|
||||
export async function POST(_request: NextRequest, { params }: Params) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const { organization, session } = await requireOrganizationAccess({
|
||||
permission: PERMISSIONS.crmDocumentLibraryPublish
|
||||
});
|
||||
const version = await publishDocumentLibraryVersion({
|
||||
versionId: id,
|
||||
organizationId: organization.id,
|
||||
userId: session.user.id
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Document library version published successfully',
|
||||
version
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
return NextResponse.json({ message: error.message }, { status: error.status });
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
return NextResponse.json({ message: error.message }, { status: 400 });
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ message: 'Unable to publish document library version' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { deleteDraftDocumentLibraryVersion } from '@/features/foundation/document-library/server/service';
|
||||
import { PERMISSIONS } from '@/lib/auth/rbac';
|
||||
import { AuthError, requireOrganizationAccess } from '@/lib/auth/session';
|
||||
|
||||
type Params = { params: Promise<{ id: string }> };
|
||||
|
||||
export async function DELETE(_request: NextRequest, { params }: Params) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const { organization, session } = await requireOrganizationAccess({
|
||||
permission: PERMISSIONS.crmDocumentLibraryUpload
|
||||
});
|
||||
const version = await deleteDraftDocumentLibraryVersion({
|
||||
versionId: id,
|
||||
organizationId: organization.id,
|
||||
userId: session.user.id
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Draft document library version deleted successfully',
|
||||
version
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
return NextResponse.json({ message: error.message }, { status: error.status });
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
return NextResponse.json({ message: error.message }, { status: 400 });
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ message: 'Unable to delete draft document library version' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user