Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Allow for custom "shops" table for automatic migrations #1192

Merged
merged 11 commits into from
Sep 9, 2022
8 changes: 6 additions & 2 deletions src/Storage/Commands/Charge.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Osiset\ShopifyApp\Storage\Commands;

use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Osiset\ShopifyApp\Contracts\Commands\Charge as ChargeCommand;
use Osiset\ShopifyApp\Contracts\Queries\Charge as ChargeQuery;
use Osiset\ShopifyApp\Objects\Enums\ChargeStatus;
Expand Down Expand Up @@ -50,10 +51,12 @@ public function make(ChargeTransfer $chargeObj): ChargeId
return $obj instanceof Carbon;
};

$userTableId = Str::singular(Util::getShopifyConfig('table_names.shops', 'users')) . '_id';

$chargeClass = Util::getShopifyConfig('models.charge', ChargeModel::class);
$charge = new $chargeClass();
$charge->plan_id = $chargeObj->planId->toNative();
$charge->user_id = $chargeObj->shopId->toNative();
$charge->$userTableId = $chargeObj->shopId->toNative();
$charge->charge_id = $chargeObj->chargeReference->toNative();
$charge->type = $chargeObj->chargeType->toNative();
$charge->status = $chargeObj->chargeStatus->toNative();
Expand Down Expand Up @@ -89,10 +92,11 @@ public function delete(ChargeReference $chargeRef, ShopId $shopId): bool
*/
public function makeUsage(UsageChargeTransfer $chargeObj): ChargeId
{
$userTableId = Str::singular(Util::getShopifyConfig('table_names.shops', 'users')) . '_id';
gnikyt marked this conversation as resolved.
Show resolved Hide resolved
// Create the charge
$chargeClass = Util::getShopifyConfig('models.charge', ChargeModel::class);
$charge = new $chargeClass();
$charge->user_id = $chargeObj->shopId->toNative();
$charge->$userTableId = $chargeObj->shopId->toNative();
$charge->charge_id = $chargeObj->chargeReference->toNative();
$charge->type = $chargeObj->chargeType->toNative();
$charge->status = $chargeObj->chargeStatus->toNative();
Expand Down
16 changes: 14 additions & 2 deletions src/Storage/Models/Charge.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
use Osiset\ShopifyApp\Objects\Enums\ChargeStatus;
use Osiset\ShopifyApp\Objects\Enums\ChargeType;
use Osiset\ShopifyApp\Objects\Values\ChargeId;
Expand All @@ -18,14 +19,15 @@ class Charge extends Model
{
use SoftDeletes;

protected $userTableId = 'user_id';
gnikyt marked this conversation as resolved.
Show resolved Hide resolved

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'type',
'user_id',
'charge_id',
'plan_id',
'status',
Expand All @@ -49,6 +51,16 @@ class Charge extends Model
*/
protected $dates = ['deleted_at'];

public function __construct(array $attributes = [])
{
$this->userTableId = Str::singular(Util::getShopifyConfig('table_names.shops', 'users')) . '_id';
$this->fillable[] = $this->userTableId;

parent::__construct($attributes);


}

/**
* Get table name.
*
Expand Down Expand Up @@ -88,7 +100,7 @@ public function shop(): BelongsTo
{
return $this->belongsTo(
Util::getShopifyConfig('user_model'),
'user_id',
$this->userTableId,
'id'
);
}
Expand Down
6 changes: 5 additions & 1 deletion src/Storage/Queries/Charge.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Osiset\ShopifyApp\Storage\Queries;

use Illuminate\Support\Str;
use Osiset\ShopifyApp\Contracts\Queries\Charge as IChargeQuery;
use Osiset\ShopifyApp\Objects\Values\ChargeId;
use Osiset\ShopifyApp\Objects\Values\ChargeReference;
Expand All @@ -21,13 +22,16 @@ class Charge implements IChargeQuery
*/
protected $chargeModel;

protected $userTableId;

/**
* Init for charge command.
*/
public function __construct()
{
$chargeClass = Util::getShopifyConfig('models.charge', ChargeModel::class);
$this->chargeModel = new $chargeClass();
$this->userTableId = Str::singular(Util::getShopifyConfig('table_names.shops', 'users')) . '_id';
}


Expand Down Expand Up @@ -61,7 +65,7 @@ public function getByReferenceAndShopId(ChargeReference $chargeRef, ShopId $shop
{
return $this->chargeModel->query()
->where('charge_id', $chargeRef->toNative())
->where('user_id', $shopId->toNative())
->where($this->userTableId, $shopId->toNative())
->get()
->first();
}
Expand Down
5 changes: 5 additions & 0 deletions src/resources/config/shopify-app.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,5 +470,10 @@
* The table name for Plan model.
*/
'plans' => 'plans',

/*
* The table name for the Shop.
*/
'shops' => 'users',
]
];
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ class CreateShopsTable extends Migration
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
Schema::table(Util::getShopifyConfig('table_names.shops'), function (Blueprint $table) {
$table->boolean('shopify_grandfathered')->default(false);
$table->string('shopify_namespace')->nullable(true)->default(null);
$table->boolean('shopify_freemium')->default(false);
$table->integer('plan_id')->unsigned()->nullable();

if (! Schema::hasColumn('users', 'deleted_at')) {
if (! Schema::hasColumn(Util::getShopifyConfig('table_names.shops'), 'deleted_at')) {
$table->softDeletes();
}

Expand All @@ -35,8 +35,8 @@ public function up(): void
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropForeign('users_plan_id_foreign');
Schema::table(Util::getShopifyConfig('table_names.shops'), function (Blueprint $table) {
$table->dropForeign(Util::getShopifyConfig('table_names.shops') . '_plan_id_foreign');
$table->dropColumn([
'shopify_grandfathered',
'shopify_namespace',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Schema;
use Osiset\ShopifyApp\Util;
use Illuminate\Support\Str;

class CreateChargesTable extends Migration
{
Expand Down Expand Up @@ -79,13 +80,13 @@ public function up()
$table->softDeletes();

if ($this->getLaravelVersion() < 5.8) {
$table->integer('user_id')->unsigned();
$table->integer(Str::singular(Util::getShopifyConfig('table_names.shops')) . '_id')->unsigned();
} else {
$table->bigInteger('user_id')->unsigned();
$table->bigInteger(Str::singular(Util::getShopifyConfig('table_names.shops')) . '_id')->unsigned();
}

// Linking
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign(Str::singular(Util::getShopifyConfig('table_names.shops')) . '_id')->references('id')->on(Util::getShopifyConfig('table_names.shops'))->onDelete('cascade');
$table->foreign('plan_id')->references('id')->on(Util::getShopifyConfig('table_names.plans', 'plans'));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Osiset\ShopifyApp\Util;

class AddPasswordUpdatedAtToUsersTable extends Migration
{
Expand All @@ -13,7 +14,7 @@ class AddPasswordUpdatedAtToUsersTable extends Migration
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
Schema::table(Util::getShopifyConfig('table_names.shops'), function (Blueprint $table) {
$table->date('password_updated_at')->nullable();
});
}
Expand All @@ -25,7 +26,7 @@ public function up()
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
Schema::table(Util::getShopifyConfig('table_names.shops'), function (Blueprint $table) {
$table->dropColumn('password_updated_at');
});
}
Expand Down