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
7 changes: 5 additions & 2 deletions src/Storage/Commands/Charge.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ public function make(ChargeTransfer $chargeObj): ChargeId
return $obj instanceof Carbon;
};

$shopTableId = Util::getShopsTableForeignKey();

$chargeClass = Util::getShopifyConfig('models.charge', ChargeModel::class);
$charge = new $chargeClass();
$charge->plan_id = $chargeObj->planId->toNative();
$charge->user_id = $chargeObj->shopId->toNative();
$charge->$shopTableId = $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 +91,11 @@ public function delete(ChargeReference $chargeRef, ShopId $shopId): bool
*/
public function makeUsage(UsageChargeTransfer $chargeObj): ChargeId
{
$shopTableId = Util::getShopsTableForeignKey();
// Create the charge
$chargeClass = Util::getShopifyConfig('models.charge', ChargeModel::class);
$charge = new $chargeClass();
$charge->user_id = $chargeObj->shopId->toNative();
$charge->$shopTableId = $chargeObj->shopId->toNative();
$charge->charge_id = $chargeObj->chargeReference->toNative();
$charge->type = $chargeObj->chargeType->toNative();
$charge->status = $chargeObj->chargeStatus->toNative();
Expand Down
10 changes: 8 additions & 2 deletions src/Storage/Models/Charge.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class Charge extends Model
*/
protected $fillable = [
'type',
'user_id',
'charge_id',
'plan_id',
'status',
Expand All @@ -49,6 +48,13 @@ class Charge extends Model
*/
protected $dates = ['deleted_at'];

public function __construct(array $attributes = [])
{
$this->fillable[] = Util::getShopsTableForeignKey();

parent::__construct($attributes);
}

/**
* Get table name.
*
Expand Down Expand Up @@ -88,7 +94,7 @@ public function shop(): BelongsTo
{
return $this->belongsTo(
Util::getShopifyConfig('user_model'),
'user_id',
Util::getShopsTableForeignKey(),
'id'
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Storage/Queries/Charge.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getByReferenceAndShopId(ChargeReference $chargeRef, ShopId $shop
{
return $this->chargeModel->query()
->where('charge_id', $chargeRef->toNative())
->where('user_id', $shopId->toNative())
->where(Util::getShopsTableForeignKey(), $shopId->toNative())
->get()
->first();
}
Expand Down
21 changes: 21 additions & 0 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,25 @@ public static function getGraphQLWebhookTopic(string $topic): string
->upper()
->replaceMatches('/[^A-Z_]/', '_');
}


/**
* Get the table name for shop
*
* @return string
*/
public static function getShopsTable(): string
{
return Util::getShopifyConfig('table_names.shops') ?? 'users';
}

/**
* Get the table foreign key for shop
*
* @return string
*/
public static function getShopsTableForeignKey(): string
{
return Str::singular(self::getShopsTable()) . '_id';
}
}
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::getShopsTable(), 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::getShopsTable(), '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::getShopsTable(), function (Blueprint $table) {
$table->dropForeign(Util::getShopsTable() . '_plan_id_foreign');
$table->dropColumn([
'shopify_grandfathered',
'shopify_namespace',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ public function up()
$table->softDeletes();

if ($this->getLaravelVersion() < 5.8) {
$table->integer('user_id')->unsigned();
$table->integer(Util::getShopsTableForeignKey())->unsigned();
} else {
$table->bigInteger('user_id')->unsigned();
$table->bigInteger(Util::getShopsTableForeignKey())->unsigned();
}

// Linking
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign(Util::getShopsTableForeignKey())->references('id')->on(Util::getShopsTable())->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::getShopsTable(), 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::getShopsTable(), function (Blueprint $table) {
$table->dropColumn('password_updated_at');
});
}
Expand Down