Skip to content

Commit 2367e44

Browse files
committed
Update
- Fixed : fix #96
1 parent 54cd8d8 commit 2367e44

File tree

7 files changed

+109
-9
lines changed

7 files changed

+109
-9
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\Jobs\ComputeDashboardExpensesJob;
6+
use Illuminate\Broadcasting\Channel;
7+
use Illuminate\Broadcasting\InteractsWithSockets;
8+
use Illuminate\Broadcasting\PresenceChannel;
9+
use Illuminate\Broadcasting\PrivateChannel;
10+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
11+
use Illuminate\Foundation\Events\Dispatchable;
12+
use Illuminate\Queue\SerializesModels;
13+
14+
class ExpenseAfterRefreshEvent
15+
{
16+
use Dispatchable, InteractsWithSockets, SerializesModels;
17+
18+
public $event;
19+
20+
/**
21+
* Create a new event instance.
22+
*
23+
* @return void
24+
*/
25+
public function __construct( $event )
26+
{
27+
$this->event = $event;
28+
}
29+
30+
/**
31+
* Get the channels the event should broadcast on.
32+
*
33+
* @return \Illuminate\Broadcasting\Channel|array
34+
*/
35+
public function broadcastOn()
36+
{
37+
return new PrivateChannel('channel-name');
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\Models\DashboardDay;
6+
use App\Services\DateService;
7+
use Illuminate\Broadcasting\InteractsWithSockets;
8+
use Illuminate\Broadcasting\PrivateChannel;
9+
use Illuminate\Foundation\Events\Dispatchable;
10+
use Illuminate\Queue\SerializesModels;
11+
12+
class ExpenseBeforeRefreshEvent
13+
{
14+
use Dispatchable, InteractsWithSockets, SerializesModels;
15+
16+
public $dashboardDay;
17+
public $date;
18+
19+
/**
20+
* Create a new event instance.
21+
*
22+
* @return void
23+
*/
24+
public function __construct( DashboardDay $dashboardDay, DateService $date )
25+
{
26+
$this->dashboardDay = $dashboardDay;
27+
$this->date = $date;
28+
}
29+
30+
/**
31+
* Get the channels the event should broadcast on.
32+
*
33+
* @return \Illuminate\Broadcasting\Channel|array
34+
*/
35+
public function broadcastOn()
36+
{
37+
return new PrivateChannel('channel-name');
38+
}
39+
}

app/Jobs/ComputeDashboardExpensesJob.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace App\Jobs;
44

55
use App\Events\ExpenseAfterCreateEvent;
6+
use App\Events\ExpenseAfterRefreshEvent;
7+
use App\Events\ExpenseBeforeRefreshEvent;
68
use App\Events\ExpenseHistoryAfterCreatedEvent;
79
use App\Events\ExpenseHistoryBeforeDeleteEvent;
810
use App\Models\DashboardDay;
@@ -82,14 +84,11 @@ public function handle()
8284
* the day the expense has been created
8385
*/
8486
$reports->each( function( $dashboardDay ) use ( &$now ) {
85-
RefreshExpenseJob::dispatch( $dashboardDay )
86-
->delay( $now );
87-
87+
event( new ExpenseBeforeRefreshEvent( $dashboardDay, $now ) );
8888
$now->addMinute();
8989
});
9090

91-
AfterExpenseComputedJob::dispatch( $this->event )
92-
->delay( $now->addSecond( 10 ) );
91+
event( new ExpenseAfterRefreshEvent( $this->event, $now->addSeconds(10 ) ) );
9392
}
9493
}
9594
}

app/Listeners/ExpensesEventSubscriber.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
namespace App\Listeners;
44

55
use App\Events\ExpenseAfterCreateEvent;
6+
use App\Events\ExpenseAfterRefreshEvent;
7+
use App\Events\ExpenseBeforeRefreshEvent;
68
use App\Events\ExpenseHistoryAfterCreatedEvent;
79
use App\Events\ExpenseHistoryBeforeDeleteEvent;
10+
use App\Jobs\AfterExpenseComputedJob;
811
use App\Jobs\ComputeDashboardExpensesJob;
12+
use App\Jobs\RefreshExpenseJob;
913
use App\Services\ExpenseService;
1014

1115
class ExpensesEventSubscriber
@@ -56,5 +60,25 @@ function( $event ) {
5660
ExpenseHistoryBeforeDeleteEvent::class,
5761
fn( $event ) => ComputeDashboardExpensesJob::dispatch( $event )
5862
);
63+
64+
/**
65+
* Will dispatch event for refreshing expenses
66+
* for a specific date
67+
*/
68+
$event->listen(
69+
ExpenseBeforeRefreshEvent::class,
70+
fn( $event ) => RefreshExpenseJob::dispatch( $event->dashboardDay )->delay( $event->date )
71+
);
72+
73+
/**
74+
* Once all expenses has been refreshed
75+
* this job will delete an expense history in
76+
* case the event was made from a deletion action.
77+
*/
78+
$event->listen(
79+
ExpenseAfterRefreshEvent::class,
80+
fn( $event ) => AfterExpenseComputedJob::dispatch( $event->event )
81+
->delay( $event->date )
82+
);
5983
}
6084
}

app/Services/Setup.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,6 @@ public function runMigration( Request $request )
120120
Artisan::call( 'config:cache' );
121121
Artisan::call( 'migrate --path=/database/migrations/default' );
122122
Artisan::call( 'migrate --path=/database/migrations/v1_0' );
123-
Artisan::call( 'migrate --path=/database/migrations/v1_1' );
124-
Artisan::call( 'migrate --path=/database/migrations/v1_2' );
125-
Artisan::call( 'migrate --path=/database/migrations/v1_3' );
126123
Artisan::call( 'vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"' );
127124

128125
/**

database/migrations/v1_3/2020_12_08_221102_dec8_add_columns_to_expenses_history.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ class Dec8AddColumnsToExpensesHistory extends Migration
1515
public function up()
1616
{
1717
Schema::table('nexopos_expenses_history', function (Blueprint $table) {
18-
$table->string( 'status' )->default( ExpenseHistory::STATUS_ACTIVE );
18+
if ( ! Schema::hasColumn( 'nexopos_expenses_history', 'status' ) ) {
19+
$table->string( 'status' )->default( ExpenseHistory::STATUS_ACTIVE );
20+
}
1921
});
2022
}
2123

0 commit comments

Comments
 (0)