From 5c3d6cfd6f68b6eb7192aba47463bd0541bfbf48 Mon Sep 17 00:00:00 2001
From: Miqdad <ahmadmiqdad27@gmail.com>
Date: Sat, 20 Sep 2025 09:27:05 +0700
Subject: <Miqdad> add role

---
 app/page.tsx | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 81 insertions(+), 14 deletions(-)

(limited to 'app/page.tsx')

diff --git a/app/page.tsx b/app/page.tsx
index 363b6b6..07a89f1 100644
--- a/app/page.tsx
+++ b/app/page.tsx
@@ -14,8 +14,51 @@ import { useEffect, useState } from "react";
 import { useRouter } from "next/navigation";
 import { getAuth } from "./lib/api/auth";
 
+// ====== ROLE EMAIL LISTS ======
+const DRIVER_EMAILS = new Set(
+  ["driverindoteknik@gmail.com", "sulistianaridwan8@gmail.com"]
+    .map(e => e.toLowerCase())
+);
+
+const DISPATCH_EMAILS = new Set(
+  ["rahmat.afiudin@gmail.com", "it@fixcomart.co.id"]
+    .map(e => e.toLowerCase())
+);
+
+function extractEmailFromAuth(auth: unknown): string | null {
+  if (auth && typeof auth === "object" && "email" in (auth as any)) {
+    const email = (auth as any).email;
+    if (typeof email === "string") return email;
+  }
+  if (auth && typeof auth === "object" && "token" in (auth as any)) {
+    const t = (auth as any).token;
+    if (typeof t === "string") {
+      const parts = t.split(".");
+      if (parts.length === 3) {
+        try {
+          const payload = JSON.parse(atob(parts[1]));
+          return payload?.email ?? payload?.preferred_username ?? payload?.sub ?? null;
+        } catch {}
+      }
+    }
+  }
+  if (typeof auth === "string") {
+    const parts = auth.split(".");
+    if (parts.length === 3) {
+      try {
+        const payload = JSON.parse(atob(parts[1]));
+        return payload?.email ?? payload?.preferred_username ?? payload?.sub ?? null;
+      } catch {}
+    }
+  }
+  return null;
+}
+
 export default function Home() {
   const [isLogin, setIsLogin] = useState<boolean>(true);
+  const [isDriver, setIsDriver] = useState<boolean>(false);
+  const [isDispatch, setIsDispatch] = useState<boolean>(false);
+
   const {
     barcode,
     imageSj,
@@ -26,17 +69,28 @@ export default function Home() {
     setImagePackage,
     setImageDispatch,
   } = useCameraStore();
-  const [isLoading, setIsLoading] = useState<boolean>(false);
 
+  const [isLoading, setIsLoading] = useState<boolean>(false);
   const router = useRouter();
 
   useEffect(() => {
     const token = getAuth();
+    console.log("FE auth (akan dipakai untuk header Token):", token);
 
     if (!token) {
       router.push("/login");
     } else {
       setIsLogin(true);
+
+      const email = extractEmailFromAuth(token);
+      const lower = (email ?? "").toLowerCase();
+
+      // PRIORITAS: dispatch > driver
+      const dispatchFlag = DISPATCH_EMAILS.has(lower);
+      const driverFlag = DRIVER_EMAILS.has(lower) && !dispatchFlag;
+
+      setIsDispatch(dispatchFlag);
+      setIsDriver(driverFlag);
     }
   }, [router]);
 
@@ -44,8 +98,15 @@ export default function Home() {
     event.preventDefault();
     setIsLoading(true);
 
-    if (!barcode || !imageSj || !imagePackage || !imageDispatch) {
-      alert("Barcode dan gambar harus tersedia.");
+    // Hanya role dispatch yang wajib foto Dispatch
+    const needDispatch = isDispatch;
+
+    if (!barcode || !imageSj || !imagePackage || (needDispatch && !imageDispatch)) {
+      alert(
+        needDispatch
+          ? "Barcode, Foto SJ, Foto Penerima, dan Foto Dispatch harus tersedia."
+          : "Barcode, Foto SJ, dan Foto Penerima harus tersedia."
+      );
       setIsLoading(false);
       return;
     }
@@ -53,19 +114,22 @@ export default function Home() {
     try {
       const newSjImage = imageSj.replace(/^.*?,/, "");
       const newPackageImage = imagePackage.replace(/^.*?,/, "");
-      const newDispatchImage = imageDispatch.replace(/^.*?,/, "");
+      const newDispatchImage = imageDispatch ? imageDispatch.replace(/^.*?,/, "") : undefined;
 
-      const data = {
-        sj_document: newSjImage, 
-        paket_document: newPackageImage, 
-        dispatch_document: newDispatchImage, 
+      const data: any = {
+        sj_document: newSjImage,
+        paket_document: newPackageImage,
       };
+      if (!isDriver && newDispatchImage) {
+        data.dispatch_document = newDispatchImage;
+      }
 
       const response = await odooApi(
         "PUT",
         `/api/v1/stock-picking/${barcode}/documentation`,
         data
       );
+
       if (response.status.code == 200) {
         alert("Berhasil Submit Data");
         setBarcode("");
@@ -73,11 +137,11 @@ export default function Home() {
         setImagePackage("");
         setImageDispatch("");
         setIsLoading(false);
-      }else if(response.status.code == 404){
-        alert("Gagal Submit Data, Picking Code Tidak Ditemukan " );
+      } else if (response.status.code == 404) {
+        alert("Gagal Submit Data, Picking Code Tidak Ditemukan ");
         setIsLoading(false);
-      }else{
-        alert("Gagal Submit Data, Silahkan Coba Lagi" );
+      } else {
+        alert("Gagal Submit Data, Silahkan Coba Lagi");
         setIsLoading(false);
       }
       return response.data;
@@ -107,7 +171,7 @@ export default function Home() {
             <div className="flex justify-between">
               <SjCamera />
               <PackageCamera />
-              <DispatchCamera />
+              {!isDriver && <DispatchCamera />} {/* disembunyikan untuk driver */}
             </div>
             <div className="h-2"></div>
 
@@ -148,8 +212,10 @@ export default function Home() {
                 </div>
               </>
             )}
+
             <div className="h-2"></div>
-            {imageDispatch && (
+
+            {!isDriver && imageDispatch && (
               <>
                 <label className="block mt-2 text-sm font-medium text-gray-700 text-center">
                   Gambar Foto Dispatch
@@ -166,6 +232,7 @@ export default function Home() {
                 </div>
               </>
             )}
+
             <div>
               <div className="h-4"></div>
               <Button
-- 
cgit v1.2.3

