feat: filter approver berdasarkan group pada project dan division task
- project/task approval: filter isApprover berdasarkan desa + group project - project/task approval: supadmin tetap hanya filter desa - division/task approval: expose idGroup dari Division pada response cat=data - division/task approval: filter isApprover berdasarkan desa + group division - division/task approval PUT: ganti getApproverStatus dengan cek langsung berdasarkan village, group, dan keanggotaan division admin
This commit is contained in:
@@ -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({
|
const approvers = await prisma.user.findMany({
|
||||||
where: {
|
where: {
|
||||||
isActive: true,
|
isActive: true,
|
||||||
idVillage,
|
idVillage,
|
||||||
OR: [
|
OR: [
|
||||||
{ isApprover: true },
|
{ isApprover: true, idGroup },
|
||||||
{ UserRole: { id: 'supadmin' } }
|
{ UserRole: { id: 'supadmin' } }
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -198,7 +198,10 @@ export async function POST(request: Request, context: { params: { id: string } }
|
|||||||
|
|
||||||
const task = await prisma.projectTask.findUnique({
|
const task = await prisma.projectTask.findUnique({
|
||||||
where: { id, isActive: true },
|
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) {
|
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
|
// Notifikasi ke semua approver di desa dan group yang sama
|
||||||
const approverTargets = await getApproversInVillage(String(userMobile.idVillage));
|
const approverTargets = await getApproversInVillage(String(userMobile.idVillage), task.Project.idGroup);
|
||||||
await sendNotification({
|
await sendNotification({
|
||||||
targets: approverTargets,
|
targets: approverTargets,
|
||||||
idUserFrom: userMobile.id,
|
idUserFrom: userMobile.id,
|
||||||
idContent: task.idProject,
|
idContent: task.Project.id,
|
||||||
title: 'Pengajuan Penyelesaian Tugas',
|
title: 'Pengajuan Penyelesaian Tugas',
|
||||||
desc: task.title,
|
desc: task.title,
|
||||||
});
|
});
|
||||||
@@ -271,7 +274,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
|
|||||||
|
|
||||||
const task = await prisma.projectTask.findUnique({
|
const task = await prisma.projectTask.findUnique({
|
||||||
where: { id, isActive: true },
|
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) {
|
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
|
// Notifikasi ke submitter
|
||||||
const submitterTarget = await getUserNotifTarget(pendingApproval.idUser);
|
const submitterTarget = await getUserNotifTarget(pendingApproval.idUser);
|
||||||
@@ -312,7 +315,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
|
|||||||
await sendNotification({
|
await sendNotification({
|
||||||
targets: [submitterTarget],
|
targets: [submitterTarget],
|
||||||
idUserFrom: userMobile.id,
|
idUserFrom: userMobile.id,
|
||||||
idContent: task.idProject,
|
idContent: task.Project.id,
|
||||||
title: 'Tugas Disetujui',
|
title: 'Tugas Disetujui',
|
||||||
desc: task.title,
|
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
|
// Notifikasi ke submitter
|
||||||
const submitterTarget = await getUserNotifTarget(pendingApproval.idUser);
|
const submitterTarget = await getUserNotifTarget(pendingApproval.idUser);
|
||||||
@@ -347,7 +350,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
|
|||||||
await sendNotification({
|
await sendNotification({
|
||||||
targets: [submitterTarget],
|
targets: [submitterTarget],
|
||||||
idUserFrom: userMobile.id,
|
idUserFrom: userMobile.id,
|
||||||
idContent: task.idProject,
|
idContent: task.Project.id,
|
||||||
title: 'Tugas Ditolak',
|
title: 'Tugas Ditolak',
|
||||||
desc: task.title,
|
desc: task.title,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -25,6 +25,9 @@ export async function GET(request: Request, context: { params: { id: string } })
|
|||||||
where: {
|
where: {
|
||||||
id: String(id),
|
id: String(id),
|
||||||
isActive: true
|
isActive: true
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
Division: { select: { idGroup: true } }
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -33,7 +36,7 @@ export async function GET(request: Request, context: { params: { id: string } })
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (kategori == "data") {
|
if (kategori == "data") {
|
||||||
allData = data
|
allData = { ...data, idGroup: data.Division.idGroup }
|
||||||
} else if (kategori == "progress") {
|
} else if (kategori == "progress") {
|
||||||
const dataProgress = await prisma.divisionProjectTask.findMany({
|
const dataProgress = await prisma.divisionProjectTask.findMany({
|
||||||
where: {
|
where: {
|
||||||
|
|||||||
@@ -96,13 +96,19 @@ async function sendNotification({
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function getApproversForDivision(idVillage: string, idDivision: string): Promise<NotifTarget[]> {
|
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([
|
const [globalApprovers, divisionAdmins] = await Promise.all([
|
||||||
prisma.user.findMany({
|
prisma.user.findMany({
|
||||||
where: {
|
where: {
|
||||||
isActive: true,
|
isActive: true,
|
||||||
idVillage,
|
idVillage,
|
||||||
OR: [
|
OR: [
|
||||||
{ isApprover: true },
|
{ isApprover: true, idGroup },
|
||||||
{ UserRole: { id: 'supadmin' } }
|
{ 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 });
|
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const canApprove = await getApproverStatus(userMobile.id);
|
const taskForAuth = await prisma.divisionProjectTask.findUnique({
|
||||||
if (!canApprove) {
|
where: { id, isActive: true },
|
||||||
// Check if division admin
|
select: { idDivision: true }
|
||||||
const task = await prisma.divisionProjectTask.findUnique({
|
});
|
||||||
where: { id, isActive: true },
|
if (!taskForAuth) {
|
||||||
select: { idDivision: true }
|
return NextResponse.json({ success: false, message: "Tugas tidak ditemukan" }, { status: 200 });
|
||||||
});
|
}
|
||||||
if (task) {
|
|
||||||
const isDivAdmin = await prisma.divisionMember.count({
|
const [division, userFull, isDivAdmin] = await Promise.all([
|
||||||
where: { idDivision: task.idDivision, idUser: userMobile.id, isAdmin: true, isActive: true }
|
prisma.division.findUnique({
|
||||||
});
|
where: { id: taskForAuth.idDivision },
|
||||||
if (isDivAdmin === 0) {
|
select: { idGroup: true, idVillage: true }
|
||||||
return NextResponse.json({ success: false, message: "Anda tidak memiliki izin untuk menyetujui atau menolak tugas" }, { status: 200 });
|
}),
|
||||||
}
|
prisma.user.findUnique({
|
||||||
} else {
|
where: { id: userMobile.id },
|
||||||
return NextResponse.json({ success: false, message: "Tugas tidak ditemukan" }, { status: 200 });
|
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({
|
const task = await prisma.divisionProjectTask.findUnique({
|
||||||
|
|||||||
Reference in New Issue
Block a user