---
# System prepended metadata

title: Product Modular x Chatroom

---

# Product Modular x Chatroom

- Chatroom 模板GitLab [http://192.168.1.151:8765/chat-room-modular](http://192.168.1.151:8765/chat-room-modular)
- (Test) Product Modular x Chatroom 測試專案
- 專案路徑: [http://192.168.1.151:8765/test/test-backend/-/tree/yan-backend](http://192.168.1.151:8765/test/test-backend/-/tree/yan-backend)
- <span style="color:red">綠色的部分為新增的code，紅色則是移除的code。</span>

## 套件安裝:
- [JWT](https://github.com/PHP-Open-Source-Saver/jwt-auth)
- [model with uuid](https://github.com/goldspecdigital/laravel-eloquent-uuid)

## 調整清單:

### Database:
- 刪除：

- 新增：
    - [alter users add column group_id](#Alter_users_add_column_group_id)
    - [rename users to admins](#Rename_users_to_admins)
    - [chatroom_chatrooms_table](#Chatroom_chatrooms)
    - [chatroom_chat_logs_table](#Chatroom_chat_logs)
    - [chatroom_contact_messages_table](#Chatroom_contact_messages)
    - [chatroom_question_answers_table](#Chatroom_question_answers)
    - [chatroom_groups_table](#Chatroom_groups)
    - [chatroom_clients_table](#Chatroom_clients)
    - [chatroom_admin_groups](#Chatroom_admin_group)

### Models:
- 刪除：
    - User
- 新增：
    - [Admin](#Admin)
    - [ChatLog](#ChatLog)
    - [Chatroom](#Chatroom)
    - [Client](#Client)
    - [ContactMessage](#ContactMessage)
    - [Group](#Group)
    - [QuestionAnswer](#QuestionAnswer)
    

### Config:
- 修改：
    - [auth.php](#auth)
    - [chatroom](#chatroom)

### Routes:
- 新增：
    - [api-admin.php](#API_admin)
    - [api.php](#API)
    
### Controller:
- 修改：
    - [app/Http/Controllers/Api/Admin/AccountController.php](#Admin_AccountController)
    - [app/Http/Controllers/Api/Admin/AuthenticatedSessionController.php](#Admin_AuthenticatedSessionController)
    - [app/Http/Controllers/Api/Admin/ChatLogController.php](#Admin_ChatLogController)
    - [app/Http/Controllers/Api/Admin/ContactMessageController.php](#Admin_ContactMessageController)
    - [app/Http/Controllers/Api/Admin/GroupController.php](#Admin_GroupController)
    - [app/Http/Controllers/Api/Admin/QuestionAnswerController.php](#Admin_QuestionAnswerController)
    - [app/Http/Controllers/Api/Client/ClientController.php](#ClientController)
    - [app/Http/Controllers/Api/Client/ContactMessageController.php](#ContactMessageController)
    - [app/Http/Controllers/Api/LocaleController.php](#LocaleController)


### Commands:
- 新增：
    - admin commands - [Create admin cmd](#Create_admin_cmd)

### Export:
- 新增：
    - [Chat_logs_export](#Chat_logs_export)
    - [Contact_message_export](#Contact_message_export)

### Mail:
- 新增：
    - [ContactMessage](#Contact_message)

### Views:
- 新增：
    - [ContactMessageView](#ContactMessageView)
    - [ResetPasswordView](#ResetPasswordView)

---
## Database:

### Alter_users_add_column_group_id
> users table 新增欄位
> code 路徑: [http://192.168.1.151:8765/test/test-backend/-/commit/759c5b95910f0273b709bc8bc7f83a1b4a383d1a#eba36d33efe24d9c9e7e8b75b2b42f02bc5c7467](http://192.168.1.151:8765/test/test-backend/-/commit/759c5b95910f0273b709bc8bc7f83a1b4a383d1a#eba36d33efe24d9c9e7e8b75b2b42f02bc5c7467)

### Rename_users_to_admins
> 將 users table 重新命名為 admins
> code 路徑: [http://192.168.1.151:8765/test/test-backend/-/blob/yan-backend/database/migrations/2022_12_20_083421_rename_table_users_to_admins.php](http://192.168.1.151:8765/test/test-backend/-/blob/yan-backend/database/migrations/2022_12_20_083421_rename_table_users_to_admins.php)

### Chatroom_chatrooms
> 新增聊天室房間table
> code 路徑: [http://192.168.1.151:8765/test/test-backend/-/commit/759c5b95910f0273b709bc8bc7f83a1b4a383d1a#824b8d2038c5f93f0876e6e1e5abbbf82c809ec0](http://192.168.1.151:8765/test/test-backend/-/commit/759c5b95910f0273b709bc8bc7f83a1b4a383d1a#824b8d2038c5f93f0876e6e1e5abbbf82c809ec0)

### Chatroom_chat_logs
> 聊天室聊天記錄table
> code 路徑：[http://192.168.1.151:8765/test/test-backend/-/commit/759c5b95910f0273b709bc8bc7f83a1b4a383d1a#b3d2606dd993689944724b615724879599ea6baa](http://192.168.1.151:8765/test/test-backend/-/commit/759c5b95910f0273b709bc8bc7f83a1b4a383d1a#b3d2606dd993689944724b615724879599ea6baa)

### Chatroom_contact_messages
> 聊天室留言table
> code 路徑：[http://192.168.1.151:8765/test/test-backend/-/commit/759c5b95910f0273b709bc8bc7f83a1b4a383d1a#a7169472cf90421870f26bda8c9ac10aff720c9d](http://192.168.1.151:8765/test/test-backend/-/commit/759c5b95910f0273b709bc8bc7f83a1b4a383d1a#a7169472cf90421870f26bda8c9ac10aff720c9d)

### Chatroom_question_answers
> 聊天室問答table
> code 路徑：[http://192.168.1.151:8765/test/test-backend/-/commit/759c5b95910f0273b709bc8bc7f83a1b4a383d1a#7e688276d12b3e13d33de06a3d398e5cac6134ba](http://192.168.1.151:8765/test/test-backend/-/commit/759c5b95910f0273b709bc8bc7f83a1b4a383d1a#7e688276d12b3e13d33de06a3d398e5cac6134ba)

### Chatroom_groups
> 聊天室頻道table
> code 路徑：[http://192.168.1.151:8765/test/test-backend/-/commit/759c5b95910f0273b709bc8bc7f83a1b4a383d1a#0b73550a9aac7ac321264c9271644a08e8ba11cb](http://192.168.1.151:8765/test/test-backend/-/commit/759c5b95910f0273b709bc8bc7f83a1b4a383d1a#0b73550a9aac7ac321264c9271644a08e8ba11cb)

### Chatroom_clients
> 聊天室前台使用者table
> code 路徑：[http://192.168.1.151:8765/test/test-backend/-/commit/759c5b95910f0273b709bc8bc7f83a1b4a383d1a#52ec2621754dffccf10b766b312a66db2cf7f663](http://192.168.1.151:8765/test/test-backend/-/commit/759c5b95910f0273b709bc8bc7f83a1b4a383d1a#52ec2621754dffccf10b766b312a66db2cf7f663)

### Chatroom_admin_group
> 聊天室管理員頻道認領table
> code 路徑：[http://192.168.1.151:8765/test/test-backend/-/commit/759c5b95910f0273b709bc8bc7f83a1b4a383d1a#5b9a6c0d2d56c8fe92d412e9250962981d64c317](http://192.168.1.151:8765/test/test-backend/-/commit/759c5b95910f0273b709bc8bc7f83a1b4a383d1a#5b9a6c0d2d56c8fe92d412e9250962981d64c317)

---

## Models:

### Admin
> 刪除 User.php 替換為 Admin.php
<pre><code><span style="color:green">
    <?php

    namespace App\Models;

    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Illuminate\Notifications\Notifiable;
    use Laravel\Sanctum\HasApiTokens;
    use PHPOpenSourceSaver\JWTAuth\Contracts\JWTSubject;

    class Admin extends Authenticatable implements JWTSubject
    {
        /**
         * The table associated with the model.
         *
         * @var string
         */
        protected $table = 'admins';

        use HasApiTokens, Notifiable;

        /**
         * role:
         *  1. 王一管理者
         *  2. 客戶主要管理者
         *  4. 客戶次要管理者
         */
        static $role = [
            'supervisor' => 1,
            'client_admin' => 2,
            'client_sub_admin' => 4,
            'chat'  => 8,
        ];

        /**
         * The attributes that are mass assignable.
         *
         * @var array<int, string>
         */
        protected $fillable = [
            'name',
            'email',
            'password',
        ];

        /**
         * The attributes that should be hidden for serialization.
         *
         * @var array<int, string>
         */
        protected $hidden = [
            'password',
            'remember_token',
            'two_factor_recovery_codes',
            'two_factor_secret',
            'email_verified_at',
            'status',   // 目前沒有用處 Jacky 2022-11-15
        ];

        /**
         * The attributes that should be cast.
         *
         * @var array<string, string>
         */
        protected $casts = [
            'email_verified_at' => 'datetime',
        ];

        function scopeIsSupervisor()
        {
            return ($this->role  == 1);
        }

        /**
         * Chatroom 
         */
        public function groups()
        {
            return $this->belongsToMany(Group::class, 'chatroom_admin_group', 'admin_id', 'group_id');
        }

        public function scopeChat($query)
        {
            // return $query->whereRaw('(role & 2) = 2')->orWhereRaw('(role & 1) = 1');
            return $query->whereRaw('(role & 1) = 1');
        }

        /**
         * JWT
         */
        public function getJWTIdentifier()
        {
            return $this->getkey();
        }

        public function getJWTCustomClaims()
        {
            return [
                'type' => 2
            ];
        }
    }

</span></code></pre>

### ChatLog:
> 聊天記錄
- code path: app/Models/ChatLog.php
- [http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#f356a84962c8a90ea1f78d974e79b43f4b5a1411](http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#f356a84962c8a90ea1f78d974e79b43f4b5a1411)

### Chatroom:
- code path: app/Models/Chatroom.php
- [http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#bde9c40c3fb4880c181a226b69d1e1d09ac7b65a](http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#bde9c40c3fb4880c181a226b69d1e1d09ac7b65a)

### Client:
> 前台使用者
- code path: app/Models/Client.php
- [http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#7dec208e02520693ead0041fd6dc2de10e8f8441](http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#7dec208e02520693ead0041fd6dc2de10e8f8441)

### ContactMessage:
> 前台使用者留言
- code path: app/Models/ContactMessage.php
- [http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#60b85836b768cd15d7b43acee6d8a1fc5413a38c](http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#60b85836b768cd15d7b43acee6d8a1fc5413a38c)

### Group:
> 聊天室頻道
- code path: app/Models/Group.php
- [http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#41b0edf0bffb33bab4bd1060e995856c16139ce4](http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#41b0edf0bffb33bab4bd1060e995856c16139ce4)

### QuestionAnswer:
> 聊天室問答
- code path: app/Models/QuestionAnswer.php
- [http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#df33d3770dd4f0051734dbdb755636d138fb3ae0](http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#df33d3770dd4f0051734dbdb755636d138fb3ae0)


---

## Commands:
### Create_admin_cmd
> 避免由程式碼寫入帳密，而改由開發者用命令列自行輸入。
新增路徑: <span style="color:blue">app/Console/Commands/CreateAdmin.php</span>
<pre><code><span style="color:green">
    <?php

    namespace App\Console\Commands;

    use App\Models\Admin;
    use Illuminate\Console\Command;
    use Illuminate\Support\Facades\Hash;

    class CreateAdmin extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'admin:create';

        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'create admin account';

        /**
         * Execute the console command.
         *
         * @return int
         */
        public function handle()
        {
            $name = $this->ask('What is your name?');
            $email = $this->ask('What is your account and email ?');
            if (Admin::where('email', $email)->first()) {
                $this->error('Email is already exist!');
                return;
            }
            while (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                $email = $this->ask('Illegal email, please enter again');
            }
            $password = $this->secret('What is your password?');
            while(!preg_match('/^(?=.*\d)(?=.*[a-zA-Z])[\da-zA-Z\W]{8,20}$/', $password)) {
                $password = $this->secret('Password format is incorrect. 8-20 characters, including at least one number and one letter.');
            }
            $password_confirmed = $this->secret('Enter your password again:');
            if ($password != $password_confirmed) {
                $this->error('Password and confirm password does not match!');
                return;
            }

            try {
                $now = date('Y-m-d H:i:s');
                $user = new Admin();
                $user->name = $name;
                $user->email = $email;
                $user->role = Admin::$role['supervisor'];
                $user->password = Hash::make($password);
                $user->save();

            } catch (\Exception $err) {
                $this->error($err->getMessage());
                return;
            }   
            $this->info('Created admin success!');
            return;
        }
    }

</span></code></pre>

---

## Config:
### auth
> Websocket 使用 JWT 進行驗證，因此 admin 及 client 登入都需要取得 JWT token。
<pre><code>
    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
    
        <span style="color:green">'chatroom_admin' => [
            'driver' => 'jwt',
            'provider' => 'chatroom_admins',
        ],
        'chatroom_client' => [
            'driver' => 'jwt',
            'provider' => 'chatroom_clients',
        ]</span>
    ],        
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            <span style="color:red">'model' => App\Models\User::class,</span>
            <span style="color:green">'model' => App\Models\Admin::class,</span>
        ],

        <span style="color:green">'chatroom_admins' => [
            'driver' => 'eloquent',
            'model' => App\Models\Admin::class,
        ],

        'chatroom_clients' => [
            'driver' => 'eloquent',
            'model' => App\Models\Client::class,
        ],</span>

        
        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

</code></pre>

### chatroom
> 有分企業板及主辦單位版
<pre>
    
---
## Routes:

### API_admin
> 新增聊天室路由
- <span style="color:red">綠色的部分為新增的code，紅色則是移除的code。
- code 路徑：routes/api-admin.php
- [http://192.168.1.151:8765/test/test-backend/-/commit/483ba47128c1981e48cdfff55ae0aebdea7154b7#8ef23a56158eacad326c2cf73d9294ed3ddd6f98](http://192.168.1.151:8765/test/test-backend/-/commit/483ba47128c1981e48cdfff55ae0aebdea7154b7#8ef23a56158eacad326c2cf73d9294ed3ddd6f98)



    use App\Http\Controllers\Api\Admin\SceneProjectController;
    use App\Http\Controllers\Api\Admin\AccountController;
    use App\Http\Controllers\Api\Admin\BannerWallController;
    <span style="color:green">use App\Http\Controllers\Api\Admin\ChatLogController;</span>
    // use App\Http\Controllers\Api\Admin\CollisionController;
    use App\Http\Controllers\Api\Admin\ContactInformationController;
    use App\Http\Controllers\Api\Admin\ContactInformationModifyController;
    <span style="color:green">use App\Http\Controllers\Api\Admin\ContactMessageController;</span>
    // use App\Http\Controllers\Api\Admin\CubeController;
    use App\Http\Controllers\Api\Admin\GlobalSettingController;
    use App\Http\Controllers\Api\Admin\LocaleController;
    use App\Http\Controllers\Api\Admin\UserController;
    use App\Http\Controllers\Api\Admin\VideoWallController;
    use Illuminate\Support\Facades\Route;
    <span style="color:green">use App\Http\Controllers\Api\Admin\GroupController;
    use App\Http\Controllers\Api\Admin\QuestionAnswerController;
    use App\Http\Controllers\Api\Admin\AuthenticatedSessionController;


    // Sanctum + JWT admin login
    Route::group(['middleware' => config('fortify.middleware', ['web'])], function () {
        $limiter = config('fortify.limiters.login');
        Route::post('admin/login', [AuthenticatedSessionController::class, 'store'])->middleware(array_filter([
            'guest:'.config('fortify.guard'),
            $limiter ? 'throttle:'.$limiter : null,
        ]))->name('api.admin.login');

        Route::post('admin/logout', [AuthenticatedSessionController::class, 'destroy'])
        ->name('api.admin.logout');
    });</span>

    Route::middleware('auth:sanctum')->group(function () {
        Route::prefix('admin')->name('api.admin.')->group(function () {
                Route::resource('/scene-pro', SceneProController::class)->except(['create', 'edit']);
                // 刪除 cube image
                Route::delete('/scene-pro/{scene_pro}/cube', [SceneProController::class, 'destroyCubeImage'])->name('scene-pro.cube.destroy');

                <span style="color:green">/**
                 * 聊天室管理
                 */
                Route::put('group/sort', [GroupController::class, 'sorting']);
                Route::resource('group',GroupController::class)->except(['edit']);
                Route::put('group/{group}/set-status',[GroupController::class,'setStatus']);

                Route::middleware('auth:chatroom_admin')->group(function () {
                    /**
                     * 聊天室問答 v
                     */
                    Route::get('question-answer/{group}',[QuestionAnswerController::class, 'index']);
                    Route::post('question-answer',[QuestionAnswerController::class, 'store']);
                    Route::put('question-answer/{questionAnswer}',[QuestionAnswerController::class, 'update']);
                    Route::delete('question-answer/{questionAnswer}',[QuestionAnswerController::class, 'destroy']);
                    Route::put('question-answer/{group}/sort',[QuestionAnswerController::class, 'sorting']);

                    /**
                     * 聊天記錄
                     */
                    Route::get('chat-logs',[ChatLogController::class,'index']);
                    Route::get('chat-logs/{group}/{client}',[ChatLogController::class,'show']);
                    Route::delete('chat-logs/{group}/{client}',[ChatLogController::class,'destroy']);
                    Route::get('chat-logs/{group}/{client}/export',[ChatLogController::class,'export']);

                    /**
                     * 留言紀錄 v
                     */
                    Route::get('contact-message',[ContactMessageController::class, 'index']);
                    Route::get('contact-message/{group}/{client}',[ContactMessageController::class, 'show']);
                    Route::get('contact-message/{group}/{client}/export',[ContactMessageController::class, 'export']);
                    Route::put('contact-message/{contactMessage}/read', [ContactMessageController::class, 'read']);
                    Route::put('contact-message/{contactMessage}/resolve', [ContactMessageController::class, 'resolve']);
                });</span>
            });
        });
    });

</code></pre>

### API
> 新增聊天室前台 API
- code 路徑：routes/api.php
- [http://192.168.1.151:8765/test/test-backend/-/commit/35444c0f628723b0362d5ed3a0b422b8bddd86c6#b337d07ab0e7d54bf5547a0321a43ecafbd73ec1](http://192.168.1.151:8765/test/test-backend/-/commit/35444c0f628723b0362d5ed3a0b422b8bddd86c6#b337d07ab0e7d54bf5547a0321a43ecafbd73ec1)
<pre><code>

    <?php

    use App\Http\Controllers\Api\ApiController;
    <span style="color:green">use App\Http\Controllers\Api\Client\ClientController;
    use App\Http\Controllers\Api\ContactMessageController;</span>
    use App\Models\ContactInformation;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Route;
    <span style="color:green">use App\Http\Controllers\Api\ProfileController;</span>

    include_once(base_path('routes/api-admin.php'));

    // Route::get('/locale', [ApiController::class, 'locale']);

    // Route::middleware('auth:sanctum')->group(function () {

    // });

    Route::name('api.')->group(function () {
      Route::get('/global-setting', [ApiController::class, 'globalSetting'])->name('global-setting');
      Route::middleware('locale-check')->group(function () {
         */
        Route::get('/seo', [ApiController::class, 'seo'])->name('seo');
        Route::get('/scene-project/{scene_project}/scene', [ApiController::class, 'sceneProjectScene'])->name('scene-project.scene');
        
        <span style="color:green">/**
         * Client 前台註冊/登入
         */
        Route::post('login',[ClientController::class,'register_or_login'])->name('client');

        /**
         * 參展商基本資料
         */
        Route::get('profile', [ProfileController::class, 'index'])->name('profile');

        // /**
        //  * 得到現在開放什麼語系
        //  */
        // Route::get('locale',[LocaleController::class,'index'])->name('locale');

        /**
         * 離線留言功能
         */
        Route::post('contact-message',[ContactMessageController::class, 'store'])->name('contact-message');
      });</span>
</code></pre>

---

## Controller:
### Admin_AccountController
> 將原本的 role 判斷改為讀取 admin model 中的 static 參數。

- <span style="color:red">綠色的部分為新增的code，紅色則是移除的code。</span>
<pre><code>
    use App\Models\Admin;

    {
        use ApiResponseTrait;
        
        <span style="color:green">
        /**
         * @var   int    $supervisor          王一管理者
         * @var   int    $client_admin        客戶主要管理者
         * @var   int    $client_sub_admin    客戶次要管理者
         */
        private $supervisor;
        private $client_admin;
        private $client_sub_admin;
        private $chat;
        </span>

        

        function __construct()
        {
            $this->middleware(function ($request, $next) {
                <span style="color:red">
                if (!in_array($request->user()->role, [1, 2])) {
                    return $this->apiResponse(null, 403, 'Permission denied', 403);
                }
                return $next($request);</span>
                <span style="color:green">
                $this->supervisor = User::$role['supervisor'];
                $this->client_admin = User::$role['client_admin'];
                $this->client_sub_admin = User::$role['client_sub_admin'];
                $this->chat = Admin::$role['chat'];

                if (!in_array($request->user()->role, [$this->supervisor, $this->client_admin, $this->chat])) {
                    return $this->apiResponse(null, 403, 'Permission denied', 403);
                }
                return $next($request);</span>
            });
                
        }
        
        public function index()
        {
            <span style="color:red">switch (request()->user()->role) {
                case 1:
                case $this->supervisor:
                    $users = User::get();
                    break;
                case 2:
                    $users = User::whereIn('role', [2, 3])->get();
                case $this->client_admin:
                    $users = User::whereIn('role', [$this->client_admin, $this->client_sub_admin])->get();
                    break;
            }
            // $users = User::where('email', '<>', 'info@kingone-design.com')->get();</span>
            
             <span style="color:green">switch (request()->user()->role) {
                case $this->supervisor:
                    $users = User::get();
                    break;
                case $this->client_admin:
                    $users = User::whereIn('role', [$this->client_admin, $this->client_sub_admin])->get();
                    break;
            }</span>

            $this->purify($users, ['created_at', 'updated_at', 'id']);
            return $this->apiResponse($users);
        }

        <span style="color:red"></span>

        public function store(Request $request)
        {
            $request->validate([
                'name' => 'required',
                'email' => 'required|email|unique:users,email',
                <span style="color:red">'password' => 'required|min:4',
                'password_confirmation' => 'required|same:password|min:4',
                'role' => 'numeric|in:1,2,3',</span>
                <span style="color:green">'password' => 'required|regex:/^(?=.*\d)(?=.*[a-zA-Z])[\da-zA-Z\W]{8,20}$/',
                'password_confirmation' => 'required|same:password',
                'role' => 'numeric|in:1,2,4,8',</span>
            ]);

            $user = new User();
            switch ($request->user()->role) {
                <span style="color:red">case 1:
                    if (!in_array($request->role, [1, 2])) {
                        return $this->apiResponse(null, 422, 'User role is invalid.', 422);
                    }
                    $user->role = $request->role;
                    break;
                case 2:
                    $user->role = 3;
                    break;</span>
                <span style="color:green">case $this->supervisor:
                    if (!in_array($request->role, [$this->supervisor, $this->client_admin, $this->chat])) {
                        return $this->apiResponse(null, 422, 'User role is invalid.', 422);
                    }
                    $user->role = $request->role;
                    break;
                case $this->client_admin:
                    $user->role = $this->client_sub_admin;
                    break;</span>
                
            }
            $user->name = $request->name;
            $user->email = $request->email;
            $user->password = Hash::make($request->password);
            $user->save();

            return $this->index();
        }
    
        public function destroy(Request $request)
        {
            $request->validate([
                'email' => 'required|email',
            ]);
            <span style="color:red">if ($request->email == 'info@kingone-design.com') {
                return $this->apiResponse(null, 403, 'Access Denied!!', 403);
            }

            if(!$user = User::where('email', $request->email)->first()) {
                return $this->apiResponse(null, 422, 'User does not exist.', 422);
            }

            if ($request->user()->role == 2 && $user->role !== 3) {
                return $this->apiResponse(null, 403, 'Access Denied!!', 403);
            }</span>
            
            <span style="color:green">if(!$user = Admin::where('email', $request->email)->first()) {
                return $this->apiResponse(null, 422, 'User does not exist.', 422);
            }

            if ($user->role == $this->supervisor && Admin::where('role', $this->supervisor)->count() == 1) {
                return $this->apiResponse(null, 403, 'Must remain one supervisor!!', 403);
            }

            if ($request->user()->role == $this->client_admin && $user->role !== $this->client_sub_admin) {
                return $this->apiResponse(null, 403, 'Access Denied!!', 403);
            }
            </span>
            $user->delete();
            if ($request->email == $request->user()->email) {
                Auth::guard('web')->logout();
                return $this->apiResponse(null, 401, 'You have been logout.', 401);
            } else {
                return $this->index();
            }
        }

</code></pre>

### Admin_AuthenticatedSessionController
> 改寫 Fourtify 登入function
- code 路徑：app/Http/Controllers/Api/Admin/AuthenticatedSessionController.php
- [http://192.168.1.151:8765/test/test-backend/-/commit/483ba47128c1981e48cdfff55ae0aebdea7154b7#eeba4528bb5ed7977a0c084433334b149d152200](http://192.168.1.151:8765/test/test-backend/-/commit/483ba47128c1981e48cdfff55ae0aebdea7154b7#eeba4528bb5ed7977a0c084433334b149d152200)


### Admin_ChatLogController
> sqs
- code path: app/Http/Controllers/Api/Admin/ChatLogController.php
- [http://192.168.1.151:8765/test/test-backend/-/blob/yan-backend/app/Http/Controllers/Api/Admin/ChatLogController.php](http://192.168.1.151:8765/test/test-backend/-/blob/yan-backend/app/Http/Controllers/Api/Admin/ChatLogController.php)

### Admin_ContactMessageController
> 聊天室小編不在線上前台使用者留言API
- code path: app/Http/Controllers/Api/Admin/ContactMessageController.php
- [http://192.168.1.151:8765/test/test-backend/-/blob/yan-backend/app/Http/Controllers/Api/Admin/ContactMessageController.php](http://192.168.1.151:8765/test/test-backend/-/blob/yan-backend/app/Http/Controllers/Api/Admin/ContactMessageController.php)

### Admin_GroupController
> 頻道
- code path: app/Http/Controllers/Api/Admin/GroupController.php
- [http://192.168.1.151:8765/test/test-backend/-/blob/yan-backend/app/Http/Controllers/Api/Admin/GroupController.php](http://192.168.1.151:8765/test/test-backend/-/blob/yan-backend/app/Http/Controllers/Api/Admin/GroupController.php)

### Admin_QuestionAnswerController
> 問答
- code path: app/Http/Controllers/Api/Admin/QuestionAnswerController.php
- [http://192.168.1.151:8765/test/test-backend/-/blob/yan-backend/app/Http/Controllers/Api/Admin/QuestionAnswerController.php](http://192.168.1.151:8765/test/test-backend/-/blob/yan-backend/app/Http/Controllers/Api/Admin/QuestionAnswerController.php)

### ClientController
> 前台訪客
- code path: app/Http/Controllers/Api/Client/ClientController.php
- [http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#b868f17cc77d38d328d70b8ba37b46bb25e83935](http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#b868f17cc77d38d328d70b8ba37b46bb25e83935)

### ContactMessageController
> 沒有小編在線上的留言 API
- code path: app/Http/Controllers/Api/ContactMessageController.php
- [http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#776310415b1b9360f3a7953b187578903140b5c7](http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#776310415b1b9360f3a7953b187578903140b5c7)

### LocaleController
> 語系
- code path: app/Http/Controllers/Api/Admin/
- [http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#6234346fa4f45089b9fd494cd7a6a6c56986f641](http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#6234346fa4f45089b9fd494cd7a6a6c56986f641)


- code path: app/Http/Controllers/Api/Admin/
- []()

---

## Export:

### Chat_logs_export
> 聊天記錄匯出
- code path: app/Exports/ChatLogsExport.php
- [http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#4f88cfc89b43f2da7ccd624262015d3f5c768974](http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#4f88cfc89b43f2da7ccd624262015d3f5c768974)

### Contact_message_export
> 留言紀錄匯出
- code path: app/Exports/ContactMessageExport.php
- [http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#e6841a459d07c265e30f2cf2395e08be5e38fa83](http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#e6841a459d07c265e30f2cf2395e08be5e38fa83)

## Mail:

### Contact_message
- code path: app/Mail/ContactMessage.php
- [http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#c5030c5992fd72cee17d37166f7392ac532fe358](http://192.168.1.151:8765/test/test-backend/-/commit/339ea59efa3c9a7505c92e2677614081d4ff7caa#c5030c5992fd72cee17d37166f7392ac532fe358)

## View:

### ContactMessageView
- code path: resources/views/mails/contactMessage.blade.pug
- [http://192.168.1.151:8765/test/test-backend/-/commit/5c115edb2fbe98a6b57ad9a6863b9b5a5481eb5d#3025c7e73adca322ed30b2a5419c54e304e205a5](http://192.168.1.151:8765/test/test-backend/-/commit/5c115edb2fbe98a6b57ad9a6863b9b5a5481eb5d#3025c7e73adca322ed30b2a5419c54e304e205a5)

### ResetPasswordView
- code path: resources/views/mails/resetPassword.blade.pug
- [http://192.168.1.151:8765/test/test-backend/-/commit/5c115edb2fbe98a6b57ad9a6863b9b5a5481eb5d#78ae6776c253e306354172afc13eb6c117e7d53d](http://192.168.1.151:8765/test/test-backend/-/commit/5c115edb2fbe98a6b57ad9a6863b9b5a5481eb5d#78ae6776c253e306354172afc13eb6c117e7d53d)