Skip to content

Commit fac5f2c

Browse files
authored
Merge pull request #43 from tipoff/chx2/feature/30-update-migrations
Update migrations #30
2 parents 8bb447a + 05a8a27 commit fac5f2c

13 files changed

+243
-22
lines changed

database/factories/ContactFactory.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
<?php
1+
<?php
22

33
declare(strict_types=1);
44

55
namespace Tipoff\Forms\Database\Factories;
66

77
use Illuminate\Database\Eloquent\Factories\Factory;
8+
use Tipoff\Forms\Enums\FormType;
89
use Tipoff\Forms\Models\Contact;
910

1011
class ContactFactory extends Factory
@@ -14,7 +15,7 @@ class ContactFactory extends Factory
1415
public function definition()
1516
{
1617
return [
17-
'form_type' => $this->faker->randomElement(['contact', 'reservation', 'parties', 'groups', 'employment']),
18+
'form_type' => $this->faker->randomElement(FormType::getValues()),
1819
'location_id' => randomOrCreate(app('location')),
1920
'email' => $this->faker->email,
2021
'first_name' => $this->faker->firstName,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tipoff\Forms\Database\Factories;
6+
7+
use Illuminate\Database\Eloquent\Factories\Factory;
8+
use Tipoff\Forms\Models\Contact;
9+
use Tipoff\Forms\Models\ContactResponse;
10+
11+
class ContactResponseFactory extends Factory
12+
{
13+
protected $model = ContactResponse::class;
14+
15+
public function definition()
16+
{
17+
return [
18+
'contact_id' => Contact::factory()->create()->id,
19+
'emailed_at' => $this->faker->dateTime,
20+
'creator_id' => randomOrCreate(app('user')),
21+
];
22+
}
23+
}

database/migrations/2020_05_20_100000_create_contacts_table.php

+3-12
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function up()
1212
{
1313
Schema::create('contacts', function (Blueprint $table) {
1414
$table->id();
15-
$table->string('form_type')->index(); // String title of the form type so know which one was completed
15+
$table->string('form_type')->index();
1616
$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.
1717
$table->foreignIdFor(app('location'))->index();
1818
$table->string('email');
@@ -22,17 +22,8 @@ public function up()
2222
$table->string('company_name')->nullable();
2323
$table->string('phone')->nullable(); // Will need to format before saving
2424
$table->text('message')->nullable();
25-
$table->json('additional_fields')->nullable();
26-
27-
// Move application specific data to additonal_fields
28-
$table->smallInteger('participants')->nullable();
29-
$table->date('requested_date')->nullable();
30-
$table->time('requested_time')->nullable(); // Stored in location timezone
31-
32-
// Move response fields to different table (contact followup or something like that)
33-
$table->dateTime('emailed_at')->nullable();
34-
$table->dateTime('closed_at')->nullable();
35-
25+
$table->json('fields')->nullable();
26+
3627
$table->softDeletes(); // Soft delete if email bounces or if the contact submission is spam.
3728
$table->timestamps();
3829
});

database/migrations/2021_02_13_100000_add_contact_permissions.php

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ public function up()
1313
'create contacts' => ['Owner', 'Executive', 'Staff'],
1414
'update contacts' => ['Owner', 'Executive', 'Staff'],
1515
'delete contacts' => ['Owner', 'Executive', 'Staff'],
16+
'view contact responses' => ['Owner', 'Executive', 'Staff'],
17+
'create contact responses' => ['Owner', 'Executive', 'Staff'],
18+
'update contact responses' => ['Owner', 'Executive', 'Staff'],
19+
'delete contact responses' => ['Owner', 'Executive', 'Staff'],
1620
];
1721

1822
$this->createPermissions($permissions);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Database\Migrations\Migration;
6+
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Support\Facades\Schema;
8+
use Tipoff\Forms\Models\Contact;
9+
10+
class CreateContactResponsesTable extends Migration
11+
{
12+
public function up()
13+
{
14+
Schema::create('contact_responses', function (Blueprint $table) {
15+
$table->id();
16+
$table->foreignIdFor(Contact::class);
17+
$table->text('message')->nullable(); // Message will be communication to or from contact user
18+
$table->text('comment')->nullable(); // Comment is internal communication. This is more like documentation so it is different than a note attachment from tipoff/notes
19+
$table->dateTime('emailed_at')->nullable();
20+
21+
$table->foreignIdFor(app('user'), 'creator_id');
22+
$table->timestamp('created_at');
23+
});
24+
}
25+
}

src/Enums/FormType.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tipoff\Forms\Enums;
6+
7+
use MabeEnum\Enum;
8+
9+
/**
10+
* @method static FormType CONTACT()
11+
* @method static FormType EMPLOYMENT()
12+
* @method static FormType GROUPS()
13+
* @method static FormType PARTIES()
14+
* @method static FormType RESERVATIONS()
15+
* @psalm-immutable
16+
*/
17+
class FormType extends Enum
18+
{
19+
const CONTACT = 'contact';
20+
const EMPLOYMENT = 'employment';
21+
const GROUPS = 'groups';
22+
const PARTIES = 'parties';
23+
const RESERVATIONS = 'reservations';
24+
}

src/FormsServiceProvider.php

+4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
use Tipoff\Forms\Commands\AttachContactUsers;
88
use Tipoff\Forms\Models\Contact;
9+
use Tipoff\Forms\Models\ContactResponse;
910
use Tipoff\Forms\Policies\ContactPolicy;
11+
use Tipoff\Forms\Policies\ContactResponsePolicy;
1012
use Tipoff\Support\TipoffPackage;
1113
use Tipoff\Support\TipoffServiceProvider;
1214

@@ -17,9 +19,11 @@ public function configureTipoffPackage(TipoffPackage $package): void
1719
$package
1820
->hasPolicies([
1921
Contact::class => ContactPolicy::class,
22+
ContactResponse::class => ContactResponsePolicy::class,
2023
])
2124
->hasNovaResources([
2225
\Tipoff\Forms\Nova\Contact::class,
26+
\Tipoff\Forms\Nova\ContactResponse::class,
2327
])
2428
->hasCommands([
2529
AttachContactUsers::class,

src/Models/Contact.php

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Contact extends BaseModel
2424
protected $casts = [
2525
'emailed_at' => 'datetime',
2626
'requested_date' => 'date',
27+
'fields' => 'array',
2728
];
2829

2930
protected static function boot()
@@ -76,6 +77,11 @@ public function getContactStatusHistory(): Collection
7677
return $this->getStatusHistory(ContactStatus::statusType());
7778
}
7879

80+
public function response()
81+
{
82+
return $this->hasOne(ContactResponse::class);
83+
}
84+
7985
public function user()
8086
{
8187
return $this->belongsTo(app('user'));

src/Models/ContactResponse.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tipoff\Forms\Models;
6+
7+
use Illuminate\Database\Eloquent\SoftDeletes;
8+
use Tipoff\Support\Models\BaseModel;
9+
use Tipoff\Support\Traits\HasPackageFactory;
10+
11+
class ContactResponse extends BaseModel
12+
{
13+
use HasPackageFactory;
14+
use SoftDeletes;
15+
16+
const UPDATED_AT = null;
17+
18+
public function contact()
19+
{
20+
return $this->belongsTo(Contact::class);
21+
}
22+
}

src/Nova/Contact.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Laravel\Nova\Http\Requests\NovaRequest;
1919
use Laravel\Nova\Panel;
2020
use Tipoff\Forms\Enums\ContactStatus;
21+
use Tipoff\Forms\Enums\FormType;
2122
use Tipoff\Support\Nova\BaseResource;
2223
use Tipoff\Support\Nova\Fields\Enum;
2324

@@ -61,11 +62,11 @@ public function fields(Request $request)
6162
return $contact->getContactStatus();
6263
})->attach(ContactStatus::class),
6364
Select::make('Form Type')->options([
64-
'contact' => 'Contact Page',
65-
'reservation' => 'Reservation Page',
66-
'parties' => 'Private Parties Page',
67-
'groups' => 'Team Building Page',
68-
'employment' => 'Employment Page',
65+
FormType::CONTACT => 'Contact',
66+
FormType::RESERVATIONS => 'Reservations',
67+
FormType::PARTIES => 'Parties',
68+
FormType::GROUPS => 'Groups',
69+
FormType::EMPLOYMENT => 'Employment',
6970
])->required()->hideWhenUpdating(),
7071
nova('location') ? BelongsTo::make('Location', 'location', nova('location'))->required()->hideWhenUpdating() : null,
7172
nova('user') ? BelongsTo::make('User', 'user', nova('user'))->required()->hideWhenUpdating() : null,
@@ -83,9 +84,9 @@ protected function submissionFields()
8384
{
8485
return [
8586
Text::make('Number', 'reference_number')->exceptOnForms(),
86-
Number::make('Participants')->nullable()->hideWhenUpdating(),
87-
Date::make('Requested Date')->nullable()->hideWhenUpdating(),
88-
Text::make('Requested Time')->nullable()->hideWhenUpdating(),
87+
Number::make('Participants', 'fields->participants')->nullable()->hideWhenUpdating(),
88+
Date::make('Requested Date', 'fields->requested_date')->nullable()->hideWhenUpdating(),
89+
Text::make('Requested Time', 'fields->requested_time')->nullable()->hideWhenUpdating(),
8990
Text::make('Company Name')->nullable()->hideWhenUpdating(),
9091
];
9192
}

src/Nova/ContactResponse.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tipoff\Forms\Nova;
6+
7+
use Illuminate\Http\Request;
8+
use Laravel\Nova\Fields\DateTime;
9+
use Laravel\Nova\Fields\ID;
10+
use Laravel\Nova\Fields\Textarea;
11+
use Laravel\Nova\Http\Requests\NovaRequest;
12+
use Laravel\Nova\Panel;
13+
use Tipoff\Support\Nova\BaseResource;
14+
15+
class ContactResponse extends BaseResource
16+
{
17+
public static $model = \Tipoff\Forms\Models\ContactResponse::class;
18+
19+
public static $search = [
20+
'id',
21+
];
22+
23+
public static $group = 'Forms';
24+
25+
public function fieldsForIndex(NovaRequest $request)
26+
{
27+
return array_filter([
28+
ID::make(),
29+
]);
30+
}
31+
32+
public function fields(Request $request)
33+
{
34+
return array_filter([
35+
new Panel('Data Fields', $this->dataFields()),
36+
Textarea::make('Message')->rows(3)->alwaysShow()->nullable()->hideWhenUpdating(),
37+
Textarea::make('Comment')->rows(3)->alwaysShow()->nullable()->hideWhenUpdating(),
38+
]);
39+
}
40+
41+
protected function dataFields(): array
42+
{
43+
return [
44+
ID::make(),
45+
DateTime::make('Emailed At', 'emailed_at')->exceptOnForms(),
46+
];
47+
}
48+
}
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tipoff\Forms\Policies;
6+
7+
use Illuminate\Auth\Access\HandlesAuthorization;
8+
use Tipoff\Forms\Models\ContactResponse;
9+
use Tipoff\Support\Contracts\Models\UserInterface;
10+
11+
class ContactResponsePolicy
12+
{
13+
use HandlesAuthorization;
14+
15+
public function viewAny(UserInterface $user): bool
16+
{
17+
return $user->hasPermissionTo('view contact responses') ? true : false;
18+
}
19+
20+
public function view(UserInterface $user, ContactResponse $contact_response): bool
21+
{
22+
return $user->hasPermissionTo('view contact responses') ? true : false;
23+
}
24+
25+
public function create(UserInterface $user): bool
26+
{
27+
return $user->hasPermissionTo('create contact responses') ? true : false;
28+
}
29+
30+
public function update(UserInterface $user, ContactResponse $contact_response): bool
31+
{
32+
return $user->hasPermissionTo('update contact responses') ? true : false;
33+
}
34+
35+
public function delete(UserInterface $user, ContactResponse $contact_response): bool
36+
{
37+
return $user->hasPermissionTo('delete contact responses') ? true : false;
38+
}
39+
40+
public function restore(UserInterface $user, ContactResponse $contact_response): bool
41+
{
42+
return false;
43+
}
44+
45+
public function forceDelete(UserInterface $user, ContactResponse $contact_response): bool
46+
{
47+
return false;
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tipoff\Forms\Tests\Unit\Models;
6+
7+
use Illuminate\Foundation\Testing\DatabaseTransactions;
8+
use Illuminate\Foundation\Testing\WithFaker;
9+
use Tipoff\Forms\Models\ContactResponse;
10+
use Tipoff\Forms\Tests\TestCase;
11+
12+
class ContactResponseModelTest extends TestCase
13+
{
14+
use DatabaseTransactions;
15+
use WithFaker;
16+
17+
/** @test */
18+
public function create()
19+
{
20+
$model = ContactResponse::factory()->create();
21+
$this->assertNotNull($model);
22+
}
23+
}

0 commit comments

Comments
 (0)