]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
[ERP-184] база и апи приёма чат-логов origin/feature_smirnov_erp-184_chatbot_api_logs
authorAlexander Smirnov <fredeom@mail.ru>
Mon, 23 Sep 2024 13:15:02 +0000 (16:15 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Mon, 23 Sep 2024 13:15:02 +0000 (16:15 +0300)
erp24/api2/controllers/ChatbotActionController.php [new file with mode: 0644]
erp24/migrations/m240923_084450_create_table_chatbot_action.php [new file with mode: 0755]
erp24/records/ChatbotAction.php [new file with mode: 0644]

diff --git a/erp24/api2/controllers/ChatbotActionController.php b/erp24/api2/controllers/ChatbotActionController.php
new file mode 100644 (file)
index 0000000..7dfac16
--- /dev/null
@@ -0,0 +1,47 @@
+<?php
+
+namespace app\controllers;
+
+use Yii;
+use yii\helpers\Json;
+use yii_app\helpers\ClientHelper;
+use yii_app\records\ChatbotAction;
+
+class ChatbotActionController extends BaseController
+{
+    public function actionLog() {
+        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
+
+        $request = Yii::$app->request->getRawBody();
+
+        try {
+            $result = Json::decode($request);
+        } catch (\Exception $ex) {
+            return $this->asJson(['error' => ['code' => 400, 'message' => 'Json body invalid']]);
+        }
+
+        foreach (['phone', 'action'] as $field) {
+            if (empty($result[$field])) {
+                return $this->asJson(['error_id' => 2, 'error' => ['code' => 400, 'message' => 'Missing ' . $field]]);
+            }
+        }
+
+        $phone = ClientHelper::phoneClear($result['phone']);
+        if (!ClientHelper::phoneVerify($phone)) {
+            return $this->asJson(['error_id' => 3, 'error' => ['code' => 400, 'message' => 'Phone is required']]);
+        }
+
+        $chatbotAction = new ChatbotAction;
+        $chatbotAction->phone = $phone;
+        $chatbotAction->action = $result['action'];
+        $chatbotAction->created_at = date('Y-m-d H:i:s');
+        $chatbotAction->json = $result['payload'] ? Json::encode($result['payload']) : null;
+        $chatbotAction->save();
+
+        if ($chatbotAction->getErrors()) {
+            return $this->asJson(['error_id' => 4, 'error' => ['code' => 400, 'message' => Json::encode($chatbotAction->getErrors())]]);
+        }
+
+        return $this->asJson(['response' => true]);
+    }
+}
\ No newline at end of file
diff --git a/erp24/migrations/m240923_084450_create_table_chatbot_action.php b/erp24/migrations/m240923_084450_create_table_chatbot_action.php
new file mode 100755 (executable)
index 0000000..6e8d1fb
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Class m240923_084450_create_table_chatbot_action
+ */
+class m240923_084450_create_table_chatbot_action extends Migration
+{
+    const TABLE_NAME = 'erp24.chatbot_action';
+
+    /**
+     * {@inheritdoc}
+     */
+    public function safeUp()
+    {
+        $this->createTable(self::TABLE_NAME, [
+            'id' => $this->primaryKey(),
+            'phone' => $this->string(16)->notNull()->comment('Номер телефона клиента'),
+            'created_at' => $this->dateTime()->notNull()->comment('Дата создания'),
+            'action' => $this->string()->notNull()->comment('Текстовое описание действия. Например, pressInfoBtn'),
+            'json' => $this->text()->null()->comment('Доп.поле в формате json')
+        ]);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function safeDown()
+    {
+        $this->dropTable(self::TABLE_NAME);
+    }
+}
diff --git a/erp24/records/ChatbotAction.php b/erp24/records/ChatbotAction.php
new file mode 100644 (file)
index 0000000..207d696
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+
+namespace yii_app\records;
+
+use Yii;
+
+/**
+ * This is the model class for table "chatbot_action".
+ *
+ * @property int $id
+ * @property string $phone Номер телефона клиента
+ * @property string $created_at Дата создания
+ * @property string $action Текстовое описание действия. Например, pressInfoBtn
+ * @property string|null $json Доп.поле в формате json
+ */
+class ChatbotAction extends \yii\db\ActiveRecord
+{
+    /**
+     * {@inheritdoc}
+     */
+    public static function tableName()
+    {
+        return 'chatbot_action';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['phone', 'created_at', 'action'], 'required'],
+            [['created_at'], 'safe'],
+            [['json'], 'string'],
+            [['phone'], 'string', 'max' => 16],
+            [['action'], 'string', 'max' => 255],
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function attributeLabels()
+    {
+        return [
+            'id' => 'ID',
+            'phone' => 'Phone',
+            'created_at' => 'Created At',
+            'action' => 'Action',
+            'json' => 'Json',
+        ];
+    }
+}