You can generate a job by following command.
php artisan mojura:job [Job] [Module] [Directory] [--force]
php artisan mojura:job LoginUserJob Core
Generated Job file will be at app/Modules/CoreModule/Jobs/LoginUserJob.php
Parameters
-
[Job]: The name of the job class you want to create (required).
-
[Module]: The module where the job class will be created (required).
-
[Directory]: The specific directory within the module where the job class will be placed (optional).
-
[--force]: If set, it will overwrite any existing job class with the same name (optional).
Job Class Implementation
<?php
namespace App\Modules\CoreModule\Jobs;
use InnoAya\Mojura\Core\Job;
use App\Enums\UserStatusEnum;
use App\Exceptions\UnauthorizedException;
use App\Models\User;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Hash;
class LoginUserJob extends Job
{
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(private array $payload)
{
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
try {
$user = User::with('role')
->where('status', UserStatusEnum::ACTIVE->value)
->where(function ($query) {
$query->where('username', $this->payload['identifier'])
->orwhere('email', $this->payload['identifier'])
->orWhere('mobile_number', $this->payload['identifier']);
})
->firstOrFail();
} catch (ModelNotFoundException $_) {
throw new UnauthorizedException('Wrong Credentials');
}
if (Hash::check($this->payload['password'], $user->password)) {
return [
'access_token' => $user->createToken('Authentication Token')->plainTextToken,
'user_data' => $user
];
} else {
throw new UnauthorizedException('Wrong Credentials');
}
}
}
Queue Job Class Implementation
namespace App\Modules\CoreModule\Jobs;
use InnoAya\Mojura\Core\QueueableJob;
class NotifyUserLoginViaEmailJob extends QueueableJob
{
/**
* Create a new queueable job instance.
*
* @return void
*/
public function __construct(private array $payload)
{
}
public function handle(): void
{
// notify to logged in user will be processed in the queue
}
}