10
10
use Coderstm \Traits \HasPermissionGroup ;
11
11
use Illuminate \Support \Facades \DB ;
12
12
use Illuminate \Foundation \Auth \User as Authenticatable ;
13
+ use Coderstm \Database \Factories \AdminFactory ;
13
14
use Illuminate \Notifications \Notifiable ;
14
15
use Illuminate \Contracts \Auth \MustVerifyEmail ;
16
+ use Coderstm \Exceptions \ImportFailedException ;
17
+ use Coderstm \Exceptions \ImportSkippedException ;
18
+ use League \ISO3166 \ISO3166 ;
15
19
16
20
class Admin extends Authenticatable
17
21
{
@@ -22,6 +26,7 @@ class Admin extends Authenticatable
22
26
protected $ fillable = [
23
27
'first_name ' ,
24
28
'last_name ' ,
29
+ 'gender ' ,
25
30
'email ' ,
26
31
'password ' ,
27
32
'phone_number ' ,
@@ -138,4 +143,68 @@ public function getShortCodes(): array
138
143
'{{ADMIN_PHONE_NUMBER}} ' => $ this ->phone_number ,
139
144
];
140
145
}
146
+
147
+ public static function getMappedAttributes (): array
148
+ {
149
+ return [
150
+ "First Name " => 'first_name ' ,
151
+ "Surname " => 'last_name ' ,
152
+ "Gender " => 'gender ' ,
153
+ "Email Address " => 'email ' ,
154
+ "Phone Number " => 'phone_number ' ,
155
+ "Status " => 'status ' ,
156
+ "Password " => 'password ' ,
157
+ "Created At " => 'created_at ' ,
158
+ "Address Line1 " => 'line1 ' ,
159
+ "Address Line2 " => 'line2 ' ,
160
+ "Country " => 'country ' ,
161
+ "State " => 'state ' ,
162
+ "State Code " => 'state_code ' ,
163
+ "City " => 'city ' ,
164
+ "Postcode/Zip " => 'postal_code ' ,
165
+ ];
166
+ }
167
+
168
+ public static function createFromCsv (array $ attributes = [], array $ options = [])
169
+ {
170
+ $ replaceByEmail = isset ($ options ['email_overwrite ' ]) && $ options ['email_overwrite ' ];
171
+ $ user = static ::where ('email ' , $ attributes ['email ' ])->first ();
172
+
173
+ if (!$ replaceByEmail && $ user ) {
174
+ throw new ImportFailedException ;
175
+ } else if ($ user && ($ user ->wasRecentlyUpdated || $ user ->wasRecentlyCreated )) {
176
+ throw new ImportSkippedException ;
177
+ }
178
+
179
+ if (isset ($ attributes ['password ' ])) {
180
+ $ attributes ['password ' ] = bcrypt ($ attributes ['password ' ]);
181
+ }
182
+
183
+ if (isset ($ attributes ['country ' ])) {
184
+ $ country = (new ISO3166 )->name ($ attributes ['country ' ]);
185
+ $ attributes ['country_code ' ] = $ country ['alpha2 ' ];
186
+ }
187
+
188
+ $ user = static ::firstOrNew ([
189
+ 'email ' => $ attributes ['email ' ]
190
+ ], $ attributes );
191
+
192
+ if (isset ($ attributes ['created_at ' ]) && !empty ($ attributes ['created_at ' ])) {
193
+ $ user ->created_at = $ attributes ['created_at ' ];
194
+ }
195
+
196
+ $ user ->save ();
197
+
198
+ $ user ->updateOrCreateAddress ($ attributes );
199
+ }
200
+
201
+ /**
202
+ * Create a new factory instance for the model.
203
+ *
204
+ * @return \Illuminate\Database\Eloquent\Factories\Factory
205
+ */
206
+ protected static function newFactory ()
207
+ {
208
+ return AdminFactory::new ();
209
+ }
141
210
}
0 commit comments