Przeglądaj źródła

make refferal system

jauhar54 2 lat temu
rodzic
commit
09af4b2552

+ 5
- 2
app/Helpers/Helper.php Wyświetl plik

@@ -10,7 +10,7 @@ use URL;
10 10
 use Spatie\Permission\Models\Permission;
11 11
 use Spatie\Permission\Models\Role;
12 12
 use Carbon\Carbon;
13
-
13
+use Illuminate\Support\Str;
14 14
 class Helper
15 15
 {
16 16
     public static function success_alert($message)
@@ -297,7 +297,10 @@ class Helper
297 297
         $image = $string;  // your base64 encoded
298 298
         $image = str_replace('data:image/png;base64,', '', $image);
299 299
         $image = str_replace(' ', '+', $image);
300
-        $imageName = str_random(10).'.'.'png';
300
+        $imageName = Str::random(10).'.'.'png';
301
+        if (!file_exists(storage_path(). '/images/'. $nama_module)) {
302
+            mkdir(storage_path(). '/images/'. $nama_module, 0777, true);
303
+        }
301 304
         \File::put(storage_path(). '/images/'. $nama_module . '/' . $imageName, base64_decode($image));
302 305
 
303 306
         $return = URL::to('storage/images/'. $nama_module . '/' . $imageName);

+ 260
- 0
app/Http/Controllers/Api/V1/TimelineController.php Wyświetl plik

@@ -0,0 +1,260 @@
1
+<?php
2
+
3
+namespace App\Http\Controllers\Api\V1;
4
+
5
+use App\Http\Controllers\Controller;
6
+use Illuminate\Support\Facades\DB;
7
+use App\Model\Timeline;
8
+use Illuminate\Support\Facades\Auth;
9
+use Illuminate\Http\Request;
10
+use Illuminate\Support\Facades\Storage;
11
+use Validator;
12
+use Helper;
13
+
14
+
15
+class TimelineController extends Controller
16
+{
17
+    public function __construct()
18
+    {
19
+        DB::enableQueryLog();
20
+    }
21
+
22
+    public function getAll()
23
+    {
24
+        $query = Timeline::get();
25
+
26
+        if($query){
27
+            $result = $query;
28
+
29
+            $res_status = true;
30
+            $msg = 'Mendapatkan Data';
31
+            $status_msg = $msg;
32
+
33
+            return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
34
+
35
+        }else {
36
+
37
+            $res_status = false;
38
+            $msg = 'Data tidak ditemukan';
39
+            $status_msg = $msg;
40
+
41
+            return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
42
+        }
43
+    }
44
+
45
+    public function getByUser($user_id)
46
+    {
47
+        $query = Timeline::where('user_id', Helper::hash($user_id, 'decode'))->get();
48
+
49
+        if($query){
50
+            $result = $query;
51
+
52
+            $res_status = true;
53
+            $msg = 'Mendapatkan Data';
54
+            $status_msg = $msg;
55
+
56
+            return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
57
+
58
+        }else {
59
+
60
+            $res_status = false;
61
+            $msg = 'Data tidak ditemukan';
62
+            $status_msg = $msg;
63
+
64
+            return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
65
+        }
66
+    }
67
+
68
+    public function getSingle($data_id)
69
+    {
70
+        $query = Timeline::where('id', Helper::hash($data_id, 'decode'))->get();
71
+
72
+        if($query){
73
+            $result = $query;
74
+
75
+            $res_status = true;
76
+            $msg = 'Mendapatkan Data';
77
+            $status_msg = $msg;
78
+
79
+            return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
80
+
81
+        }else {
82
+
83
+            $res_status = false;
84
+            $msg = 'Data tidak ditemukan';
85
+            $status_msg = $msg;
86
+
87
+            return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
88
+        }
89
+    }
90
+    
91
+    public function postStore(Request $request)
92
+    {
93
+        $post = $request->all();
94
+        // $file = $request->file('gambar');
95
+        //dd($post);
96
+        /**digunakan untuk set rule validator */
97
+        $rules = [
98
+            'gambar' => 'required',
99
+            'user_id' => 'required',
100
+            // 'gambar' => 'required|file|max:1000',
101
+        ];
102
+        /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
103
+        // $alert = [
104
+        //     'file'  => 'File maksimal 1MB'
105
+        // ];
106
+        // $validator = Validator::make($post, $rules, $alert);
107
+        $validator = Validator::make($post, $rules);
108
+
109
+        if ($validator->passes()) {
110
+
111
+            /**menggunakan transaction */
112
+            DB::beginTransaction();
113
+
114
+            // $nama_file = time()."_".$file->getClientOriginalName();
115
+
116
+            // $tujuan_upload = 'gambar';
117
+
118
+            // //$file->move($tujuan_upload,$nama_file);
119
+            // Storage::putFileAs('public/images/Timeline/', $file, $nama_file );
120
+
121
+            $query                  = new Timeline();
122
+            $query->is_approved     = 'pending';
123
+            $query->caption         = $request->caption;
124
+            $query->timeline_photo  = Helper::base64_to_image($request->gambar, 'timeline');
125
+            $query->user_id         = Helper::hash(Request::all()['user_id'], 'decode');
126
+            $query->save();
127
+
128
+            $query->hash_id = Helper::hash($query->id, 'encode');
129
+            $query->user_id = Helper::hash($query->user_id, 'encode');
130
+
131
+
132
+            if($query){
133
+                $result = $query;
134
+    
135
+                $res_status = true;
136
+                $msg = 'Mendapatkan Data';
137
+                $status_msg = $msg;
138
+    
139
+                return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
140
+    
141
+            }else {
142
+    
143
+                $res_status = false;
144
+                $msg = 'Data tidak ditemukan';
145
+                $status_msg = $msg;
146
+    
147
+                return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
148
+            }
149
+        }
150
+    }
151
+
152
+    public function postUpdate(Request $request)
153
+    {        
154
+        $post = $request->all();
155
+
156
+        $rules = [
157
+            'id' => 'required',
158
+            'gambar' => 'required',
159
+            'user_id' => 'required',
160
+        ];
161
+
162
+        $validator = Validator::make($post, $rules);
163
+
164
+        if(!$validator->fails()){
165
+            $Timeline = Timeline::where('id', Helper::hash(Request::all()['id'], 'decode'))->firstOrFail();
166
+            
167
+            $Timeline->name                        = $request->name;
168
+            $Timeline->Timeline_identity     = $request->Timeline_identity;
169
+            $Timeline->Timeline_type         = $request->Timeline_type;
170
+            $Timeline->Timeline_photo        = Helper::base64_to_image($request->gambar, 'Timeline');
171
+            $Timeline->user_id                     = Helper::hash(Request::all()['user_id'], 'decode');
172
+            $Timeline->save();
173
+
174
+            $Timeline->hash_id = Helper::hash($Timeline->id, 'encode');
175
+            $Timeline->user_id = Helper::hash($Timeline->user_id, 'encode');
176
+
177
+            $result = $Timeline;
178
+            $res_status = true;
179
+            $msg = 'Berhasil membuat user';
180
+            $status_msg = $msg;
181
+
182
+            return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
183
+
184
+        }else {
185
+
186
+            $res_status = false;
187
+            $msg = 'Gagal Membuat user';
188
+            $status_msg = $validator->errors();
189
+
190
+            return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
191
+        }
192
+    }
193
+
194
+    public function delete()
195
+    {
196
+        $validator = Validator::make(Request::all(), [
197
+            'id' => ['required'],
198
+        ]);
199
+        if(!$validator->fails()){
200
+            $query = Timeline::find(Helper::hash(Request::all()['id'], 'decode'))->detroy();
201
+
202
+            if($query){
203
+                $result = $query;
204
+                $res_status = true;
205
+                $msg = 'Data berhasil dihapus';
206
+                $status_msg = $msg;
207
+
208
+                return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
209
+            }else{
210
+                $result = $query;
211
+                $res_status = false;
212
+                $msg = 'Data gagal dihapus';
213
+                $status_msg = $msg;
214
+
215
+                return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
216
+            }
217
+        }else{
218
+            $res_status = false;
219
+            $msg = 'Data tidak ditemukan';
220
+            $status_msg = $validator->errors();
221
+
222
+            return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
223
+        }
224
+        
225
+
226
+        if($query){
227
+            $result = $query;
228
+
229
+            $res_status = true;
230
+            $msg = 'Mendapatkan Data';
231
+            $status_msg = $msg;
232
+
233
+            return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
234
+
235
+        }else {
236
+
237
+            $res_status = false;
238
+            $msg = 'Data tidak ditemukan';
239
+            $status_msg = $msg;
240
+
241
+            return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
242
+        }
243
+    }
244
+
245
+    public function resSuccess($param = null, $status = null, $msg = null, $status_msg = null, $result = null)
246
+    {
247
+        $response['response'] = array(
248
+            'status'     => $status,
249
+            'message'    => $msg,
250
+            'status_msg' => $status_msg,
251
+        );
252
+        $response['param'] = !empty($param) ? $param : '';
253
+
254
+        $response['results'] = $result;
255
+
256
+        return response()->json($response, 200);
257
+    }
258
+
259
+
260
+}

+ 1
- 1
app/Http/Controllers/Api/V1/TransportationController.php Wyświetl plik

@@ -122,7 +122,7 @@ class TransportationController extends Controller
122 122
             $query->transportation_identity     = $request->transportation_identity;
123 123
             $query->transportation_type         = $request->transportation_type;
124 124
             $query->transportation_photo        = Helper::base64_to_image($request->gambar, 'transportation');
125
-            $query->user_id                     = Helper::hash(Request::all()['user_id'], 'decode');
125
+            $query->user_id                     = Helper::hash($request->user_id, 'decode');
126 126
             $query->save();
127 127
 
128 128
             $query->hash_id = Helper::hash($query->id, 'encode');

+ 54
- 16
app/Http/Controllers/Api/V1/UserController.php Wyświetl plik

@@ -12,7 +12,9 @@ use Hash;
12 12
 use Helper;
13 13
 use Illuminate\Support\Facades\Password;
14 14
 use App\Model\Profile;
15
-
15
+use App\Model\RefferalLog;
16
+use App\Model\PointLog;
17
+use App\Model\Points;
16 18
 class UserController extends Controller
17 19
 {
18 20
     public function __construct()
@@ -89,6 +91,21 @@ class UserController extends Controller
89 91
         }
90 92
     }
91 93
 
94
+    public function refferalCode($length) {
95
+        $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
96
+        $charactersLength = strlen($characters);
97
+        $randomString = '';
98
+        for ($i = 0; $i < $length; $i++) {
99
+            $randomString .= $characters[rand(0, $charactersLength - 1)];
100
+        }
101
+        $check_reff = User::where('refferal_code', '=', $randomString)->first();
102
+        if(!empty($check_reff)){
103
+            $this->refferalCode(6);
104
+            exit;
105
+        }
106
+        return $randomString;
107
+    }
108
+
92 109
     public function postRegister(Request $request)
93 110
     {        
94 111
         $validator = Validator::make($request->all(), [
@@ -97,31 +114,52 @@ class UserController extends Controller
97 114
             'password' => ['required', 'string', 'min:8', 'confirmed'],
98 115
         ]);
99 116
 
117
+        $check_refferal = User::where('refferal_code', '=', $request->refferal)->first();
118
+
100 119
         if(!$validator->fails()){
101 120
             $result = User::create([
102 121
                 'name' => $request->name,
103 122
                 'email' => $request->email,
104 123
                 'password' => Hash::make($request->password),
124
+                'refferal_code' => $this->refferalCode(6),
105 125
                 'gambar' => !empty($request->gambar)?Helper::base64_to_image($request->gambar, 'transportation'):'',
106 126
             ]);
127
+            $refferal_log = null;
128
+            if($check_refferal){
129
+                $refferal_log = new RefferalLog();
130
+                $refferal_log->user_id = $result->id;
131
+                $refferal_log->user_refferal = $check_refferal->id;
132
+                // $refferal_log->user_refferal = Helper::hash($check_refferal->id, 'decode');
133
+                $refferal_log->type = "refferal";
134
+                $refferal_log->save();
135
+
136
+                $refferal_log->hash_id = Helper::hash($refferal_log->id, 'encode');
137
+                $refferal_log->user_id = Helper::hash($refferal_log->user_id, 'encode');
138
+                $refferal_log->user_refferal = Helper::hash($refferal_log->user_refferal, 'encode');
139
+
140
+                $pointLog = new PointLog();
141
+                $pointLog->user_id = $result->id;
142
+                $pointLog->type_point = 'refferal';
143
+                $pointLog->point = Points::where('type', 'refferal')->pluck('value')->first();
144
+                $pointLog->save();
145
+
146
+                $pointLog2 = new PointLog();
147
+                $pointLog2->user_id = $check_refferal->id;
148
+                $pointLog2->type_point = 'invitation';
149
+                $pointLog2->point = Points::where('type', 'invitation')->pluck('value')->first();
150
+                $pointLog2->save();
151
+            }else{
152
+                $res_status = false;
153
+                $msg = 'Kode Refferal Tidak Valid';
154
+                $status_msg = $validator->errors();
155
+    
156
+                return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
157
+            }
158
+          
107 159
 
108 160
             $data_role['role_id'] = 2; //driver role
109 161
             $result->assignRole($data_role);
110
-            
111
-            // $file = $request->file('photo');
112
-            // if($file){
113
-            //     $user = User::find($result->id);
114
-            //     // $nama_file = time()."_".$file->getClientOriginalName();
115
-            //     // $tujuan_upload = 'photo';
116
-            //     // //$file->move($tujuan_upload,$nama_file);
117
-            //     // Storage::putFileAs('public/photo/user/', $file, $nama_file );
118
-            //     // //File::delete('image/'.$user->photo);
119
-            //     // Storage::delete('public/photo/user/'.$user->photo);
120
-            //     // $user->photo = $nama_file;
121
-
122
-            //     $user->save();
123
-            // }
124
-
162
+            $result->refferal = $refferal_log;
125 163
             $res_status = true;
126 164
             $msg = 'Berhasil membuat user';
127 165
             $status_msg = $msg;

+ 15
- 0
app/Model/RefferalLog.php Wyświetl plik

@@ -0,0 +1,15 @@
1
+<?php
2
+
3
+namespace App\Model;
4
+
5
+use Illuminate\Database\Eloquent\Model;
6
+
7
+class RefferalLog extends Model
8
+{
9
+    //protected $connection = 'connection-name'; //jika menggunakan 2 DB modelnya harus dikasih ini
10
+    //protected $primaryKey = 'your_key_name'; // set primarykeymu jika bukan ID
11
+    protected $table = 'refferal_logs';
12
+    protected $fillable = [];
13
+    //public $incrementing = false; //jika primary keynya string
14
+    //public $timestamps = false; //matiin created_at, updated_at
15
+}

+ 20
- 0
app/Model/Timeline.php Wyświetl plik

@@ -0,0 +1,20 @@
1
+<?php
2
+
3
+namespace App\Model;
4
+
5
+use Illuminate\Database\Eloquent\Model;
6
+
7
+class Timeline extends Model
8
+{
9
+    //
10
+    //protected $connection = 'connection-name'; //jika menggunakan 2 DB modelnya harus dikasih ini
11
+    //protected $primaryKey = 'your_key_name'; // set primarykeymu jika bukan ID
12
+    protected $table = 'timelines';
13
+    protected $fillable = [
14
+      'user_id',
15
+      'timeline_photo',
16
+      'caption',
17
+      'is_approved'
18
+    ];
19
+
20
+}

+ 1
- 0
app/User.php Wyświetl plik

@@ -21,6 +21,7 @@ class User extends Authenticatable
21 21
         'name',
22 22
         'password',
23 23
         'email',
24
+        'refferal_code'
24 25
     ];
25 26
 
26 27
     /**

+ 32
- 0
database/migrations/2022_04_11_170841_add_refferal_id_to_users_table.php Wyświetl plik

@@ -0,0 +1,32 @@
1
+<?php
2
+
3
+use Illuminate\Database\Migrations\Migration;
4
+use Illuminate\Database\Schema\Blueprint;
5
+use Illuminate\Support\Facades\Schema;
6
+
7
+class AddRefferalIdToUsersTable extends Migration
8
+{
9
+    /**
10
+     * Run the migrations.
11
+     *
12
+     * @return void
13
+     */
14
+    public function up()
15
+    {
16
+        Schema::table('users', function (Blueprint $table) {
17
+            $table->string('refferal_code');
18
+        });
19
+    }
20
+
21
+    /**
22
+     * Reverse the migrations.
23
+     *
24
+     * @return void
25
+     */
26
+    public function down()
27
+    {
28
+        Schema::table('users', function (Blueprint $table) {
29
+            $table->dropColumn('refferal_code');
30
+        });
31
+    }
32
+}

+ 34
- 0
database/migrations/2022_04_11_172838_create_refferal_logs_table.php Wyświetl plik

@@ -0,0 +1,34 @@
1
+<?php
2
+
3
+use Illuminate\Database\Migrations\Migration;
4
+use Illuminate\Database\Schema\Blueprint;
5
+use Illuminate\Support\Facades\Schema;
6
+
7
+class CreateRefferalLogsTable extends Migration
8
+{
9
+    /**
10
+     * Run the migrations.
11
+     *
12
+     * @return void
13
+     */
14
+    public function up()
15
+    {
16
+        Schema::create('refferal_logs', function (Blueprint $table) {
17
+            $table->id();
18
+            $table->bigInteger('user_id')->unsigned();
19
+            $table->bigInteger('user_refferal')->unsigned()->nullable();
20
+            $table->string('type');
21
+            $table->timestamps();
22
+        });
23
+    }
24
+
25
+    /**
26
+     * Reverse the migrations.
27
+     *
28
+     * @return void
29
+     */
30
+    public function down()
31
+    {
32
+        Schema::dropIfExists('refferal_logs');
33
+    }
34
+}

+ 32
- 0
database/migrations/2022_04_12_052246_add_is_approved_to_timelines_table.php Wyświetl plik

@@ -0,0 +1,32 @@
1
+<?php
2
+
3
+use Illuminate\Database\Migrations\Migration;
4
+use Illuminate\Database\Schema\Blueprint;
5
+use Illuminate\Support\Facades\Schema;
6
+
7
+class AddIsApprovedToTimelinesTable extends Migration
8
+{
9
+    /**
10
+     * Run the migrations.
11
+     *
12
+     * @return void
13
+     */
14
+    public function up()
15
+    {
16
+        Schema::table('timelines', function (Blueprint $table) {
17
+            $table->enum('is_approved', ['pending', 'approved', 'rejected']);
18
+        });
19
+    }
20
+
21
+    /**
22
+     * Reverse the migrations.
23
+     *
24
+     * @return void
25
+     */
26
+    public function down()
27
+    {
28
+        Schema::table('timelines', function (Blueprint $table) {
29
+            $table->dropColumn('is_approved');
30
+        });
31
+    }
32
+}

BIN
public/assets/img/logo/logo.png Wyświetl plik


+ 9
- 2
resources/views/admin/layouts/navbars/sidebar.blade.php Wyświetl plik

@@ -1,4 +1,12 @@
1 1
 
2
+<style>
3
+@media (min-width: 768px){
4
+    .navbar-vertical.navbar-expand-md .navbar-brand-img {
5
+        max-height: 10.5rem;
6
+    }
7
+}
8
+
9
+</style>
2 10
 <nav class="navbar navbar-vertical fixed-left fixed-nav-sidoni navbar-expand-md navbar-light bg-white" id="sidenav-main">
3 11
     <div class="container-fluid">
4 12
         <!-- Toggler -->
@@ -7,8 +15,7 @@
7 15
         </button>
8 16
         <!-- Brand -->
9 17
         <a class="navbar-brand pt-0 pb-0 mt-3" href="{{ route('admin.dashboard') }}">
10
-            <img src="{{ asset('storage/jasaraharja.png') }}" class="navbar-brand-img" alt="...">
11
-            {{-- <h1>Jasa Raharja</h1> --}}
18
+            <img src="{{ asset('assets/img/logo/logo.png') }}" class="navbar-brand-img" alt="...">
12 19
         </a>
13 20
         <!-- User -->
14 21
         <ul class="nav align-items-center d-md-none">

+ 7
- 5
resources/views/auth/login.blade.php Wyświetl plik

@@ -108,9 +108,7 @@
108 108
         .navbar-brand img{
109 109
             height: 46px;
110 110
         }
111
-        .login-title img{
112
-            height: 50px;
113
-        }
111
+
114 112
         .login-title h3{
115 113
             font-weight: 700;
116 114
             font-size: 1.5em;
@@ -137,6 +135,11 @@
137 135
         .vh-100{
138 136
             height: 100vh;
139 137
         }
138
+        @media (min-width: 768px){
139
+            .navbar-brand-img {
140
+                max-height: 10.5rem;
141
+            }
142
+        }
140 143
     </style>
141 144
 </head>
142 145
 <body>
@@ -146,8 +149,7 @@
146 149
                 <div class="col-lg-6">
147 150
                     <div class="login-box p-5 shadow bg-white rounded text-center">
148 151
                         <div class="login-title text-center">
149
-                        <!-- <a class="navbar-brand" href="#"><img src="{{ asset('assets') }}/img/logo/logo-pmi.svg" class="navbar-brand-img mb-4" alt="..."></a> -->
150
-                        <h2>Logo</h2>
152
+                        <img src="{{ asset('assets/img/logo/logo.png') }}" class="navbar-brand-img" alt="...">
151 153
                         <h3 class="font-weight-normal mb-5">Login Admin</h3>
152 154
 
153 155
                         </div>

+ 16
- 0
routes/api_v1.php Wyświetl plik

@@ -80,6 +80,22 @@ Route::group(
80 80
 );
81 81
 // End Transportation
82 82
 
83
+// Start Timline
84
+Route::group(
85
+    [
86
+        'prefix'    => 'timeline',
87
+        'as'        => 'timeline.',
88
+    ],
89
+    function () {
90
+        Route::get('/all', ['as' => 'index', 'uses' => 'TimelineController@getAll']);
91
+        Route::get('/find-by-user/{data_id?}', ['as' => 'find_by_user', 'uses' => 'TimelineController@getByUser']);
92
+        Route::post('/store', ['as' => 'store', 'uses' => 'TimelineController@postStore']);
93
+        Route::post('/update', ['as' => 'store', 'uses' => 'TimelineController@postUpdate']);
94
+        Route::post('/delete', ['as' => 'store', 'uses' => 'TimelineController@delete']);
95
+    }
96
+);
97
+// End Timeline
98
+
83 99
 // Start News
84 100
 Route::group(
85 101
     [

BIN
storage/images/transportation/Tot63i6p9g.png Wyświetl plik


BIN
storage/images/transportation/qXLXUVkC8B.png Wyświetl plik


Ładowanie…
Anuluj
Zapisz