first(); } public function findByName($name) { return Customer::where('name', $name)->first(); } public function findByEmail($email) { return Customer::where('email', $email)->first(); } public function fetchByImportFromWeb($userWebId) { return Customer::select('id')->where([ ["import_from", "order_bcorp"], ["import_id", $userWebId] ])->first(); } public function fetchByImportFromAndroid($userAndroidId) { return Customer::select('id')->where([ ["import_from", "android"], ["import_id", $userAndroidId] ])->first(); } public function fetchByImport($from, $importId) { return Customer::select('id')->where([ ["import_from", $from], ["import_id", $importId] ])->first(); } public function getRandomId($limit = 10) { $customer = Customer::limit($limit)->inRandomOrder(); return $customer->first()->id ?? null; } public function getPublicPathIdCardUpload() { return public_path(Customer::$public_path_id_card); } public function select2Paginate(?string $search): PaginateCollectionAttr { $customer = Customer::selectRaw('*, id, name as text')->with('customerPhone', 'customerPhones'); $builder = (new PaginateCollectionAttrBuilder) ->build( $customer, (new PaginateBuilderAttr)->setSearchKeyword($search), new PaginateSchemaCustomer ); $newCollections = $builder->getCollections()->map(function ($customer) { $customerPhone = $customer->customerPhone->phone_number ?? null; $customer['text'] = $customer->name . ' | ' . $customerPhone; return $customer; }); return $builder->setCollections($newCollections); } public function paginate(PaginateBuilderAttr $paginateBuilderAttr): PaginateCollectionAttr { $builder = (new PaginateCollectionAttrBuilder) ->build( Customer::with('customerPhones'), $paginateBuilderAttr, new PaginateSchemaCustomer ); try { //code... $customerChatFacade = app(CustomerChatFacade::class); $newCollections = $builder->getCollections()->map(function ($customer) use ($customerChatFacade) { $customer['unread_chat_count'] = $customerChatFacade->countChatByCustomerId($customer->id); return $customer; }); } catch (\Throwable $th) { $newCollections = $builder->getCollections(); } return $builder->setCollections($newCollections); } public function fetch($id) { return Customer::with('customerPhone', 'customerPhones', 'customerAddresses.province', 'customerAddresses.city', 'customerAddresses.subdistrict', 'customerAddresses.subdistrictArea')->findOrFail($id); } public function create(CustomerAttr $customerAttr) { $customer = Customer::create([ 'name' => $customerAttr->getName(), 'email' => $customerAttr->getEmail(), 'password' => $customerAttr->getPassword() ? bcrypt($customerAttr->getPassword()) : null, 'address' => $customerAttr->getAddress(), 'province_id' => $customerAttr->getProvince(), 'city_id' => $customerAttr->getCity(), 'subdistrict_id' => $customerAttr->getSubdistrict(), 'subdistrict_area_id' => $customerAttr->getSubdistrictAreaId(), 'email_verification' => $customerAttr->getEmailVerification(), 'qualification' => $customerAttr->getQualification(), 'gender' => $customerAttr->getGender(), 'date_of_birth' => $customerAttr->getDateOfBirth(), 'bank_info' => $customerAttr->getBankInfo(), 'id_card' => $customerAttr->getIdCard(), 'id_card_address' => $customerAttr->getIdCardAddress(), 'uploaded_id_card' => $customerAttr->getUploadedIdCard(), 'email_verification' => $customerAttr->getEmailVerification(), 'phone_verification' => $customerAttr->getPhoneVerification(), 'import_from' => $customerAttr->getImportFrom(), 'import_id' => $customerAttr->getImportId(), 'kafdig_user_id' => $customerAttr->getKafdigUserId(), 'created_at' => $customerAttr->getCreatedAt() ?? date("Y-m-d H:i:s"), 'token' => $customerAttr->getToken() ]); return $customer; } public function update(CustomerAttr $customerAttr, $id) { $customer = $this->fetch($id); $customer->update([ 'name' => $customerAttr->getName(), 'email' => $customerAttr->getEmail(), 'password' => $customerAttr->getPassword() ? bcrypt($customerAttr->getPassword()) : "", 'address' => $customerAttr->getAddress(), 'province_id' => $customerAttr->getProvince(), 'city_id' => $customerAttr->getCity(), 'subdistrict_id' => $customerAttr->getSubdistrict(), 'subdistrict_area_id' => $customerAttr->getSubdistrictAreaId(), 'qualification' => $customerAttr->getQualification(), 'gender' => $customerAttr->getGender(), 'date_of_birth' => $customerAttr->getDateOfBirth(), 'bank_info' => $customerAttr->getBankInfo(), 'id_card' => $customerAttr->getIdCard(), 'id_card_address' => $customerAttr->getIdCardAddress(), 'uploaded_id_card' => $customerAttr->getUploadedIdCard(), 'email_verification' => $customerAttr->getEmailVerification(), 'phone_verification' => $customerAttr->getPhoneVerification(), 'import_from' => $customerAttr->getImportFrom(), 'import_id' => $customerAttr->getImportId() ]); return $this->fetch($id); } public function delete($id) { $customer = $this->fetch($id); $customer->delete(); return $customer; } public function updateAddress($id, CustomerAttr $customerAttr) { $customer = Customer::where('id', $id)->update([ 'address' => $customerAttr->getAddress(), 'province_id' => $customerAttr->getProvince(), 'city_id' => $customerAttr->getCity(), 'subdistrict_id' => $customerAttr->getSubdistrict(), 'subdistrict_area_id' => $customerAttr->getSubdistrictAreaId() ]); return $customer; } public function select2PaginateByKafdigUserId($id, ?string $search):PaginateCollectionAttr { $customer = Customer::selectRaw('*, id, name as text')->with('customerPhone')->where('kafdig_user_id', $id); return (new PaginateCollectionAttrBuilder) ->build( $customer, (new PaginateBuilderAttr)->setSearchKeyword($search), new PaginateSchemaCustomer ); } public function PaginateByKafdigUserId($id, PaginateBuilderAttr $paginateBuilderAttr): PaginateCollectionAttr { return (new PaginateCollectionAttrBuilder) ->build( Customer::with('customerPhones')->where('kafdig_user_id', $id), $paginateBuilderAttr, new PaginateSchemaCustomer ); } public function verifyEmail($id) { $res = $this->fetch($id); $res->update([ 'email_verification' => 1 ]); return $this->fetch($id); } public function verifyPhoneNumber($id) { $res = $this->fetch($id); $res->update([ 'phone_verification' => 1 ]); return $this->fetch($id); } public function fetchKafdig($token) { $res = Customer::where('token', $token)->first(); return $res ? $this->fetch($res->id) : abort(404); } public function deleteKafdig($token) { $res = $this->fetchKafdig($token); $res->delete(); return $res; } public function changePassword($request) { $oldPassword = Auth::guard('jwt')->user()->password; $userId = Auth::guard('jwt')->user()->id; if (Hash::check($request->old_password, $oldPassword)) { $res = Customer::where('id', $userId)->first(); $res->update([ 'password' => Hash::make($request->password) ]); } else { return abort(422, __("message.password_not_match")); } return $res; } }