Compare commits
9 Commits
amalia/15-
...
amalia/21-
| Author | SHA1 | Date | |
|---|---|---|---|
| c0a4d584af | |||
| 9ac105e7bc | |||
| 10457e96e8 | |||
| 9ad934c99f | |||
| 5bfcde32ed | |||
| 8240d608ad | |||
| fd7d08d38a | |||
| b95fd9543c | |||
| 7622c58ce4 |
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "sistem-desa-mandiri",
|
||||
"version": "0.1.13",
|
||||
"version": "0.1.16",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "next dev --experimental-https",
|
||||
|
||||
1
prisma/migrations/20260518071507_auto/migration.sql
Normal file
1
prisma/migrations/20260518071507_auto/migration.sql
Normal file
@@ -0,0 +1 @@
|
||||
-- This is an empty migration.
|
||||
1
prisma/migrations/20260519080535_auto/migration.sql
Normal file
1
prisma/migrations/20260519080535_auto/migration.sql
Normal file
@@ -0,0 +1 @@
|
||||
-- This is an empty migration.
|
||||
1
prisma/migrations/20260521030721_auto/migration.sql
Normal file
1
prisma/migrations/20260521030721_auto/migration.sql
Normal file
@@ -0,0 +1 @@
|
||||
-- This is an empty migration.
|
||||
@@ -1,3 +1,4 @@
|
||||
import { isValidApiKey } from "@/lib/apiKey";
|
||||
import { prisma } from "@/module/_global";
|
||||
import cors from "@elysiajs/cors";
|
||||
import { swagger } from "@elysiajs/swagger";
|
||||
@@ -6,20 +7,6 @@ import _ from "lodash";
|
||||
import moment from "moment";
|
||||
import "moment/locale/id";
|
||||
|
||||
const CACHE_TTL_MS = 60_000;
|
||||
let apiKeyCache: Set<string> = new Set();
|
||||
let cacheExpiresAt = 0;
|
||||
|
||||
async function isValidApiKey(incoming: string): Promise<boolean> {
|
||||
const now = Date.now();
|
||||
if (now > cacheExpiresAt) {
|
||||
const rows = await prisma.apiKey.findMany({ where: { isActive: true }, select: { key: true } });
|
||||
apiKeyCache = new Set(rows.map((r) => r.key));
|
||||
cacheExpiresAt = now + CACHE_TTL_MS;
|
||||
}
|
||||
return apiKeyCache.has(incoming);
|
||||
}
|
||||
|
||||
const AiServer = new Elysia({ prefix: "/api/ai" })
|
||||
.use(cors({
|
||||
origin: "*",
|
||||
|
||||
@@ -96,13 +96,13 @@ async function sendNotification({
|
||||
}
|
||||
}
|
||||
|
||||
async function getApproversInVillage(idVillage: string): Promise<NotifTarget[]> {
|
||||
async function getApproversInVillage(idVillage: string, idGroup: string): Promise<NotifTarget[]> {
|
||||
const approvers = await prisma.user.findMany({
|
||||
where: {
|
||||
isActive: true,
|
||||
idVillage,
|
||||
OR: [
|
||||
{ isApprover: true },
|
||||
{ isApprover: true, idGroup },
|
||||
{ UserRole: { id: 'supadmin' } }
|
||||
]
|
||||
},
|
||||
@@ -198,7 +198,10 @@ export async function POST(request: Request, context: { params: { id: string } }
|
||||
|
||||
const task = await prisma.projectTask.findUnique({
|
||||
where: { id, isActive: true },
|
||||
select: { id: true, status: true, idProject: true, title: true }
|
||||
select: {
|
||||
id: true, status: true, title: true,
|
||||
Project: { select: { id: true, idGroup: true } }
|
||||
}
|
||||
});
|
||||
|
||||
if (!task) {
|
||||
@@ -227,14 +230,14 @@ export async function POST(request: Request, context: { params: { id: string } }
|
||||
})
|
||||
]);
|
||||
|
||||
await recalculateProjectStatus(task.idProject);
|
||||
await recalculateProjectStatus(task.Project.id);
|
||||
|
||||
// Notifikasi ke semua approver
|
||||
const approverTargets = await getApproversInVillage(String(userMobile.idVillage));
|
||||
// Notifikasi ke semua approver di desa dan group yang sama
|
||||
const approverTargets = await getApproversInVillage(String(userMobile.idVillage), task.Project.idGroup);
|
||||
await sendNotification({
|
||||
targets: approverTargets,
|
||||
idUserFrom: userMobile.id,
|
||||
idContent: task.idProject,
|
||||
idContent: task.Project.id,
|
||||
title: 'Pengajuan Penyelesaian Tugas',
|
||||
desc: task.title,
|
||||
});
|
||||
@@ -271,7 +274,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
|
||||
|
||||
const task = await prisma.projectTask.findUnique({
|
||||
where: { id, isActive: true },
|
||||
select: { id: true, status: true, idProject: true, title: true }
|
||||
select: { id: true, status: true, title: true, Project: { select: { id: true } } }
|
||||
});
|
||||
|
||||
if (!task) {
|
||||
@@ -304,7 +307,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
|
||||
})
|
||||
]);
|
||||
|
||||
await recalculateProjectStatus(task.idProject);
|
||||
await recalculateProjectStatus(task.Project.id);
|
||||
|
||||
// Notifikasi ke submitter
|
||||
const submitterTarget = await getUserNotifTarget(pendingApproval.idUser);
|
||||
@@ -312,7 +315,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
|
||||
await sendNotification({
|
||||
targets: [submitterTarget],
|
||||
idUserFrom: userMobile.id,
|
||||
idContent: task.idProject,
|
||||
idContent: task.Project.id,
|
||||
title: 'Tugas Disetujui',
|
||||
desc: task.title,
|
||||
});
|
||||
@@ -339,7 +342,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
|
||||
})
|
||||
]);
|
||||
|
||||
await recalculateProjectStatus(task.idProject);
|
||||
await recalculateProjectStatus(task.Project.id);
|
||||
|
||||
// Notifikasi ke submitter
|
||||
const submitterTarget = await getUserNotifTarget(pendingApproval.idUser);
|
||||
@@ -347,7 +350,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
|
||||
await sendNotification({
|
||||
targets: [submitterTarget],
|
||||
idUserFrom: userMobile.id,
|
||||
idContent: task.idProject,
|
||||
idContent: task.Project.id,
|
||||
title: 'Tugas Ditolak',
|
||||
desc: task.title,
|
||||
});
|
||||
|
||||
@@ -25,6 +25,9 @@ export async function GET(request: Request, context: { params: { id: string } })
|
||||
where: {
|
||||
id: String(id),
|
||||
isActive: true
|
||||
},
|
||||
include: {
|
||||
Division: { select: { idGroup: true } }
|
||||
}
|
||||
});
|
||||
|
||||
@@ -33,7 +36,7 @@ export async function GET(request: Request, context: { params: { id: string } })
|
||||
}
|
||||
|
||||
if (kategori == "data") {
|
||||
allData = data
|
||||
allData = { ...data, idGroup: data.Division.idGroup }
|
||||
} else if (kategori == "progress") {
|
||||
const dataProgress = await prisma.divisionProjectTask.findMany({
|
||||
where: {
|
||||
|
||||
@@ -96,13 +96,19 @@ async function sendNotification({
|
||||
}
|
||||
|
||||
async function getApproversForDivision(idVillage: string, idDivision: string): Promise<NotifTarget[]> {
|
||||
const division = await prisma.division.findUnique({
|
||||
where: { id: idDivision },
|
||||
select: { idGroup: true }
|
||||
});
|
||||
const idGroup = division?.idGroup;
|
||||
|
||||
const [globalApprovers, divisionAdmins] = await Promise.all([
|
||||
prisma.user.findMany({
|
||||
where: {
|
||||
isActive: true,
|
||||
idVillage,
|
||||
OR: [
|
||||
{ isApprover: true },
|
||||
{ isApprover: true, idGroup },
|
||||
{ UserRole: { id: 'supadmin' } }
|
||||
]
|
||||
},
|
||||
@@ -285,23 +291,35 @@ export async function PUT(request: Request, context: { params: { id: string } })
|
||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
|
||||
}
|
||||
|
||||
const canApprove = await getApproverStatus(userMobile.id);
|
||||
if (!canApprove) {
|
||||
// Check if division admin
|
||||
const task = await prisma.divisionProjectTask.findUnique({
|
||||
where: { id, isActive: true },
|
||||
select: { idDivision: true }
|
||||
});
|
||||
if (task) {
|
||||
const isDivAdmin = await prisma.divisionMember.count({
|
||||
where: { idDivision: task.idDivision, idUser: userMobile.id, isAdmin: true, isActive: true }
|
||||
});
|
||||
if (isDivAdmin === 0) {
|
||||
return NextResponse.json({ success: false, message: "Anda tidak memiliki izin untuk menyetujui atau menolak tugas" }, { status: 200 });
|
||||
}
|
||||
} else {
|
||||
return NextResponse.json({ success: false, message: "Tugas tidak ditemukan" }, { status: 200 });
|
||||
}
|
||||
const taskForAuth = await prisma.divisionProjectTask.findUnique({
|
||||
where: { id, isActive: true },
|
||||
select: { idDivision: true }
|
||||
});
|
||||
if (!taskForAuth) {
|
||||
return NextResponse.json({ success: false, message: "Tugas tidak ditemukan" }, { status: 200 });
|
||||
}
|
||||
|
||||
const [division, userFull, isDivAdmin] = await Promise.all([
|
||||
prisma.division.findUnique({
|
||||
where: { id: taskForAuth.idDivision },
|
||||
select: { idGroup: true, idVillage: true }
|
||||
}),
|
||||
prisma.user.findUnique({
|
||||
where: { id: userMobile.id },
|
||||
select: { isApprover: true, idGroup: true, idVillage: true, UserRole: { select: { id: true } } }
|
||||
}),
|
||||
prisma.divisionMember.count({
|
||||
where: { idDivision: taskForAuth.idDivision, idUser: userMobile.id, isAdmin: true, isActive: true }
|
||||
})
|
||||
]);
|
||||
|
||||
const isSupadmin = APPROVER_ROLES.includes(userFull?.UserRole?.id ?? '');
|
||||
const isGroupApprover = !!(userFull?.isApprover &&
|
||||
userFull.idVillage === division?.idVillage &&
|
||||
userFull.idGroup === division?.idGroup);
|
||||
|
||||
if (!isSupadmin && !isGroupApprover && isDivAdmin === 0) {
|
||||
return NextResponse.json({ success: false, message: "Anda tidak memiliki izin untuk menyetujui atau menolak tugas" }, { status: 200 });
|
||||
}
|
||||
|
||||
const task = await prisma.divisionProjectTask.findUnique({
|
||||
|
||||
@@ -1242,6 +1242,7 @@ const MonitoringServer = new Elysia({ prefix: "/api/monitoring" })
|
||||
email: true,
|
||||
isWithoutOTP: true,
|
||||
isActive: true,
|
||||
isApprover: true,
|
||||
idUserRole: true,
|
||||
idVillage: true,
|
||||
idGroup: true,
|
||||
@@ -1328,6 +1329,7 @@ const MonitoringServer = new Elysia({ prefix: "/api/monitoring" })
|
||||
gender: item.gender,
|
||||
isWithoutOTP: item.isWithoutOTP,
|
||||
isActive: item.isActive,
|
||||
isApprover: item.isApprover,
|
||||
role: item.UserRole?.name,
|
||||
village: item.Village?.name,
|
||||
group: item.Group?.name,
|
||||
@@ -1440,7 +1442,7 @@ const MonitoringServer = new Elysia({ prefix: "/api/monitoring" })
|
||||
}
|
||||
)
|
||||
.post("/edit-user", async ({ body, set }) => {
|
||||
const { id, name, nik, phone, email, gender, idUserRole, idVillage, idGroup, idPosition, isActive, isWithoutOTP } = body;
|
||||
const { id, name, nik, phone, email, gender, idUserRole, idVillage, idGroup, idPosition, isActive, isWithoutOTP, isApprover } = body;
|
||||
|
||||
try {
|
||||
const cekId = await prisma.user.findFirst({
|
||||
@@ -1492,6 +1494,7 @@ const MonitoringServer = new Elysia({ prefix: "/api/monitoring" })
|
||||
idPosition,
|
||||
isActive,
|
||||
isWithoutOTP,
|
||||
isApprover,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1522,6 +1525,7 @@ const MonitoringServer = new Elysia({ prefix: "/api/monitoring" })
|
||||
idPosition: t.Optional(t.Union([t.String(), t.Null()], { description: "ID Posisi" })),
|
||||
isActive: t.Boolean({ description: "Aktif" }),
|
||||
isWithoutOTP: t.Boolean({ description: "Tanpa OTP" }),
|
||||
isApprover: t.Boolean({ description: "Approver" }),
|
||||
}),
|
||||
detail: {
|
||||
summary: "Edit User",
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { isValidApiKey } from "@/lib/apiKey";
|
||||
import { prisma } from "@/module/_global";
|
||||
import cors from "@elysiajs/cors";
|
||||
import { swagger } from "@elysiajs/swagger";
|
||||
@@ -11,20 +12,40 @@ const NocServer = new Elysia({ prefix: "/api/noc" })
|
||||
.use(cors({
|
||||
origin: "*",
|
||||
methods: ["GET", "POST", "OPTIONS"],
|
||||
allowedHeaders: ["Content-Type", "x-api-key"],
|
||||
}))
|
||||
.use(swagger({
|
||||
path: "/docs", // Karena prefix instance adalah /api/noc, maka ini akan diakses di /api/noc/docs
|
||||
path: "/docs",
|
||||
documentation: {
|
||||
info: {
|
||||
title: "Sistem Desa Mandiri - NOC API",
|
||||
version: "1.0.0",
|
||||
description: "API Khusus untuk kebutuhan NOC (Network Operation Center) dan Monitoring Desa",
|
||||
},
|
||||
components: {
|
||||
securitySchemes: {
|
||||
ApiKeyAuth: {
|
||||
type: "apiKey",
|
||||
in: "header",
|
||||
name: "x-api-key",
|
||||
},
|
||||
},
|
||||
},
|
||||
security: [{ ApiKeyAuth: [] }],
|
||||
tags: [
|
||||
{ name: "NOC", description: "Endpoint khusus monitoring" }
|
||||
]
|
||||
}
|
||||
}))
|
||||
.onBeforeHandle(async ({ request, set, path }) => {
|
||||
if (path.startsWith("/api/noc/docs")) return;
|
||||
|
||||
const incoming = request.headers.get("x-api-key");
|
||||
if (!incoming || !(await isValidApiKey(incoming))) {
|
||||
set.status = 401;
|
||||
return { success: false, message: "Unauthorized" };
|
||||
}
|
||||
})
|
||||
|
||||
// ── GET /api/noc/active-divisions ──────────────────────────────────────────
|
||||
.get(
|
||||
|
||||
15
src/lib/apiKey.ts
Normal file
15
src/lib/apiKey.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
|
||||
const CACHE_TTL_MS = 60_000;
|
||||
let apiKeyCache: Set<string> = new Set();
|
||||
let cacheExpiresAt = 0;
|
||||
|
||||
export async function isValidApiKey(incoming: string): Promise<boolean> {
|
||||
const now = Date.now();
|
||||
if (now > cacheExpiresAt) {
|
||||
const rows = await prisma.apiKey.findMany({ where: { isActive: true }, select: { key: true } });
|
||||
apiKeyCache = new Set(rows.map((r) => r.key));
|
||||
cacheExpiresAt = now + CACHE_TTL_MS;
|
||||
}
|
||||
return apiKeyCache.has(incoming);
|
||||
}
|
||||
Reference in New Issue
Block a user