Browse Source

feat: order_item_kitchen_note flag option for kitchen and status fixes

Fernando 4 months ago
parent
commit
ec1c1c9c83

+ 1 - 1
controllers/CategoryCreateController.php

@@ -26,7 +26,7 @@ class CategoryCreateController
              ->key('category_name', v::stringType()->notEmpty()->regex('/^[\p{L}\p{N}\s\-\'\"]+$/u'))
              ->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $companyId = $body['company_id'];

+ 1 - 1
controllers/CategoryDeleteController.php

@@ -26,7 +26,7 @@ class CategoryDeleteController
              ->key('category_name', v::stringType()->notEmpty()->regex('/^[\p{L}\p{N}\s\-\'\"]+$/u'))
              ->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $companyId = $body['company_id'];

+ 1 - 1
controllers/CategoryGetController.php

@@ -25,7 +25,7 @@ class CategoryGetController
             v::key('company_id', v::intType()->positive())
              ->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $companyId = (int) $body['company_id'];

+ 1 - 1
controllers/CategoryUpdateFlagController.php

@@ -27,7 +27,7 @@ class CategoryUpdateFlagController
              ->key('category_flag', v::stringType()->notEmpty())
              ->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $companyId = $body['company_id'];

+ 1 - 1
controllers/DescriptionCreateController.php

@@ -20,7 +20,7 @@ class DescriptionCreateController
              ->key('description_text', v::stringType()->notEmpty()->regex('/^[\p{L}\p{N}\s\-\'\"]+$/u'))
              ->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $model = new DescriptionModel();

+ 1 - 1
controllers/DescriptionUpdateController.php

@@ -20,7 +20,7 @@ class DescriptionUpdateController
              ->key('description_text', v::stringType()->notEmpty())
              ->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $productId = (int) $body['product_id'];

+ 1 - 1
controllers/KitchenGetController.php

@@ -24,7 +24,7 @@ class KitchenGetController
         try {
             v::key('company_id', v::intType()->positive())->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $companyId = (int) $body['company_id'];

+ 1 - 1
controllers/LoginController.php

@@ -20,7 +20,7 @@ class LoginController
              ->key('password', v::stringType()->notEmpty())
              ->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $username = $body['username'];

+ 1 - 1
controllers/OrderCreateController.php

@@ -33,7 +33,7 @@ class OrderCreateController
              ->key('status_status', v::stringType()->notEmpty()->in(['Aberta', 'Finalizada', 'Cancelada']))
              ->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $tableId = (int) $body['table_id'];

+ 1 - 1
controllers/OrderDeleteController.php

@@ -26,7 +26,7 @@ class OrderDeleteController
                 ->key('company_id', v::intType()->positive())
                 ->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
     
         $orderId = (int) $body['order_id'];

+ 1 - 1
controllers/OrderGetController.php

@@ -30,7 +30,7 @@ class OrderGetController
                 ->key('table_id', v::optional(v::intType()->positive()), false) // Adicionado table_id como opcional
                 ->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $companyId = (int) $body['company_id'];

+ 1 - 1
controllers/OrderItemCreateController.php

@@ -28,7 +28,7 @@ class OrderItemCreateController
               ->key('kitchen_note', v::optional(v::stringType()), false)
               ->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $companyId = (int) $body['company_id'];

+ 1 - 1
controllers/OrderItemDeleteController.php

@@ -27,7 +27,7 @@ class OrderItemDeleteController
                 ->key('company_id', v::intType()->positive()) 
                 ->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $orderItemId = (int) $body['order_item_id'];

+ 1 - 5
controllers/OrderItemGetController.php

@@ -32,17 +32,13 @@ class OrderItemGetController
                 }
             }
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $companyId = (int) $body['company_id'];
         $orderId = isset($body['order_id']) ? (int) $body['order_id'] : null;
         $page = isset($body['page']) ? (int) $body['page'] : 1;
 
-        if ($page < 1) {
-            $page = 1;
-        }
-
         $orderItems = $this->model->getOrderItemsByOrderId($orderId, $companyId, $page);
 
         $productModel = new ProductModel();

+ 4 - 4
controllers/OrderUpdateController.php

@@ -25,19 +25,19 @@ class OrderUpdateController
             v::key('company_id', v::intType()->positive())
                 ->key('order_id', v::intType()->positive())
                 ->key('status_status', v::stringType()->notEmpty()->in(['Aberta', 'Finalizada', 'Cancelada']), true)
-                ->optional(v::key('order_flag', v::stringType()->notEmpty()->in(['Dinheiro', 'PIX', 'Cartão de Crédito', 'Cartão de Débito']), true))
+                ->optional(v::key('order_flag', v::stringType()->notEmpty()->in(['Dinheiro', 'PIX', 'Cartão de Crédito', 'Cartão de Débito', 'p', 'a']), true))
                 ->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $companyId    = (int) $body['company_id'];
         $orderId      = (int) $body['order_id'];
         $statusStatus = $body['status_status'];
-        $orderFlag    = $body['order_flag'] ?? null;
+        $orderFlag    = $body['order_flag'] ?? '';
 
         $statusId = $this->model->getStatusIdByName($statusStatus);
-        if ($statusId === null) {
+        if ($statusId === '') {
             return ResponseLib::sendFail("Invalid status_status Provided: '{$statusStatus}'", [], "E_VALIDATE")->withStatus(400);
         }
 

+ 1 - 1
controllers/ProductCreateController.php

@@ -37,7 +37,7 @@ class ProductCreateController
         }
 
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $companyId = $body['company_id'];

+ 1 - 1
controllers/ProductDeleteController.php

@@ -26,7 +26,7 @@ class ProductDeleteController
              ->key('product_name', v::stringType()->notEmpty()->regex('/^[\p{L}\p{N}\s\-\'\"]+$/u'))
              ->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $companyId = (int) $body['company_id'];

+ 1 - 1
controllers/ProductGetController.php

@@ -26,7 +26,7 @@ class ProductGetController
              ->key('show_description', v::optional(v::boolType()), false)
              ->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $companyId = (int) $body['company_id'];

+ 1 - 1
controllers/ProductUpdateController.php

@@ -30,7 +30,7 @@ class ProductUpdateController
              ->key('product_is_kitchen', v::optional(v::boolType()), false)
              ->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $hasProductName = isset($body['product_name']) && $body['product_name'] !== null;

+ 1 - 1
controllers/RegisterController.php

@@ -22,7 +22,7 @@ class RegisterController
              ->key('role_id', v::intType()->positive())
              ->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $username = $body['username'];

+ 1 - 1
controllers/ReportsGetController.php

@@ -26,7 +26,7 @@ class ReportsGetController
             v::optional(v::key('page', v::intType()->positive()))->assert($body);
             v::optional(v::key('limit', v::intType()->positive()))->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $companyId = (int) $body['company_id'];

+ 1 - 1
controllers/TableCreateController.php

@@ -28,7 +28,7 @@ class TableCreateController
              ->key('status_id', v::intVal()->positive())
              ->assert($body);
         } catch (NestedValidationException $e) {
-            return ResponseLib::sendFail($e->getMessages(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail($e->getMessages(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $companyId = (int) $body['company_id'];

+ 1 - 1
controllers/TableDeleteController.php

@@ -30,7 +30,7 @@ class TableDeleteController
              ->key('company_id', v::intType()->positive())
              ->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $deleted = $this->model->deleteTableByNumber((string)$tableNumber, (int)$companyId);

+ 1 - 1
controllers/TableGetController.php

@@ -26,7 +26,7 @@ class TableGetController
             v::key('company_id', v::intType()->positive())
              ->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $companyId = $body['company_id'];

+ 1 - 1
controllers/TableUpdateController.php

@@ -28,7 +28,7 @@ class TableUpdateController
              ->key('status_status', v::stringType()->notEmpty())
              ->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $tableNumber = $body['table_number'];

+ 1 - 1
controllers/UserDeleteController.php

@@ -26,7 +26,7 @@ class UserDeleteController
              ->key('user_name', v::stringType()->notEmpty()->alnum(' _'))  // permite espaço e underline
              ->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $companyId = (int) $body['company_id'];

+ 1 - 1
controllers/UserGetController.php

@@ -24,7 +24,7 @@ class UserGetController
         try {
             v::key('company_id', v::intType()->positive())->assert($body);
         } catch (ValidationException $e) {
-            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
         $companyId = (int) $body['company_id'];

+ 6 - 6
migrations/migrations_v1.sql

@@ -82,7 +82,7 @@ CREATE TABLE "order_item" (
     "order_id" INTEGER NOT NULL,
     "product_id" INTEGER NOT NULL,
     "company_id" INTEGER NOT NULL,
-    "kitchen_note" TEXT NOT NULL DEFAULT '',
+    "order_item_kitchen_note" TEXT NOT NULL DEFAULT '',
     FOREIGN KEY ("order_id") REFERENCES "order" ("order_id"),
     FOREIGN KEY ("product_id") REFERENCES "product" ("product_id"),
     FOREIGN KEY ("company_id") REFERENCES "company" ("company_id")
@@ -111,11 +111,11 @@ CREATE TABLE "fee" (
 );
 
 CREATE TABLE api_key (
-    api_key_id INTEGER PRIMARY KEY AUTOINCREMENT,
-    user_id INTEGER NOT NULL,
-    api_key_user TEXT NOT NULL,
-    api_key_secret TEXT NOT NULL,
-    FOREIGN KEY (user_id) REFERENCES user(user_id)
+    "api_key_id" INTEGER PRIMARY KEY AUTOINCREMENT,
+    "user_id" INTEGER NOT NULL,
+    "api_key_user" TEXT NOT NULL,
+    "api_key_secret" TEXT NOT NULL,
+    FOREIGN KEY ("user_id") REFERENCES "user" ("user_id")
 );
 
 CREATE TABLE "description" (

+ 15 - 13
models/KitchenModel.php

@@ -17,18 +17,19 @@ class KitchenModel
     public function getKitchenOrders(int $companyId): array
     {
         $sql = "
-            SELECT o.order_id,
-                o.table_id,
-                o.order_created_at,
-                p.product_name,
-                oi.kitchen_note
-            FROM 'order' o
-            NATURAL JOIN order_item oi
-            NATURAL JOIN product p
-            WHERE o.company_id = :company_id
-              AND o.order_flag = 'a'
-              AND p.product_is_kitchen = 1
-            ORDER BY o.order_created_at ASC;   
+            SELECT order_id,
+                table_id,
+                order_created_at,
+                order_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);
@@ -44,13 +45,14 @@ class KitchenModel
                     'order_id'        => $row['order_id'],
                     'table_id'        => $row['table_id'],
                     'order_created_at'=> $row['order_created_at'],
+                    'order_flag'=> $row['order_flag'],
                     'items'           => []
                 ];
             }
 
             $orders[$orderId]['items'][] = [
                 'product_name' => $row['product_name'],
-                'kitchen_note' => $row['kitchen_note'] ?? ''
+                'kitchen_note' => $row['order_item_kitchen_note'] ?? ''
             ];
         }
 

+ 3 - 3
models/OrderItemModel.php

@@ -35,8 +35,8 @@ class OrderItemModel
         }
     
         $stmt = $this->pdo->prepare("
-            INSERT INTO order_item (order_id, product_id, company_id, kitchen_note)
-            VALUES (:order_id, :product_id, :company_id, :kitchen_note)
+            INSERT INTO order_item (order_id, product_id, company_id, order_item_kitchen_note)
+            VALUES (:order_id, :product_id, :company_id, :order_item_kitchen_note)
         ");
     
         try {
@@ -44,7 +44,7 @@ class OrderItemModel
                 'order_id' => $orderId,
                 'product_id' => $productId,
                 'company_id' => $companyId,
-                'kitchen_note' => $kitchenNote
+                'order_item_kitchen_note' => $kitchenNote
             ]);
             return $executed ? (int)$this->pdo->lastInsertId() : false;
         } catch (\PDOException $e) {

+ 2 - 2
public/index.php

@@ -52,13 +52,13 @@ $app->post('/user/delete', $cors, $authJwt, \Controllers\UserDeleteController::c
 
 
 // Category Routes
-$app->post('/category/get', $cors, \Controllers\CategoryGetController::class);
+$app->post('/category/get', $cors, $authJwt, \Controllers\CategoryGetController::class);
 $app->post('/category/create', $cors, $authJwt, \Controllers\CategoryCreateController::class);
 $app->post('/category/delete', $cors, $authJwt, \Controllers\CategoryDeleteController::class);
 $app->post('/category/updatef', $cors, $authJwt, \Controllers\CategoryUpdateFlagController::class);
 
 // Product Routes
-$app->post('/product/get', $cors, \Controllers\ProductGetController::class);
+$app->post('/product/get', $cors, $authJwt, \Controllers\ProductGetController::class);
 $app->post('/product/create', $cors, $authJwt, \Controllers\ProductCreateController::class);
 $app->post('/product/update', $cors, $authJwt, \Controllers\ProductUpdateController::class);
 $app->post('/product/delete', $cors, $authJwt, \Controllers\ProductDeleteController::class);