Skip to content

Commit

Permalink
Merge pull request #1397 from bakaphp/cronjob-caddielab
Browse files Browse the repository at this point in the history
Cronjob caddielab
  • Loading branch information
kaioken committed May 29, 2024
2 parents bb15d12 + 04a3c93 commit 58c6fb7
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Console\Commands\Connectors\Notifications;

use Illuminate\Console\Command;
use Kanvas\Apps\Repositories\AppsRepository;
use Kanvas\Connectors\Notifications\Jobs\MailCaddieLabJob;

class MailCaddieLabCommand extends Command
{
protected $signature = 'kanvas:internal-mail-caddie-lab {apps_id}';

public function handle()
{
$this->info('Sending internal mail to Caddie Lab');
MailCaddieLabJob::dispatch(AppsRepository::findFirstByKey($this->argument('apps_id')));
}
}
5 changes: 3 additions & 2 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ class Kernel extends ConsoleKernel
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
*
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command(RunHealthChecksCommand::class)->everyMinute();
$schedule->command(DispatchQueueCheckJobsCommand::class)->everyMinute();
$schedule->command(ScheduleCheckHeartbeatCommand::class)->everyMinute();
/* $schedule->command(MailCaddieLabCommand::class, [getenv('CADDIE_APP_KEY')])
->dailyAt('13:00')
->timezone('America/Santo_Domingo') ; */
}

/**
Expand Down
46 changes: 18 additions & 28 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions database/migrations/Guild/2024_05_28_030618_apps_id_people.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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::table('peoples', function (Blueprint $table) {
//
$table->bigInteger('apps_id')->unsigned()->nullable()->after('companies_id');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('peoples', function (Blueprint $table) {
//
$table->dropColumn('apps_id');
});
}
};
4 changes: 4 additions & 0 deletions routes/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Schedule;
use Illuminate\Support\Facades\Schema;
use Kanvas\Apps\Repositories\AppsRepository;
use Kanvas\Connectors\Jobs\MailCaddieLabJob;

/*
|--------------------------------------------------------------------------
Expand Down
59 changes: 59 additions & 0 deletions src/Domains/Connectors/Notifications/Jobs/MailCaddieLabJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Kanvas\Connectors\Notifications\Jobs;

use Baka\Traits\KanvasJobsTrait;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Notification;
use Kanvas\Apps\Models\Apps;
use Kanvas\Guild\Customers\Repositories\PeoplesRepository;
use Kanvas\Notifications\Templates\Blank;

class MailCaddieLabJob implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use SerializesModels;
use KanvasJobsTrait;
use Queueable;

public function __construct(
public Apps $app
) {
}

public function handle()
{
$peoples = PeoplesRepository::getByDaysCreated(7, $this->app);
$this->sendMails($peoples, 'join-caddie');

$peoples = PeoplesRepository::getByDaysCreated(28, $this->app);
$this->sendMails($peoples, 'join-caddie');
}

public function sendMails(Collection $peoples, string $template)
{
foreach ($peoples as $people) {
$email = $people->emails()->first();
$url = $this->app->get('billing_url') . '/' . '?email=' . $email->value . '&paid=false';
$notification = new Blank(
$template,
['membershipUpgradeUrl' => $url, 'app' => $this->app],
['mail'],
$people
);
$notification->setSubject('Join to Caddie Lab');
echo ' Sending email to ' . $email->value . "\n";
if (! $people->get('paid_subscription')) {
Notification::route('mail', $email->value)->notify($notification);
}
}
}
}
1 change: 1 addition & 0 deletions src/Domains/Guild/Customers/Actions/CreatePeopleAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function execute(): People
$company = $this->peopleData->branch->company()->firstOrFail();

$attributes = [
'apps_id' => $this->peopleData->app->getId(),
'users_id' => $this->peopleData->user->getId(),
'firstname' => $this->peopleData->firstname,
'middlename' => $this->peopleData->middlename,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use Baka\Contracts\CompanyInterface;
use Baka\Traits\SearchableTrait;
use Baka\Users\Contracts\UserInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Kanvas\Apps\Models\Apps;
use Kanvas\Exceptions\ModelNotFoundException as ExceptionsModelNotFoundException;
use Kanvas\Guild\Customers\Enums\ContactTypeEnum;
use Kanvas\Guild\Customers\Models\Contact;
Expand Down Expand Up @@ -51,6 +53,13 @@ public static function getByEmail(string $email, CompanyInterface $company): ?Pe
->first();
}

public static function getByDaysCreated(int $days, Apps $app): Collection
{
return People::whereRaw('DATEDIFF(NOW(), created_at) = ?', [$days])
->where('apps_id', $app->getId())
->get();
}

public static function findByEmailOrCreate(string $email, UserInterface $user, CompanyInterface $company, ?string $name): People
{
$people = self::getByEmail($email, $company);
Expand Down
13 changes: 13 additions & 0 deletions src/Kanvas/Users/Repositories/UsersRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Baka\Users\Contracts\UserInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\DB;
use Kanvas\Apps\Models\Apps;
use Kanvas\Companies\Models\Companies;
use Kanvas\Companies\Models\CompaniesBranches;
Expand Down Expand Up @@ -94,6 +95,17 @@ public static function getUserOfAppById(int $id, ?AppInterface $app = null): Use
->firstOrFail();
}

public static function getUsersByDaysCreated(int $days, ?AppInterface $app = null): Collection
{
$app = $app ?? app(Apps::class);

return Users::join('users_associated_apps', 'users_associated_apps.users_id', '=', 'users.id')
->whereRaw('DATEDIFF(CURDATE(), users_associated_apps.created_at) = ?', [$days])
->select('users.*')
->groupBy('users.id')
->get();
}

/**
* getAll.
* @psalm-suppress MixedReturnStatement
Expand All @@ -104,6 +116,7 @@ public static function getAll(int $companiesId): Collection
->join('users_associated_company', 'users_associated_company.users_id', 'users.id')
->where('users_associated_company.companies_id', $companiesId)
->whereNot('users.id', auth()->user()->id)
->select('users.*')
->get();
}

Expand Down

0 comments on commit 58c6fb7

Please sign in to comment.