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:
@@ -97,7 +97,9 @@ export default async function projectsRoutes(fastify: FastifyInstance): Promise<
|
|||||||
const id = parseId(request.params.id, reply);
|
const id = parseId(request.params.id, reply);
|
||||||
if (id === null) return;
|
if (id === null) return;
|
||||||
|
|
||||||
const result = await deleteProject(id);
|
const body = request.body as Record<string, unknown>;
|
||||||
|
const deleteFiles = !!body?.delete_files;
|
||||||
|
const result = await deleteProject(id, deleteFiles);
|
||||||
if (result && 'error' in result) {
|
if (result && 'error' in result) {
|
||||||
if (result.error === 'not_found') return error(reply, 'Projekt nenalezen', 404);
|
if (result.error === 'not_found') return error(reply, 'Projekt nenalezen', 404);
|
||||||
if (result.error === 'has_order') return error(reply, 'Nelze smazat projekt propojený s objednávkou. Nejdříve smažte objednávku.', 400);
|
if (result.error === 'has_order') return error(reply, 'Nelze smazat projekt propojený s objednávkou. Nejdříve smažte objednávku.', 400);
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
import prisma from '../config/database';
|
import prisma from '../config/database';
|
||||||
import { generateSharedNumber } from './numbering.service';
|
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'];
|
const ALLOWED_SORT_FIELDS = ['id', 'project_number', 'name', 'status', 'created_at'];
|
||||||
|
|
||||||
@@ -50,7 +53,11 @@ export async function getProject(id: number) {
|
|||||||
where: { id },
|
where: { id },
|
||||||
include: { customers: true, users: true, quotations: true, orders: true, project_notes: { orderBy: { created_at: 'desc' } } },
|
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>) {
|
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,
|
notes: body.notes ? String(body.notes) : null,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (project.project_number && nasFileManager.isConfigured()) {
|
||||||
|
nasFileManager.createProjectFolder(project.project_number, project.name || '');
|
||||||
|
}
|
||||||
|
|
||||||
return project;
|
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;
|
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 });
|
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;
|
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 } });
|
const existing = await prisma.projects.findUnique({ where: { id } });
|
||||||
if (!existing) return { error: 'not_found' as const };
|
if (!existing) return { error: 'not_found' as const };
|
||||||
if (existing.order_id) return { error: 'has_order' 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 } });
|
await prisma.projects.delete({ where: { id } });
|
||||||
return existing;
|
return existing;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user