Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor account model #3570

Merged
merged 34 commits into from Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
4618ec3
move the canDowngrade method to a new helper (Account Helper)
djaiss Feb 8, 2020
4de3309
remove the timezone() method that was not used anywhere
djaiss Feb 8, 2020
b598db1
move the getStorageSize() method to a new StorageHelper helper
djaiss Feb 8, 2020
7a360a1
move hasReachedAccountStorageLimit() method to the StorageHelper helper
djaiss Feb 8, 2020
0b03f3e
move yearly statistics methods to AccountHelper helper
djaiss Feb 8, 2020
df60b88
Apply fixes from StyleCI
djaiss Feb 8, 2020
ba63090
move the hasLimitations() method to AccountHelper
djaiss Feb 8, 2020
fc9a6e3
Apply fixes from StyleCI
djaiss Feb 8, 2020
1f69d39
move accountHasLimitations method to AccountHelper
djaiss Feb 8, 2020
00ac276
Apply fixes from StyleCI
djaiss Feb 8, 2020
638b62b
move getRemindersForMonth() to AccountHelper
djaiss Feb 8, 2020
1f98d86
Apply fixes from StyleCI
djaiss Feb 8, 2020
a30e05f
move replaceGender to GenderHelper
djaiss Feb 8, 2020
b2571f8
Apply fixes from StyleCI
djaiss Feb 8, 2020
23cf2f7
move defaultGender() method to AccountHelper
djaiss Feb 8, 2020
1ebc91d
Merge branch '2020-02-08-refactor-account' of github.com:monicahq/mon…
djaiss Feb 8, 2020
b74bc4c
Apply fixes from StyleCI
djaiss Feb 8, 2020
ea74655
move hasAny() method to InstanceHelper helper
djaiss Feb 8, 2020
3d3cb30
Apply fixes from StyleCI
djaiss Feb 8, 2020
846255b
fix in code
djaiss Feb 9, 2020
27a5c9d
Apply fixes from StyleCI
djaiss Feb 9, 2020
2e5a6e6
Merge branch 'master' into 2020-02-08-refactor-account
djaiss Feb 11, 2020
09fe912
Apply fixes from StyleCI
djaiss Feb 11, 2020
660973b
Merge branch 'master' into 2020-02-08-refactor-account
djaiss Feb 12, 2020
c423d95
Update resources/views/settings/subscriptions/downgrade-checklist.bla…
djaiss Feb 13, 2020
697044c
Update resources/views/settings/subscriptions/downgrade-checklist.bla…
djaiss Feb 13, 2020
80b74dc
merge master
djaiss Feb 13, 2020
6ed279f
Apply fixes from StyleCI
djaiss Feb 13, 2020
1aa06a4
fix
djaiss Feb 13, 2020
dc7c7d1
Merge branch '2020-02-08-refactor-account' of github.com:monicahq/mon…
djaiss Feb 13, 2020
28b73e8
Apply fixes from StyleCI
djaiss Feb 13, 2020
350caa3
Update RelationshipsController.php
djaiss Feb 13, 2020
76f92ed
Merge branch '2020-02-08-refactor-account' of github.com:monicahq/mon…
djaiss Feb 13, 2020
539abed
Merge branch 'master' into 2020-02-08-refactor-account
djaiss Feb 13, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/Console/Commands/SetupProduction.php
Expand Up @@ -3,6 +3,7 @@
namespace App\Console\Commands;

use function Safe\touch;
use App\Helpers\InstanceHelper;
use App\Models\Account\Account;
use Illuminate\Console\Command;

Expand Down Expand Up @@ -67,7 +68,7 @@ public function handle()
$this->info('| You can now sign in to your account:');
$this->line('| username: '.$email);
$this->line('| password: <hidden>');
} elseif (Account::hasAny()) {
} elseif (InstanceHelper::hasAtLeastOneAccount()) {
$this->info('| You can now log in to your account');
} else {
$this->info('| You can now register to the first account by opening the application:');
Expand Down
202 changes: 202 additions & 0 deletions app/Helpers/AccountHelper.php
@@ -0,0 +1,202 @@
<?php

namespace App\Helpers;

use App\Models\Contact\Gender;
use App\Models\Account\Account;
use Illuminate\Support\Collection;

class AccountHelper
{
/**
* Indicates whether the given account has limitations with her current
* plan.
*
* @return bool
*/
public static function hasLimitations(Account $account): bool
{
if ($account->has_access_to_paid_version_for_free) {
return false;
}

if (! config('monica.requires_subscription')) {
return false;
}

if ($account->isSubscribed()) {
return false;
}

return true;
}

/**
* Indicate whether an account has reached the contact limit if the account
* is on a free trial.
*
* @param Account $account
* @return bool
*/
public static function hasReachedContactLimit(Account $account): bool
{
return $account->contacts()->real()->active()->count() >= config('monica.number_of_allowed_contacts_free_account');
}

/**
* Check if the account can be downgraded, based on a set of rules.
*
* @param Account $account
* @return bool
*/
public static function canDowngrade(Account $account): bool
{
$canDowngrade = true;
$numberOfUsers = $account->users()->count();
$numberPendingInvitations = $account->invitations()->count();
$numberContacts = $account->contacts()->count();

// number of users in the account should be == 1
if ($numberOfUsers > 1) {
$canDowngrade = false;
}

// there should not be any pending user invitations
if ($numberPendingInvitations > 0) {
$canDowngrade = false;
}

// there should not be more than the number of contacts allowed
if ($numberContacts > config('monica.number_of_allowed_contacts_free_account')) {
$canDowngrade = false;
}

return $canDowngrade;
}

/**
* Get the default gender for this account.
*
* @param Account $account
* @return string
*/
public static function getDefaultGender(Account $account): string
{
$defaultGenderType = Gender::MALE;

if ($account->default_gender_id) {
$defaultGender = Gender::where([
'account_id' => $account->id,
])->find($account->default_gender_id);

if ($defaultGender) {
$defaultGenderType = $defaultGender->type;
}
}

return $defaultGenderType;
}

/**
* Get the reminders for the month given in parameter.
* - 0 means current month
* - 1 means month+1
* - 2 means month+2...
*
* @param Account $account
* @param int $month
*/
public static function getUpcomingRemindersForMonth(Account $account, int $month)
{
$startOfMonth = now(DateHelper::getTimezone())->addMonthsNoOverflow($month)->startOfMonth();

// don't get reminders for past events:
if ($startOfMonth->isPast()) {
$startOfMonth = now(DateHelper::getTimezone());
}

$endOfMonth = now(DateHelper::getTimezone())->addMonthsNoOverflow($month)->endOfMonth();

return $account->reminderOutboxes()
->with(['reminder', 'reminder.contact'])
->whereBetween('planned_date', [$startOfMonth, $endOfMonth])
->where('nature', 'reminder')
->orderBy('planned_date', 'asc')
->get();
}

/**
* Get the number of activities grouped by year.
*
* @param Account $account
* @return Collection
*/
public static function getYearlyActivitiesStatistics(Account $account): Collection
{
$activitiesStatistics = collect([]);
$activities = $account->activities()
->select('happened_at')
->latest('happened_at')
->get();
$years = [];

foreach ($activities as $activity) {
$yearStatistic = $activity->happened_at->format('Y');
$foundInYear = false;

foreach ($years as $year => $number) {
if ($year == $yearStatistic) {
$years[$year] = $number + 1;
$foundInYear = true;
}
}

if (! $foundInYear) {
$years[$yearStatistic] = 1;
}
}

foreach ($years as $year => $number) {
$activitiesStatistics->put($year, $number);
}

return $activitiesStatistics;
}

/**
* Get the number of calls grouped by year.
*
* @return Collection
*/
public static function getYearlyCallStatistics(Account $account): Collection
{
$callsStatistics = collect([]);
$calls = $account->calls()
->select('called_at')
->latest('called_at')
->get();
$years = [];

foreach ($calls as $call) {
$yearStatistic = $call->called_at->format('Y');
$foundInYear = false;

foreach ($years as $year => $number) {
if ($year == $yearStatistic) {
$years[$year] = $number + 1;
$foundInYear = true;
}
}

if (! $foundInYear) {
$years[$yearStatistic] = 1;
}
}

foreach ($years as $year => $number) {
$callsStatistics->put($year, $number);
}

return $callsStatistics;
}
}
48 changes: 48 additions & 0 deletions app/Helpers/GenderHelper.php
@@ -0,0 +1,48 @@
<?php

namespace App\Helpers;

use App\Models\Contact\Gender;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use Illuminate\Support\Collection;

class GenderHelper
{
/**
* Return a collection of genders.
*
* @return Collection
*/
public static function getGendersInput()
{
$genders = auth()->user()->account->genders->map(function ($gender) {
return [
'id' => $gender->id,
'name' => $gender->name,
];
});
$genders = CollectionHelper::sortByCollator($genders, 'name');
$genders->prepend(['id' => '', 'name' => trans('app.gender_no_gender')]);

return $genders;
}

/**
* Replaces a specific gender of all the contacts in the account with another
* gender.
*
* @param Account $account
* @param Gender $genderToDelete
* @param Gender $genderToReplaceWith
* @return bool
*/
public static function replace(Account $account, Gender $genderToDelete, Gender $genderToReplaceWith): bool
{
Contact::where('account_id', $account->id)
->where('gender_id', $genderToDelete->id)
->update(['gender_id' => $genderToReplaceWith->id]);

return true;
}
}
25 changes: 0 additions & 25 deletions app/Helpers/GendersHelper.php

This file was deleted.

12 changes: 12 additions & 0 deletions app/Helpers/InstanceHelper.php
Expand Up @@ -4,7 +4,9 @@

use function Safe\json_decode;
use App\Models\Account\Account;
use App\Models\Instance\Instance;
use App\Models\Settings\Currency;
use Illuminate\Support\Facades\DB;
use function Safe\file_get_contents;

class InstanceHelper
Expand Down Expand Up @@ -60,4 +62,14 @@ public static function getChangelogEntries($limit = null)

return $changelogs;
}

/**
* Check if the instance has at least one account.
*
* @return bool
*/
public static function hasAtLeastOneAccount(): bool
{
return DB::table('accounts')->count() > 0;
}
djaiss marked this conversation as resolved.
Show resolved Hide resolved
}
44 changes: 44 additions & 0 deletions app/Helpers/StorageHelper.php
@@ -0,0 +1,44 @@
<?php

namespace App\Helpers;

use App\Models\Account\Account;
use Illuminate\Support\Facades\DB;

class StorageHelper
{
/**
* Get the storage size of the account, in bytes.
*
* @param Account $account
* @return int
*/
public static function getAccountStorageSize(Account $account): int
{
$documentsSize = DB::table('documents')
->where('account_id', $account->id)
->sum('filesize');
$photosSize = DB::table('photos')
->where('account_id', $account->id)
->sum('filesize');

return $documentsSize + $photosSize;
}

/**
* Indicates whether the account has the reached the maximum storage size.
*
* @param Account $account
* @return bool
*/
public static function hasReachedAccountStorageLimit(Account $account): bool
{
if (! config('monica.requires_subscription')) {
return false;
}

$currentAccountSize = self::getAccountStorageSize($account);

return $currentAccountSize > (config('monica.max_storage_size') * 1000000);
}
}
5 changes: 3 additions & 2 deletions app/Http/Controllers/Api/ApiActivitiesController.php
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Helpers\AccountHelper;
use App\Models\Contact\Contact;
use App\Models\Account\Activity;
use Illuminate\Database\QueryException;
Expand Down Expand Up @@ -31,7 +32,7 @@ public function index(Request $request)
}

return ActivityResource::collection($activities)->additional(['meta' => [
'statistics' => auth()->user()->account->getYearlyActivitiesStatistics(),
'statistics' => AccountHelper::getYearlyActivitiesStatistics(auth()->user()->account),
]]);
}

Expand Down Expand Up @@ -158,7 +159,7 @@ public function activities(Request $request, $contactId)
}

return ActivityResource::collection($activities)->additional(['meta' => [
'statistics' => auth()->user()->account->getYearlyActivitiesStatistics(),
'statistics' => AccountHelper::getYearlyActivitiesStatistics(auth()->user()->account),
]]);
}
}