fix: 🐛 resolve duplicate entry when ArchiveStatsJob is dispatched #18
This commit is contained in:
parent
8280ee5458
commit
59617df1e8
2 changed files with 93 additions and 47 deletions
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\ArchiveCampaign;
|
||||
use App\Models\ArchiveCoupon;
|
||||
use App\Models\ArchiveStat;
|
||||
use App\Models\Statistique;
|
||||
use Illuminate\Bus\Queueable;
|
||||
|
|
@ -19,39 +21,41 @@ class ArchiveStatsJob implements ShouldQueue
|
|||
use Queueable;
|
||||
use SerializesModels;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
// Construct method.
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
public function handle(): void
|
||||
{
|
||||
try {
|
||||
DB::transaction(function () {
|
||||
Statistique::query()
|
||||
->where('date', '<', now())
|
||||
->each(function ($stat) {
|
||||
$archiveStat = new ArchiveStat();
|
||||
$archiveStat->quantity = $stat->stat_couponPrinted;
|
||||
$archiveStat->date = $stat->date;
|
||||
$archiveStat->archive_coupon_id = $stat->stat_couponId;
|
||||
$archiveStat->archive_campaign_id = $stat->stat_campId;
|
||||
$archiveStat->saveOrFail();
|
||||
->each(function (Statistique $stat) {
|
||||
$archiveCampaign = ArchiveCampaign::query()->where('campaign_id', $stat->stat_campId)->first();
|
||||
$archiveCoupon = ArchiveCoupon::query()->where('coupon_code', $stat->stat_couponId)->first();
|
||||
if (!is_null($archiveCampaign) && !is_null($archiveCoupon)) {
|
||||
$options = [
|
||||
['date', $stat->date],
|
||||
['archive_coupon_id', $archiveCoupon->getKey()],
|
||||
['archive_campaign_id', $archiveCampaign->getKey()],
|
||||
];
|
||||
$archiveStat = ArchiveStat::query()->where($options)->first();
|
||||
if (!is_null($archiveStat)) {
|
||||
$newQuantity = $archiveStat->quantity + $stat->stat_couponPrinted;
|
||||
$archiveStat->quantity = $newQuantity;
|
||||
$archiveStat->saveOrFail();
|
||||
} else {
|
||||
$this->createArchiveStat($stat, $archiveCampaign->getKey(), $archiveCoupon->getKey());
|
||||
}
|
||||
} else {
|
||||
$this->createArchiveStat($stat);
|
||||
}
|
||||
});
|
||||
|
||||
Statistique::query()
|
||||
->where('date', '<', now())
|
||||
->delete();
|
||||
Statistique::query()
|
||||
->where('date', '<', now())
|
||||
->delete();
|
||||
});
|
||||
} catch (\Exception $e) {
|
||||
Log::error(
|
||||
|
|
@ -64,4 +68,25 @@ class ArchiveStatsJob implements ShouldQueue
|
|||
\report($e);
|
||||
} //end try
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new archive statistique.
|
||||
*
|
||||
* @param \App\Models\Statistique $stat
|
||||
* @param integer|null $archiveCampaignKey
|
||||
* @param integer|null $archiveCouponKey
|
||||
* @return void
|
||||
*/
|
||||
private function createArchiveStat(
|
||||
Statistique $stat,
|
||||
?int $archiveCampaignKey = null,
|
||||
?int $archiveCouponKey = null
|
||||
): void {
|
||||
$newArchiveStat = new ArchiveStat();
|
||||
$newArchiveStat->quantity = $stat->stat_couponPrinted;
|
||||
$newArchiveStat->date = $stat->date;
|
||||
$newArchiveStat->archive_coupon_id = $archiveCouponKey ?? $stat->stat_couponId;
|
||||
$newArchiveStat->archive_campaign_id = $archiveCampaignKey ?? $stat->stat_campId;
|
||||
$newArchiveStat->saveOrFail();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,6 +28,8 @@ class ArchiveStat extends Model
|
|||
protected $fillable = [
|
||||
'date',
|
||||
'quantity',
|
||||
'archive_coupon_id',
|
||||
'archive_campaign_id',
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
@ -38,37 +40,56 @@ class ArchiveStat extends Model
|
|||
protected static function booted(): void
|
||||
{
|
||||
static::creating(function (self $model) {
|
||||
$campaign = Campagne::withTrashed()->where('camp_id', $model->archive_campaign_id)->firstOrFail();
|
||||
$archiveCampaign = ArchiveCampaign::query()->firstOrCreate(
|
||||
[
|
||||
'campaign_id' => $model->archive_campaign_id
|
||||
],
|
||||
[
|
||||
'name' => $campaign->camp_name,
|
||||
'archive_site_id' => $campaign->camp_siteId,
|
||||
]
|
||||
);
|
||||
$model->archiveCampaign()->associate($archiveCampaign);
|
||||
|
||||
$coupon = Coupon::withTrashed()->where('coupon_id', $model->archive_coupon_id)->firstOrFail();
|
||||
$archiveCoupon = ArchiveCoupon::query()->firstOrCreate(
|
||||
[
|
||||
'coupon_code' => $model->archive_coupon_id
|
||||
],
|
||||
[
|
||||
'name' => $coupon->coupon_code,
|
||||
]
|
||||
);
|
||||
$model->archiveCoupon()->associate($archiveCoupon);
|
||||
});
|
||||
static::updating(function () {
|
||||
throw new \RuntimeException('Upating an archived stat is not authorized');
|
||||
$model->archive_campaign_id = static::resolveArchiveCampaignId($model->archive_campaign_id);
|
||||
$model->archive_coupon_id = static::resolveArchiveCouponId($model->archive_coupon_id);
|
||||
});
|
||||
static::deleting(function () {
|
||||
throw new \RuntimeException('Deleting an archived stat is not authorized');
|
||||
});
|
||||
}
|
||||
|
||||
// * METHODS
|
||||
|
||||
/**
|
||||
* Get archive campaign key.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
protected static function resolveArchiveCampaignId(int $id): int
|
||||
{
|
||||
$archiveCampaign = ArchiveCampaign::query()->find($id);
|
||||
if ($archiveCampaign) {
|
||||
return $archiveCampaign->getKey();
|
||||
}
|
||||
|
||||
$campaign = Campagne::query()->where('camp_id', $id)->firstOrFail();
|
||||
|
||||
return ArchiveCampaign::query()->firstOrCreate(
|
||||
['campaign_id' => $id],
|
||||
['name' => $campaign->camp_name, 'archive_site_id' => $campaign->camp_siteId]
|
||||
)->getKey();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get archive coupon key.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
protected static function resolveArchiveCouponId(int $id): int
|
||||
{
|
||||
$archiveCoupon = ArchiveCoupon::query()->find($id);
|
||||
if ($archiveCoupon) {
|
||||
return $archiveCoupon->getKey();
|
||||
}
|
||||
|
||||
$coupon = Coupon::withTrashed()->where('coupon_id', $id)->firstOrFail();
|
||||
|
||||
return ArchiveCoupon::query()->firstOrCreate(
|
||||
['coupon_code' => $id],
|
||||
['name' => $coupon->coupon_code]
|
||||
)->getKey();
|
||||
}
|
||||
|
||||
// * RELATIONS
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue