Files
opensourcepos/app/Models/JobThrottle.php
objecttothis bb99a22f59 fix(job-throttle): return saved throttle id to prevent unintended deletion
- JobThrottle::saveValue now returns int throttle_id instead of bool:
  new inserts return the DB-generated insertID, updates return the
  existing throttleId.
- Jobs controller collects saved ids into $notToDelete using the
  returned value, so newly inserted throttles are correctly tracked.

Fixes bug where newly inserted throttles had no id captured,
causing them to be wrongly deleted in the post-save cleanup step.

Signed-off-by: objecttothis <17935339+objecttothis@users.noreply.github.com>
2026-09-17 15:29:58 +04:00

84 lines
2.0 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Database\ResultInterface;
use CodeIgniter\Model;
/**
* JobThrottle class
*/
class JobThrottle extends Model
{
protected $table = 'job_throttles';
protected $primaryKey = 'throttle_id';
protected $useAutoIncrement = true;
protected $useSoftDeletes = false;
protected $allowedFields = [
'max_count',
'period',
'deleted'
];
/**
* @param int $throttleId
* @return bool
*/
public function exists(int $throttleId): bool
{
$builder = $this->db->table('job_throttles');
$builder->where('throttle_id', $throttleId);
return ($builder->get()->getNumRows() >= 1);
}
/**
* @param array $throttleData
* @param int $throttleId
* @return int Returns the throttle_id of the saved row (new id if inserted)
*/
public function saveValue(array $throttleData, int $throttleId): int
{
$throttleDataToSave = [
'max_count' => $throttleData['max_count'],
'period' => $throttleData['period'],
'deleted' => 0
];
if (!$this->exists($throttleId)) {
$builder = $this->db->table('job_throttles');
$builder->insert($throttleDataToSave);
return (int)$this->db->insertID();
}
$builder = $this->db->table('job_throttles');
$builder->where('throttle_id', $throttleId);
$builder->update($throttleDataToSave);
return $throttleId;
}
/**
* @return ResultInterface
*/
public function getAll(): ResultInterface
{
$builder = $this->db->table('job_throttles');
$builder->where('deleted', 0);
return $builder->get();
}
/**
* Deletes one throttle
*/
public function delete($throttleId = null, bool $purge = false): bool
{
$builder = $this->db->table('job_throttles');
$builder->where('throttle_id', $throttleId);
return $builder->update(['deleted' => 1]);
}
}