Эх сурвалжийг харах

fix: on kitchen/get route, we now send table_number instead of table_id; feature: changes to database to include user_id on the order_item table; feature: sending user_name on the kitchen/get route

EduLascala 4 сар өмнө
parent
commit
c0662ccc28

+ 1 - 1
README.md

@@ -6,7 +6,7 @@
 
 3. run ```php -S localhost:8000 -t public```
 
-4. on another terminal run ```curl -X POST http://localhost:8000/register -H "Content-Type: application/json" -d '{"username":"du","email":"teste@email.com", "password":"du123456", "company_id":1, "role_id":1}'```
+4. on another terminal run ```curl -X POST http://localhost:8000/register -H "Content-Type: application/json" -d '{"username":"admin","email":"dulascala@hotmail.com", "password":"admin123", "company_id":1, "role_id":1}'```
 
 5. on the other terminal run ```./bin/testgetjwt```
 

+ 8 - 5
controllers/OrderItemCreateController.php

@@ -20,6 +20,8 @@ class OrderItemCreateController
     public function __invoke(ServerRequestInterface $request)
     {
         $body = json_decode((string)$request->getBody(), true) ?? [];
+        $userId = $request->getAttribute('api_user_id'); // Pega o user_id do token JWT
+        error_log("User ID: " . $userId);
 
         try {
             v::key('company_id', v::intType()->positive())
@@ -37,11 +39,12 @@ class OrderItemCreateController
         $kitchenNote = $body['kitchen_note'] ?? '';
 
         $created = $this->model->createOrderItem(
-            $orderId,
-            $productId,
-            $companyId,
-            $kitchenNote
-        );
+        $orderId,
+        $productId,
+        $companyId,
+        $userId,
+        $kitchenNote
+    );
 
         return $created
             ? ResponseLib::sendOk(['created' => true, 'order_item_id' => $created])

+ 1 - 1
middlewares/JWTAuthMiddleware.php

@@ -41,7 +41,7 @@ class JwtAuthMiddleware
             }
 
             // 4. Verifica se o usuário existe e está ativo no banco (similar ao HMAC)
-            $dbFile = $_ENV['DB_FILE'] ?? 'bridge.db';
+            $dbFile = $_ENV['DB_FILE'] ?? 'test.db';
             $dbPath = __DIR__ . '/../' . $dbFile;
             $pdo = new \PDO("sqlite:" . $dbPath);
             $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

+ 2 - 0
migrations/migrations_v1.sql

@@ -80,11 +80,13 @@ CREATE TABLE "order" (
 CREATE TABLE "order_item" (
     "order_item_id" INTEGER PRIMARY KEY AUTOINCREMENT,
     "order_id" INTEGER NOT NULL,
+    "user_id" INTEGER NOT NULL,
     "product_id" INTEGER NOT NULL,
     "company_id" INTEGER NOT NULL,
     "order_item_flag" TEXT NOT NULL,
     "order_item_kitchen_note" TEXT NOT NULL DEFAULT '',
     FOREIGN KEY ("order_id") REFERENCES "order" ("order_id"),
+    FOREIGN KEY ("user_id") REFERENCES "user" ("user_id"),
     FOREIGN KEY ("product_id") REFERENCES "product" ("product_id"),
     FOREIGN KEY ("company_id") REFERENCES "company" ("company_id")
 );

+ 49 - 45
models/KitchenModel.php

@@ -17,52 +17,56 @@ class KitchenModel
     public function getKitchenOrders(int $companyId): array
     {
         $sql = "
-            SELECT 
-                order_id,
-                table_id,
-                order_name,
-                order_created_at,
-                order_flag,
-                order_item_id,
-                order_item_flag,
-                product_name,
-                order_item_kitchen_note
-            FROM 'order' 
-            NATURAL JOIN order_item 
-            NATURAL JOIN product 
-            WHERE company_id = :company_id
-              AND order_flag IN ('a', 'p')
-              AND product_is_kitchen = 1
-            ORDER BY order_created_at ASC;   
-        ";
-    
-        $stmt = $this->pdo->prepare($sql);
-        $stmt->execute(['company_id' => $companyId]);
-        $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
+    SELECT 
+        o.order_id,
+        t.table_number,
+        u.user_name,
+        o.order_name,
+        o.order_created_at,
+        o.order_flag,
+        oi.order_item_id,
+        oi.order_item_flag,
+        p.product_name,
+        oi.order_item_kitchen_note
+    FROM `order` o
+    INNER JOIN order_item oi ON o.order_id = oi.order_id
+    INNER JOIN product p ON oi.product_id = p.product_id
+    INNER JOIN `table` t ON o.table_id = t.table_id
+    INNER JOIN user u ON o.user_id = u.user_id
+    WHERE o.company_id = :company_id
+      AND o.order_flag IN ('a', 'p')
+      AND p.product_is_kitchen = 1
+    ORDER BY o.order_created_at ASC;
+";
+
+$stmt = $this->pdo->prepare($sql);
+$stmt->execute(['company_id' => $companyId]);
+$rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
     
         $orders = [];
-        foreach ($rows as $row) {
-            $orderId = $row['order_id'];
-        
-            if (!isset($orders[$orderId])) {
-                $orders[$orderId] = [
-                    'order_id'         => $row['order_id'],
-                    'table_id'         => $row['table_id'],
-                    'order_name'       => $row['order_name'],
-                    'order_created_at' => $row['order_created_at'],
-                    'order_flag'       => $row['order_flag'],
-                    'items'            => []
-                ];
-            }
-        
-            $orders[$orderId]['items'][] = [
-                'order_item_id'    => $row['order_item_id'],
-                'order_item_flag'  => $row['order_item_flag'],
-                'product_name'     => $row['product_name'],
-                'kitchen_note'     => $row['order_item_kitchen_note'] ?? ''
-            ];
-        }
-    
-        return array_values($orders);
+foreach ($rows as $row) {
+    $orderId = $row['order_id'];
+    if (!isset($orders[$orderId])) {
+        $orders[$orderId] = [
+    'order_id'         => $row['order_id'],
+    'table_number'     => $row['table_number'],
+    'user_name'        => $row['user_name'],
+    'order_name'       => $row['order_name'],
+    'order_created_at' => $row['order_created_at'],
+    'order_flag'       => $row['order_flag'],
+    'items'            => []
+];
+
+    }
+
+    $orders[$orderId]['items'][] = [
+        'order_item_id'    => $row['order_item_id'],
+        'order_item_flag'  => $row['order_item_flag'],
+        'product_name'     => $row['product_name'],
+        'kitchen_note'     => $row['order_item_kitchen_note'] ?? ''
+    ];
+}
+
+return array_values($orders);
     }
 }

+ 25 - 22
models/OrderItemModel.php

@@ -23,6 +23,7 @@ class OrderItemModel
         int $orderId,
         int $productId,
         int $companyId,
+        int $userId,
         string $kitchenNote = ''
     ): int|false
     {
@@ -40,28 +41,30 @@ class OrderItemModel
         }
 
         $stmt = $this->pdo->prepare("
-            INSERT INTO order_item (
-                order_id,
-                product_id,
-                company_id,
-                order_item_flag,
-                order_item_kitchen_note
-            ) VALUES (
-                :order_id,
-                :product_id,
-                :company_id,
-                'a',
-                :order_item_kitchen_note
-            )
-        ");
-
-        try {
-            $executed = $stmt->execute([
-                'order_id' => $orderId,
-                'product_id' => $productId,
-                'company_id' => $companyId,
-                'order_item_kitchen_note' => $kitchenNote
-            ]);
+        INSERT INTO order_item (
+            order_id,
+            product_id,
+            user_id,
+            company_id,
+            order_item_flag,
+            order_item_kitchen_note
+        ) VALUES (
+            :order_id,
+            :product_id,
+            :user_id,
+            :company_id,
+            'a',
+            :order_item_kitchen_note
+        )
+    ");
+    try {
+        $executed = $stmt->execute([
+            'order_id' => $orderId,
+            'product_id' => $productId,
+            'user_id' => $userId, // Adicione a variável
+            'company_id' => $companyId,
+            'order_item_kitchen_note' => $kitchenNote
+        ]);
 
             if ($executed) {
                 if (!empty($product['product_is_kitchen']) && $product['product_is_kitchen'] == 1) {