feat: Add support for project cancellation status across the application

This commit is contained in:
2025-09-11 16:19:46 +02:00
parent 2735d46552
commit 95ef139843
13 changed files with 116 additions and 50 deletions

View File

@@ -8,6 +8,7 @@ import {
deleteProject,
} from "@/lib/queries/projects";
import { logFieldChange } from "@/lib/queries/fieldHistory";
import { addNoteToProject } from "@/lib/queries/notes";
import initializeDatabase from "@/lib/init-db";
import { NextResponse } from "next/server";
import { withReadAuth, withUserAuth } from "@/lib/middleware/auth";
@@ -42,6 +43,7 @@ async function getProjectHandler(req, { params }) {
}
async function updateProjectHandler(req, { params }) {
try {
const { id } = await params;
const data = await req.json();
@@ -73,6 +75,26 @@ async function updateProjectHandler(req, { params }) {
}
}
// Special handling for project cancellation
if (data.project_status === 'cancelled' && originalProject.project_status !== 'cancelled') {
const now = new Date();
const cancellationDate = now.toLocaleDateString('pl-PL', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit'
});
const cancellationNote = `Projekt został wycofany w dniu ${cancellationDate}`;
try {
addNoteToProject(parseInt(id), cancellationNote, userId, true); // true for is_system
} catch (error) {
console.error('Failed to log project cancellation:', error);
}
}
updateProject(parseInt(id), data, userId);
// Get updated project
@@ -93,6 +115,13 @@ async function updateProjectHandler(req, { params }) {
);
return NextResponse.json(updatedProject);
} catch (error) {
console.error("Error in updateProjectHandler:", error);
return NextResponse.json(
{ error: "Internal server error", details: error.message },
{ status: 500 }
);
}
}
async function deleteProjectHandler(req, { params }) {

View File

@@ -29,6 +29,7 @@ function ProjectsMapPageContent() {
in_progress_design: true,
in_progress_construction: true,
fulfilled: true,
cancelled: true,
});
const [activeBaseLayer, setActiveBaseLayer] = useState("OpenStreetMap");
const [activeOverlays, setActiveOverlays] = useState([]);
@@ -57,6 +58,11 @@ function ProjectsMapPageContent() {
label: "Completed",
shortLabel: "Zakończony",
},
cancelled: {
color: "#EF4444",
label: "Cancelled",
shortLabel: "Wycofany",
},
};
// Toggle all status filters

View File

@@ -119,6 +119,7 @@ export default function ProjectListPage() {
case "in_progress_design": return t('projectStatus.in_progress_design');
case "in_progress_construction": return t('projectStatus.in_progress_construction');
case "fulfilled": return t('projectStatus.fulfilled');
case "cancelled": return t('projectStatus.cancelled');
default: return "-";
}
};

View File

@@ -38,6 +38,10 @@ export default function ProjectStatusDropdown({
label: t("projectStatus.fulfilled"),
variant: "success",
},
cancelled: {
label: t("projectStatus.cancelled"),
variant: "danger",
},
};
const handleChange = async (newStatus) => {
if (newStatus === status) {
@@ -50,11 +54,19 @@ export default function ProjectStatusDropdown({
setIsOpen(false);
try {
await fetch(`/api/projects/${project.project_id}`, {
const updateData = { ...project, project_status: newStatus };
const response = await fetch(`/api/projects/${project.project_id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ ...project, project_status: newStatus }),
body: JSON.stringify(updateData),
});
if (!response.ok) {
const errorData = await response.json();
console.error('Update failed:', errorData);
}
window.location.reload();
} catch (error) {
console.error("Failed to update status:", error);

View File

@@ -29,6 +29,10 @@ export default function ProjectStatusDropdownDebug({
label: "Completed",
variant: "success",
},
cancelled: {
label: "Cancelled",
variant: "danger",
},
};
const handleChange = async (newStatus) => {

View File

@@ -29,6 +29,10 @@ export default function ProjectStatusDropdownSimple({
label: "Completed",
variant: "success",
},
cancelled: {
label: "Cancelled",
variant: "danger",
},
};
const handleChange = async (newStatus) => {

View File

@@ -31,6 +31,7 @@ const statusColors = {
pending: "bg-yellow-100 text-yellow-800",
in_progress: "bg-orange-100 text-orange-800",
fulfilled: "bg-gray-100 text-gray-800",
cancelled: "bg-red-100 text-red-800",
};
const statusTranslations = {
@@ -39,6 +40,7 @@ const statusTranslations = {
pending: "Oczekujący",
in_progress: "W trakcie",
fulfilled: "Zakończony",
cancelled: "Wycofany",
};
export default function ProjectCalendarWidget({

View File

@@ -32,6 +32,7 @@ export default function ProjectMap({
label: "In Progress (Construction)",
},
fulfilled: { color: "#10B981", label: "Completed" },
cancelled: { color: "#EF4444", label: "Cancelled" },
};
useEffect(() => {

View File

@@ -105,6 +105,7 @@ const translations = {
in_progress_design: "W realizacji (projektowanie)",
in_progress_construction: "W realizacji (realizacja)",
fulfilled: "Zakończony",
cancelled: "Wycofany",
unknown: "Nieznany"
},
@@ -541,6 +542,7 @@ const translations = {
in_progress_design: "In Progress (Design)",
in_progress_construction: "In Progress (Construction)",
fulfilled: "Completed",
cancelled: "Cancelled",
unknown: "Unknown"
},

View File

@@ -31,7 +31,7 @@ export default function initializeDatabase() {
contact TEXT,
notes TEXT,
project_type TEXT CHECK(project_type IN ('design', 'construction', 'design+construction')) DEFAULT 'design',
project_status TEXT CHECK(project_status IN ('registered', 'in_progress_design', 'in_progress_construction', 'fulfilled')) DEFAULT 'registered',
project_status TEXT CHECK(project_status IN ('registered', 'in_progress_design', 'in_progress_construction', 'fulfilled', 'cancelled')) DEFAULT 'registered',
FOREIGN KEY (contract_id) REFERENCES contracts(contract_id)
);
@@ -113,7 +113,7 @@ export default function initializeDatabase() {
// Migration: Add project_status column to projects table
try {
db.exec(`
ALTER TABLE projects ADD COLUMN project_status TEXT CHECK(project_status IN ('registered', 'in_progress_design', 'in_progress_construction', 'fulfilled')) DEFAULT 'registered';
ALTER TABLE projects ADD COLUMN project_status TEXT CHECK(project_status IN ('registered', 'in_progress_design', 'in_progress_construction', 'fulfilled', 'cancelled')) DEFAULT 'registered';
`);
} catch (e) {
// Column already exists, ignore error

View File

@@ -16,13 +16,13 @@ export function getNotesByProjectId(project_id) {
.all(project_id);
}
export function addNoteToProject(project_id, note, created_by = null) {
export function addNoteToProject(project_id, note, created_by = null, is_system = false) {
db.prepare(
`
INSERT INTO notes (project_id, note, created_by, note_date)
VALUES (?, ?, ?, CURRENT_TIMESTAMP)
INSERT INTO notes (project_id, note, created_by, is_system, note_date)
VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP)
`
).run(project_id, note, created_by);
).run(project_id, note, created_by, is_system ? 1 : 0);
}
export function getNotesByTaskId(task_id) {

View File

@@ -110,7 +110,7 @@ export function updateProject(id, data, userId = null) {
coordinates = ?, assigned_to = ?, updated_at = CURRENT_TIMESTAMP
WHERE project_id = ?
`);
stmt.run(
const result = stmt.run(
data.contract_id,
data.project_name,
data.project_number,
@@ -130,6 +130,9 @@ export function updateProject(id, data, userId = null) {
data.assigned_to || null,
id
);
console.log('Update result:', result);
return result;
}
export function deleteProject(id) {

View File

@@ -15,6 +15,8 @@ export const formatProjectStatus = (status) => {
return "W realizacji (realizacja)";
case "fulfilled":
return "Zakończony";
case "cancelled":
return "Wycofany";
default:
return "-";
}