Harden credit flows and add RevenueCat webhook
This commit is contained in:
@@ -25,6 +25,11 @@ class Event extends Model
|
||||
return $this->belongsTo(Tenant::class);
|
||||
}
|
||||
|
||||
public function eventType(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(EventType::class);
|
||||
}
|
||||
|
||||
public function photos(): HasMany
|
||||
{
|
||||
return $this->hasMany(Photo::class);
|
||||
@@ -46,4 +51,3 @@ class Event extends Model
|
||||
->withTimestamps();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -78,11 +78,20 @@ class Tenant extends Model
|
||||
|
||||
public function decrementCredits(int $amount, string $reason = 'event_create', ?string $note = null, ?int $relatedPurchaseId = null): bool
|
||||
{
|
||||
if ($this->event_credits_balance < $amount) {
|
||||
return false;
|
||||
if ($amount <= 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($amount, $reason, $note, $relatedPurchaseId) {
|
||||
$operation = function () use ($amount, $reason, $note, $relatedPurchaseId) {
|
||||
$locked = static::query()
|
||||
->whereKey($this->getKey())
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
|
||||
if (! $locked || $locked->event_credits_balance < $amount) {
|
||||
return false;
|
||||
}
|
||||
|
||||
EventCreditsLedger::create([
|
||||
'tenant_id' => $this->id,
|
||||
'delta' => -$amount,
|
||||
@@ -91,13 +100,33 @@ class Tenant extends Model
|
||||
'note' => $note,
|
||||
]);
|
||||
|
||||
return $this->decrement('event_credits_balance', $amount);
|
||||
});
|
||||
$locked->event_credits_balance -= $amount;
|
||||
$locked->save();
|
||||
|
||||
$this->event_credits_balance = $locked->event_credits_balance;
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
return $this->runCreditOperation($operation);
|
||||
}
|
||||
|
||||
public function incrementCredits(int $amount, string $reason = 'manual_adjust', ?string $note = null, ?int $relatedPurchaseId = null): bool
|
||||
{
|
||||
return DB::transaction(function () use ($amount, $reason, $note, $relatedPurchaseId) {
|
||||
if ($amount <= 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$operation = function () use ($amount, $reason, $note, $relatedPurchaseId) {
|
||||
$locked = static::query()
|
||||
->whereKey($this->getKey())
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
|
||||
if (! $locked) {
|
||||
return false;
|
||||
}
|
||||
|
||||
EventCreditsLedger::create([
|
||||
'tenant_id' => $this->id,
|
||||
'delta' => $amount,
|
||||
@@ -106,7 +135,25 @@ class Tenant extends Model
|
||||
'note' => $note,
|
||||
]);
|
||||
|
||||
return $this->increment('event_credits_balance', $amount);
|
||||
});
|
||||
$locked->event_credits_balance += $amount;
|
||||
$locked->save();
|
||||
|
||||
$this->event_credits_balance = $locked->event_credits_balance;
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
return $this->runCreditOperation($operation);
|
||||
}
|
||||
|
||||
private function runCreditOperation(callable $operation): bool
|
||||
{
|
||||
$connection = DB::connection();
|
||||
|
||||
if ($connection->transactionLevel() > 0) {
|
||||
return (bool) $operation();
|
||||
}
|
||||
|
||||
return (bool) $connection->transaction($operation);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user