refactor: replace custom CRM with Monica fork
Some checks failed
Build & Push Monica Image to Gitea Registry / build-and-push (push) Failing after 9s
Some checks failed
Build & Push Monica Image to Gitea Registry / build-and-push (push) Failing after 9s
This commit is contained in:
50
app/Services/Instance/AuditLog/LogAccountAction.php
Normal file
50
app/Services/Instance/AuditLog/LogAccountAction.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Instance\AuditLog;
|
||||
|
||||
use App\Services\BaseService;
|
||||
use App\Models\Instance\AuditLog;
|
||||
|
||||
class LogAccountAction extends BaseService
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the service.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'account_id' => 'required|integer|exists:accounts,id',
|
||||
'author_id' => 'required|integer|exists:users,id',
|
||||
'about_contact_id' => 'nullable|integer|exists:contacts,id',
|
||||
'author_name' => 'required|string|max:255',
|
||||
'audited_at' => 'required|date',
|
||||
'action' => 'required|string|max:255',
|
||||
'should_appear_on_dashboard' => 'nullable|boolean',
|
||||
'objects' => 'required|json',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Log an action that happened in an account.
|
||||
*
|
||||
* @param array $data
|
||||
* @return AuditLog
|
||||
*/
|
||||
public function execute(array $data): AuditLog
|
||||
{
|
||||
$this->validate($data);
|
||||
|
||||
return AuditLog::create([
|
||||
'account_id' => $data['account_id'],
|
||||
'author_id' => $data['author_id'],
|
||||
'about_contact_id' => $this->nullOrValue($data, 'about_contact_id'),
|
||||
'author_name' => $data['author_name'],
|
||||
'audited_at' => $data['audited_at'],
|
||||
'action' => $data['action'],
|
||||
'objects' => $data['objects'],
|
||||
'should_appear_on_dashboard' => $this->valueOrFalse($data, 'should_appear_on_dashboard'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
115
app/Services/Instance/Geolocalization/GetGPSCoordinate.php
Normal file
115
app/Services/Instance/Geolocalization/GetGPSCoordinate.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Instance\Geolocalization;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use App\Models\Account\Place;
|
||||
use App\Services\BaseService;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Http\Client\RequestException;
|
||||
use App\Exceptions\RateLimitedSecondException;
|
||||
use App\Exceptions\MissingEnvVariableException;
|
||||
|
||||
class GetGPSCoordinate extends BaseService
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the service.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'account_id' => 'required|integer|exists:accounts,id',
|
||||
'place_id' => 'required|integer|exists:places,id',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the latitude and longitude from a place.
|
||||
* This method uses LocationIQ to process the geocoding.
|
||||
*
|
||||
* @param array $data
|
||||
* @return Place|null
|
||||
*/
|
||||
public function execute(array $data)
|
||||
{
|
||||
$this->validateWeatherEnvVariables();
|
||||
|
||||
$this->validate($data);
|
||||
|
||||
$place = Place::where('account_id', $data['account_id'])
|
||||
->findOrFail($data['place_id']);
|
||||
|
||||
return $this->query($place);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure that geolocation env variables are set.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function validateWeatherEnvVariables()
|
||||
{
|
||||
if (! config('monica.enable_geolocation') || is_null(config('monica.location_iq_api_key'))) {
|
||||
throw new MissingEnvVariableException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the query to send with the API call.
|
||||
*
|
||||
* @param Place $place
|
||||
* @return string|null
|
||||
*/
|
||||
private function buildQuery(Place $place): ?string
|
||||
{
|
||||
if (($q = $place->getAddressAsString()) === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$query = http_build_query([
|
||||
'format' => 'json',
|
||||
'key' => config('monica.location_iq_api_key'),
|
||||
'q' => $q,
|
||||
]);
|
||||
|
||||
return Str::finish(config('location.location_iq_url'), '/').'search.php?'.$query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Actually make the call to the reverse geocoding API.
|
||||
*
|
||||
* @param Place $place
|
||||
* @return Place|null
|
||||
*/
|
||||
private function query(Place $place): ?Place
|
||||
{
|
||||
if (($query = $this->buildQuery($place)) === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$response = Http::get($query);
|
||||
$response->throw();
|
||||
|
||||
$place->latitude = $response->json('0.lat');
|
||||
$place->longitude = $response->json('0.lon');
|
||||
$place->save();
|
||||
|
||||
return $place;
|
||||
} catch (RequestException $e) {
|
||||
if ($e->response->status() === 429 && ($error = $e->response->json('error')) && $error === 'Rate Limited Second') {
|
||||
throw new RateLimitedSecondException($e);
|
||||
} elseif ($e->response->status() !== 404 && $e->response->status() !== 400) {
|
||||
Log::error(__CLASS__.' '.__FUNCTION__.': Error making the call: '.$e->getMessage(), [
|
||||
'query' => Str::of($query)->replace(config('monica.location_iq_api_key'), '******'),
|
||||
$e,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
45
app/Services/Instance/IdHasher.php
Normal file
45
app/Services/Instance/IdHasher.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Instance;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Vinkla\Hashids\Facades\Hashids;
|
||||
use App\Exceptions\WrongIdException;
|
||||
|
||||
class IdHasher
|
||||
{
|
||||
/**
|
||||
* Prefix for ids.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $prefix;
|
||||
|
||||
/**
|
||||
* Create a new IdHasher.
|
||||
*
|
||||
* @param string|null $prefix
|
||||
*/
|
||||
public function __construct($prefix = null)
|
||||
{
|
||||
$this->prefix = $prefix ?? config('hashids.default_prefix');
|
||||
}
|
||||
|
||||
public function encodeId($id)
|
||||
{
|
||||
return $this->prefix.Hashids::encode($id);
|
||||
}
|
||||
|
||||
public function decodeId($hash)
|
||||
{
|
||||
if (Str::startsWith($hash, $this->prefix)) {
|
||||
$result = Hashids::decode(Str::after($hash, $this->prefix));
|
||||
|
||||
if (count($result) > 0) {
|
||||
return $result[0]; // result is always an array due to quirk in Hashids libary
|
||||
}
|
||||
}
|
||||
|
||||
throw new WrongIdException();
|
||||
}
|
||||
}
|
||||
75
app/Services/Instance/TokenClean.php
Normal file
75
app/Services/Instance/TokenClean.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Instance;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use App\Models\User\SyncToken;
|
||||
use App\Events\TokenDeleteEvent;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class TokenClean
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the service.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'dryrun' => 'boolean',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @var Carbon
|
||||
*/
|
||||
private $timefix;
|
||||
|
||||
/**
|
||||
* Clean token list.
|
||||
*
|
||||
* @param array $data
|
||||
*/
|
||||
public function execute(array $data)
|
||||
{
|
||||
$this->timefix = now()->addDays(-7);
|
||||
|
||||
DB::table('synctoken')
|
||||
->orderBy('user_id')
|
||||
->groupBy('user_id', 'name')
|
||||
->select(DB::raw('user_id, name, max(timestamp) as timestamp'))
|
||||
->chunk(200, function ($tokens) use ($data) {
|
||||
foreach ($tokens as $token) {
|
||||
$this->handleUserToken($data, $token->user_id, $token->name, $token->timestamp);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle tokens for a user.
|
||||
*
|
||||
* @param array $data
|
||||
* @param int $userId
|
||||
* @param string $tokenName
|
||||
* @param string $timestamp
|
||||
*/
|
||||
private function handleUserToken(array $data, int $userId, string $tokenName, string $timestamp)
|
||||
{
|
||||
$tokens = SyncToken::where([
|
||||
['user_id', $userId],
|
||||
['name', $tokenName],
|
||||
['timestamp', '<', Carbon::parse($timestamp)],
|
||||
['timestamp', '<', $this->timefix],
|
||||
])
|
||||
->orderByDesc('timestamp')
|
||||
->get();
|
||||
|
||||
foreach ($tokens as $token) {
|
||||
event(new TokenDeleteEvent($token));
|
||||
if (! $data['dryrun']) {
|
||||
$token->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
120
app/Services/Instance/Weather/GetWeatherInformation.php
Normal file
120
app/Services/Instance/Weather/GetWeatherInformation.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Instance\Weather;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use App\Models\Account\Place;
|
||||
use App\Services\BaseService;
|
||||
use App\Models\Account\Weather;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use App\Exceptions\NoCoordinatesException;
|
||||
use App\Exceptions\MissingEnvVariableException;
|
||||
use Illuminate\Http\Client\HttpClientException;
|
||||
|
||||
class GetWeatherInformation extends BaseService
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the service.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'account_id' => 'required|integer|exists:accounts,id',
|
||||
'place_id' => 'required|integer|exists:places,id',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the weather information.
|
||||
*
|
||||
* @param array $data
|
||||
* @return Weather|null
|
||||
*
|
||||
* @throws \Illuminate\Validation\ValidationException if the array that is given in parameter is not valid
|
||||
* @throws \App\Exceptions\MissingEnvVariableException if the weather services are not enabled
|
||||
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException if the Place object is not found
|
||||
*/
|
||||
public function execute(array $data): ?Weather
|
||||
{
|
||||
$this->validateWeatherEnvVariables();
|
||||
|
||||
$this->validate($data);
|
||||
|
||||
$place = Place::where('account_id', $data['account_id'])
|
||||
->findOrFail($data['place_id']);
|
||||
|
||||
if (is_null($place->latitude)) {
|
||||
throw new NoCoordinatesException();
|
||||
}
|
||||
|
||||
return $this->query($place, 'en');
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure that weather env variables are set.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function validateWeatherEnvVariables()
|
||||
{
|
||||
if (! config('monica.enable_weather') || is_null(config('monica.weatherapi_key'))) {
|
||||
throw new MissingEnvVariableException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Actually make the call to Darksky.
|
||||
*
|
||||
* @param Place $place
|
||||
* @return Weather|null
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function query(Place $place, ?string $lang = null): ?Weather
|
||||
{
|
||||
$query = $this->buildQuery($place, $lang);
|
||||
|
||||
try {
|
||||
$response = Http::get($query);
|
||||
$response->throw();
|
||||
|
||||
return Weather::create([
|
||||
'account_id' => $place->account_id,
|
||||
'place_id' => $place->id,
|
||||
'weather_json' => $response->object(),
|
||||
]);
|
||||
} catch (HttpClientException $e) {
|
||||
Log::error(__CLASS__.' '.__FUNCTION__.': Error making the call: '.$e->getMessage(), [
|
||||
'query' => Str::of($query)->replace(config('monica.weatherapi_key'), '******'),
|
||||
$e,
|
||||
]);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the query that will be send to Darksky.
|
||||
*
|
||||
* @param Place $place
|
||||
* @return string
|
||||
*/
|
||||
private function buildQuery(Place $place, ?string $lang = null)
|
||||
{
|
||||
$coords = $place->latitude.','.$place->longitude;
|
||||
|
||||
$query = [
|
||||
'key' => config('monica.weatherapi_key'),
|
||||
'q' => $coords,
|
||||
'lang' => $lang ?? 'en',
|
||||
];
|
||||
if ($lang !== null && $lang !== 'en') {
|
||||
$query['lang'] = $lang;
|
||||
}
|
||||
|
||||
return Str::of(config('location.weatherapi_url'))->rtrim('/').'?'.http_build_query($query);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user