diff --git a/src/Storage/Commands/Charge.php b/src/Storage/Commands/Charge.php index 05ca3c20..18c23a3f 100644 --- a/src/Storage/Commands/Charge.php +++ b/src/Storage/Commands/Charge.php @@ -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(); @@ -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(); diff --git a/src/Storage/Models/Charge.php b/src/Storage/Models/Charge.php index f9e10828..4908608c 100644 --- a/src/Storage/Models/Charge.php +++ b/src/Storage/Models/Charge.php @@ -25,7 +25,6 @@ class Charge extends Model */ protected $fillable = [ 'type', - 'user_id', 'charge_id', 'plan_id', 'status', @@ -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. * @@ -88,7 +94,7 @@ public function shop(): BelongsTo { return $this->belongsTo( Util::getShopifyConfig('user_model'), - 'user_id', + Util::getShopsTableForeignKey(), 'id' ); } diff --git a/src/Storage/Queries/Charge.php b/src/Storage/Queries/Charge.php index 41a6235f..75ba4912 100644 --- a/src/Storage/Queries/Charge.php +++ b/src/Storage/Queries/Charge.php @@ -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(); } diff --git a/src/Util.php b/src/Util.php index ee88ebe3..b468e0f4 100644 --- a/src/Util.php +++ b/src/Util.php @@ -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 self::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'; + } } diff --git a/src/resources/config/shopify-app.php b/src/resources/config/shopify-app.php index eb7db3dd..c4aaf7e1 100644 --- a/src/resources/config/shopify-app.php +++ b/src/resources/config/shopify-app.php @@ -470,7 +470,12 @@ * The table name for Plan model. */ 'plans' => 'plans', + + /* + * The table name for the Shop. + */ + 'shops' => 'users', ], - 'session_token_refresh_interval' => env('SESSION_TOKEN_REFRESH_INTERVAL', 2000) + 'session_token_refresh_interval' => env('SESSION_TOKEN_REFRESH_INTERVAL', 2000), ]; diff --git a/src/resources/database/migrations/2020_01_29_230905_create_shops_table.php b/src/resources/database/migrations/2020_01_29_230905_create_shops_table.php index d7414bc4..c87f0b16 100644 --- a/src/resources/database/migrations/2020_01_29_230905_create_shops_table.php +++ b/src/resources/database/migrations/2020_01_29_230905_create_shops_table.php @@ -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(); } @@ -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', diff --git a/src/resources/database/migrations/2020_01_29_231006_create_charges_table.php b/src/resources/database/migrations/2020_01_29_231006_create_charges_table.php index 5f63d9d5..74da5107 100644 --- a/src/resources/database/migrations/2020_01_29_231006_create_charges_table.php +++ b/src/resources/database/migrations/2020_01_29_231006_create_charges_table.php @@ -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')); }); } diff --git a/src/resources/database/migrations/2021_04_21_103633_add_password_updated_at_to_users_table.php b/src/resources/database/migrations/2021_04_21_103633_add_password_updated_at_to_users_table.php index ab3ff59b..21751b3c 100644 --- a/src/resources/database/migrations/2021_04_21_103633_add_password_updated_at_to_users_table.php +++ b/src/resources/database/migrations/2021_04_21_103633_add_password_updated_at_to_users_table.php @@ -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 { @@ -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(); }); } @@ -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'); }); }