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:
58
app/Services/User/AcceptPolicy.php
Normal file
58
app/Services/User/AcceptPolicy.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\User;
|
||||
|
||||
use App\Models\User\User;
|
||||
use App\Models\Settings\Term;
|
||||
use App\Services\BaseService;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
|
||||
class AcceptPolicy extends BaseService
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the service.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'account_id' => 'required|integer|exists:accounts,id',
|
||||
'user_id' => 'required|integer|exists:users,id',
|
||||
'ip_address' => 'nullable|string|max:255',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Accept the latest user policy.
|
||||
*
|
||||
* @param array $data
|
||||
* @return Term
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function execute(array $data): Term
|
||||
{
|
||||
$this->validate($data);
|
||||
|
||||
try {
|
||||
$user = User::where('account_id', $data['account_id'])
|
||||
->findOrFail($data['user_id']);
|
||||
} catch (ModelNotFoundException $e) {
|
||||
throw new ModelNotFoundException(trans('app.error_user_account'));
|
||||
}
|
||||
|
||||
$latestTerm = Term::latest()->first();
|
||||
|
||||
if (! $latestTerm) {
|
||||
throw new \Exception(trans('app.error_no_term'));
|
||||
}
|
||||
|
||||
$user->terms()->syncWithoutDetaching([$latestTerm->id => [
|
||||
'account_id' => $user->account_id,
|
||||
'ip_address' => $this->nullOrValue($data, 'ip_address'),
|
||||
]]);
|
||||
|
||||
return $latestTerm;
|
||||
}
|
||||
}
|
||||
154
app/Services/User/CreateUser.php
Normal file
154
app/Services/User/CreateUser.php
Normal file
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\User;
|
||||
|
||||
use App\Models\User\User;
|
||||
use App\Services\BaseService;
|
||||
use App\Helpers\RequestHelper;
|
||||
use App\Helpers\CountriesHelper;
|
||||
use App\Models\Settings\Currency;
|
||||
use Illuminate\Support\Facades\App;
|
||||
|
||||
class CreateUser extends BaseService
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the service.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'account_id' => 'required|integer|exists:accounts,id',
|
||||
'first_name' => 'required|max:255',
|
||||
'last_name' => 'required|max:255',
|
||||
'email' => 'required|email|max:255|unique:users',
|
||||
'password' => 'required|min:6',
|
||||
'locale' => 'nullable',
|
||||
'ip_address' => 'nullable',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a user.
|
||||
*
|
||||
* @param array $data
|
||||
* @return User
|
||||
*/
|
||||
public function execute(array $data): User
|
||||
{
|
||||
$this->validate($data);
|
||||
|
||||
$ipAddress = $data['ip_address'] ?? RequestHelper::ip();
|
||||
|
||||
$user = $this->createUser($data);
|
||||
$user = $this->setRegionalParameters($user, $ipAddress);
|
||||
$user->save();
|
||||
|
||||
app(AcceptPolicy::class)->execute([
|
||||
'account_id' => $user->account_id,
|
||||
'user_id' => $user->id,
|
||||
'ip_address' => $ipAddress,
|
||||
]);
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a user.
|
||||
*
|
||||
* @param array $data
|
||||
* @return User
|
||||
*/
|
||||
private function createUser($data): User
|
||||
{
|
||||
// create the user
|
||||
$user = new User();
|
||||
$user->account_id = $data['account_id'];
|
||||
$user->first_name = $data['first_name'];
|
||||
$user->last_name = $data['last_name'];
|
||||
$user->email = $data['email'];
|
||||
$user->password = bcrypt($data['password']);
|
||||
$user->locale = $data['locale'] ?? App::getLocale();
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the regional default parameters.
|
||||
*
|
||||
* @param User $user
|
||||
* @param string|null $ipAddress
|
||||
* @return User
|
||||
*/
|
||||
private function setRegionalParameters($user, $ipAddress): User
|
||||
{
|
||||
$infos = RequestHelper::infos($ipAddress);
|
||||
|
||||
// Associate timezone and currency
|
||||
$currencyCode = $infos['currency'];
|
||||
$timezone = $infos['timezone'];
|
||||
if ($infos['country']) {
|
||||
$country = CountriesHelper::getCountry($infos['country']);
|
||||
} else {
|
||||
$country = CountriesHelper::getCountryFromLocale($user->locale);
|
||||
}
|
||||
|
||||
// Timezone
|
||||
if (! is_null($timezone)) {
|
||||
$user->timezone = $timezone;
|
||||
} elseif (! is_null($country)) {
|
||||
$user->timezone = CountriesHelper::getDefaultTimezone($country);
|
||||
} else {
|
||||
$user->timezone = config('app.timezone');
|
||||
}
|
||||
|
||||
// Currency
|
||||
if ((! is_null($currencyCode)
|
||||
&& ! $this->associateCurrency($user, $currencyCode))
|
||||
|| ! is_null($country)) {
|
||||
foreach ($country->getCurrencies() as $currency) {
|
||||
if ($this->associateCurrency($user, $currency['iso_4217_code'])) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Temperature scale
|
||||
if (! is_null($country)) {
|
||||
switch ($country->getIsoAlpha2()) {
|
||||
case 'US':
|
||||
case 'BZ':
|
||||
case 'KY':
|
||||
$user->temperature_scale = 'fahrenheit';
|
||||
break;
|
||||
default:
|
||||
$user->temperature_scale = 'celsius';
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
$user->temperature_scale = 'celsius';
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Associate currency with the User.
|
||||
*
|
||||
* @param User $user
|
||||
* @param string $currency
|
||||
* @return bool
|
||||
*/
|
||||
private function associateCurrency($user, $currency): bool
|
||||
{
|
||||
$currencyObj = Currency::where('iso', $currency)->first();
|
||||
if (! is_null($currencyObj)) {
|
||||
$user->currency()->associate($currencyObj);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
57
app/Services/User/EmailChange.php
Normal file
57
app/Services/User/EmailChange.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\User;
|
||||
|
||||
use App\Models\User\User;
|
||||
use App\Services\BaseService;
|
||||
use App\Models\Account\Account;
|
||||
|
||||
class EmailChange extends BaseService
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the service.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'account_id' => 'required|integer|exists:accounts,id',
|
||||
'email' => 'required|email|unique:users',
|
||||
'user_id' => 'required|integer',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update email of the user.
|
||||
*
|
||||
* @param array $data
|
||||
* @return User
|
||||
*/
|
||||
public function execute(array $data): User
|
||||
{
|
||||
$this->validate($data);
|
||||
|
||||
/** @var User */
|
||||
$user = User::where('account_id', $data['account_id'])
|
||||
->findOrFail($data['user_id']);
|
||||
|
||||
// Change email of the user
|
||||
$user->email = $data['email'];
|
||||
|
||||
/** @var int $count */
|
||||
$count = Account::count();
|
||||
if (config('monica.signup_double_optin') && $count > 1) {
|
||||
// Resend validation token
|
||||
$user->email_verified_at = null;
|
||||
$user->save();
|
||||
|
||||
$user->sendEmailVerificationNotification();
|
||||
} else {
|
||||
$user->save();
|
||||
$user->markEmailAsVerified();
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
48
app/Services/User/UpdateViewPreference.php
Normal file
48
app/Services/User/UpdateViewPreference.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\User;
|
||||
|
||||
use App\Models\User\User;
|
||||
use App\Services\BaseService;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
|
||||
class UpdateViewPreference extends BaseService
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the service.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'account_id' => 'required|integer|exists:accounts,id',
|
||||
'user_id' => 'required|integer|exists:users,id',
|
||||
'preference' => 'required|string|max:255',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the contact view preference.
|
||||
*
|
||||
* @param array $data
|
||||
* @return User
|
||||
*/
|
||||
public function execute(array $data): User
|
||||
{
|
||||
$this->validate($data);
|
||||
|
||||
try {
|
||||
/** @var User */
|
||||
$user = User::where('account_id', $data['account_id'])
|
||||
->findOrFail($data['user_id']);
|
||||
} catch (ModelNotFoundException $e) {
|
||||
throw new ModelNotFoundException(trans('app.error_user_account'));
|
||||
}
|
||||
|
||||
$user->contacts_sort_order = $data['preference'];
|
||||
$user->save();
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user