Reset availability minutes at the end of the month
This commit is contained in:
parent
b36c886068
commit
464e9b58ee
|
@ -2,10 +2,12 @@
|
|||
|
||||
namespace App\Console;
|
||||
|
||||
use App\Jobs\NotifyUsersManualModeOn;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
||||
|
||||
use App\Jobs\UpdateAvailabilityWithSchedulesJob;
|
||||
use App\Jobs\NotifyUsersManualModeOn;
|
||||
use App\Jobs\ResetAvailabilityMinutes;
|
||||
|
||||
class Kernel extends ConsoleKernel
|
||||
{
|
||||
|
@ -16,6 +18,7 @@ class Kernel extends ConsoleKernel
|
|||
{
|
||||
$schedule->job(new UpdateAvailabilityWithSchedulesJob)->everyThirtyMinutes();
|
||||
$schedule->job(new NotifyUsersManualModeOn)->dailyAt('7:00');
|
||||
$schedule->job(new ResetAvailabilityMinutes)->monthlyOn(1, '0:00');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\AvailabilityMinutesArchive;
|
||||
|
||||
class ResetAvailabilityMinutes implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
//Get users so we can send notifications
|
||||
$users = User::select("id", "availability_minutes")
|
||||
->where("hidden", false)
|
||||
->get();
|
||||
|
||||
//Reset availability minutes
|
||||
User::where("hidden", false)
|
||||
->update(['availability_minutes' => 0]);
|
||||
|
||||
//Archive availability minutes
|
||||
$year = date("Y");
|
||||
$month = date("m");
|
||||
foreach ($users as $user) {
|
||||
$row = new AvailabilityMinutesArchive();
|
||||
$row->availability_minutes = $user->availability_minutes;
|
||||
$row->year = $year;
|
||||
$row->month = $month;
|
||||
$row->user()->associate($user->id);
|
||||
$row->save();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class AvailabilityMinutesArchive extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'availability_minutes',
|
||||
'year',
|
||||
'month'
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the user.
|
||||
*/
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('availability_minutes_archives', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->integer('availability_minutes');
|
||||
$table->integer('year');
|
||||
$table->integer('month');
|
||||
$table->unsignedBigInteger('user_id')->unsigned();
|
||||
$table->foreign('user_id')->references('id')->on('users')->cascadeOnDelete();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('availability_minutes_archives');
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue