- Add Arcada outgoingMail

This commit is contained in:
Vincent BRIDIER 2021-02-19 16:32:08 +01:00
parent daba3098c5
commit 9136f04024
10 changed files with 279 additions and 10 deletions

View file

@ -0,0 +1,209 @@
<?php
namespace App\Http\Controllers\Api\v1;
use App\Http\Controllers\Controller;
use App\Http\Resources\StatusResource;
use App\Mail\CourrierArcadiaError;
use App\Models\Customer;
use DOMDocument;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
class ArcadiaController extends Controller
{
public function outgoingMail(Request $request)
{
$validator = Validator::make($request->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();
}
}

View file

@ -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();

View file

@ -0,0 +1,38 @@
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class CourrierArcadiaError extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
public $subject, $msg, $xml;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($subject, $msg, $xml)
{
$this->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']);
}
}

View file

@ -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")){

View file

@ -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",

View file

@ -55,6 +55,11 @@ return [
'visibility' => 'public',
],
'arcadia' => [
'driver' => 'local',
'root' => storage_path('export/arcadia'),
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),

View file

@ -88,6 +88,8 @@ return [
'name' => env('MAIL_FROM_NAME', 'Example'),
],
'MAIL_ADMIN' => env('MAIL_ADMIN'),
/*
|--------------------------------------------------------------------------
| Markdown Mail Settings

View file

@ -0,0 +1,2 @@
<p>Bonjour,</p>
<p>{{ $msg }}</p>

View file

@ -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']);
});

2
storage/export/arcadia/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
*
!.gitignore