121 lines
3.7 KiB
TypeScript
121 lines
3.7 KiB
TypeScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import { randomUUID } from 'node:crypto'; // Use standard node crypto
|
|
import { hakAksesPayloadSchema, type HakAksesPayload } from '~/server/utils/schemas/permissionSchema';
|
|
|
|
const filePath = path.resolve('data/mock/hakAkses.json');
|
|
|
|
// Helper to read JSON file
|
|
const readData = (): HakAksesPayload[] => {
|
|
try {
|
|
if (!fs.existsSync(filePath)) {
|
|
// Ensure directory exists
|
|
const dir = path.dirname(filePath);
|
|
if (!fs.existsSync(dir)) {
|
|
fs.mkdirSync(dir, { recursive: true });
|
|
}
|
|
fs.writeFileSync(filePath, '[]', 'utf-8');
|
|
return [];
|
|
}
|
|
const data = fs.readFileSync(filePath, 'utf-8');
|
|
return JSON.parse(data);
|
|
} catch (error) {
|
|
console.error('Error reading hakAkses.json:', error);
|
|
return [];
|
|
}
|
|
};
|
|
|
|
// Helper to write JSON file
|
|
const writeData = (data: HakAksesPayload[]): boolean => {
|
|
try {
|
|
fs.writeFileSync(filePath, JSON.stringify(data, null, 4), 'utf-8');
|
|
return true;
|
|
} catch (error) {
|
|
console.error('Error writing hakAkses.json:', error);
|
|
return false;
|
|
}
|
|
};
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const method = event.method;
|
|
|
|
// GET - List all hak akses
|
|
if (method === 'GET') {
|
|
const data = readData();
|
|
return {
|
|
success: true,
|
|
data
|
|
};
|
|
}
|
|
|
|
// POST - Create or Update hak akses
|
|
if (method === 'POST') {
|
|
try {
|
|
const rawBody = await readBody(event);
|
|
|
|
// Validate payload with Zod Schema (Contract testing)
|
|
const validationResult = hakAksesPayloadSchema.safeParse(rawBody);
|
|
|
|
if (!validationResult.success) {
|
|
return {
|
|
success: false,
|
|
message: 'Payload invalid: Gagal menyimpan hak akses',
|
|
error: validationResult.error.format()
|
|
};
|
|
}
|
|
|
|
const body = validationResult.data;
|
|
const data = readData();
|
|
|
|
if (body.id) {
|
|
// Update existing
|
|
const index = data.findIndex(h => h.id === body.id);
|
|
if (index !== -1) {
|
|
data[index] = {
|
|
...data[index],
|
|
...body
|
|
};
|
|
} else {
|
|
data.push(body as HakAksesPayload);
|
|
}
|
|
} else {
|
|
// Create new
|
|
body.id = randomUUID();
|
|
|
|
// For backward compatibility with HakAkses.vue UI that expects 'namaHakAkses'
|
|
if (!body.namaHakAkses) {
|
|
body.namaHakAkses = body.role || body.group || body.namaTipeUser || "Unknown";
|
|
}
|
|
if (!body.status) {
|
|
body.status = 'aktif';
|
|
}
|
|
|
|
data.push(body as HakAksesPayload);
|
|
}
|
|
|
|
const success = writeData(data);
|
|
|
|
if (success) {
|
|
return {
|
|
success: true,
|
|
message: 'Hak akses berhasil disimpan',
|
|
data: body.id ? body : data[data.length - 1]
|
|
};
|
|
} else {
|
|
throw new Error('Failed to save data');
|
|
}
|
|
} catch (error) {
|
|
return {
|
|
success: false,
|
|
message: 'Gagal memproses request',
|
|
error: error instanceof Error ? error.message : 'Unknown error'
|
|
};
|
|
}
|
|
}
|
|
|
|
return {
|
|
success: false,
|
|
message: 'Method not allowed'
|
|
};
|
|
});
|