EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
route.ts
Go to the documentation of this file.
1import { NextRequest, NextResponse } from 'next/server';
2import { getStoredAuth } from '@/lib/server/auth';
3import fs from 'fs/promises';
4import path from 'path';
5
6/**
7 * Department Hierarchy Configuration API
8 * Stores and retrieves the hierarchical relationship between departments
9 * This is frontend configuration - backend departments remain flat
10 */
11
12const CONFIG_DIR = path.join(process.cwd(), 'data', 'config');
13const HIERARCHY_FILE = path.join(CONFIG_DIR, 'department-hierarchy.json');
14
15interface DepartmentHierarchy {
16 [parentDeptId: number]: number[]; // Maps parent ID to array of valid child IDs
17}
18
19// Ensure config directory exists
20async function ensureConfigDir() {
21 try {
22 await fs.mkdir(CONFIG_DIR, { recursive: true });
23 } catch (error) {
24 console.error('Error creating config directory:', error);
25 }
26}
27
28// GET - Retrieve department hierarchy configuration
29export async function GET(request: NextRequest) {
30 try {
31 // Verify authentication
32 const authData = await getStoredAuth();
33 if (!authData || !authData.authenticated) {
34 return NextResponse.json(
35 { error: 'Authentication required' },
36 { status: 401 }
37 );
38 }
39
40 try {
41 await ensureConfigDir();
42 const data = await fs.readFile(HIERARCHY_FILE, 'utf-8');
43 const hierarchy: DepartmentHierarchy = JSON.parse(data);
44
45 return NextResponse.json({
46 success: true,
47 data: hierarchy
48 });
49 } catch (error: any) {
50 // File doesn't exist or is invalid - return empty hierarchy
51 if (error.code === 'ENOENT') {
52 return NextResponse.json({
53 success: true,
54 data: {}
55 });
56 }
57 throw error;
58 }
59
60 } catch (error) {
61 console.error('Department hierarchy GET error:', error);
62 return NextResponse.json(
63 { error: 'Failed to retrieve department hierarchy' },
64 { status: 500 }
65 );
66 }
67}
68
69// POST - Save department hierarchy configuration
70export async function POST(request: NextRequest) {
71 try {
72 // Verify authentication
73 const authData = await getStoredAuth();
74 if (!authData || !authData.authenticated) {
75 return NextResponse.json(
76 { error: 'Authentication required' },
77 { status: 401 }
78 );
79 }
80
81 const body = await request.json();
82 const hierarchy: DepartmentHierarchy = body.hierarchy || {};
83
84 // Validate structure
85 for (const [parentId, children] of Object.entries(hierarchy)) {
86 if (!Array.isArray(children)) {
87 return NextResponse.json(
88 { error: 'Invalid hierarchy structure - children must be arrays' },
89 { status: 400 }
90 );
91 }
92 }
93
94 // Save to file
95 await ensureConfigDir();
96 await fs.writeFile(HIERARCHY_FILE, JSON.stringify(hierarchy, null, 2), 'utf-8');
97
98 return NextResponse.json({
99 success: true,
100 message: 'Department hierarchy saved successfully',
101 data: hierarchy
102 });
103
104 } catch (error) {
105 console.error('Department hierarchy POST error:', error);
106 return NextResponse.json(
107 { error: 'Failed to save department hierarchy' },
108 { status: 500 }
109 );
110 }
111}