Skip to content

Commit

Permalink
remove can_expire
Browse files Browse the repository at this point in the history
  • Loading branch information
eidolex committed Dec 31, 2023
1 parent 4852102 commit d5e51c8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 14 deletions.
4 changes: 1 addition & 3 deletions database/2023_12_31_153356_create_credits_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public function up(): void
->default(0);
$table->unsignedDecimal('remaining_balance', 64, 8)
->default(0);
$table->boolean('can_expire')
->index();
$table->dateTime('expires_at')
->nullable()
->index();
Expand All @@ -44,7 +42,7 @@ public function up(): void
ALTER TABLE {$creditTable}
ADD COLUMN unique_non_expire_holder VARCHAR(255) GENERATED ALWAYS AS (
CASE
WHEN can_expire = 0 THEN MD5(CONCAT(holder_type, '::', holder_id))
WHEN expires_at IS NULL THEN MD5(CONCAT(holder_type, '::', holder_id))
ELSE NULL
END
) STORED;
Expand Down
3 changes: 1 addition & 2 deletions src/Models/Credit.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class Credit extends Model
'holder_id',
'initial_balance',
'remaining_balance',
'can_expire',
'expires_at',
'meta',
];
Expand All @@ -36,7 +35,7 @@ public function scopeIsHolder(Builder $query, Model $holder): Builder
public function scopeNotExpired(Builder $query): Builder
{
return $query->where(function (Builder $query) {
$query->where('can_expire', false)
$query->whereNull('expires_at')
->orWhere('expires_at', '>', now());
});
}
Expand Down
14 changes: 5 additions & 9 deletions src/Services/CreditService.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function addCreditToHolder(
'meta' => $meta,
]);

if (! $expiresAt) {
if (!$expiresAt) {
$credit = $this->addNonExpiringCredit(
$holder,
$amount,
Expand Down Expand Up @@ -82,18 +82,17 @@ public function addNonExpiringCredit(
float $amount,
): Credit {
$credit = $this->creditModel->query()
->where('can_expire', false)
->whereNull('expires_at')
->isHolder($holder)
->lockForUpdate()
->first();

if (! $credit) {
if (!$credit) {
$credit = $this->creditModel->newInstance([
'holder_type' => $holder->getMorphClass(),
'holder_id' => $holder->getKey(),
'initial_balance' => 0,
'remaining_balance' => 0,
'can_expire' => false,
]);
}

Expand All @@ -114,7 +113,6 @@ public function addExpiringCredit(
'initial_balance' => $amount,
'remaining_balance' => $amount,
'expires_at' => $expiresAt,
'can_expire' => true,
]);
}

Expand All @@ -126,7 +124,7 @@ public function useCredit(
?array $meta = null,
) {

if (! $this->checkHolderCredit($holder, $amount)) {
if (!$this->checkHolderCredit($holder, $amount)) {
throw new \Exception('Insufficient credit');
}

Expand Down Expand Up @@ -160,7 +158,6 @@ public function useCredit(
: abs($remainingAmount),
'holder_type' => $credit->holder_type,
'holder_id' => $credit->holder_id,
'can_expire' => $credit->can_expire,
];

$details[] = [
Expand Down Expand Up @@ -203,15 +200,14 @@ public function getRemainingCreditByHolderForUpdate(
->isHolder($holder)
->notExpired()
->hasRemainingBalance()
->orderBy('can_expire', 'desc')
->orderBy('unique_non_expire_holder', 'asc')
->orderBy('expires_at', 'asc')
->lockForUpdate()
->get([
'id',
'remaining_balance',
'holder_type',
'holder_id',
'can_expire',
]);
}

Expand Down

0 comments on commit d5e51c8

Please sign in to comment.