feat: integrate NAS file operations with project CRUD
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import prisma from '../config/database';
|
||||
import { generateSharedNumber } from './numbering.service';
|
||||
import { NasFileManager } from './nas-file-manager';
|
||||
|
||||
const nasFileManager = new NasFileManager();
|
||||
|
||||
const ALLOWED_SORT_FIELDS = ['id', 'project_number', 'name', 'status', 'created_at'];
|
||||
|
||||
@@ -50,7 +53,11 @@ export async function getProject(id: number) {
|
||||
where: { id },
|
||||
include: { customers: true, users: true, quotations: true, orders: true, project_notes: { orderBy: { created_at: 'desc' } } },
|
||||
});
|
||||
return project || null;
|
||||
if (!project) return null;
|
||||
return {
|
||||
...project,
|
||||
has_nas_folder: project.project_number ? nasFileManager.projectFolderExists(project.project_number) : false,
|
||||
};
|
||||
}
|
||||
|
||||
export async function createProject(body: Record<string, any>) {
|
||||
@@ -68,6 +75,11 @@ export async function createProject(body: Record<string, any>) {
|
||||
notes: body.notes ? String(body.notes) : null,
|
||||
},
|
||||
});
|
||||
|
||||
if (project.project_number && nasFileManager.isConfigured()) {
|
||||
nasFileManager.createProjectFolder(project.project_number, project.name || '');
|
||||
}
|
||||
|
||||
return project;
|
||||
}
|
||||
|
||||
@@ -86,14 +98,23 @@ export async function updateProject(id: number, body: Record<string, any>) {
|
||||
if (body.end_date !== undefined) data.end_date = body.end_date ? new Date(String(body.end_date)) : null;
|
||||
|
||||
await prisma.projects.update({ where: { id }, data });
|
||||
|
||||
if (existing.name !== data.name && existing.project_number && nasFileManager.isConfigured()) {
|
||||
nasFileManager.renameProjectFolder(existing.project_number, String(data.name || ''));
|
||||
}
|
||||
|
||||
return existing;
|
||||
}
|
||||
|
||||
export async function deleteProject(id: number) {
|
||||
export async function deleteProject(id: number, deleteFiles: boolean = false) {
|
||||
const existing = await prisma.projects.findUnique({ where: { id } });
|
||||
if (!existing) return { error: 'not_found' as const };
|
||||
if (existing.order_id) return { error: 'has_order' as const };
|
||||
|
||||
if (deleteFiles && existing.project_number && nasFileManager.isConfigured()) {
|
||||
await nasFileManager.deleteProjectFolder(existing.project_number);
|
||||
}
|
||||
|
||||
await prisma.projects.delete({ where: { id } });
|
||||
return existing;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user