Skip to content

CUR-4163-add background job for publishing #1325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 15 additions & 25 deletions app/Http/Controllers/Api/V1/MicroSoftTeamController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use App\Models\Playlist;
use App\Models\Project;
use App\Models\Activity;
use App\Jobs\PublishProject;
use App\User;
use Redirect;

Expand Down Expand Up @@ -201,11 +202,12 @@ public function createMsTeamClass(MSTeamCreateClassRequest $createClassRequest)
$authUser = auth()->user();
$token = $authUser->msteam_access_token;
$response = json_decode($this->microsoftTeamRepository->createMsTeamClass($token, $data),true);

if($response['code'] === 202) {
return response([
'message' => 'Class have been created successfully',
'classId' => $response['classId']
'classId' => $response['classId'],
'aSyncUrl'=> $response['aSyncURL'],
], 200);
}

Expand All @@ -221,11 +223,11 @@ public function createMsTeamClass(MSTeamCreateClassRequest $createClassRequest)
* Publish the project activities as an assignment
*
* @urlParam Project $project required The Id of a project. Example: 9
* @bodyParam classId required string Id of the class. Example: Test Class
* @bodyParam classId optional string Id of the class. Example: bebe45d4-d0e6-4085-b418-e98a51db70c3
*
* @response 200 {
* "message": [
* "Project has been published successfully."
* "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."
* ]
* }
*
Expand All @@ -234,40 +236,28 @@ public function createMsTeamClass(MSTeamCreateClassRequest $createClassRequest)
* "Project must be shared as we are temporarily publishing the shared link."
* ]
* }
*
* @response 500 {
* "errors": "MS Team error message",
* "statusCode" : MS team status code
* }
*
* @param MSTeamCreateAssignmentRequest $createAssignmentRequest
* @param Project $project
* @return Response
*/
public function publishProject(MSTeamCreateAssignmentRequest $createAssignmentRequest, Project $project)
{
$createAssignmentRequest->validated();
$data = $createAssignmentRequest->validated();

if(!$project->shared) { // temporary check will remove it in future
return response([
'errors' => 'Project must be shared as we are temporarily publishing the shared link.',
], 500);
}
$authUser = auth()->user();
$token = $authUser->msteam_access_token;
$classId = $createAssignmentRequest->get('classId');

$response = json_decode($this->microsoftTeamRepository->createMSTeamAssignment($token, $classId, $project), true);

if($response['code'] === 201) {
return response([
'message' => 'Project has been published successfully.',
], 200);
}
$classId = isset($data['classId']) ? $data['classId'] : '';

// pushed publishing of project in background
PublishProject::dispatch(auth()->user(), $project, $classId)->delay(now()->addSecond());

return response([
'errors' => $response['message'],
'statusCode' => $response['code']
], 500);
'message' => "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.",
], 200);

}
}
3 changes: 2 additions & 1 deletion app/Http/Requests/V1/MSTeamCreateAssignmentRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public function authorize()
public function rules()
{
return [
'classId' => 'required|string|max:255'
'classId' => 'UUID|max:255',

];
}
}
77 changes: 77 additions & 0 deletions app/Jobs/PublishProject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?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\Repositories\MicrosoftTeam\MicrosoftTeamRepositoryInterface;
use App\Models\Project;
use App\User;
use App\Notifications\ProjectPublishNotification;


class PublishProject implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* @var User
*/
protected $user;

/**
* @var Project
*/
protected $project;

/**
* @var string
*/
protected $classId;

/**
* @var string
*/
protected $aSyncUrl;

/**
* Create a new job instance.
*
* @return void
*/
public function __construct(User $user, Project $project, $classId)
{
$this->user = $user;
$this->project = $project;
$this->classId = $classId;
$this->aSyncUrl = '';
}

/**
* Execute the job.
*
* @return void
*/
public function handle(MicrosoftTeamRepositoryInterface $microsoftTeamRepository)
{
try {
if(empty($this->classId)) {
$response = $microsoftTeamRepository->createMsTeamClass($this->user->msteam_access_token, ['displayName'=>$this->project->name]);
\Log::info($response);
$class = json_decode($response, true);
$this->classId = $class['classId'];
$this->aSyncUrl = $class['aSyncURL'];
}

$microsoftTeamRepository->createMSTeamAssignment($this->user->msteam_access_token, $this->classId, $this->project, $this->aSyncUrl);
$userName = rtrim($this->user->first_name . ' ' . $this->user->last_name, ' ');

$this->user->notify(new ProjectPublishNotification($userName, $this->project->name));
} catch (\Exception $e) {
\Log::error($e->getMessage());
}
}
}
91 changes: 91 additions & 0 deletions app/Notifications/ProjectPublishNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Storage;
use Illuminate\Notifications\Messages\BroadcastMessage;
use Carbon\Carbon;

class ProjectPublishNotification extends Notification
{
use Queueable;

/**
* @var string
*/
public $userName;

/**
* @var string
*/
public $projectName;

/**
* Create a new notification instance.
*
*
* @return void
*/
public function __construct($userName, $projectName)
{
$this->userName = $userName;
$this->projectName = $projectName;

}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [ 'database'];
}

/*
* G*et the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toDatabase($notifiable)
{
$message = "Project [$this->projectName] has been published into Microsoft Team successfully.";

return [
'message' => $message,
'project' => $this->projectName,

];
}



/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}

/**
* Return broadcast message type
* @return string
*/
public function broadcastType()
{
return 'Publish Notification';
}
}
Loading