Skip to content

Commit 2350448

Browse files
authored
CUR-4163-add background job for publishing (#1325)
* CUR-4163-add background job for publishing * CUR-4163 PR review changes * CUR-4163 PR review changes * CUR-4163 PR review changes * CUR-4163 pr review changes * CUR-4163 pr review changes
1 parent 8dc79b2 commit 2350448

File tree

6 files changed

+258
-45
lines changed

6 files changed

+258
-45
lines changed

app/Http/Controllers/Api/V1/MicroSoftTeamController.php

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use App\Models\Playlist;
2020
use App\Models\Project;
2121
use App\Models\Activity;
22+
use App\Jobs\PublishProject;
2223
use App\User;
2324
use Redirect;
2425

@@ -201,11 +202,12 @@ public function createMsTeamClass(MSTeamCreateClassRequest $createClassRequest)
201202
$authUser = auth()->user();
202203
$token = $authUser->msteam_access_token;
203204
$response = json_decode($this->microsoftTeamRepository->createMsTeamClass($token, $data),true);
204-
205+
205206
if($response['code'] === 202) {
206207
return response([
207208
'message' => 'Class have been created successfully',
208-
'classId' => $response['classId']
209+
'classId' => $response['classId'],
210+
'aSyncUrl'=> $response['aSyncURL'],
209211
], 200);
210212
}
211213

@@ -221,11 +223,11 @@ public function createMsTeamClass(MSTeamCreateClassRequest $createClassRequest)
221223
* Publish the project activities as an assignment
222224
*
223225
* @urlParam Project $project required The Id of a project. Example: 9
224-
* @bodyParam classId required string Id of the class. Example: Test Class
226+
* @bodyParam classId optional string Id of the class. Example: bebe45d4-d0e6-4085-b418-e98a51db70c3
225227
*
226228
* @response 200 {
227229
* "message": [
228-
* "Project has been published successfully."
230+
* "Your request to publish project [project->name] into MS Team has been received and is being processed.<br>You will be alerted in the notification section in the title bar when complete."
229231
* ]
230232
* }
231233
*
@@ -234,40 +236,28 @@ public function createMsTeamClass(MSTeamCreateClassRequest $createClassRequest)
234236
* "Project must be shared as we are temporarily publishing the shared link."
235237
* ]
236238
* }
237-
*
238-
* @response 500 {
239-
* "errors": "MS Team error message",
240-
* "statusCode" : MS team status code
241-
* }
242-
*
243239
* @param MSTeamCreateAssignmentRequest $createAssignmentRequest
244240
* @param Project $project
245241
* @return Response
246242
*/
247243
public function publishProject(MSTeamCreateAssignmentRequest $createAssignmentRequest, Project $project)
248244
{
249-
$createAssignmentRequest->validated();
245+
$data = $createAssignmentRequest->validated();
250246

251247
if(!$project->shared) { // temporary check will remove it in future
252248
return response([
253249
'errors' => 'Project must be shared as we are temporarily publishing the shared link.',
254250
], 500);
255251
}
256-
$authUser = auth()->user();
257-
$token = $authUser->msteam_access_token;
258-
$classId = $createAssignmentRequest->get('classId');
259-
260-
$response = json_decode($this->microsoftTeamRepository->createMSTeamAssignment($token, $classId, $project), true);
261-
262-
if($response['code'] === 201) {
263-
return response([
264-
'message' => 'Project has been published successfully.',
265-
], 200);
266-
}
252+
$classId = isset($data['classId']) ? $data['classId'] : '';
267253

254+
// pushed publishing of project in background
255+
PublishProject::dispatch(auth()->user(), $project, $classId)->delay(now()->addSecond());
256+
268257
return response([
269-
'errors' => $response['message'],
270-
'statusCode' => $response['code']
271-
], 500);
258+
'message' => "Your request to publish project [$project->name] into MS Team has been received and is being processed. <br>
259+
You will be alerted in the notification section in the title bar when complete.",
260+
], 200);
261+
272262
}
273263
}

app/Http/Requests/V1/MSTeamCreateAssignmentRequest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public function authorize()
2424
public function rules()
2525
{
2626
return [
27-
'classId' => 'required|string|max:255'
27+
'classId' => 'UUID|max:255',
28+
2829
];
2930
}
3031
}

app/Jobs/PublishProject.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Contracts\Queue\ShouldQueue;
7+
use Illuminate\Foundation\Bus\Dispatchable;
8+
use Illuminate\Queue\InteractsWithQueue;
9+
use Illuminate\Queue\SerializesModels;
10+
use App\Repositories\MicrosoftTeam\MicrosoftTeamRepositoryInterface;
11+
use App\Models\Project;
12+
use App\User;
13+
use App\Notifications\ProjectPublishNotification;
14+
15+
16+
class PublishProject implements ShouldQueue
17+
{
18+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
19+
20+
/**
21+
* @var User
22+
*/
23+
protected $user;
24+
25+
/**
26+
* @var Project
27+
*/
28+
protected $project;
29+
30+
/**
31+
* @var string
32+
*/
33+
protected $classId;
34+
35+
/**
36+
* @var string
37+
*/
38+
protected $aSyncUrl;
39+
40+
/**
41+
* Create a new job instance.
42+
*
43+
* @return void
44+
*/
45+
public function __construct(User $user, Project $project, $classId)
46+
{
47+
$this->user = $user;
48+
$this->project = $project;
49+
$this->classId = $classId;
50+
$this->aSyncUrl = '';
51+
}
52+
53+
/**
54+
* Execute the job.
55+
*
56+
* @return void
57+
*/
58+
public function handle(MicrosoftTeamRepositoryInterface $microsoftTeamRepository)
59+
{
60+
try {
61+
if(empty($this->classId)) {
62+
$response = $microsoftTeamRepository->createMsTeamClass($this->user->msteam_access_token, ['displayName'=>$this->project->name]);
63+
\Log::info($response);
64+
$class = json_decode($response, true);
65+
$this->classId = $class['classId'];
66+
$this->aSyncUrl = $class['aSyncURL'];
67+
}
68+
69+
$microsoftTeamRepository->createMSTeamAssignment($this->user->msteam_access_token, $this->classId, $this->project, $this->aSyncUrl);
70+
$userName = rtrim($this->user->first_name . ' ' . $this->user->last_name, ' ');
71+
72+
$this->user->notify(new ProjectPublishNotification($userName, $this->project->name));
73+
} catch (\Exception $e) {
74+
\Log::error($e->getMessage());
75+
}
76+
}
77+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace App\Notifications;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Contracts\Queue\ShouldQueue;
7+
use Illuminate\Notifications\Messages\MailMessage;
8+
use Illuminate\Notifications\Notification;
9+
use Illuminate\Support\Facades\Storage;
10+
use Illuminate\Notifications\Messages\BroadcastMessage;
11+
use Carbon\Carbon;
12+
13+
class ProjectPublishNotification extends Notification
14+
{
15+
use Queueable;
16+
17+
/**
18+
* @var string
19+
*/
20+
public $userName;
21+
22+
/**
23+
* @var string
24+
*/
25+
public $projectName;
26+
27+
/**
28+
* Create a new notification instance.
29+
*
30+
*
31+
* @return void
32+
*/
33+
public function __construct($userName, $projectName)
34+
{
35+
$this->userName = $userName;
36+
$this->projectName = $projectName;
37+
38+
}
39+
40+
/**
41+
* Get the notification's delivery channels.
42+
*
43+
* @param mixed $notifiable
44+
* @return array
45+
*/
46+
public function via($notifiable)
47+
{
48+
return [ 'database'];
49+
}
50+
51+
/*
52+
* G*et the array representation of the notification.
53+
*
54+
* @param mixed $notifiable
55+
* @return array
56+
*/
57+
public function toDatabase($notifiable)
58+
{
59+
$message = "Project [$this->projectName] has been published into Microsoft Team successfully.";
60+
61+
return [
62+
'message' => $message,
63+
'project' => $this->projectName,
64+
65+
];
66+
}
67+
68+
69+
70+
/**
71+
* Get the array representation of the notification.
72+
*
73+
* @param mixed $notifiable
74+
* @return array
75+
*/
76+
public function toArray($notifiable)
77+
{
78+
return [
79+
//
80+
];
81+
}
82+
83+
/**
84+
* Return broadcast message type
85+
* @return string
86+
*/
87+
public function broadcastType()
88+
{
89+
return 'Publish Notification';
90+
}
91+
}

0 commit comments

Comments
 (0)