From 44ca6f86d6e3799dc9bd3ef2f96d0539e6ae8a56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Mon, 25 Oct 2021 17:37:31 +0200 Subject: [PATCH 01/15] Ticket 375 : Fix duplicate Export CRM for newsletter --- app/Http/Controllers/CustomerController.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/CustomerController.php b/app/Http/Controllers/CustomerController.php index ef34a83..7ae92c5 100644 --- a/app/Http/Controllers/CustomerController.php +++ b/app/Http/Controllers/CustomerController.php @@ -9,6 +9,7 @@ use App\Models\Customer; use App\Models\CustomerInfo; use App\Models\CustomerLogin; use App\Models\SingleOptin; +use Exception; use Illuminate\Database\Query\JoinClause; use Illuminate\Http\Request; use Illuminate\Support\Carbon; @@ -179,11 +180,11 @@ class CustomerController extends Controller $continue = true; // continue case CustomerExportType::customer(): - $i = 0; + $page = 1; /** @var \Illuminate\Database\Eloquent\Builder $req */ $req = CustomerInfo::where('brand_id', $request->brand); $total = $req->count(); - $pages = $total / $pagination; + $pages = \ceil($total / $pagination); do { /** @var \Illuminate\Pagination\LengthAwarePaginator $items */ $items = $req->with('customer', function ($q) { @@ -192,7 +193,7 @@ class CustomerController extends Controller $pagination, ['lastname', 'firstname', 'hashcode', 'customer_id'], 'page', - $i + $page ); $items->map(function (CustomerInfo $customerInfo) use ($w) { $w->insertOne([ @@ -202,7 +203,7 @@ class CustomerController extends Controller 'hashcode' => $customerInfo->hashcode ]); }); - } while ($i++ <= $pages); + } while ($page++ <= $pages); if (!isset($continue)) { break; } @@ -218,6 +219,8 @@ class CustomerController extends Controller ]); }); break; + default: + throw new Exception(sprintf('Unhandled CustomerExportType %s', $exportType->label)); } //we create a callable to output the CSV in chunk From ffaaef435618383124c61773fe5a9dcf8028a20b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Mon, 25 Oct 2021 17:42:10 +0200 Subject: [PATCH 02/15] Ticket 391 : fix ApiV1 email strtolower --- app/Http/Controllers/Api/V1/CustomerController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Api/V1/CustomerController.php b/app/Http/Controllers/Api/V1/CustomerController.php index cee4ff1..f993420 100644 --- a/app/Http/Controllers/Api/V1/CustomerController.php +++ b/app/Http/Controllers/Api/V1/CustomerController.php @@ -43,7 +43,7 @@ class CustomerController extends Controller } $brandId = $request->brand_id; - $email = filter_var($request->email, FILTER_SANITIZE_EMAIL); + $email = strtolower(filter_var($request->email, FILTER_SANITIZE_EMAIL)); $firstname = filter_var($request->firstname, FILTER_SANITIZE_STRING); $lastname = filter_var($request->lastname, FILTER_SANITIZE_STRING); $optIn = filter_var($request->opt_in, FILTER_SANITIZE_STRING); @@ -55,7 +55,7 @@ class CustomerController extends Controller // Si le customer n'existe pas déjà if (!$customer) { $customer = new Customer(); - $customer->email = strtolower($email); + $customer->email = $email; $customer->save(); $singleOptin = SingleOptin::where([['email', $email], ['brand_id', $brandId]])->first(); if ($singleOptin) { From f41b6ae6448487a3019a3304f343107547785372 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Mon, 25 Oct 2021 17:48:43 +0200 Subject: [PATCH 03/15] Add more data sanitize on base request for user info --- app/Http/Requests/Api/V2/BaseApiRequest.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/Http/Requests/Api/V2/BaseApiRequest.php b/app/Http/Requests/Api/V2/BaseApiRequest.php index 4dec00b..851fef6 100644 --- a/app/Http/Requests/Api/V2/BaseApiRequest.php +++ b/app/Http/Requests/Api/V2/BaseApiRequest.php @@ -2,9 +2,11 @@ namespace App\Http\Requests\Api\V2; +use ArondeParon\RequestSanitizer\Sanitizers\Capitalize; use ArondeParon\RequestSanitizer\Sanitizers\Lowercase; use ArondeParon\RequestSanitizer\Sanitizers\RemoveNonNumeric; use ArondeParon\RequestSanitizer\Sanitizers\Trim; +use ArondeParon\RequestSanitizer\Sanitizers\TrimDuplicateSpaces; use ArondeParon\RequestSanitizer\Traits\SanitizesInputs; use Illuminate\Foundation\Http\FormRequest; @@ -13,9 +15,15 @@ abstract class BaseApiRequest extends FormRequest use SanitizesInputs; protected $sanitizers = [ + 'firstname' => [Trim::class, TrimDuplicateSpaces::class, Capitalize::class], + 'lastname' => [Trim::class, TrimDuplicateSpaces::class, Capitalize::class], + 'address' => [Trim::class, TrimDuplicateSpaces::class], + 'address2' => [Trim::class, TrimDuplicateSpaces::class], + 'city' => [Trim::class, TrimDuplicateSpaces::class], 'password' => Trim::class, + 'phone' => Trim::class, 'email' => [Lowercase::class, Trim::class], - 'postcode' => RemoveNonNumeric::class + 'postcode' => [RemoveNonNumeric::class, Trim::class] ]; /** From 97ccbf0b1ed4bf64cbfc2a8899eba3065468e778 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Mon, 25 Oct 2021 17:57:46 +0200 Subject: [PATCH 04/15] Fix for country save on Customer info not handling BaseEnom correct exception --- app/Models/CustomerInfo.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Models/CustomerInfo.php b/app/Models/CustomerInfo.php index e657fc6..829b680 100644 --- a/app/Models/CustomerInfo.php +++ b/app/Models/CustomerInfo.php @@ -44,7 +44,7 @@ class CustomerInfo extends Model if (ctype_digit($customerInfo->country)) { try { $customerInfo->newcountry = Country::make((int)$customerInfo->country); - } catch (\TypeError $e) { + } catch (\BadMethodCallException $e) { // TODO change this when country column is updated // Set afghanistan as default country for now $customerInfo->newcountry = Country::afghanistan(); @@ -68,7 +68,7 @@ class CustomerInfo extends Model if (ctype_digit($customerInfo->country)) { try { $customerInfo->newcountry = Country::make((int)$customerInfo->country); - } catch (\TypeError $e) { + } catch (\BadMethodCallException $e) { // TODO change this when country column is updated // Set afghanistan as default country for now $customerInfo->newcountry = Country::afghanistan(); From 2636e6b29d4bc035ddf19526cb8d6e5795e6682c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Tue, 26 Oct 2021 09:17:02 +0200 Subject: [PATCH 05/15] Remove report method from handler and add RedirectTo::class to $dontReport exception --- app/Exceptions/Handler.php | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 15f9fb3..0940f66 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -28,7 +28,8 @@ class Handler extends ExceptionHandler * @var array */ protected $dontReport = [ - ApiErrorException::class + ApiErrorException::class, + RedirectTo::class ]; /** @@ -51,22 +52,6 @@ class Handler extends ExceptionHandler // } - /** - * Report or log an exception. - * - * @param \Throwable $exception - * @return void - * - * @throws \Exception - */ - public function report(Throwable $e) - { - if (get_class($e) == RedirectTo::class) { - abort(redirect($e->url)); - } - parent::report($e); - } - /** * Render an exception into an HTTP response. * From eba24439a8e455869b5396761122ef73f5a871f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Tue, 26 Oct 2021 09:59:15 +0200 Subject: [PATCH 06/15] Fix customer test after client firstname and last name ucfirst force on crm --- tests/Unit/CrmClientV2CustomerTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Unit/CrmClientV2CustomerTest.php b/tests/Unit/CrmClientV2CustomerTest.php index 2ea1a2e..e1c348d 100644 --- a/tests/Unit/CrmClientV2CustomerTest.php +++ b/tests/Unit/CrmClientV2CustomerTest.php @@ -155,13 +155,13 @@ class CrmClientV2CustomerTest extends CrmClientTester $this->assertTrue($customer->save()); $customer = $api->getCustomerOrNew(static::$info->hashcode); - $this->assertEquals('toto', $customer->firstname); + $this->assertEquals('Toto', $customer->firstname); $customer->lastname = 'pouet'; $this->assertTrue($customer->save()); $customer = $api->getCustomerOrNew(static::$info->hashcode); - $this->assertEquals('pouet', $customer->lastname); + $this->assertEquals('Pouet', $customer->lastname); $customer->country = Country::belgium(); $customer->save(); @@ -223,7 +223,7 @@ class CrmClientV2CustomerTest extends CrmClientTester $this->assertTrue($customer->save()); - $this->assertEquals('toto', $customer->firstname); + $this->assertEquals('Toto', $customer->firstname); $this->assertEquals(null, $customer->password); } From 622fd15a7e74998f301916448a005b01a38a4bee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Tue, 26 Oct 2021 10:00:27 +0200 Subject: [PATCH 07/15] change unit test server ip from 127.0.0.25 to 127.0.0.1 (seems to have stoped working on wsl ?) --- .env.testing.example | 2 +- composer.json | 4 ++-- phpunit.xml | 2 +- tests/Unit/CrmClientTester.php | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.env.testing.example b/.env.testing.example index 658141c..a69adf0 100644 --- a/.env.testing.example +++ b/.env.testing.example @@ -2,7 +2,7 @@ APP_NAME=Laravel APP_ENV=testing APP_KEY=base64:UypKzhOMAs2aDu3ovgw/lINvWZQWicbz9nfUXDpDzeQ= APP_DEBUG=true -APP_URL="http://127.0.0.25:8888" +APP_URL="http://127.0.0.1:8888" LOG_CHANNEL=stack LOG_LEVEL=debug diff --git a/composer.json b/composer.json index d723901..ce5e54c 100644 --- a/composer.json +++ b/composer.json @@ -94,8 +94,8 @@ "configclear": "@php artisan config:clear", "runtestmigration": "@php artisan migrate:fresh --env=testing --seed", "runtest": "@php artisan test", - "runserve": "@php artisan serve --host=127.0.0.25 --port=8888 --env=testing&", - "killserve": "kill $(ps -f | grep '[1]27.0.0.25:8888' | awk '{print $2}')", + "runserve": "@php artisan serve --host=127.0.0.1 --port=8888 --env=testing&", + "killserve": "kill $(ps -f | grep '[1]27.0.0.1:8888' | awk '{print $2}')", "post-autoload-dump": [ "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", "@php artisan package:discover --ansi" diff --git a/phpunit.xml b/phpunit.xml index 7b01f29..8919029 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -19,7 +19,7 @@ - + diff --git a/tests/Unit/CrmClientTester.php b/tests/Unit/CrmClientTester.php index 6059a01..416cc9f 100644 --- a/tests/Unit/CrmClientTester.php +++ b/tests/Unit/CrmClientTester.php @@ -39,7 +39,7 @@ abstract class CrmClientTester extends TestCase } config(['crm.token' => self::$service->bearer_token]); config(['crm.password' => self::$service->bearer_pwd]); - config(['crm.endpoint' => 'http://127.0.0.25:8888']); + config(['crm.endpoint' => 'http://127.0.0.1:8888']); } /** From 6a0a94b6336e2c206221c8fa578b619645632602 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Tue, 26 Oct 2021 10:00:36 +0200 Subject: [PATCH 08/15] bump client dep --- clients/V2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/V2 b/clients/V2 index c5249d3..fa22cdf 160000 --- a/clients/V2 +++ b/clients/V2 @@ -1 +1 @@ -Subproject commit c5249d37a14d125dfa85d49cea495da4efb79490 +Subproject commit fa22cdf03b0138580612cd1dc56ec5eccbcf0326 From 7223ebfd8546b1fe85d4643e18a6ef30a34b92e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Tue, 26 Oct 2021 10:02:44 +0200 Subject: [PATCH 09/15] Fix wrong assertion in unit test --- tests/Unit/CrmClientV2CustomerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/CrmClientV2CustomerTest.php b/tests/Unit/CrmClientV2CustomerTest.php index e1c348d..c667018 100644 --- a/tests/Unit/CrmClientV2CustomerTest.php +++ b/tests/Unit/CrmClientV2CustomerTest.php @@ -223,7 +223,7 @@ class CrmClientV2CustomerTest extends CrmClientTester $this->assertTrue($customer->save()); - $this->assertEquals('Toto', $customer->firstname); + $this->assertEquals('toto', $customer->firstname); $this->assertEquals(null, $customer->password); } From fd80875410be8828875ca3ecff9438fc5de7d0b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Tue, 26 Oct 2021 12:01:40 +0200 Subject: [PATCH 10/15] Change customer status to boolean --- app/Http/Controllers/Api/V1/CustomerController.php | 8 +++----- app/Http/Controllers/Api/V2/CustomerController.php | 6 +++--- app/Models/CustomerLogin.php | 8 ++++++++ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Api/V1/CustomerController.php b/app/Http/Controllers/Api/V1/CustomerController.php index f993420..0482273 100644 --- a/app/Http/Controllers/Api/V1/CustomerController.php +++ b/app/Http/Controllers/Api/V1/CustomerController.php @@ -103,8 +103,6 @@ class CustomerController extends Controller $customerLogin->last_connection = now(); $customerLogin->brand_id = $brandId; $customerLogin->status = 0; - // Par défaut à 0 pour activation par mail - //$customerLogin->status = $request->status !== null ? 1 : 0; $customer->logins()->save($customerLogin); @@ -295,7 +293,7 @@ class CustomerController extends Controller $login = $customer->logins()->where('brand_id', $brandId)->first(); if ($login) { if (Hash::check($password, $login->password)) { - if ($login->status === 0) { + if ($login->status === false) { $infos = $customer->infos()->where('brand_id', $brandId)->first(); // "Le compte de l'utilisateur n'est pas activé" return new StatusResource( @@ -397,8 +395,8 @@ class CustomerController extends Controller if ($customer) { $login = $customer->logins()->where('brand_id', $brandId)->first(); if ($login) { - if ($login->status === 0) { - $login->status = 1; + if ($login->status === false) { + $login->status = true; $login->save(); // "Le compte a bien été activé" return new StatusResource(['status' => 'OK', 'message' => 'Update complete.']); diff --git a/app/Http/Controllers/Api/V2/CustomerController.php b/app/Http/Controllers/Api/V2/CustomerController.php index 06f48b4..1c00810 100644 --- a/app/Http/Controllers/Api/V2/CustomerController.php +++ b/app/Http/Controllers/Api/V2/CustomerController.php @@ -180,7 +180,7 @@ class CustomerController extends Controller throw new ApiErrorException(ApiError::authFailed()); } - if ($customerLogin->status === 0) { + if (!$customerLogin->status) { throw new ApiErrorException(ApiError::customerNotActivated()); } @@ -201,8 +201,8 @@ class CustomerController extends Controller $this->authorize('customerBelongsToService', $customer); $login = $customer->login($service->brand, Customer::FIRST_OR_FAIL); - if ($login->status === 0) { - $login->status = 1; + if (!$login->status) { + $login->status = true; $login->save(); return response()->json(); } else { diff --git a/app/Models/CustomerLogin.php b/app/Models/CustomerLogin.php index 6922714..5c26542 100644 --- a/app/Models/CustomerLogin.php +++ b/app/Models/CustomerLogin.php @@ -10,10 +10,18 @@ class CustomerLogin extends Model { use HasFactory; + protected $attributes = [ + 'status' => false + ]; + protected $fillable = [ 'password' ]; + protected $casts = [ + 'status' => 'bool' + ]; + protected static function booted() { static::creating(function ($customerLogin) { From c4de30ae7e568e2b50650ea8f76ad769ebfdf1f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Tue, 26 Oct 2021 12:02:10 +0200 Subject: [PATCH 11/15] Full type hint for CustomerResource vars --- app/Http/Resources/Api/V2/CustomerResource.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/Http/Resources/Api/V2/CustomerResource.php b/app/Http/Resources/Api/V2/CustomerResource.php index da2a64a..e97b479 100644 --- a/app/Http/Resources/Api/V2/CustomerResource.php +++ b/app/Http/Resources/Api/V2/CustomerResource.php @@ -9,13 +9,13 @@ use Illuminate\Http\Resources\Json\JsonResource; class CustomerResource extends JsonResource { - /** @var Customer */ + /** @var \App\Models\Customer */ private $customer; - /** @var CustomerInfo */ + /** @var \App\Models\CustomerInfo */ private $info; - /** @var CustomerLogin */ + /** @var \App\Models\CustomerLogin */ private $login; private static $skeleton = [ From 7c26772d6977a266fabc608c1e3cd3fd0002b0cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Tue, 26 Oct 2021 12:08:37 +0200 Subject: [PATCH 12/15] Customer set bool cast for opt_in --- app/Models/CustomerInfo.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/Models/CustomerInfo.php b/app/Models/CustomerInfo.php index 829b680..3da4e03 100644 --- a/app/Models/CustomerInfo.php +++ b/app/Models/CustomerInfo.php @@ -17,6 +17,10 @@ class CustomerInfo extends Model 'newcountry' => Country::class ]; + protected $casts = [ + 'opt_in' => 'bool' + ]; + protected $fillable = [ 'firstname', 'lastname', From b8b6ae72394cdece78d07423d31b37767e4bf1d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Tue, 26 Oct 2021 12:36:52 +0200 Subject: [PATCH 13/15] Customer Resources return empty string for address2 when null --- app/Http/Resources/Api/V2/CustomerResource.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Http/Resources/Api/V2/CustomerResource.php b/app/Http/Resources/Api/V2/CustomerResource.php index e97b479..cbf066c 100644 --- a/app/Http/Resources/Api/V2/CustomerResource.php +++ b/app/Http/Resources/Api/V2/CustomerResource.php @@ -75,7 +75,8 @@ class CustomerResource extends JsonResource $this->getSkeleton() ); - $data['country'] = $this->info->newcountry->value; + $data['country'] = $this->info->newcountry->value; + $data['address2'] = $this->info->address2 ?? ''; return $data; } From 4ad98dc015cbf89a9ff806b2d0b52c13c8573550 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Tue, 26 Oct 2021 12:37:21 +0200 Subject: [PATCH 14/15] Fix data change for customer test on firstname --- tests/Unit/CrmClientV2CustomerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/CrmClientV2CustomerTest.php b/tests/Unit/CrmClientV2CustomerTest.php index c667018..e1c348d 100644 --- a/tests/Unit/CrmClientV2CustomerTest.php +++ b/tests/Unit/CrmClientV2CustomerTest.php @@ -223,7 +223,7 @@ class CrmClientV2CustomerTest extends CrmClientTester $this->assertTrue($customer->save()); - $this->assertEquals('toto', $customer->firstname); + $this->assertEquals('Toto', $customer->firstname); $this->assertEquals(null, $customer->password); } From 0569182bfb010faf0d029b0e851f296fb95b021b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Tue, 26 Oct 2021 12:52:03 +0200 Subject: [PATCH 15/15] bump version to 2.2.3 bump client --- clients/V2 | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/clients/V2 b/clients/V2 index fa22cdf..390033d 160000 --- a/clients/V2 +++ b/clients/V2 @@ -1 +1 @@ -Subproject commit fa22cdf03b0138580612cd1dc56ec5eccbcf0326 +Subproject commit 390033d3da4b5f9447410656f3d23787557588b5 diff --git a/composer.json b/composer.json index ce5e54c..b23e82f 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "ldc/ldccrmv2client", "type": "project", - "version": "2.2.1", + "version": "2.2.3", "description": "LDC CRM V2", "authors": [ {