Skip to content

Commit

Permalink
feat: make archived contact readonly (#5285)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Jun 22, 2021
1 parent 99bd8e1 commit a3fdac9
Show file tree
Hide file tree
Showing 70 changed files with 376 additions and 43 deletions.
2 changes: 2 additions & 0 deletions app/Http/Controllers/Contacts/AvatarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class AvatarController extends Controller
*/
public function edit(Contact $contact)
{
$contact->throwInactive();

return view('people.avatar.edit')
->withContact($contact);
}
Expand Down
8 changes: 2 additions & 6 deletions app/Http/Controllers/Contacts/CallsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,8 @@ public function destroy(Request $request, Contact $contact, Call $call): ?JsonRe
'call_id' => $call->id,
];

try {
if (app(DestroyCall::class)->execute($data)) {
return $this->respondObjectDeleted($call->id);
}
} catch (\Exception $e) {
return $this->respondNotFound();
if (app(DestroyCall::class)->execute($data)) {
return $this->respondObjectDeleted($call->id);
}

return null;
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/Contacts/ConversationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public function store(Request $request, Contact $contact)
*/
public function edit(Request $request, Contact $contact, Conversation $conversation)
{
$contact->throwInactive();

// preparing the messages for the Vue component
$messages = collect([]);
foreach ($conversation->messages as $message) {
Expand Down
8 changes: 8 additions & 0 deletions app/Http/Controllers/Contacts/DebtController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public function create(Contact $contact)
*/
public function store(DebtRequest $request, Contact $contact)
{
$contact->throwInactive();

$contact->debts()->create(
$request->only([
'in_debt',
Expand Down Expand Up @@ -87,6 +89,8 @@ public function show(Contact $contact, Debt $debt): void
*/
public function edit(Contact $contact, Debt $debt)
{
$contact->throwInactive();

return view('people.debt.edit')
->withContact($contact)
->withAccountHasLimitations(AccountHelper::hasLimitations(auth()->user()->account))
Expand All @@ -104,6 +108,8 @@ public function edit(Contact $contact, Debt $debt)
*/
public function update(DebtRequest $request, Contact $contact, Debt $debt)
{
$contact->throwInactive();

$debt->update(
$request->only([
'in_debt',
Expand All @@ -130,6 +136,8 @@ public function update(DebtRequest $request, Contact $contact, Debt $debt)
*/
public function destroy(Contact $contact, Debt $debt)
{
$contact->throwInactive();

$debt->delete();

return redirect()->route('people.show', $contact)
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/Contacts/DocumentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public function index(Request $request, Contact $contact)
*/
public function store(Request $request, Contact $contact): Document
{
$contact->throwInactive();

return app(UploadDocument::class)->execute([
'account_id' => auth()->user()->account_id,
'contact_id' => $contact->id,
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/Contacts/IntroductionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class IntroductionsController extends Controller
*/
public function edit(Contact $contact)
{
$contact->throwInactive();

$contacts = $contact->siblingContacts()
->real()
->active()
Expand All @@ -41,6 +43,8 @@ public function edit(Contact $contact)
*/
public function update(Request $request, Contact $contact)
{
$contact->throwInactive();

$contact = app(UpdateContactIntroduction::class)->execute([
'account_id' => auth()->user()->account_id,
'contact_id' => $contact->id,
Expand Down
6 changes: 6 additions & 0 deletions app/Http/Controllers/Contacts/NotesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public function index(Contact $contact)
*/
public function store(NotesRequest $request, Contact $contact)
{
$contact->throwInactive();

return $contact->notes()->create([
'account_id' => auth()->user()->account_id,
'body' => $request->input('body'),
Expand Down Expand Up @@ -72,6 +74,8 @@ public function toggle(NoteToggleRequest $request, Contact $contact, Note $note)
*/
public function update(NotesRequest $request, Contact $contact, Note $note): Note
{
$contact->throwInactive();

$note->update(
$request->only([
'body',
Expand All @@ -92,6 +96,8 @@ public function update(NotesRequest $request, Contact $contact, Note $note): Not
*/
public function destroy(Contact $contact, Note $note): void
{
$contact->throwInactive();

$note->delete();
}
}
4 changes: 4 additions & 0 deletions app/Http/Controllers/Contacts/PetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public function index(Contact $contact)
*/
public function store(PetsRequest $request, Contact $contact)
{
$contact->throwInactive();

$pet = $contact->pets()->create(
$request->only([
'pet_category_id',
Expand All @@ -83,6 +85,8 @@ public function store(PetsRequest $request, Contact $contact)
*/
public function update(PetsRequest $request, Contact $contact, Pet $pet)
{
$contact->throwInactive();

$pet->update(
$request->only([
'pet_category_id',
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/Contacts/RelationshipsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public function store(Request $request, Contact $contact)
*/
public function edit(Contact $contact, Relationship $relationship)
{
$contact->throwInactive();

$otherContact = $relationship->ofContact;

$now = now();
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/Contacts/TagsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public function get(Request $request, Contact $contact)
*/
public function update(Request $request, Contact $contact): void
{
$contact->throwInactive();

$tags = $request->all();

// detaching all the tags
Expand Down
28 changes: 7 additions & 21 deletions app/Http/Controllers/ContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,7 @@ public function show(Contact $contact)
*/
public function edit(Contact $contact)
{
if (! $contact->is_active) {
return back()->withErrors(trans('people.archived_contact_readonly'));
}
$contact->throwInactive();

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

// process birthday dates
// TODO: remove this part entirely when we redo this whole SpecialDate
Expand Down Expand Up @@ -499,9 +495,7 @@ public function destroy(Request $request, Contact $contact)
*/
public function editWork(Request $request, Contact $contact)
{
if (! $contact->is_active) {
return back()->withErrors(trans('people.archived_contact_readonly'));
}
$contact->throwInactive();

return view('people.work.edit')
->withContact($contact);
Expand All @@ -517,9 +511,7 @@ 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->throwInactive();

$contact = app(UpdateWorkInformation::class)->execute([
'account_id' => auth()->user()->account_id,
Expand All @@ -543,9 +535,7 @@ public function updateWork(Request $request, Contact $contact)
*/
public function editFoodPreferences(Request $request, Contact $contact)
{
if (! $contact->is_active) {
return back()->withErrors(trans('people.archived_contact_readonly'));
}
$contact->throwInactive();

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

Expand All @@ -564,9 +554,7 @@ 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->throwInactive();

$contact = app(UpdateContactFoodPreferences::class)->execute([
'account_id' => auth()->user()->account_id,
Expand Down Expand Up @@ -631,9 +619,7 @@ public function vCard(Contact $contact)
*/
public function stayInTouch(Request $request, Contact $contact)
{
if (! $contact->is_active) {
return back()->withErrors(trans('people.archived_contact_readonly'));
}
$contact->throwInactive();

$frequency = intval($request->input('frequency'));
$state = $request->input('state');
Expand Down
1 change: 1 addition & 0 deletions app/Jobs/Avatars/UpdateAllGravatars.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function handle()
{
$contacts = Contact::where('avatar_source', 'gravatar')
->orWhere('avatar_gravatar_url', '<>', '')
->active()
->get();

foreach ($contacts as $contact) {
Expand Down
10 changes: 10 additions & 0 deletions app/Models/Contact/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use App\Models\Relationship\Relationship;
use Illuminate\Database\Eloquent\Builder;
use App\Models\ModelBindingHasher as Model;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
Expand Down Expand Up @@ -1503,4 +1504,13 @@ public function updateConsulted()

$this->timestamps = $timestamps;
}

public function throwInactive()
{
if (! $this->is_active) {
throw ValidationException::withMessages([
trans('people.archived_contact_readonly'),
]);
}
}
}
2 changes: 2 additions & 0 deletions app/Services/Account/Photo/UploadPhoto.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public function execute(array $data): ?Photo
$contact = Contact::where('account_id', $data['account_id'])
->findOrFail($data['contact_id']);

$contact->throwInactive();

$array = null;
if (Arr::has($data, 'photo')) {
$array = $this->importPhoto($data);
Expand Down
4 changes: 3 additions & 1 deletion app/Services/Contact/Address/CreateAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ public function execute(array $data): Address
{
$this->validate($data);

Contact::where('account_id', $data['account_id'])
$contact = Contact::where('account_id', $data['account_id'])
->findOrFail($data['contact_id']);

$contact->throwInactive();

$place = $this->createPlace($data);

$address = Address::create([
Expand Down
2 changes: 2 additions & 0 deletions app/Services/Contact/Address/DestroyAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public function execute(array $data): bool
$address = Address::where('account_id', $data['account_id'])
->findOrFail($data['address_id']);

$address->contact->throwInactive();

app(DestroyPlace::class)->execute([
'account_id' => $data['account_id'],
'place_id' => $address->place_id,
Expand Down
4 changes: 3 additions & 1 deletion app/Services/Contact/Address/UpdateAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ public function execute(array $data): Address
->where('contact_id', $data['contact_id'])
->findOrFail($data['address_id']);

Contact::where('account_id', $data['account_id'])
$contact = Contact::where('account_id', $data['account_id'])
->findOrFail($data['contact_id']);

$contact->throwInactive();

$this->updatePlace($data, $address);

$address->update([
Expand Down
2 changes: 2 additions & 0 deletions app/Services/Contact/Avatar/UpdateAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public function execute(array $data): Contact
$contact = Contact::where('account_id', $data['account_id'])
->findOrFail($data['contact_id']);

$contact->throwInactive();

if (isset($data['photo_id'])) {
Photo::where('account_id', $data['account_id'])
->findOrFail($data['photo_id']);
Expand Down
2 changes: 2 additions & 0 deletions app/Services/Contact/Call/CreateCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public function execute(array $data): Call
$contact = Contact::where('account_id', $data['account_id'])
->findOrFail($data['contact_id']);

$contact->throwInactive();

// emotions array is left out as they are not attached during this call
$call = Call::create(Arr::except($data, ['emotions']));

Expand Down
2 changes: 2 additions & 0 deletions app/Services/Contact/Call/DestroyCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public function execute(array $data): bool

$contact = $call->contact;

$contact->throwInactive();

// delete all associations with emotions
$call->emotions()->sync([]);

Expand Down
2 changes: 2 additions & 0 deletions app/Services/Contact/Call/UpdateCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public function execute(array $data): Call
$call = Call::where('account_id', $data['account_id'])
->findOrFail($data['call_id']);

$call->contact->throwInactive();

$call->update([
'called_at' => $data['called_at'],
'content' => (empty($data['content']) ? null : $data['content']),
Expand Down
2 changes: 2 additions & 0 deletions app/Services/Contact/Contact/DestroyContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public function execute(array $data): bool
$contact = Contact::where('account_id', $data['account_id'])
->findOrFail($data['contact_id']);

$contact->throwInactive();

$this->destroyRelationships($data, $contact);

$contact->deleteAvatars();
Expand Down
2 changes: 2 additions & 0 deletions app/Services/Contact/Contact/UpdateBirthdayInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public function execute(array $data)
$contact = Contact::where('account_id', $data['account_id'])
->findOrFail($data['contact_id']);

$contact->throwInactive();

$this->clearRelatedReminder($contact);

$this->clearRelatedSpecialDate($contact);
Expand Down
2 changes: 2 additions & 0 deletions app/Services/Contact/Contact/UpdateContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public function execute(array $data): Contact
$this->contact = Contact::where('account_id', $data['account_id'])
->findOrFail($data['contact_id']);

$this->contact->throwInactive();

// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function execute(array $data): Contact
$contact = Contact::where('account_id', $data['account_id'])
->findOrFail($data['contact_id']);

$contact->throwInactive();
if ($contact->is_partial) {
throw ValidationException::withMessages([
'contact_id' => 'The contact can\'t be a partial contact',
Expand Down
2 changes: 2 additions & 0 deletions app/Services/Contact/Contact/UpdateContactIntroduction.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public function execute(array $data): Contact
$contact = Contact::where('account_id', $data['account_id'])
->findOrFail($data['contact_id']);

$contact->throwInactive();

if ($contact->is_partial) {
throw ValidationException::withMessages([
'contact_id' => 'The contact can\'t be a partial contact',
Expand Down
2 changes: 2 additions & 0 deletions app/Services/Contact/Contact/UpdateDeceasedInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function execute(array $data)
$contact = Contact::where('account_id', $data['account_id'])
->findOrFail($data['contact_id']);

$contact->throwInactive();

$this->clearRelatedReminder($contact);

$this->clearRelatedSpecialDate($contact);
Expand Down
Loading

0 comments on commit a3fdac9

Please sign in to comment.