diff --git a/database/factories/ContactFactory.php b/database/factories/ContactFactory.php index c9f610c..a79b1cd 100644 --- a/database/factories/ContactFactory.php +++ b/database/factories/ContactFactory.php @@ -15,14 +15,14 @@ class ContactFactory extends Factory public function definition() { return [ - 'form_type' => $this->faker->randomElement(FormType::getValues()), - 'location_id' => randomOrCreate(app('location')), - 'email' => $this->faker->email, - 'first_name' => $this->faker->firstName, - 'last_name' => $this->faker->lastName, - 'phone' => '555-555-5555', - 'company_name' => $this->faker->company, - 'message' => $this->faker->paragraph, + 'form_type' => $this->faker->randomElement(FormType::getValues()), + 'location_id' => randomOrCreate(app('location')), + 'email_address_id' => randomOrCreate(app('email_address')), + 'first_name' => $this->faker->firstName, + 'last_name' => $this->faker->lastName, + 'phone' => '555-555-5555', + 'company_name' => $this->faker->company, + 'message' => $this->faker->paragraph, ]; } } diff --git a/database/migrations/2020_05_20_100000_create_contacts_table.php b/database/migrations/2020_05_20_100000_create_contacts_table.php index de4744a..e85e996 100644 --- a/database/migrations/2020_05_20_100000_create_contacts_table.php +++ b/database/migrations/2020_05_20_100000_create_contacts_table.php @@ -15,7 +15,7 @@ public function up() $table->string('form_type')->index(); $table->string('reference_number')->index()->unique(); // Generated by system. This is identifier used to communicate with customer about their contact form. Reference number is emailed to them. $table->foreignIdFor(app('location'))->index(); - $table->string('email'); + $table->foreignIdFor(app('email_address'))->index(); $table->foreignIdFor(app('user'))->nullable()->index(); $table->string('first_name')->nullable(); $table->string('last_name')->nullable(); diff --git a/src/Commands/AttachContactUsers.php b/src/Commands/AttachContactUsers.php deleted file mode 100644 index b2dad8f..0000000 --- a/src/Commands/AttachContactUsers.php +++ /dev/null @@ -1,52 +0,0 @@ -get(); - foreach ($contacts as $contact) { - $user = app('user')->where('email', '=', $contact->email)->first(); - if ($user) { - $contact->user_id = $user->id; - $contact->save(); - } - } - } -} diff --git a/src/FormsServiceProvider.php b/src/FormsServiceProvider.php index eb04cd7..24e715f 100644 --- a/src/FormsServiceProvider.php +++ b/src/FormsServiceProvider.php @@ -4,7 +4,6 @@ namespace Tipoff\Forms; -use Tipoff\Forms\Commands\AttachContactUsers; use Tipoff\Forms\Models\Contact; use Tipoff\Forms\Models\ContactResponse; use Tipoff\Forms\Policies\ContactPolicy; @@ -25,9 +24,6 @@ public function configureTipoffPackage(TipoffPackage $package): void \Tipoff\Forms\Nova\Contact::class, \Tipoff\Forms\Nova\ContactResponse::class, ]) - ->hasCommands([ - AttachContactUsers::class, - ]) ->name('forms') ->hasConfigFile(); } diff --git a/src/Models/Contact.php b/src/Models/Contact.php index a5f8c3b..733e2d0 100644 --- a/src/Models/Contact.php +++ b/src/Models/Contact.php @@ -77,6 +77,11 @@ public function getContactStatusHistory(): Collection return $this->getStatusHistory(ContactStatus::statusType()); } + public function email() + { + return $this->hasOne(app('email_address')); + } + public function response() { return $this->hasOne(ContactResponse::class); diff --git a/src/Nova/Contact.php b/src/Nova/Contact.php index 2de1e96..3b152cd 100644 --- a/src/Nova/Contact.php +++ b/src/Nova/Contact.php @@ -6,7 +6,6 @@ use Dniccum\PhoneNumber\PhoneNumber; use Illuminate\Http\Request; -use Inspheric\Fields\Email; use Laravel\Nova\Fields\BelongsTo; use Laravel\Nova\Fields\Date; use Laravel\Nova\Fields\DateTime; @@ -71,7 +70,7 @@ public function fields(Request $request) nova('location') ? BelongsTo::make('Location', 'location', nova('location'))->required()->hideWhenUpdating() : null, nova('user') ? BelongsTo::make('User', 'user', nova('user'))->required()->hideWhenUpdating() : null, PhoneNumber::make('Phone')->format('###-###-####')->disableValidation()->useMaskPlaceholder()->linkOnDetail()->hideWhenUpdating(), - Email::make('Email', 'user.email')->clickable()->hideWhenUpdating(), + nova('email_address') ? BelongsTo::make('Email Address', 'email_address', nova('email_address'))->sortable() : null, Textarea::make('Message')->rows(3)->alwaysShow()->nullable()->hideWhenUpdating(), new Panel('Submission Details', $this->submissionFields()), diff --git a/tests/Unit/Commands/AttachContactUsersCommandTest.php b/tests/Unit/Commands/AttachContactUsersCommandTest.php deleted file mode 100644 index d666f53..0000000 --- a/tests/Unit/Commands/AttachContactUsersCommandTest.php +++ /dev/null @@ -1,35 +0,0 @@ -artisan('attach:users'); - $this->assertDatabaseCount('contacts', 0); - } - - /** @test */ - public function user_found() - { - //Create a user and contact with same email, but not attached - $user = randomOrCreate(app('user')); - Contact::factory()->create([ - 'email' => $user->email, - ]); - - $this->artisan('attach:users'); - $contact = Contact::where('email', '=', $user->email)->first(); - $this->assertNotNull($contact->user); - } -}