Explorar o código

feat: user and company id in cpr register

Fernando hai 3 semanas
pai
achega
a440ad4b62

+ 7 - 2
controllers/B3CprRegisterController.php

@@ -66,6 +66,11 @@ class B3CprRegisterController
             return ResponseLib::sendFail('Authenticated user not found', [], 'E_VALIDATE')->withStatus(401);
         }
 
+        $companyId = (int)($request->getAttribute('api_company_id') ?? 0);
+        if ($companyId <= 0) {
+            return ResponseLib::sendFail('Authenticated company not found', [], 'E_VALIDATE')->withStatus(401);
+        }
+
         $statusId = $this->statusModel->getIdByStatus('pending');
         if ($statusId === null) {
             return ResponseLib::sendFail('Pending status not found', [], 'E_DATABASE')->withStatus(500);
@@ -79,7 +84,7 @@ class B3CprRegisterController
         }
 
         try {
-            $record = $this->cprModel->create($cpr, $statusId, $paymentId);
+            $record = $this->cprModel->create($cpr, $statusId, $paymentId, $userId, $companyId);
         } catch (\InvalidArgumentException $e) {
             return ResponseLib::sendFail($e->getMessage(), [], 'E_VALIDATE')->withStatus(400);
         } catch (\Throwable $e) {
@@ -107,4 +112,4 @@ class B3CprRegisterController
 
         return Response::json(['raw' => $result['raw'] ?? null, 'status' => $status])->withStatus($status ?: 502);
     }
-}
+}

+ 3 - 2
middlewares/JwtAuthMiddleware.php

@@ -39,7 +39,7 @@ class JwtAuthMiddleware
                 $pdo = $GLOBALS['pdo'];
             }
             
-            $stmt = $pdo->prepare('SELECT user_id FROM "user" WHERE user_id = :user_id AND user_email = :email AND user_flag = \'a\'');
+            $stmt = $pdo->prepare('SELECT user_id, company_id FROM "user" WHERE user_id = :user_id AND user_email = :email AND user_flag = \'a\'');
             $stmt->execute(['user_id' => $userId, 'email' => $email]);
             $user = $stmt->fetch(\PDO::FETCH_ASSOC);
 
@@ -49,7 +49,8 @@ class JwtAuthMiddleware
 
             $request = $request
                 ->withAttribute('api_user', $email)
-                ->withAttribute('api_user_id', $userId);
+                ->withAttribute('api_user_id', $userId)
+                ->withAttribute('api_company_id', $user['company_id']);
 
             return $next($request);
 

+ 12 - 0
migrations/migration_user_id.sql

@@ -0,0 +1,12 @@
+BEGIN;
+ALTER TABLE "cpr" ADD COLUMN user_id UUID;
+ALTER TABLE "cpr" ADD CONSTRAINT fk_user
+    FOREIGN KEY (user_id)
+    REFERENCES "user"(user_id)
+    ON DELETE RESTRICT;
+ALTER TABLE "cpr" ADD COLUMN company_id UUID;
+ALTER TABLE "cpr" ADD CONSTRAINT fk_company
+    FOREIGN KEY (company_id)
+    REFERENCES "company"(company_id)
+    ON DELETE RESTRICT;
+COMMIT;

+ 23 - 3
models/CprModel.php

@@ -61,10 +61,10 @@ class CprModel
         $meta = $this->getColumnsMeta();
         unset($meta['cpr_id']);
 
-        return array_diff_key($meta, ['status_id' => true, 'payment_id' => true]);
+        return array_diff_key($meta, ['status_id' => true, 'payment_id' => true, 'user_id' => true, 'company_id' => true]);
     }
 
-    public function create(array $data, int $statusId, int $paymentId): array
+    public function create(array $data, int $statusId, int $paymentId, int $userId, int $companyId): array
     {
         $data = $this->flattenB3Arrays($data);
 
@@ -93,6 +93,20 @@ class CprModel
                 continue;
             }
 
+            if ($column === 'user_id') {
+                $columns[] = '"user_id"';
+                $placeholders[] = ':user_id';
+                $params['user_id'] = $userId;
+                continue;
+            }
+
+            if ($column === 'company_id') {
+                $columns[] = '"company_id"';
+                $placeholders[] = ':company_id';
+                $params['company_id'] = $companyId;
+                continue;
+            }
+
             if (!array_key_exists($column, $data)) {
                 if ($info['nullable']) {
                     $columns[] = '"' . $column . '"';
@@ -141,6 +155,12 @@ class CprModel
         if (isset($record['payment_id'])) {
             $record['payment_id'] = (int)$record['payment_id'];
         }
+        if (isset($record['user_id'])) {
+            $record['user_id'] = (int)$record['user_id'];
+        }
+        if (isset($record['company_id'])) {
+            $record['company_id'] = (int)$record['company_id'];
+        }
 
         return $record;
     }
@@ -303,4 +323,4 @@ class CprModel
 
         return $record ?: null;
     }
-}
+}