Skip to content

Commit

Permalink
fix: fix unarchive on limited account (#5256)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin committed Jun 17, 2021
1 parent aa4e3df commit 8357d0f
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 9 deletions.
52 changes: 43 additions & 9 deletions app/Http/Controllers/ContactsController.php
Expand Up @@ -19,7 +19,6 @@
use App\Jobs\UpdateLastConsultedDate;
use Illuminate\Http\RedirectResponse;
use Illuminate\Contracts\View\Factory;
use App\Models\Relationship\Relationship;
use Barryvdh\Debugbar\Facade as Debugbar;
use App\Services\User\UpdateViewPreference;
use Illuminate\Validation\ValidationException;
Expand Down Expand Up @@ -176,14 +175,14 @@ public function missing(Request $request)
*/
private function createForm(Request $request, bool $isContactMissing = false)
{
if (AccountHelper::hasReachedContactLimit(auth()->user()->account)
&& AccountHelper::hasLimitations(auth()->user()->account)
$accountHasLimitations = AccountHelper::hasLimitations(auth()->user()->account);

if ($accountHasLimitations
&& AccountHelper::hasReachedContactLimit(auth()->user()->account)
&& ! auth()->user()->account->legacy_free_plan_unlimited_contacts) {
return redirect()->route('settings.subscriptions.index');
}

$accountHasLimitations = AccountHelper::hasLimitations(auth()->user()->account);

return view('people.create')
->withAccountHasLimitations($accountHasLimitations)
->withIsContactMissing($isContactMissing)
Expand Down Expand Up @@ -341,10 +340,14 @@ public function show(Contact $contact)
*
* @param Contact $contact
*
* @return View
* @return View|RedirectResponse
*/
public function edit(Contact $contact)
{
if (! $contact->is_active) {
return back()->withErrors(trans('people.archived_contact_readonly'));
}

$now = now();
$age = (string) (! is_null($contact->birthdate) ? $contact->birthdate->getAge() : 0);
$birthdate = ! is_null($contact->birthdate) ? $contact->birthdate->date->toDateString() : $now->toDateString();
Expand Down Expand Up @@ -384,6 +387,10 @@ public function edit(Contact $contact)
*/
public function update(Request $request, Contact $contact)
{
if (! $contact->is_active) {
return back()->withErrors(trans('people.archived_contact_readonly'));
}

// process birthday dates
// TODO: remove this part entirely when we redo this whole SpecialDate
// thing
Expand Down Expand Up @@ -494,10 +501,14 @@ public function destroy(Request $request, Contact $contact)
* @param Request $request
* @param Contact $contact
*
* @return View
* @return View|RedirectResponse
*/
public function editWork(Request $request, Contact $contact)
{
if (! $contact->is_active) {
return back()->withErrors(trans('people.archived_contact_readonly'));
}

return view('people.work.edit')
->withContact($contact);
}
Expand All @@ -512,6 +523,10 @@ public function editWork(Request $request, Contact $contact)
*/
public function updateWork(Request $request, Contact $contact)
{
if (! $contact->is_active) {
return back()->withErrors(trans('people.archived_contact_readonly'));
}

$contact = app(UpdateWorkInformation::class)->execute([
'account_id' => auth()->user()->account_id,
'author_id' => auth()->user()->id,
Expand All @@ -530,10 +545,14 @@ public function updateWork(Request $request, Contact $contact)
* @param Request $request
* @param Contact $contact
*
* @return View
* @return View|RedirectResponse
*/
public function editFoodPreferences(Request $request, Contact $contact)
{
if (! $contact->is_active) {
return back()->withErrors(trans('people.archived_contact_readonly'));
}

$accountHasLimitations = AccountHelper::hasLimitations(auth()->user()->account);

return view('people.food-preferences.edit')
Expand All @@ -551,6 +570,10 @@ public function editFoodPreferences(Request $request, Contact $contact)
*/
public function updateFoodPreferences(Request $request, Contact $contact)
{
if (! $contact->is_active) {
return back()->withErrors(trans('people.archived_contact_readonly'));
}

$contact = app(UpdateContactFoodPreferences::class)->execute([
'account_id' => auth()->user()->account_id,
'contact_id' => $contact->id,
Expand Down Expand Up @@ -610,10 +633,14 @@ public function vCard(Contact $contact)
*
* @param Request $request
* @param Contact $contact
* @return int
* @return int|RedirectResponse
*/
public function stayInTouch(Request $request, Contact $contact)
{
if (! $contact->is_active) {
return back()->withErrors(trans('people.archived_contact_readonly'));
}

$frequency = intval($request->input('frequency'));
$state = $request->input('state');

Expand Down Expand Up @@ -663,6 +690,13 @@ public function favorite(Request $request, Contact $contact)
*/
public function archive(Request $request, Contact $contact)
{
if (! $contact->is_active
&& AccountHelper::hasReachedContactLimit(auth()->user()->account)
&& AccountHelper::hasLimitations(auth()->user()->account)
&& ! auth()->user()->account->legacy_free_plan_unlimited_contacts) {
abort(402);
}

$contact->is_active = ! $contact->is_active;
$contact->save();

Expand Down
12 changes: 12 additions & 0 deletions app/Services/Contact/Contact/UpdateContact.php
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Support\Arr;
use App\Services\BaseService;
use App\Helpers\AccountHelper;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Jobs\Avatars\GenerateDefaultAvatar;
use App\Services\Contact\Description\SetPersonalDescription;
Expand Down Expand Up @@ -63,6 +65,16 @@ public function execute(array $data): Contact
$this->contact = Contact::where('account_id', $data['account_id'])
->findOrFail($data['contact_id']);

// Test is the account is limited and the contact should be updated as real contact
$account = Account::find($data['account_id']);
if ($this->contact->is_partial
&& ! $this->valueOrFalse($this->data, 'is_partial')
&& AccountHelper::hasReachedContactLimit($account)
&& AccountHelper::hasLimitations($account)
&& ! $account->legacy_free_plan_unlimited_contacts) {
abort(402);
}

$this->updateGeneralInformation();
$this->updateDescription();
$this->updateBirthDayInformation();
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en/people.php
Expand Up @@ -42,6 +42,7 @@
'people_list_account_upgrade_cta' => 'Upgrade now',
'people_list_untagged' => 'View untagged contacts',
'people_list_filter_untag' => 'Showing all untagged contacts',
'archived_contact_readonly' => 'Archived contact can’t be edited, please unarchive it first.',

// people add
'people_add_title' => 'Add a new person',
Expand Down
36 changes: 36 additions & 0 deletions tests/Unit/Controllers/Contact/ContactsControllerTest.php
@@ -0,0 +1,36 @@
<?php

namespace Tests\Unit\Controllers\Contact;

use Tests\FeatureTestCase;
use App\Helpers\AccountHelper;
use App\Models\Contact\Contact;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ContactsControllerTest extends FeatureTestCase
{
use DatabaseTransactions;

/** @test */
public function it_cant_unarchive_contact_if_limited_account()
{
config(['monica.requires_subscription' => true]);
$user = $this->signin();

$contact = factory(Contact::class)->create([
'account_id' => $user->account_id,
'is_active' => false,
]);

factory(Contact::class, 10)->create([
'account_id' => $user->account_id,
]);

$this->assertTrue(AccountHelper::hasReachedContactLimit($user->account));
$this->assertTrue(AccountHelper::hasLimitations($user->account));

$response = $this->put("/people/{$contact->hashID()}/archive");

$response->assertStatus(402);
}
}
4 changes: 4 additions & 0 deletions tests/Unit/Services/BaseServiceTest.php
Expand Up @@ -113,5 +113,9 @@ public function it_returns_the_default_value_or_the_given_value(): void
$this->assertFalse(
$stub->valueOrFalse($array, 'value')
);

$this->assertFalse(
$stub->valueOrFalse([], 'value')
);
}
}

0 comments on commit 8357d0f

Please sign in to comment.