Files
system-erp/src/lib/permissions.ts
T
2026-11-30 11:22:21 +07:00

255 lines
8.9 KiB
TypeScript

export type Role =
| "ADMIN"
| "MANAGER"
| "INVENTORY"
| "SALES"
| "PURCHASING"
| "ACCOUNTANT";
export const ROLE_PERMISSIONS: Record<
Role,
{
allowedRoutes: string[];
defaultRoute: string;
accessSummary: string;
allowedNotifTypes: ("STOCK_IN" | "STOCK_OUT" | "MONEY_IN" | "MONEY_OUT" | "RMA_SERVICE" | "SYSTEM")[];
defaultNotifPrefs: {
notifStockIn: boolean;
notifStockOut: boolean;
notifMoneyIn: boolean;
notifMoneyOut: boolean;
notifRma: boolean;
};
canAddUser: boolean;
canManageProducts: boolean;
canAdjustStock: boolean;
canCreateSalesOrder: boolean;
canUpdateSalesStatus: boolean;
canCreateRequisition: boolean;
canCreatePurchaseOrder: boolean;
canApproveFinancePurchase: boolean;
canReceiveGoods: boolean;
canCreateJournal: boolean;
canViewFinance: boolean;
canViewUsers: boolean;
canViewAudit: boolean;
canViewPriceAndHpp: boolean; // Khusus Sales, Finance, Manager, dan Admin
}
> = {
ADMIN: {
allowedRoutes: ["/", "/inventory", "/sales", "/purchases", "/finance", "/users", "/settings"],
defaultRoute: "/",
accessSummary: "Akses penuh seluruh modul, manajemen pengguna, dan konfigurasi notifikasi.",
allowedNotifTypes: ["STOCK_IN", "STOCK_OUT", "MONEY_IN", "MONEY_OUT", "RMA_SERVICE", "SYSTEM"],
defaultNotifPrefs: {
notifStockIn: true,
notifStockOut: true,
notifMoneyIn: true,
notifMoneyOut: true,
notifRma: true,
},
canAddUser: true,
canManageProducts: true,
canAdjustStock: true,
canCreateSalesOrder: true,
canUpdateSalesStatus: true,
canCreateRequisition: true,
canCreatePurchaseOrder: true,
canApproveFinancePurchase: true,
canReceiveGoods: true,
canCreateJournal: true,
canViewFinance: true,
canViewUsers: true,
canViewAudit: true,
canViewPriceAndHpp: true,
},
MANAGER: {
allowedRoutes: ["/", "/inventory", "/sales", "/purchases", "/finance", "/users", "/settings"],
defaultRoute: "/",
accessSummary: "Dashboard eksekutif View-Only, approval anggaran, dan pantauan seluruh divisi.",
allowedNotifTypes: ["STOCK_IN", "STOCK_OUT", "MONEY_IN", "MONEY_OUT", "RMA_SERVICE", "SYSTEM"],
defaultNotifPrefs: {
notifStockIn: true,
notifStockOut: true,
notifMoneyIn: true,
notifMoneyOut: true,
notifRma: true,
},
canAddUser: false, // Hanya ADMIN yang boleh menambah user baru!
canManageProducts: true,
canAdjustStock: true,
canCreateSalesOrder: true,
canUpdateSalesStatus: true,
canCreateRequisition: true,
canCreatePurchaseOrder: true,
canApproveFinancePurchase: true,
canReceiveGoods: true,
canCreateJournal: false, // Jurnal khusus Akuntan & Admin
canViewFinance: true,
canViewUsers: true,
canViewAudit: true,
canViewPriceAndHpp: true,
},
INVENTORY: {
allowedRoutes: ["/", "/inventory", "/sales", "/purchases", "/users"],
defaultRoute: "/",
accessSummary: "Khusus operasional gudang & teknisi: stok, SN, terima barang PO, input resi/ongkir kirim, dan device service/rusak/kanibalan.",
allowedNotifTypes: ["STOCK_IN", "STOCK_OUT", "RMA_SERVICE"],
defaultNotifPrefs: {
notifStockIn: true,
notifStockOut: true,
notifMoneyIn: false,
notifMoneyOut: false,
notifRma: true,
},
canAddUser: false,
canManageProducts: true,
canAdjustStock: true,
canCreateSalesOrder: false,
canUpdateSalesStatus: false,
canCreateRequisition: true, // Gudang boleh mengajukan restock jika barang menipis
canCreatePurchaseOrder: false,
canApproveFinancePurchase: false,
canReceiveGoods: true, // Boleh memverifikasi & menerima fisik barang datang dari distributor
canCreateJournal: false,
canViewFinance: false,
canViewUsers: true, // Bisa melihat dan mengedit profil sendiri
canViewAudit: true, // Bisa melihat riwayat PO, stok, dan pembelian operasional
canViewPriceAndHpp: false, // Gudang tidak boleh melihat harga jual & HPP keuntungan
},
SALES: {
allowedRoutes: ["/", "/sales", "/purchases", "/inventory", "/users"],
defaultRoute: "/",
accessSummary: "Khusus penjualan, pengajuan pembelian, dan layanan RMA/service customer.",
allowedNotifTypes: ["STOCK_OUT", "MONEY_IN", "RMA_SERVICE"],
defaultNotifPrefs: {
notifStockIn: false,
notifStockOut: true,
notifMoneyIn: true,
notifMoneyOut: false,
notifRma: true,
},
canAddUser: false,
canManageProducts: false, // Sales hanya melihat katalog & cek ketersediaan stok
canAdjustStock: false,
canCreateSalesOrder: true,
canUpdateSalesStatus: true,
canCreateRequisition: true, // Sales berwenang membuat Pengajuan Pembelian Barang
canCreatePurchaseOrder: false,
canApproveFinancePurchase: false,
canReceiveGoods: false,
canCreateJournal: false,
canViewFinance: false,
canViewUsers: true, // Bisa melihat dan mengedit profil sendiri
canViewAudit: true, // Bisa melihat riwayat PO, stok, dan pembelian operasional
canViewPriceAndHpp: true, // Sales berhak melihat harga jual dan HPP untuk negosiasi & penawaran
},
PURCHASING: {
allowedRoutes: ["/", "/purchases", "/inventory", "/users"],
defaultRoute: "/",
accessSummary: "Khusus pengadaan & PO ke vendor.",
allowedNotifTypes: ["STOCK_IN", "MONEY_OUT"],
defaultNotifPrefs: {
notifStockIn: true,
notifStockOut: false,
notifMoneyIn: false,
notifMoneyOut: true,
notifRma: false,
},
canAddUser: false,
canManageProducts: false,
canAdjustStock: false,
canCreateSalesOrder: false,
canUpdateSalesStatus: false,
canCreateRequisition: true,
canCreatePurchaseOrder: true, // Purchasing berwenang memproses pengajuan jadi PO ke vendor
canApproveFinancePurchase: false,
canReceiveGoods: true,
canCreateJournal: false,
canViewFinance: false,
canViewUsers: true, // Bisa melihat dan mengedit profil sendiri
canViewAudit: true, // Bisa melihat riwayat PO, stok, dan pembelian operasional
canViewPriceAndHpp: false, // Purchasing fokus ke harga beli PO ke vendor, tidak melihat margin jual & HPP perusahaan
},
ACCOUNTANT: {
allowedRoutes: ["/", "/finance", "/sales", "/purchases", "/users"],
defaultRoute: "/",
accessSummary: "Khusus keuangan & akuntansi: approve anggaran PO, biaya forwarder/pajak/PPN, pembayaran/DP vendor, pelunasan customer, payroll & jurnal.",
allowedNotifTypes: ["MONEY_IN", "MONEY_OUT"],
defaultNotifPrefs: {
notifStockIn: false,
notifStockOut: false,
notifMoneyIn: true,
notifMoneyOut: true,
notifRma: false,
},
canAddUser: false,
canManageProducts: false,
canAdjustStock: false,
canCreateSalesOrder: false,
canUpdateSalesStatus: true, // Boleh mengubah status pembayaran (PAID)
canCreateRequisition: false,
canCreatePurchaseOrder: false,
canApproveFinancePurchase: true, // Finance / Akuntan berwenang approve anggaran pembelian
canReceiveGoods: false,
canCreateJournal: true, // Akuntan berwenang membuat jurnal
canViewFinance: true,
canViewUsers: true, // Bisa melihat dan mengedit profil sendiri
canViewAudit: true,
canViewPriceAndHpp: true, // Finance berhak penuh melihat HPP & harga jual untuk pembukuan laba rugi
},
};
export function canAccessRoute(role: string | undefined, pathname: string): boolean {
if (!role) return false;
const userRole = (role as Role) || "SALES";
const permissions = ROLE_PERMISSIONS[userRole];
if (!permissions) return false;
// Exact match or sub-path match
return permissions.allowedRoutes.some(
(route) => pathname === route || (route !== "/" && pathname.startsWith(route))
);
}
export function getRolePermissions(role: string | undefined) {
const userRole = (role as Role) || "SALES";
return ROLE_PERMISSIONS[userRole] || ROLE_PERMISSIONS.SALES;
}
export function canViewPriceAndHpp(role: string | undefined): boolean {
if (!role) return false;
const userRole = (role as Role) || "SALES";
return ROLE_PERMISSIONS[userRole]?.canViewPriceAndHpp ?? false;
}
export function getDefaultLandingRoute(role: string | undefined): string {
if (!role) return "/";
const userRole = (role as Role) || "SALES";
return ROLE_PERMISSIONS[userRole]?.defaultRoute || "/";
}
export function getRoleDefaultNotifications(role: string | undefined) {
const userRole = (role as Role) || "SALES";
return (
ROLE_PERMISSIONS[userRole]?.defaultNotifPrefs || {
notifStockIn: false,
notifStockOut: true,
notifMoneyIn: true,
notifMoneyOut: false,
notifRma: true,
}
);
}
export function isRoleAllowedNotifType(
role: string | undefined,
notifType: "STOCK_IN" | "STOCK_OUT" | "MONEY_IN" | "MONEY_OUT" | "RMA_SERVICE" | "SYSTEM"
): boolean {
if (!role) return false;
const userRole = (role as Role) || "SALES";
const allowed = ROLE_PERMISSIONS[userRole]?.allowedNotifTypes;
if (!allowed) return false;
return allowed.includes(notifType);
}