fix: remove manual project creation, smart sequence release, received-invoices schema fix
- Remove ProjectCreate page, POST /projects endpoint, and next-number endpoint - Projects can only be created through orders (shared numbering sequence) - Remove dead CreateProjectSchema and createProject service function - Delete 'order' row from number_sequences (unused; code uses 'shared') - Smart sequence release: decrement last_number only when deleting the highest number - Fix received-invoices stats referencing non-existent is_deleted and amount_czk columns - Update deploy instructions in CLAUDE.md (npm install, prisma migrate deploy) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
11
CLAUDE.md
11
CLAUDE.md
@@ -293,10 +293,11 @@ When adding new features, add tests in `src/__tests__/`. Name test files `<featu
|
|||||||
5. Create tarball: `tar -czf app-ts-X.Y.Z.tar.gz dist dist-client prisma package.json package-lock.json scripts`
|
5. Create tarball: `tar -czf app-ts-X.Y.Z.tar.gz dist dist-client prisma package.json package-lock.json scripts`
|
||||||
6. Deploy via SSH to production server (`boha_admin@192.168.50.100`):
|
6. Deploy via SSH to production server (`boha_admin@192.168.50.100`):
|
||||||
- Path: `/var/www/app-ts`
|
- Path: `/var/www/app-ts`
|
||||||
- Backup: `node_modules`, `.env`, `ecosystem.config.js`
|
- Remove old files: `rm -rf dist dist-client prisma scripts package.json package-lock.json`
|
||||||
- Clean directory (keep backups only)
|
- Copy tarball to server: `scp app-ts-X.Y.Z.tar.gz boha_admin@192.168.50.100:/tmp/`
|
||||||
- Extract tarball
|
- Extract tarball: `tar -xzf /tmp/app-ts-X.Y.Z.tar.gz`
|
||||||
- Restore backups
|
- Install dependencies: `npm install --omit=dev`
|
||||||
- Restart: `pm2 reload ecosystem.config.js`
|
- Apply Prisma migrations: `npx prisma migrate deploy`
|
||||||
|
- Restart: `pm2 restart app-ts --update-env`
|
||||||
|
|
||||||
Do not push directly to production or restart services without confirmation.
|
Do not push directly to production or restart services without confirmation.
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "app-ts",
|
"name": "app-ts",
|
||||||
"version": "1.5.4",
|
"version": "1.5.5",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "dist/server.js",
|
"main": "dist/server.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -46,7 +46,6 @@ const OffersTemplates = lazy(() => import("./pages/OffersTemplates"));
|
|||||||
const Orders = lazy(() => import("./pages/Orders"));
|
const Orders = lazy(() => import("./pages/Orders"));
|
||||||
const OrderDetail = lazy(() => import("./pages/OrderDetail"));
|
const OrderDetail = lazy(() => import("./pages/OrderDetail"));
|
||||||
const Projects = lazy(() => import("./pages/Projects"));
|
const Projects = lazy(() => import("./pages/Projects"));
|
||||||
const ProjectCreate = lazy(() => import("./pages/ProjectCreate"));
|
|
||||||
const ProjectDetail = lazy(() => import("./pages/ProjectDetail"));
|
const ProjectDetail = lazy(() => import("./pages/ProjectDetail"));
|
||||||
const Invoices = lazy(() => import("./pages/Invoices"));
|
const Invoices = lazy(() => import("./pages/Invoices"));
|
||||||
const InvoiceDetail = lazy(() => import("./pages/InvoiceDetail"));
|
const InvoiceDetail = lazy(() => import("./pages/InvoiceDetail"));
|
||||||
@@ -104,7 +103,6 @@ export default function AdminApp() {
|
|||||||
<Route path="orders" element={<Orders />} />
|
<Route path="orders" element={<Orders />} />
|
||||||
<Route path="orders/:id" element={<OrderDetail />} />
|
<Route path="orders/:id" element={<OrderDetail />} />
|
||||||
<Route path="projects" element={<Projects />} />
|
<Route path="projects" element={<Projects />} />
|
||||||
<Route path="projects/new" element={<ProjectCreate />} />
|
|
||||||
<Route path="projects/:id" element={<ProjectDetail />} />
|
<Route path="projects/:id" element={<ProjectDetail />} />
|
||||||
<Route path="invoices" element={<Invoices />} />
|
<Route path="invoices" element={<Invoices />} />
|
||||||
<Route path="invoices/new" element={<InvoiceDetail />} />
|
<Route path="invoices/new" element={<InvoiceDetail />} />
|
||||||
|
|||||||
@@ -1,381 +0,0 @@
|
|||||||
import { useState, useEffect, useMemo } from "react";
|
|
||||||
import { useNavigate, Link } from "react-router-dom";
|
|
||||||
import { useAlert } from "../context/AlertContext";
|
|
||||||
import { useAuth } from "../context/AuthContext";
|
|
||||||
import { motion } from "framer-motion";
|
|
||||||
import FormField from "../components/FormField";
|
|
||||||
import Forbidden from "../components/Forbidden";
|
|
||||||
import AdminDatePicker from "../components/AdminDatePicker";
|
|
||||||
import apiFetch from "../utils/api";
|
|
||||||
|
|
||||||
const API_BASE = "/api/admin";
|
|
||||||
|
|
||||||
interface Customer {
|
|
||||||
id: number;
|
|
||||||
name: string;
|
|
||||||
company_id?: string;
|
|
||||||
city?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface User {
|
|
||||||
id: number;
|
|
||||||
name: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ProjectForm {
|
|
||||||
project_number: string;
|
|
||||||
name: string;
|
|
||||||
customer_id: number | null;
|
|
||||||
customer_name: string;
|
|
||||||
start_date: string;
|
|
||||||
responsible_user_id: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function ProjectCreate() {
|
|
||||||
const navigate = useNavigate();
|
|
||||||
const alert = useAlert();
|
|
||||||
const { hasPermission } = useAuth();
|
|
||||||
|
|
||||||
const [form, setForm] = useState<ProjectForm>({
|
|
||||||
project_number: "",
|
|
||||||
name: "",
|
|
||||||
customer_id: null,
|
|
||||||
customer_name: "",
|
|
||||||
start_date: new Date().toISOString().split("T")[0],
|
|
||||||
responsible_user_id: "",
|
|
||||||
});
|
|
||||||
const [users, setUsers] = useState<User[]>([]);
|
|
||||||
const [saving, setSaving] = useState(false);
|
|
||||||
const [errors, setErrors] = useState<Record<string, string | undefined>>({});
|
|
||||||
const [loadingNumber, setLoadingNumber] = useState(true);
|
|
||||||
|
|
||||||
// Customer selector state
|
|
||||||
const [customers, setCustomers] = useState<Customer[]>([]);
|
|
||||||
const [customerSearch, setCustomerSearch] = useState("");
|
|
||||||
const [showCustomerDropdown, setShowCustomerDropdown] = useState(false);
|
|
||||||
|
|
||||||
// Load initial data
|
|
||||||
useEffect(() => {
|
|
||||||
const load = async () => {
|
|
||||||
try {
|
|
||||||
const [numRes, custRes, usersRes] = await Promise.all([
|
|
||||||
apiFetch(`${API_BASE}/projects/next-number`),
|
|
||||||
apiFetch(`${API_BASE}/customers`),
|
|
||||||
apiFetch(`${API_BASE}/users`),
|
|
||||||
]);
|
|
||||||
|
|
||||||
const numData = await numRes.json();
|
|
||||||
if (numData.success) {
|
|
||||||
setForm((prev) => ({
|
|
||||||
...prev,
|
|
||||||
project_number:
|
|
||||||
numData.data?.next_number || numData.data?.number || "",
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
const custData = await custRes.json();
|
|
||||||
if (custData.success) {
|
|
||||||
setCustomers(
|
|
||||||
Array.isArray(custData.data)
|
|
||||||
? custData.data
|
|
||||||
: custData.data?.items || [],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const usersData = await usersRes.json();
|
|
||||||
if (usersData.success) {
|
|
||||||
const rawUsers = Array.isArray(usersData.data)
|
|
||||||
? usersData.data
|
|
||||||
: usersData.data?.items || [];
|
|
||||||
setUsers(
|
|
||||||
rawUsers.map((u: any) => ({
|
|
||||||
id: u.id,
|
|
||||||
name:
|
|
||||||
`${u.first_name || ""} ${u.last_name || ""}`.trim() ||
|
|
||||||
u.username,
|
|
||||||
})),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
alert.error("Chyba při načítání dat");
|
|
||||||
} finally {
|
|
||||||
setLoadingNumber(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
load();
|
|
||||||
}, [alert]);
|
|
||||||
|
|
||||||
// Customer filtering
|
|
||||||
const filteredCustomers = useMemo(() => {
|
|
||||||
if (!customerSearch) return customers;
|
|
||||||
const q = customerSearch.toLowerCase();
|
|
||||||
return customers.filter(
|
|
||||||
(c) =>
|
|
||||||
(c.name || "").toLowerCase().includes(q) ||
|
|
||||||
(c.company_id || "").includes(customerSearch) ||
|
|
||||||
(c.city || "").toLowerCase().includes(q),
|
|
||||||
);
|
|
||||||
}, [customers, customerSearch]);
|
|
||||||
|
|
||||||
// Close dropdown on outside click
|
|
||||||
useEffect(() => {
|
|
||||||
const handleClickOutside = () => setShowCustomerDropdown(false);
|
|
||||||
if (showCustomerDropdown) {
|
|
||||||
document.addEventListener("click", handleClickOutside);
|
|
||||||
return () => document.removeEventListener("click", handleClickOutside);
|
|
||||||
}
|
|
||||||
}, [showCustomerDropdown]);
|
|
||||||
|
|
||||||
if (!hasPermission("projects.create")) return <Forbidden />;
|
|
||||||
|
|
||||||
const selectCustomer = (customer: Customer) => {
|
|
||||||
setForm((prev) => ({
|
|
||||||
...prev,
|
|
||||||
customer_id: customer.id,
|
|
||||||
customer_name: customer.name,
|
|
||||||
}));
|
|
||||||
setErrors((prev) => ({ ...prev, customer_id: undefined }));
|
|
||||||
setCustomerSearch("");
|
|
||||||
setShowCustomerDropdown(false);
|
|
||||||
};
|
|
||||||
|
|
||||||
const clearCustomer = () => {
|
|
||||||
setForm((prev) => ({ ...prev, customer_id: null, customer_name: "" }));
|
|
||||||
};
|
|
||||||
|
|
||||||
const updateForm = (field: keyof ProjectForm, value: unknown) => {
|
|
||||||
setForm((prev) => ({ ...prev, [field]: value }));
|
|
||||||
setErrors((prev) => ({ ...prev, [field]: undefined }));
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleSave = async () => {
|
|
||||||
const newErrors: Record<string, string> = {};
|
|
||||||
if (!form.name.trim()) newErrors.name = "Název projektu je povinný";
|
|
||||||
if (!form.customer_id) newErrors.customer_id = "Vyberte zákazníka";
|
|
||||||
setErrors(newErrors);
|
|
||||||
if (Object.keys(newErrors).length > 0) return;
|
|
||||||
|
|
||||||
setSaving(true);
|
|
||||||
try {
|
|
||||||
const body = {
|
|
||||||
name: form.name.trim(),
|
|
||||||
customer_id: form.customer_id,
|
|
||||||
start_date: form.start_date,
|
|
||||||
responsible_user_id: form.responsible_user_id || null,
|
|
||||||
};
|
|
||||||
|
|
||||||
const res = await apiFetch(`${API_BASE}/projects`, {
|
|
||||||
method: "POST",
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
body: JSON.stringify(body),
|
|
||||||
});
|
|
||||||
const data = await res.json();
|
|
||||||
if (data.success) {
|
|
||||||
alert.success("Projekt byl vytvořen");
|
|
||||||
navigate(`/projects/${data.data.id}`);
|
|
||||||
} else {
|
|
||||||
alert.error(data.error || "Nepodařilo se vytvořit projekt");
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
alert.error("Chyba připojení");
|
|
||||||
} finally {
|
|
||||||
setSaving(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if (loadingNumber) {
|
|
||||||
return (
|
|
||||||
<div className="admin-skeleton" style={{ padding: 0, gap: "1.5rem" }}>
|
|
||||||
<div
|
|
||||||
className="admin-skeleton-row"
|
|
||||||
style={{ justifyContent: "space-between" }}
|
|
||||||
>
|
|
||||||
<div className="admin-skeleton-line h-8" style={{ width: "200px" }} />
|
|
||||||
</div>
|
|
||||||
<div className="admin-card">
|
|
||||||
<div className="admin-skeleton" style={{ gap: "1.25rem" }}>
|
|
||||||
{[0, 1, 2, 3].map((i) => (
|
|
||||||
<div key={i} className="admin-skeleton-row">
|
|
||||||
<div className="admin-skeleton-line w-1/4" />
|
|
||||||
<div className="admin-skeleton-line w-1/2" />
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
<motion.div
|
|
||||||
className="admin-page-header"
|
|
||||||
initial={{ opacity: 0, y: 12 }}
|
|
||||||
animate={{ opacity: 1, y: 0 }}
|
|
||||||
transition={{ duration: 0.25 }}
|
|
||||||
>
|
|
||||||
<div className="flex-row gap-4">
|
|
||||||
<Link
|
|
||||||
to="/projects"
|
|
||||||
className="admin-btn-icon"
|
|
||||||
title="Zpět"
|
|
||||||
aria-label="Zpět"
|
|
||||||
>
|
|
||||||
<svg
|
|
||||||
width="20"
|
|
||||||
height="20"
|
|
||||||
viewBox="0 0 24 24"
|
|
||||||
fill="none"
|
|
||||||
stroke="currentColor"
|
|
||||||
strokeWidth="2"
|
|
||||||
>
|
|
||||||
<path d="M19 12H5M12 19l-7-7 7-7" />
|
|
||||||
</svg>
|
|
||||||
</Link>
|
|
||||||
<div>
|
|
||||||
<h1 className="admin-page-title">Nový projekt</h1>
|
|
||||||
<p className="admin-page-subtitle">Ruční vytvoření projektu</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div className="admin-page-actions">
|
|
||||||
<button
|
|
||||||
onClick={handleSave}
|
|
||||||
disabled={saving}
|
|
||||||
className="admin-btn admin-btn-primary"
|
|
||||||
>
|
|
||||||
{saving ? "Ukládám..." : "Uložit"}
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</motion.div>
|
|
||||||
|
|
||||||
<motion.div
|
|
||||||
className="admin-card"
|
|
||||||
initial={{ opacity: 0, y: 12 }}
|
|
||||||
animate={{ opacity: 1, y: 0 }}
|
|
||||||
transition={{ duration: 0.25, delay: 0.06 }}
|
|
||||||
style={{ overflow: "visible" }}
|
|
||||||
>
|
|
||||||
<div className="admin-card-body">
|
|
||||||
<h3 className="admin-card-title">Základní údaje</h3>
|
|
||||||
<div className="admin-form">
|
|
||||||
<div className="admin-form-row">
|
|
||||||
<FormField label="Číslo projektu">
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
value={form.project_number}
|
|
||||||
readOnly
|
|
||||||
className="admin-form-input"
|
|
||||||
style={{
|
|
||||||
backgroundColor: "var(--bg-secondary)",
|
|
||||||
cursor: "default",
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</FormField>
|
|
||||||
<FormField label="Název" error={errors.name} required>
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
value={form.name}
|
|
||||||
onChange={(e) => updateForm("name", e.target.value)}
|
|
||||||
className="admin-form-input"
|
|
||||||
placeholder="Název projektu"
|
|
||||||
/>
|
|
||||||
</FormField>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="admin-form-row">
|
|
||||||
<FormField label="Zákazník" error={errors.customer_id} required>
|
|
||||||
{form.customer_id ? (
|
|
||||||
<div className="admin-customer-selected">
|
|
||||||
<span>{form.customer_name}</span>
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
onClick={clearCustomer}
|
|
||||||
className="admin-btn-icon"
|
|
||||||
title="Odebrat zákazníka"
|
|
||||||
aria-label="Odebrat zákazníka"
|
|
||||||
>
|
|
||||||
<svg
|
|
||||||
width="14"
|
|
||||||
height="14"
|
|
||||||
viewBox="0 0 24 24"
|
|
||||||
fill="none"
|
|
||||||
stroke="currentColor"
|
|
||||||
strokeWidth="2"
|
|
||||||
>
|
|
||||||
<line x1="18" y1="6" x2="6" y2="18" />
|
|
||||||
<line x1="6" y1="6" x2="18" y2="18" />
|
|
||||||
</svg>
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
) : (
|
|
||||||
<div
|
|
||||||
className="admin-customer-select"
|
|
||||||
onClick={(e) => e.stopPropagation()}
|
|
||||||
>
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
value={customerSearch}
|
|
||||||
onChange={(e) => {
|
|
||||||
setCustomerSearch(e.target.value);
|
|
||||||
setShowCustomerDropdown(true);
|
|
||||||
}}
|
|
||||||
onFocus={() => setShowCustomerDropdown(true)}
|
|
||||||
className="admin-form-input"
|
|
||||||
placeholder="Hledat zákazníka..."
|
|
||||||
/>
|
|
||||||
{showCustomerDropdown && (
|
|
||||||
<div className="admin-customer-dropdown">
|
|
||||||
{filteredCustomers.length === 0 ? (
|
|
||||||
<div className="admin-customer-dropdown-empty">
|
|
||||||
Žádní zákazníci
|
|
||||||
</div>
|
|
||||||
) : (
|
|
||||||
filteredCustomers.slice(0, 20).map((c) => (
|
|
||||||
<div
|
|
||||||
key={c.id}
|
|
||||||
className="admin-customer-dropdown-item"
|
|
||||||
onMouseDown={() => selectCustomer(c)}
|
|
||||||
>
|
|
||||||
<div>{c.name}</div>
|
|
||||||
{c.city && <div>{c.city}</div>}
|
|
||||||
</div>
|
|
||||||
))
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</FormField>
|
|
||||||
<FormField label="Datum zahájení">
|
|
||||||
<AdminDatePicker
|
|
||||||
mode="date"
|
|
||||||
value={form.start_date}
|
|
||||||
onChange={(val: string) => updateForm("start_date", val)}
|
|
||||||
/>
|
|
||||||
</FormField>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="admin-form-row">
|
|
||||||
<FormField label="Zodpovědná osoba">
|
|
||||||
<select
|
|
||||||
value={form.responsible_user_id}
|
|
||||||
onChange={(e) =>
|
|
||||||
updateForm("responsible_user_id", e.target.value)
|
|
||||||
}
|
|
||||||
className="admin-form-select"
|
|
||||||
>
|
|
||||||
<option value="">— Nevybráno —</option>
|
|
||||||
{users.map((u) => (
|
|
||||||
<option key={u.id} value={u.id}>
|
|
||||||
{u.name}
|
|
||||||
</option>
|
|
||||||
))}
|
|
||||||
</select>
|
|
||||||
</FormField>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</motion.div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -160,22 +160,6 @@ export default function Projects() {
|
|||||||
)}
|
)}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
{hasPermission("projects.create") && (
|
|
||||||
<Link to="/projects/new" className="admin-btn admin-btn-primary">
|
|
||||||
<svg
|
|
||||||
width="20"
|
|
||||||
height="20"
|
|
||||||
viewBox="0 0 24 24"
|
|
||||||
fill="none"
|
|
||||||
stroke="currentColor"
|
|
||||||
strokeWidth="2"
|
|
||||||
>
|
|
||||||
<line x1="12" y1="5" x2="12" y2="19" />
|
|
||||||
<line x1="5" y1="12" x2="19" y2="12" />
|
|
||||||
</svg>
|
|
||||||
Nový projekt
|
|
||||||
</Link>
|
|
||||||
)}
|
|
||||||
</motion.div>
|
</motion.div>
|
||||||
|
|
||||||
<motion.div
|
<motion.div
|
||||||
|
|||||||
@@ -5,19 +5,16 @@ import { success, error, parseId } from "../../utils/response";
|
|||||||
import { parsePagination, buildPaginationMeta } from "../../utils/pagination";
|
import { parsePagination, buildPaginationMeta } from "../../utils/pagination";
|
||||||
import { parseBody } from "../../schemas/common";
|
import { parseBody } from "../../schemas/common";
|
||||||
import {
|
import {
|
||||||
CreateProjectSchema,
|
|
||||||
UpdateProjectSchema,
|
UpdateProjectSchema,
|
||||||
CreateProjectNoteSchema,
|
CreateProjectNoteSchema,
|
||||||
} from "../../schemas/projects.schema";
|
} from "../../schemas/projects.schema";
|
||||||
import {
|
import {
|
||||||
listProjects,
|
listProjects,
|
||||||
getProject,
|
getProject,
|
||||||
createProject,
|
|
||||||
updateProject,
|
updateProject,
|
||||||
deleteProject,
|
deleteProject,
|
||||||
createProjectNote,
|
createProjectNote,
|
||||||
deleteProjectNote,
|
deleteProjectNote,
|
||||||
getNextProjectNumber,
|
|
||||||
} from "../../services/projects.service";
|
} from "../../services/projects.service";
|
||||||
|
|
||||||
export default async function projectsRoutes(
|
export default async function projectsRoutes(
|
||||||
@@ -61,30 +58,6 @@ export default async function projectsRoutes(
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
fastify.post(
|
|
||||||
"/",
|
|
||||||
{ preHandler: requirePermission("projects.create") },
|
|
||||||
async (request, reply) => {
|
|
||||||
const parsed = parseBody(CreateProjectSchema, request.body);
|
|
||||||
if ("error" in parsed) return error(reply, parsed.error, 400);
|
|
||||||
|
|
||||||
const project = await createProject(parsed.data);
|
|
||||||
if ("error" in project) {
|
|
||||||
return error(reply, project.error, (project as any).status ?? 400);
|
|
||||||
}
|
|
||||||
|
|
||||||
await logAudit({
|
|
||||||
request,
|
|
||||||
authData: request.authData,
|
|
||||||
action: "create",
|
|
||||||
entityType: "project",
|
|
||||||
entityId: project.id,
|
|
||||||
description: `Vytvořen projekt ${project.name}`,
|
|
||||||
});
|
|
||||||
return success(reply, { id: project.id }, 201, "Projekt byl vytvořen");
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
fastify.put<{ Params: { id: string } }>(
|
fastify.put<{ Params: { id: string } }>(
|
||||||
"/:id",
|
"/:id",
|
||||||
{ preHandler: requirePermission("projects.edit") },
|
{ preHandler: requirePermission("projects.edit") },
|
||||||
@@ -137,16 +110,6 @@ export default async function projectsRoutes(
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
// GET /api/admin/projects/next-number — shared sequence with orders (matches PHP)
|
|
||||||
fastify.get(
|
|
||||||
"/next-number",
|
|
||||||
{ preHandler: requirePermission("projects.create") },
|
|
||||||
async (_request, reply) => {
|
|
||||||
const nextNumber = await getNextProjectNumber();
|
|
||||||
return success(reply, { next_number: nextNumber });
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
// DELETE /api/admin/projects/:id/notes/:noteId
|
// DELETE /api/admin/projects/:id/notes/:noteId
|
||||||
fastify.delete<{ Params: { id: string; noteId: string } }>(
|
fastify.delete<{ Params: { id: string; noteId: string } }>(
|
||||||
"/:id/notes/:noteId",
|
"/:id/notes/:noteId",
|
||||||
|
|||||||
@@ -1,35 +1,5 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
const safeProjectNumber = z
|
|
||||||
.string()
|
|
||||||
.regex(/^[\p{L}\p{N}_\-.]+$/u, "Číslo projektu obsahuje nepovolené znaky")
|
|
||||||
.nullish();
|
|
||||||
|
|
||||||
export const CreateProjectSchema = z.object({
|
|
||||||
project_number: safeProjectNumber,
|
|
||||||
name: z.string().nullish(),
|
|
||||||
customer_id: z
|
|
||||||
.union([z.number(), z.string()])
|
|
||||||
.transform((v) => Number(v))
|
|
||||||
.nullish(),
|
|
||||||
responsible_user_id: z
|
|
||||||
.union([z.number(), z.string()])
|
|
||||||
.transform((v) => Number(v))
|
|
||||||
.nullish(),
|
|
||||||
quotation_id: z
|
|
||||||
.union([z.number(), z.string()])
|
|
||||||
.transform((v) => Number(v))
|
|
||||||
.nullish(),
|
|
||||||
order_id: z
|
|
||||||
.union([z.number(), z.string()])
|
|
||||||
.transform((v) => Number(v))
|
|
||||||
.nullish(),
|
|
||||||
status: z.string().optional().default("aktivni"),
|
|
||||||
start_date: z.string().nullish(),
|
|
||||||
end_date: z.string().nullish(),
|
|
||||||
notes: z.string().nullish(),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const UpdateProjectSchema = z.object({
|
export const UpdateProjectSchema = z.object({
|
||||||
name: z.string().nullish(),
|
name: z.string().nullish(),
|
||||||
status: z.string().optional(),
|
status: z.string().optional(),
|
||||||
@@ -58,6 +28,5 @@ export const CreateProjectNoteSchema = z.object({
|
|||||||
content: z.string().nullish(),
|
content: z.string().nullish(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export type CreateProjectInput = z.infer<typeof CreateProjectSchema>;
|
|
||||||
export type UpdateProjectInput = z.infer<typeof UpdateProjectSchema>;
|
export type UpdateProjectInput = z.infer<typeof UpdateProjectSchema>;
|
||||||
export type CreateProjectNoteInput = z.infer<typeof CreateProjectNoteSchema>;
|
export type CreateProjectNoteInput = z.infer<typeof CreateProjectNoteSchema>;
|
||||||
|
|||||||
@@ -493,7 +493,7 @@ export async function deleteInvoice(id: number) {
|
|||||||
const year = existing.invoice_number
|
const year = existing.invoice_number
|
||||||
? Number(existing.invoice_number.split("/")[1]) || new Date().getFullYear()
|
? Number(existing.invoice_number.split("/")[1]) || new Date().getFullYear()
|
||||||
: new Date().getFullYear();
|
: new Date().getFullYear();
|
||||||
await releaseInvoiceNumber(year);
|
await releaseInvoiceNumber(year, existing.invoice_number ?? undefined);
|
||||||
|
|
||||||
return existing;
|
return existing;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -110,13 +110,51 @@ async function previewNextSequence(
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Release a sequence number back to the pool.
|
* Release a sequence number back to the pool.
|
||||||
* NOTE: Blindly decrementing can cause duplicate numbers if numbers were
|
* Only decrements if the deleted entity held the current highest number,
|
||||||
* allocated after this one. Sequence numbers are consumed but not returned
|
* preventing gaps while avoiding duplicate re-allocation.
|
||||||
* to the pool — this is intentionally a no-op.
|
|
||||||
*/
|
*/
|
||||||
async function releaseSequence(_type: string, _year: number) {
|
async function releaseSequence(
|
||||||
// No-op: decrementing can cause duplicate sequence numbers.
|
type: string,
|
||||||
// Sequence numbers are consumed but never returned to the pool.
|
year: number,
|
||||||
|
deletedNumber?: string,
|
||||||
|
) {
|
||||||
|
if (!deletedNumber) return;
|
||||||
|
|
||||||
|
const existing = await prisma.$queryRaw<Array<{ last_number: number }>>`
|
||||||
|
SELECT last_number FROM number_sequences
|
||||||
|
WHERE \`type\` = ${type} AND \`year\` = ${year}
|
||||||
|
`;
|
||||||
|
if (existing.length === 0) return;
|
||||||
|
|
||||||
|
const settings = await getSettings();
|
||||||
|
const pattern =
|
||||||
|
type === "shared"
|
||||||
|
? settings?.order_number_pattern || DEFAULT_ORDER_PATTERN
|
||||||
|
: type === "invoice"
|
||||||
|
? settings?.invoice_number_pattern || DEFAULT_INVOICE_PATTERN
|
||||||
|
: settings?.offer_number_pattern || DEFAULT_OFFER_PATTERN;
|
||||||
|
const code =
|
||||||
|
type === "shared"
|
||||||
|
? settings?.order_type_code || "71"
|
||||||
|
: type === "invoice"
|
||||||
|
? settings?.invoice_type_code || "81"
|
||||||
|
: "";
|
||||||
|
const prefix = type === "offer" ? settings?.quotation_prefix || "NA" : "";
|
||||||
|
|
||||||
|
const highestNumber = applyPattern(pattern, {
|
||||||
|
year,
|
||||||
|
prefix,
|
||||||
|
code,
|
||||||
|
seq: existing[0].last_number,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (deletedNumber === highestNumber && existing[0].last_number > 0) {
|
||||||
|
await prisma.$executeRaw`
|
||||||
|
UPDATE number_sequences
|
||||||
|
SET \`last_number\` = ${existing[0].last_number - 1}
|
||||||
|
WHERE \`type\` = ${type} AND \`year\` = ${year}
|
||||||
|
`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Verify a shared number is not already used by an order or project. */
|
/** Verify a shared number is not already used by an order or project. */
|
||||||
@@ -265,19 +303,40 @@ export async function previewInvoiceNumber(
|
|||||||
return { number, next_number: number };
|
return { number, next_number: number };
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Release an offer number back to the pool (decrement sequence). */
|
/** Release an offer number back to the pool (decrement if highest). */
|
||||||
export async function releaseOfferNumber(year?: number) {
|
export async function releaseOfferNumber(
|
||||||
await releaseSequence("offer", year || new Date().getFullYear());
|
year?: number,
|
||||||
|
deletedNumber?: string,
|
||||||
|
) {
|
||||||
|
await releaseSequence(
|
||||||
|
"offer",
|
||||||
|
year || new Date().getFullYear(),
|
||||||
|
deletedNumber,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Release a shared number back to the pool (decrement sequence). */
|
/** Release a shared number back to the pool (decrement if highest). */
|
||||||
export async function releaseSharedNumber(year?: number) {
|
export async function releaseSharedNumber(
|
||||||
await releaseSequence("shared", year || new Date().getFullYear());
|
year?: number,
|
||||||
|
deletedNumber?: string,
|
||||||
|
) {
|
||||||
|
await releaseSequence(
|
||||||
|
"shared",
|
||||||
|
year || new Date().getFullYear(),
|
||||||
|
deletedNumber,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Release an invoice number back to the pool (decrement sequence). */
|
/** Release an invoice number back to the pool (decrement if highest). */
|
||||||
export async function releaseInvoiceNumber(year?: number) {
|
export async function releaseInvoiceNumber(
|
||||||
await releaseSequence("invoice", year || new Date().getFullYear());
|
year?: number,
|
||||||
|
deletedNumber?: string,
|
||||||
|
) {
|
||||||
|
await releaseSequence(
|
||||||
|
"invoice",
|
||||||
|
year || new Date().getFullYear(),
|
||||||
|
deletedNumber,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Preview what a pattern would produce (for settings UI) */
|
/** Preview what a pattern would produce (for settings UI) */
|
||||||
|
|||||||
@@ -307,7 +307,7 @@ export async function deleteOffer(id: number) {
|
|||||||
const year = existing.created_at
|
const year = existing.created_at
|
||||||
? new Date(existing.created_at).getFullYear()
|
? new Date(existing.created_at).getFullYear()
|
||||||
: new Date().getFullYear();
|
: new Date().getFullYear();
|
||||||
await releaseOfferNumber(year);
|
await releaseOfferNumber(year, existing.quotation_number ?? undefined);
|
||||||
|
|
||||||
return existing;
|
return existing;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -542,23 +542,10 @@ export async function deleteOrder(id: number) {
|
|||||||
await tx.orders.delete({ where: { id } });
|
await tx.orders.delete({ where: { id } });
|
||||||
});
|
});
|
||||||
|
|
||||||
const releasedYears = new Set<number>();
|
|
||||||
const year = existing.created_at
|
const year = existing.created_at
|
||||||
? new Date(existing.created_at).getFullYear()
|
? new Date(existing.created_at).getFullYear()
|
||||||
: new Date().getFullYear();
|
: new Date().getFullYear();
|
||||||
await releaseSharedNumber(year);
|
await releaseSharedNumber(year, existing.order_number ?? undefined);
|
||||||
releasedYears.add(year);
|
|
||||||
|
|
||||||
// Release the linked project's shared number(s) too
|
|
||||||
for (const p of linkedProjects) {
|
|
||||||
const pYear = p.created_at
|
|
||||||
? new Date(p.created_at).getFullYear()
|
|
||||||
: new Date().getFullYear();
|
|
||||||
if (!releasedYears.has(pYear)) {
|
|
||||||
await releaseSharedNumber(pYear);
|
|
||||||
releasedYears.add(pYear);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return { data: { id, order_number: existing.order_number } };
|
return { data: { id, order_number: existing.order_number } };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,5 @@
|
|||||||
import prisma from "../config/database";
|
import prisma from "../config/database";
|
||||||
import {
|
import { releaseSharedNumber } from "./numbering.service";
|
||||||
generateSharedNumber,
|
|
||||||
previewSharedNumber,
|
|
||||||
releaseSharedNumber,
|
|
||||||
} from "./numbering.service";
|
|
||||||
import { NasFileManager } from "./nas-file-manager";
|
import { NasFileManager } from "./nas-file-manager";
|
||||||
|
|
||||||
const nasFileManager = new NasFileManager();
|
const nasFileManager = new NasFileManager();
|
||||||
@@ -96,50 +92,6 @@ export async function getProject(id: number) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
import { isSharedNumberTaken } from "./numbering.service";
|
|
||||||
|
|
||||||
export async function createProject(body: Record<string, any>) {
|
|
||||||
const projectNumber =
|
|
||||||
body.project_number !== undefined && body.project_number !== null
|
|
||||||
? String(body.project_number)
|
|
||||||
: await generateSharedNumber();
|
|
||||||
|
|
||||||
if (body.project_number !== undefined && body.project_number !== null) {
|
|
||||||
const taken = await isSharedNumberTaken(String(body.project_number));
|
|
||||||
if (taken) {
|
|
||||||
return { error: "Číslo projektu je již použito", status: 400 };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const project = await prisma.projects.create({
|
|
||||||
data: {
|
|
||||||
project_number: projectNumber,
|
|
||||||
name: body.name ? String(body.name) : null,
|
|
||||||
customer_id: body.customer_id != null ? Number(body.customer_id) : null,
|
|
||||||
responsible_user_id:
|
|
||||||
body.responsible_user_id != null
|
|
||||||
? Number(body.responsible_user_id)
|
|
||||||
: null,
|
|
||||||
quotation_id:
|
|
||||||
body.quotation_id != null ? Number(body.quotation_id) : null,
|
|
||||||
order_id: body.order_id != null ? Number(body.order_id) : null,
|
|
||||||
status: body.status ? String(body.status) : "aktivni",
|
|
||||||
start_date: body.start_date ? new Date(String(body.start_date)) : null,
|
|
||||||
end_date: body.end_date ? new Date(String(body.end_date)) : null,
|
|
||||||
notes: body.notes ? String(body.notes) : null,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (project.project_number && nasFileManager.isConfigured()) {
|
|
||||||
nasFileManager.createProjectFolder(
|
|
||||||
project.project_number,
|
|
||||||
project.name || "",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return project;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function updateProject(id: number, body: Record<string, any>) {
|
export async function updateProject(id: number, body: Record<string, any>) {
|
||||||
const existing = await prisma.projects.findUnique({ where: { id } });
|
const existing = await prisma.projects.findUnique({ where: { id } });
|
||||||
if (!existing) return null;
|
if (!existing) return null;
|
||||||
@@ -206,7 +158,7 @@ export async function deleteProject(id: number, deleteFiles: boolean = false) {
|
|||||||
const year = existing.created_at
|
const year = existing.created_at
|
||||||
? new Date(existing.created_at).getFullYear()
|
? new Date(existing.created_at).getFullYear()
|
||||||
: new Date().getFullYear();
|
: new Date().getFullYear();
|
||||||
await releaseSharedNumber(year);
|
await releaseSharedNumber(year, existing.project_number ?? undefined);
|
||||||
|
|
||||||
return existing;
|
return existing;
|
||||||
}
|
}
|
||||||
@@ -248,7 +200,3 @@ export async function deleteProjectNote(projectId: number, noteId: number) {
|
|||||||
await prisma.project_notes.delete({ where: { id: noteId } });
|
await prisma.project_notes.delete({ where: { id: noteId } });
|
||||||
return note;
|
return note;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getNextProjectNumber() {
|
|
||||||
return previewSharedNumber();
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user