Skip to content

Commit

Permalink
Merge pull request #55 from tipoff/chx2/feature/refactored-models
Browse files Browse the repository at this point in the history
GMB Details & Hours #44
  • Loading branch information
drewroberts authored Mar 6, 2021
2 parents ade9b41 + 9c95f55 commit ae3b1d0
Show file tree
Hide file tree
Showing 21 changed files with 303 additions and 67 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
"require": {
"php": "^7.4|^8.0",
"drewroberts/media": "^3.2.2",
"tipoff/addresses": "^2.0.0",
"tipoff/authorization": "^2.3.1",
"tipoff/seo": "^1.0.8",
"tipoff/support": "^1.5.7"
"tipoff/seo": "^2.0.0",
"tipoff/support": "^1.5.8"
},
"require-dev": {
"tipoff/test-support": "^1.2.0"
Expand Down
33 changes: 33 additions & 0 deletions database/factories/GmbDetailFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Tipoff\Locations\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Date;
use Tipoff\Locations\Models\GmbDetail;
use Tipoff\Locations\Models\Location;

class GmbDetailFactory extends Factory
{
protected $model = GmbDetail::class;

public function definition()
{
return [
'name' => $this->faker->name,
'opened_at' => $this->faker->date('Y-m-d'),
'address' => $this->faker->address,
'address2' => $this->faker->address,
'city' => $this->faker->city,
'state' => $this->faker->state,
'zip' => $this->faker->postcode,
'phone' => $this->faker->phoneNumber,
'latitude' => $this->faker->latitude,
'longitude' => $this->faker->longitude,
'location_id' => Location::factory()->create()->id,
'created_at' => Date::now()
];
}
}
37 changes: 37 additions & 0 deletions database/factories/GmbHourFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Tipoff\Locations\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Date;
use Tipoff\Locations\Models\GmbHour;
use Tipoff\Locations\Models\Location;

class GmbHourFactory extends Factory
{
protected $model = GmbHour::class;

public function definition()
{
return [
'monday_open' => $this->faker->text,
'monday_close' => $this->faker->text,
'tuesday_open' => $this->faker->text,
'tuesday_close' => $this->faker->text,
'wednesday_open' => $this->faker->text,
'wednesday_close' => $this->faker->text,
'thursday_open' => $this->faker->text,
'thursday_close' => $this->faker->text,
'friday_open' => $this->faker->text,
'friday_close' => $this->faker->text,
'saturday_open' => $this->faker->text,
'saturday_close' => $this->faker->text,
'sunday_open' => $this->faker->text,
'sunday_close' => $this->faker->text,
'location_id' => Location::factory()->create()->id,
'created_at' => Date::now()
];
}
}
12 changes: 1 addition & 11 deletions database/factories/LocationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,8 @@

class LocationFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Location::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$city = $this->faker->city;
Expand All @@ -31,8 +21,8 @@ public function definition()
'name' => $city,
'slug' => Str::slug($city),
'title_part' => $city,
'timezone' => $this->faker->timezone,
'market_id' => randomOrCreate(Market::class),
'timezone_id' => randomOrCreate(app('timezone')),
'creator_id' => randomOrCreate(app('user')),
'updater_id' => randomOrCreate(app('user'))
];
Expand Down
12 changes: 1 addition & 11 deletions database/factories/MarketFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,8 @@

class MarketFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Market::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$city = $this->faker->city;
Expand All @@ -31,9 +21,9 @@ public function definition()
'name' => $city,
'title' => $city,
'state' => $this->faker->stateAbbr,
'timezone' => 'EST',
'content' => $this->faker->sentences(3, true),
'entered_at' => $this->faker->date(),
'timezone_id' => randomOrCreate(app('timezone')),
'creator_id' => randomOrCreate(app('user')),
'updater_id' => randomOrCreate(app('user')),
];
Expand Down
22 changes: 0 additions & 22 deletions database/migrations/2014_01_01_100000_create_timezones_table.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function up()
$table->string('name')->unique();
$table->string('title')->unique(); // Market Title for Display. Used publicly.
$table->string('state'); // Just the 2-digit abbreviation.
$table->string('timezone');
$table->foreignIdFor(app('timezone'));
$table->text('content')->nullable(); // Market specific content. Written in Markdown.
$table->date('entered_at'); // Date first location opened in the market.
$table->date('closed_at')->nullable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function up()
$table->string('name')->unique(); // Internal reference name
$table->string('abbreviation', 4)->unique(); // 3 digit abbreviation (all caps) for location. Option to add 4th digit character if necessary.
$table->string('title_part')->nullable(); // For when have more than one location in a market, this is used to generate formal title.
$table->string('timezone'); // Informal symbol such as EST or CST
$table->foreignIdFor(app('timezone'));
$table->foreignIdFor(Market::class);
$table->foreignIdFor(app('user'), 'manager_id')->nullable();
$table->string('contact_email');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public function up()
'view locations',
'create locations',
'update locations',
'delete locations',
'view markets',
'create markets',
'update markets',
'delete markets'
];
'view gmb details',
'view gmb hours',
];

$this->createPermissions($permissions);
}
Expand Down
6 changes: 6 additions & 0 deletions src/LocationsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
namespace Tipoff\Locations;

use Tipoff\Locations\Commands\SyncLocations;
use Tipoff\Locations\Models\GmbDetail;
use Tipoff\Locations\Models\GmbHour;
use Tipoff\Locations\Models\Location;
use Tipoff\Locations\Models\Market;
use Tipoff\Locations\Policies\GmbDetailPolicy;
use Tipoff\Locations\Policies\GmbHourPolicy;
use Tipoff\Locations\Policies\LocationPolicy;
use Tipoff\Locations\Policies\MarketPolicy;
use Tipoff\Support\TipoffPackage;
Expand All @@ -18,6 +22,8 @@ public function configureTipoffPackage(TipoffPackage $package): void
{
$package
->hasPolicies([
GmbDetail::class => GmbDetailPolicy::class,
GmbHour::class => GmbHourPolicy::class,
Location::class => LocationPolicy::class,
Market::class => MarketPolicy::class,
])
Expand Down
29 changes: 29 additions & 0 deletions src/Models/GmbDetail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Tipoff\Locations\Models;

use Tipoff\Support\Models\BaseModel;
use Tipoff\Support\Traits\HasCreator;
use Tipoff\Support\Traits\HasPackageFactory;
use Tipoff\Support\Traits\HasUpdater;

class GmbDetail extends BaseModel
{
use HasPackageFactory;
use HasCreator;
use HasUpdater;

public $timestamps = false;

public function location()
{
return $this->belongsTo(Location::class);
}

public function webpage()
{
return $this->belongsTo(app('webpage'));
}
}
24 changes: 24 additions & 0 deletions src/Models/GmbHour.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Tipoff\Locations\Models;

use Tipoff\Support\Models\BaseModel;
use Tipoff\Support\Traits\HasCreator;
use Tipoff\Support\Traits\HasPackageFactory;
use Tipoff\Support\Traits\HasUpdater;

class GmbHour extends BaseModel
{
use HasPackageFactory;
use HasCreator;
use HasUpdater;

public $timestamps = false;

public function location()
{
return $this->belongsTo(Location::class);
}
}
9 changes: 5 additions & 4 deletions src/Models/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ protected static function boot()
if (empty($location->market_id)) {
throw new \Exception('A location must be in a market.');
}
if (empty($location->timezone)) {
$location->timezone = 'EST';
if (empty($location->timezone_id)) {
// @todo refactor to fetch EST timezone and save it here
$location->timezone_id = 1;
}
if (empty($location->contact_email)) {
$location->contact_email = $location->slug . '@thegreatescaperoom.com';
Expand Down Expand Up @@ -288,7 +289,7 @@ public function getRevenueBookedYesterdayAttribute()
$query->where('location_id', $this->id);
})->sum('amount')
+
$bookingModel::yesterday()
$bookingModel::yesterday()
->whereHas('order', function (Builder $query) {
$query->where('location_id', $this->id);
})->sum('total_fees')) / 100, 2);
Expand All @@ -315,7 +316,7 @@ public function getRevenueBookedLastWeekAttribute()
$query->where('location_id', $this->id);
})->sum('amount')
+
$bookingModel::week()
$bookingModel::week()
->whereHas('order', function (Builder $query) {
$query->where('location_id', $this->id);
})->sum('total_fees')) / 100, 2);
Expand Down
7 changes: 4 additions & 3 deletions src/Models/Market.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ protected static function boot()
if (empty($market->entered_at)) {
$market->entered_at = '2016-01-01';
}
if (empty($market->timezone)) {
$market->timezone = 'EST';
if (empty($market->timezone_id)) {
// @todo refactor to fetch EST timezone and save it here
$market->timezone_id = 1;
}
});

Expand Down Expand Up @@ -158,7 +159,7 @@ public function getDescriptionAttribute()

$rooms = $roomModel::whereIn('location_id', $locations->pluck('id'))->whereNull('closed_at')->get();

return $this->title.' has '.$rooms->count().' different escape rooms and offers private escape games for groups & parties. Book your escape room today!';
return $this->title . ' has ' . $rooms->count() . ' different escape rooms and offers private escape games for groups & parties. Book your escape room today!';
}

public function findAllThemes()
Expand Down
2 changes: 1 addition & 1 deletion src/Nova/Filters/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tippoff\Locations\Nova\Filters;
namespace Tipoff\Locations\Nova\Filters;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
Expand Down
49 changes: 49 additions & 0 deletions src/Policies/GmbDetailPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Tipoff\Locations\Policies;

use Illuminate\Auth\Access\HandlesAuthorization;
use Tipoff\Locations\Models\GmbDetail;
use Tipoff\Support\Contracts\Models\UserInterface;

class GmbDetailPolicy
{
use HandlesAuthorization;

public function viewAny(UserInterface $user): bool
{
return $user->hasPermissionTo('view gmb details') ? true : false;
}

public function view(UserInterface $user, GmbDetail $gmb_detail): bool
{
return $user->hasPermissionTo('view gmb details') ? true : false;
}

public function create(UserInterface $user): bool
{
return false;
}

public function update(UserInterface $user, GmbDetail $gmb_detail): bool
{
return false;
}

public function delete(UserInterface $user, GmbDetail $gmb_detail): bool
{
return false;
}

public function restore(UserInterface $user, GmbDetail $gmb_detail): bool
{
return false;
}

public function forceDelete(UserInterface $user, GmbDetail $gmb_detail): bool
{
return false;
}
}
Loading

0 comments on commit ae3b1d0

Please sign in to comment.