From 9136f040245f5d0a24aef03c7dbbac98715db081 Mon Sep 17 00:00:00 2001 From: Vincent BRIDIER Date: Fri, 19 Feb 2021 16:32:08 +0100 Subject: [PATCH] - Add Arcada outgoingMail --- .../Controllers/Api/v1/ArcadiaController.php | 209 ++++++++++++++++++ .../Controllers/Api/v1/CustomerController.php | 3 +- app/Mail/CourrierArcadiaError.php | 38 ++++ clients/LDCCRM_V1.php | 4 + composer.json | 3 +- config/filesystems.php | 5 + config/mail.php | 2 + resources/views/mail/arcadia-error.blade.php | 2 + routes/api.php | 21 +- storage/export/arcadia/.gitignore | 2 + 10 files changed, 279 insertions(+), 10 deletions(-) create mode 100644 app/Http/Controllers/Api/v1/ArcadiaController.php create mode 100644 app/Mail/CourrierArcadiaError.php create mode 100644 resources/views/mail/arcadia-error.blade.php create mode 100644 storage/export/arcadia/.gitignore diff --git a/app/Http/Controllers/Api/v1/ArcadiaController.php b/app/Http/Controllers/Api/v1/ArcadiaController.php new file mode 100644 index 0000000..e998b24 --- /dev/null +++ b/app/Http/Controllers/Api/v1/ArcadiaController.php @@ -0,0 +1,209 @@ +all(), [ + 'crm_id' => 'required', + 'fields' => 'required|json', + 'type' => 'required', + ]); + + if ($validator->fails()) + return new StatusResource(['status' => 'KO', 'code' => 201, 'message' => 'Error on required fields.', 'errors' => $validator->errors()]); + + $brandId = $request->brand_id; + $crmId = $request->crm_id; + $fields = json_decode($request->fields); + $type = $request->type; + + $customer = Customer::where('id', $crmId)->first(); + if (!$customer) + return new StatusResource(['status' => 'KO', 'code' => 202, 'message' => 'User not found.']); + + $customerInfos = $customer->infos()->where('brand_id', $brandId)->first(); + + if (!$customerInfos) + return new StatusResource(['status' => 'KO', 'code' => 202, 'message' => 'User not found.']); + + $check = $this->checkRequiredFields($fields, $type); + if ($check['status'] !== 'OK') + return new StatusResource($check); + + $xml = $this->makeXML($fields, $type, $customer, $customerInfos); + + try { + Storage::disk('arcadia')->put(date ("Ymd_His") . ".xml", $xml); + return new StatusResource(['status' => 'OK']); + }catch (\Exception $e){ + Log::critical("ARCADIA XML CREATION FAILED : " . $e->getMessage()); + Mail::to(config('mail.MAIL_ADMIN'))->send(new CourrierArcadiaError("ARCADIA XML CREATION FAILED", "Une erreur est survenue lors de la création du fichier XML : " . $e->getMessage(), $xml)); + return new StatusResource(['status' => 'KO', 'code' => 204, 'message' => 'Arcadia XML Creation failed.']); + } + + } + + private function checkRequiredFields($fields, $type) + { + $errors = []; + + $key = array_search('operation', array_column($fields->root, 'fieldName')); + if ($key === false) + $errors[] = ['field' => 'operation', 'error' => 'required']; + + $key = array_search('support_code', array_column($fields->root, 'fieldName')); + if ($key === false) + $errors[] = ['field' => 'support_code', 'error' => 'required']; + + $key = array_search('label_type_support', array_column($fields->root, 'fieldName')); + if ($key === false) + $errors[] = ['field' => 'label_type_support', 'error' => 'required']; + + $key = array_search('order_date', array_column($fields->root, 'fieldName')); + if ($key === false) + $errors[] = ['field' => 'order_date', 'error' => 'required']; + + if (!preg_match("/^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/", $fields->root[$key]->fieldValue)) { + $errors[] = ['field' => 'order_date', 'error' => 'format must be DD/MM/YYYY']; + } + + if ($type === 'default'){ + + $key = array_search('order_num', array_column($fields->root, 'fieldName')); + if ($key === false) + $errors[] = ['field' => 'order_num', 'error' => 'required for default type']; + + $key = array_search('state_order_cb', array_column($fields->root, 'fieldName')); + if ($key === false) + $errors[] = ['field' => 'state_order_cb', 'error' => 'required for default type']; + + if ($fields->root[$key]->fieldValue !== 1 && $fields->root[$key]->fieldValue !== 'Null') + $errors[] = ['field' => 'state_order_cb', 'error' => 'value must be 1 or Null (Yes Null not NULL)']; + + $orderCB = $fields->root[$key]->fieldValue; + + $key = array_search('state_order_check', array_column($fields->root, 'fieldName')); + if ($key === false) + $errors[] = ['field' => 'state_order_check', 'error' => 'required for default type']; + + if ($fields->root[$key]->fieldValue !== 1 && $fields->root[$key]->fieldValue !== 'Null') + $errors[] = ['field' => 'state_order_check', 'error' => 'value must be 1 or Null (Yes Null not NULL)']; + + $orderCheck = $fields->root[$key]->fieldValue; + + if ($orderCB === $orderCheck) + $errors[] = ['field' => 'state_order', 'error' => 'state_order_cb and state_order_check values must be different']; + + $key = array_search('package_unit_weight', array_column($fields->orderLine, 'fieldName')); + if ($key === false) + $errors[] = ['field' => 'package_unit_weight', 'error' => 'required for default type']; + + }elseif ($type === 'game'){ + + $key = array_search('code_unique', array_column($fields->orderLine, 'fieldName')); + if ($key === false) + $errors[] = ['field' => 'code_unique', 'error' => 'required for game type']; + + } + + if (!empty($errors)) + return ['status' => 'NOK', 'code' => 203, 'message' => $errors]; + + return ['status' => 'OK']; + } + + private function makeXML($fields, $type, $customer, $customerInfos) + { + $pivot = [ + 'order_num' => 'Num_Commande', // required if type = default + 'order_date' => 'Date_Commande', // required - format : DD/MM/YYYY + 'operation' => 'Operation', // required + 'support_code' => 'Code_Support', // required + 'label_type_support' => 'Libelle_Type_Support', // required + 'package_unit_weight' => 'Poids_Unitaire_Colis', // required if type = default + 'qty' => 'Quantite', + 'code_on_pack' => 'Code_on_Pack', + 'code_unique' => 'Code_Unique', // required if type = game + 'state_order_cb' => 'Etat_Commande_CB', // required if type = default - format 1 ou Null (oui Null et pas NULL) + 'state_order_check' => 'Etat_Commande_Cheque', // required if type = default - format 1 ou Null (oui Null et pas NULL) + + ]; + + $dom = new DOMDocument('1.0','UTF-8'); + $dom->formatOutput = true; + + $root = $dom->createElement('export_arcadia'); + $dom->appendChild($root); + $order = $root->appendChild($dom->createElement('Commande')); + + $key = array_search('order_num', array_column($fields->root, 'fieldName')); + if ($key === false) + $order->appendChild($dom->createElement('Num_Commande')); + + foreach ($fields->root as $value){ + if (isset($pivot[$value->fieldName]) && $value->fieldName !== 'state_order_cb' && $value->fieldName !== 'state_order_check' && $value->fieldName !== 'order_num') + $order->appendChild($dom->createElement($pivot[$value->fieldName], $value->fieldValue)); + } + + if ($type === 'default') { + $key = array_search('state_order_cb', array_column($fields->root, 'fieldName')); + if ($key) + $order->appendChild($dom->createElement('Etat_Commande_CB', $fields->root[$key]->fieldValue)); + + $key = array_search('state_order_check', array_column($fields->root, 'fieldName')); + if ($key) + $order->appendChild($dom->createElement('Etat_Commande_Cheque', $fields->root[$key]->fieldValue)); + } + + $order->appendChild($dom->createElement('Email_conso'))->appendChild($dom->createCDATASection( "$customer->email")); + $order->appendChild($dom->createElement('Civilite')); + $order->appendChild($dom->createElement('Nom'))->appendChild($dom->createCDATASection( "$customerInfos->lastname")); + $order->appendChild($dom->createElement('Prenom'))->appendChild($dom->createCDATASection( "$customerInfos->firstname")); + $order->appendChild($dom->createElement('Adresse'))->appendChild($dom->createCDATASection( "$customerInfos->address")); + $order->appendChild($dom->createElement('Adresse_Complementaire'))->appendChild($dom->createCDATASection( "$customerInfos->address2")); + $order->appendChild($dom->createElement('CP'))->appendChild($dom->createCDATASection( "$customerInfos->postcode")); + $order->appendChild($dom->createElement('Ville'))->appendChild($dom->createCDATASection( "$customerInfos->city")); + $order->appendChild($dom->createElement('Telephone'))->appendChild($dom->createCDATASection( "$customerInfos->phone")); + + $orderLine = $order->appendChild($dom->createElement('Ligne_de_Commande')); + + $key = array_search('qty', array_column($fields->orderLine, 'fieldName')); + if ($key === false) + $orderLine->appendChild($dom->createElement('Quantite', '1')); + + $key = array_search('package_unit_weight', array_column($fields->orderLine, 'fieldName')); + if ($key === false) + $orderLine->appendChild($dom->createElement('Poids_Unitaire_Colis')); + + foreach ($fields->orderLine as $value){ + if (isset($pivot[$value->fieldName]) && $value->fieldName !== 'code_on_pack' && $value->fieldName !== 'code_unique') + $orderLine->appendChild($dom->createElement($pivot[$value->fieldName], $value->fieldValue)); + } + + if ($type === 'game') { + $key = array_search('code_on_pack', array_column($fields->orderLine, 'fieldName')); + if ($key) + $orderLine->appendChild($dom->createElement('Code_on_Pack'))->appendChild($dom->createCDATASection($fields->orderLine[$key]->fieldValue)); + + $key = array_search('code_unique', array_column($fields->orderLine, 'fieldName')); + if ($key) + $orderLine->appendChild($dom->createElement('Code_Unique'))->appendChild($dom->createCDATASection($fields->orderLine[$key]->fieldValue)); + } + + return $dom->saveXML(); + } +} diff --git a/app/Http/Controllers/Api/v1/CustomerController.php b/app/Http/Controllers/Api/v1/CustomerController.php index 4073222..4c0324e 100644 --- a/app/Http/Controllers/Api/v1/CustomerController.php +++ b/app/Http/Controllers/Api/v1/CustomerController.php @@ -162,6 +162,8 @@ class CustomerController extends Controller $optIn = filter_var($request->opt_in, FILTER_SANITIZE_STRING); $customerInfos = $customer->infos()->where('brand_id', $brandId)->first(); + if (!$customerInfos) + return new StatusResource(['status' => 'KO', 'code' => 105, 'message' => 'User not found.']); $customerInfos->firstname = $firstname; $customerInfos->lastname = $lastname; @@ -183,7 +185,6 @@ class CustomerController extends Controller } $customerInfos->opt_in = $optIn; - $customerInfos->brand_id = 1; $customerInfos->save(); diff --git a/app/Mail/CourrierArcadiaError.php b/app/Mail/CourrierArcadiaError.php new file mode 100644 index 0000000..889a893 --- /dev/null +++ b/app/Mail/CourrierArcadiaError.php @@ -0,0 +1,38 @@ +subject = $subject; + $this->msg = $msg; + $this->xml = $xml; + } + + /** + * Build the message. + * + * @return $this + */ + public function build() + { + $msg = $this->msg; + return $this->subject("[LDC CRM] ". $this->subject)->view('mail.arcadia-error', compact('msg'))->attachData($this->xml, date ("Ymd_His") . '.xml', ['mime' => 'application/xml']); + } +} diff --git a/clients/LDCCRM_V1.php b/clients/LDCCRM_V1.php index 4dce333..4d6c6da 100644 --- a/clients/LDCCRM_V1.php +++ b/clients/LDCCRM_V1.php @@ -79,6 +79,10 @@ class ApiV1 return $this->callAPI('POST', '/single-optin', ['email' => $email]); } + public function ArcadiaOutgoingMail($crm_id, $fields, $type){ + return $this->callAPI('POST', '/arcadia/outgoing-mail', ['crm_id' => $crm_id, 'fields' => json_encode($fields), 'type' => $type]); + } + private function callAPI($method, $url, $data = false){ if (strpos($this->endpoint, "dev")){ diff --git a/composer.json b/composer.json index 7b033cb..f298c95 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ "laravel/framework": "^8.12", "laravel/tinker": "^2.5", "ext-curl": "*", - "ext-json": "*" + "ext-json": "*", + "ext-dom": "*" }, "require-dev": { "facade/ignition": "^2.5", diff --git a/config/filesystems.php b/config/filesystems.php index 94c8112..fad67bc 100644 --- a/config/filesystems.php +++ b/config/filesystems.php @@ -55,6 +55,11 @@ return [ 'visibility' => 'public', ], + 'arcadia' => [ + 'driver' => 'local', + 'root' => storage_path('export/arcadia'), + ], + 's3' => [ 'driver' => 's3', 'key' => env('AWS_ACCESS_KEY_ID'), diff --git a/config/mail.php b/config/mail.php index 54299aa..e58d776 100644 --- a/config/mail.php +++ b/config/mail.php @@ -88,6 +88,8 @@ return [ 'name' => env('MAIL_FROM_NAME', 'Example'), ], + 'MAIL_ADMIN' => env('MAIL_ADMIN'), + /* |-------------------------------------------------------------------------- | Markdown Mail Settings diff --git a/resources/views/mail/arcadia-error.blade.php b/resources/views/mail/arcadia-error.blade.php new file mode 100644 index 0000000..90072e8 --- /dev/null +++ b/resources/views/mail/arcadia-error.blade.php @@ -0,0 +1,2 @@ +

Bonjour,

+

{{ $msg }}

diff --git a/routes/api.php b/routes/api.php index 87cfdc0..f56474f 100644 --- a/routes/api.php +++ b/routes/api.php @@ -2,6 +2,7 @@ use Illuminate\Support\Facades\Route; use App\Http\Controllers\Api\v1\CustomerController; +use App\Http\Controllers\Api\v1\ArcadiaController; /* |-------------------------------------------------------------------------- @@ -16,14 +17,18 @@ use App\Http\Controllers\Api\v1\CustomerController; Route::prefix('v1')->middleware(['auth:api', 'check-brand'])->group(function() { // Clients - Route::get('/customer/{type}/{value}', [CustomerController::class, 'show']); - Route::post('/customer/create', [CustomerController::class, 'store']); - Route::put('/customer/{id}', [CustomerController::class, 'update']); - Route::delete('/customer/{id}', [CustomerController::class, 'delete']); - Route::post('/customer/{id}/change-password', [CustomerController::class, 'changePassword']); - Route::post('/customer/{id}/activate', [CustomerController::class, 'activateAccount']); - Route::post('/customer/{id}/toggle-optin', [CustomerController::class, 'toggleOptin']); - Route::post('/customer/login', [CustomerController::class, 'authenticate']); + Route::prefix('customer')->group(function (){ + Route::get('/{type}/{value}', [CustomerController::class, 'show']); + Route::post('/create', [CustomerController::class, 'store']); + Route::put('/{id}', [CustomerController::class, 'update']); + Route::delete('/{id}', [CustomerController::class, 'delete']); + Route::post('/{id}/change-password', [CustomerController::class, 'changePassword']); + Route::post('/{id}/activate', [CustomerController::class, 'activateAccount']); + Route::post('/{id}/toggle-optin', [CustomerController::class, 'toggleOptin']); + Route::post('/login', [CustomerController::class, 'authenticate']); + }); // Optin simple Route::post('/single-optin', [CustomerController::class, 'addSingleOptin']); + // Arcadia + Route::post('/arcadia/outgoing-mail', [ArcadiaController::class, 'outgoingMail']); }); diff --git a/storage/export/arcadia/.gitignore b/storage/export/arcadia/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/storage/export/arcadia/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore