ldc-com-app-bo/app/Http/Controllers/API/SyncController.php

218 lines
8 KiB
PHP
Raw Normal View History

2021-01-15 14:47:59 +01:00
<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use App\Mail\SendFlyerFailed;
use App\Models\AppUser;
2021-01-15 14:47:59 +01:00
use App\Models\Contacts;
use App\Models\Setting;
2021-01-15 14:47:59 +01:00
use GuzzleHttp\Exception\ClientException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;
use App\Mail\SendFlyer;
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
class SyncController extends Controller
{
2022-09-29 10:09:43 +02:00
public function getUsers(Request $request)
{
return new \App\Http\Resources\AppUserCollection(\App\Models\AppUser::where('activated', 1)->get());
2021-01-15 14:47:59 +01:00
}
2022-09-29 10:09:43 +02:00
public function getContacts(Request $request)
{
2021-01-15 14:47:59 +01:00
$userId = $request->userId;
2021-10-07 15:33:59 +02:00
$this->syncContacts($request->contacts, $userId);
2021-01-15 14:47:59 +01:00
return new \App\Http\Resources\ContactsCollection(\App\Models\Contacts::where('user_id', $userId)->get());
}
2022-09-29 10:09:43 +02:00
public function getDirectories(Request $request)
{
2021-01-15 14:47:59 +01:00
return new \App\Http\Resources\DirectoriesCollection(\App\Models\Directories::all());
}
2022-09-29 10:09:43 +02:00
public function getDocuments(Request $request)
{
2022-05-11 12:36:27 +02:00
return new \App\Http\Resources\DocumentsCollection(\App\Models\Document::where('published', 1)->get());
2021-01-15 14:47:59 +01:00
}
2022-09-29 10:09:43 +02:00
public function getVersion(Request $request)
{
$app_uuid = $request->app_uuid;
$app_version = $request->app_version;
$version = Setting::select('value AS version')->where('key', 'version')->first();
$ShowIsActive = Setting::select('value AS ShowIsActive')->where('key', 'ShowIsActive')->first();
2021-01-15 14:47:59 +01:00
try {
2022-09-29 10:09:43 +02:00
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'https://store.ldc.fr/api/get-version?token=@P$oi6or!bL2-r%3C&app_uuid=' . $app_uuid);
2021-01-15 14:47:59 +01:00
2022-09-29 10:09:43 +02:00
// echo $response->getStatusCode();
// echo $response->getHeaderLine('content-type');
$body = $response->getBody();
2021-01-15 14:47:59 +01:00
$storeVersion = json_decode($body)->version;
} catch (ClientException $e) {
2022-09-29 10:09:43 +02:00
// var_dump($e->getRequest());
// var_dump($e->getResponse()->getStatusCode());
2021-01-15 14:47:59 +01:00
$storeVersion = $app_version;
}
if ($storeVersion > $app_version) {
$newApp = true;
} else {
$newApp = false;
}
return ['version' => $version->version, 'ShowIsActive' => $ShowIsActive->ShowIsActive, 'newApp' => $newApp];
}
2022-09-29 10:09:43 +02:00
public function SendFlyer(Request $request)
{
2021-01-15 14:47:59 +01:00
$emails = json_decode($request->emails);
foreach ($emails as $datas) {
2022-09-29 10:09:43 +02:00
$email = $datas->email;
2021-01-15 14:47:59 +01:00
$subject = $datas->subject;
$message = $datas->message;
$user_id = $datas->user_id;
2022-09-29 10:09:43 +02:00
$docs = json_decode($datas->documents);
2021-01-15 14:47:59 +01:00
$user = AppUser::find($user_id);
2021-01-15 14:47:59 +01:00
2022-09-29 10:09:43 +02:00
$attach = [];
2021-01-15 14:47:59 +01:00
$filesName = [];
2022-09-29 10:09:43 +02:00
$noExists = [];
2021-01-15 14:47:59 +01:00
foreach ($docs as $doc) {
$exists = Storage::disk('public')->exists('docs/' . $doc->uuid . '.' . $doc->type);
if ($exists) {
array_push($attach, URL::temporarySignedRoute(
2022-09-29 10:09:43 +02:00
'docs.download',
now()->addDays(30),
['uuid' => $doc->uuid]
2021-01-15 14:47:59 +01:00
));
array_push($filesName, $doc->title);
} else {
array_push($noExists, $doc->title);
}
}
if (count($noExists) === 0) {
$message = new SendFlyer([
'body' => $message,
'from' => $user->email,
'fromName' => $user->surname . ' ' . $user->firstname,
2022-09-29 10:09:43 +02:00
'subject' => '[' . config('app.BRAND') . '] - ' . $subject,
2021-01-15 14:47:59 +01:00
'attach' => $attach,
'filesName' => $filesName,
]);
Mail::to($email)
->cc($user->email)
->queue($message);
} else {
$message = new SendFlyerFailed([
'email' => $email,
'message' => $message,
'subject' => $subject,
'filesName' => $filesName,
'noExists' => $noExists
]);
Mail::to($user->email)
->queue($message);
2022-09-29 10:09:43 +02:00
}//end if
}//end foreach
2021-01-15 14:47:59 +01:00
return 'OK';
}
2022-09-29 10:09:43 +02:00
private function syncContacts($contacts, $userId)
{
$contacts = json_decode(json_encode($contacts), false);
2021-01-15 14:47:59 +01:00
foreach ($contacts as $contact) {
if ($contact->to_delete === true) {
$tmp = Contacts::where([
['id', '=', $contact->uniq_id],
['user_id', '=', $contact->user_id]
])->first();
if ($tmp) {
$tmp->delete();
}
} else {
if ($contact->user_id != $userId) {
$tmp = Contacts::where([
['id', '=', $contact->uniq_id],
['user_id', '=', $userId]
])->first();
if ($tmp) {
$tmp->delete();
}
}
$tmp = Contacts::where([
['id', '=', $contact->uniq_id],
['user_id', '=', $contact->user_id]
])->first();
if ($tmp) {
2022-09-29 10:09:43 +02:00
$tmp->email = $contact->email;
$tmp->type = $contact->type;
$tmp->other = $contact->other;
$tmp->select1 = $contact->select1;
$tmp->select2 = $contact->select2;
$tmp->name = $contact->name;
2021-01-15 14:47:59 +01:00
$tmp->firstname = $contact->firstname;
2022-09-29 10:09:43 +02:00
$tmp->company = $contact->company;
$tmp->address = $contact->address;
$tmp->phone = empty($contact->phone) ? "" : $contact->phone;
$tmp->products = $contact->products;
$tmp->notes = $contact->notes;
$tmp->visits = $contact->visits;
$tmp->user_id = $contact->user_id;
2021-01-15 14:47:59 +01:00
$tmp->save();
} else {
2022-09-29 10:09:43 +02:00
$tmp = new Contacts();
$tmp->id = $contact->uniq_id;
$tmp->email = $contact->email;
$tmp->type = $contact->type;
$tmp->other = $contact->other;
$tmp->select1 = $contact->select1;
$tmp->select2 = $contact->select2;
$tmp->name = $contact->name;
2021-01-15 14:47:59 +01:00
$tmp->firstname = $contact->firstname;
2022-09-29 10:09:43 +02:00
$tmp->company = $contact->company;
$tmp->address = $contact->address;
$tmp->phone = empty($contact->phone) ? "" : $contact->phone;
$tmp->products = $contact->products;
$tmp->notes = $contact->notes;
$tmp->visits = $contact->visits;
$tmp->user_id = $contact->user_id;
2021-01-15 14:47:59 +01:00
$tmp->save();
2022-09-29 10:09:43 +02:00
}//end if
}//end if
}//end foreach
2021-01-15 14:47:59 +01:00
}
2022-09-29 10:09:43 +02:00
public function processOCR(Request $request)
{
2021-01-15 14:47:59 +01:00
2022-09-29 10:09:43 +02:00
// instantiates a client
2021-01-15 14:47:59 +01:00
$imageAnnotator = new ImageAnnotatorClient([
'credentials' => public_path() . '/credentials.json'
]);
2022-09-29 10:09:43 +02:00
// the name of the image file to annotate
2021-01-15 14:47:59 +01:00
$fileName = 'test.jpg';
2022-09-29 10:09:43 +02:00
// prepare the image to be annotated
2021-01-15 14:47:59 +01:00
$image = file_get_contents(public_path() . "/" . $fileName);
2022-09-29 10:09:43 +02:00
// performs label detection on the image file
2021-01-15 14:47:59 +01:00
$response = $imageAnnotator->textDetection($image);
2022-09-29 10:09:43 +02:00
$labels = $response->getTextAnnotations();
2021-01-15 14:47:59 +01:00
foreach ($labels as $label) {
echo $label->getDescription() . "<br>";
}
}
}