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

210 lines
7.7 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
{
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
}
public function getContacts(Request $request) {
$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());
}
public function getDirectories(Request $request) {
return new \App\Http\Resources\DirectoriesCollection(\App\Models\Directories::all());
}
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
}
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 {
$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);
//echo $response->getStatusCode();
//echo $response->getHeaderLine('content-type');
$body = $response->getBody();
$storeVersion = json_decode($body)->version;
} catch (ClientException $e) {
//var_dump($e->getRequest());
//var_dump($e->getResponse()->getStatusCode());
$storeVersion = $app_version;
}
if ($storeVersion > $app_version) {
$newApp = true;
} else {
$newApp = false;
}
return ['version' => $version->version, 'ShowIsActive' => $ShowIsActive->ShowIsActive, 'newApp' => $newApp];
}
public function SendFlyer(Request $request) {
$emails = json_decode($request->emails);
foreach ($emails as $datas) {
$email = $datas->email;
$subject = $datas->subject;
$message = $datas->message;
$user_id = $datas->user_id;
$docs = json_decode($datas->documents);
$user = AppUser::find($user_id);
2021-01-15 14:47:59 +01:00
$attach = [];
$filesName = [];
$noExists = [];
foreach ($docs as $doc) {
$exists = Storage::disk('public')->exists('docs/' . $doc->uuid . '.' . $doc->type);
if ($exists) {
array_push($attach, URL::temporarySignedRoute(
'docs.download', now()->addDays(30), ['uuid' => $doc->uuid]
));
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,
2021-01-19 11:21:45 +01: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);
}
}
return 'OK';
}
private function syncContacts($contacts, $userId) {
2021-10-07 15:33:59 +02:00
$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) {
$tmp->email = $contact->email;
$tmp->type = $contact->type;
$tmp->other = $contact->other;
$tmp->select1 = $contact->select1;
$tmp->select2 = $contact->select2;
2021-01-15 14:47:59 +01:00
$tmp->name = $contact->name;
$tmp->firstname = $contact->firstname;
$tmp->company = $contact->company;
$tmp->address = $contact->address;
2021-10-07 15:33:59 +02:00
$tmp->phone = empty($contact->phone) ? "" : $contact->phone;
2021-01-15 14:47:59 +01:00
$tmp->products = $contact->products;
$tmp->notes = $contact->notes;
$tmp->visits = $contact->visits;
$tmp->user_id = $contact->user_id;
$tmp->save();
} else {
$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;
2021-01-15 14:47:59 +01:00
$tmp->name = $contact->name;
$tmp->firstname = $contact->firstname;
$tmp->company = $contact->company;
$tmp->address = $contact->address;
2021-10-07 15:33:59 +02:00
$tmp->phone = empty($contact->phone) ? "" : $contact->phone;
2021-01-15 14:47:59 +01:00
$tmp->products = $contact->products;
$tmp->notes = $contact->notes;
$tmp->visits = $contact->visits;
$tmp->user_id = $contact->user_id;
$tmp->save();
}
}
}
}
public function processOCR(Request $request) {
# instantiates a client
$imageAnnotator = new ImageAnnotatorClient([
'credentials' => public_path() . '/credentials.json'
]);
# the name of the image file to annotate
$fileName = 'test.jpg';
# prepare the image to be annotated
$image = file_get_contents(public_path() . "/" . $fileName);
# performs label detection on the image file
$response = $imageAnnotator->textDetection($image);
$labels = $response->getTextAnnotations();
foreach ($labels as $label) {
echo $label->getDescription() . "<br>";
}
}
}