Skip to content

Commit

Permalink
feat: add ability to post comment to project messages (#1322)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss committed Sep 10, 2021
1 parent b9c580e commit f9c57da
Show file tree
Hide file tree
Showing 28 changed files with 1,541 additions and 69 deletions.
21 changes: 21 additions & 0 deletions app/Helpers/LogHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,27 @@ public static function processAuditLog(AuditLog $log): string
]);
break;

case 'project_message_comment_created':
$sentence = trans('account.log_project_message_comment_created', [
'project_id' => $log->object->{'project_id'},
'project_name' => $log->object->{'project_name'},
]);
break;

case 'project_message_comment_updated':
$sentence = trans('account.log_project_message_comment_updated', [
'project_id' => $log->object->{'project_id'},
'project_name' => $log->object->{'project_name'},
]);
break;

case 'project_message_comment_destroyed':
$sentence = trans('account.log_project_message_comment_destroyed', [
'project_id' => $log->object->{'project_id'},
'project_name' => $log->object->{'project_name'},
]);
break;

default:
$sentence = '';
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

namespace App\Http\Controllers\Company\Company\Project;

use App\Helpers\DateHelper;
use App\Helpers\ImageHelper;
use Illuminate\Http\Request;
use App\Helpers\StringHelper;
use App\Helpers\InstanceHelper;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Services\Company\Project\CreateProjectMessageComment;
use App\Services\Company\Project\UpdateProjectMessageComment;
use App\Services\Company\Project\DestroyProjectMessageComment;

class ProjectMessagesCommentController extends Controller
{
/**
* Create the message's comment.
*
* @param Request $request
* @param int $companyId
* @param int $projectId
* @param int $projectMessageId
*
* @return JsonResponse
*/
public function store(Request $request, int $companyId, int $projectId, int $projectMessageId): JsonResponse
{
$loggedEmployee = InstanceHelper::getLoggedEmployee();
$loggedCompany = InstanceHelper::getLoggedCompany();

$data = [
'company_id' => $loggedCompany->id,
'author_id' => $loggedEmployee->id,
'project_id' => $projectId,
'project_message_id' => $projectMessageId,
'content' => $request->input('comment'),
];

$comment = (new CreateProjectMessageComment)->execute($data);

return response()->json([
'data' => [
'id' => $comment->id,
'content' => StringHelper::parse($comment->content),
'content_raw' => $comment->content,
'written_at' => DateHelper::formatShortDateWithTime($comment->created_at),
'author' => [
'id' => $loggedEmployee->id,
'name' => $loggedEmployee->name,
'avatar' => ImageHelper::getAvatar($loggedEmployee, 32),
'url' => route('employees.show', [
'company' => $loggedCompany,
'employee' => $loggedEmployee,
]),
],
'can_edit' => true,
'can_delete' => true,
],
], 201);
}

/**
* Edit a comment.
*
* @param Request $request
* @param int $companyId
* @param int $projectId
* @param int $projectMessageId
* @param int $commentId
*
* @return JsonResponse
*/
public function update(Request $request, int $companyId, int $projectId, int $projectMessageId, int $commentId): JsonResponse
{
$loggedEmployee = InstanceHelper::getLoggedEmployee();
$loggedCompany = InstanceHelper::getLoggedCompany();

$data = [
'company_id' => $loggedCompany->id,
'author_id' => $loggedEmployee->id,
'project_id' => $projectId,
'project_message_id' => $projectMessageId,
'comment_id' => $commentId,
'content' => $request->input('commentEdit'),
];

$comment = (new UpdateProjectMessageComment)->execute($data);

return response()->json([
'data' => [
'id' => $comment->id,
'content' => StringHelper::parse($comment->content),
'content_raw' => $comment->content,
'written_at' => DateHelper::formatShortDateWithTime($comment->created_at),
'author' => [
'id' => $loggedEmployee->id,
'name' => $loggedEmployee->name,
'avatar' => ImageHelper::getAvatar($loggedEmployee, 32),
'url' => route('employees.show', [
'company' => $loggedCompany,
'employee' => $loggedEmployee,
]),
],
'can_edit' => true,
'can_delete' => true,
],
], 200);
}

/**
* Destroy the message.
*
* @param Request $request
* @param int $companyId
* @param int $projectId
* @param int $projectMessageId
* @param int $commentId
*
* @return JsonResponse
*/
public function destroy(Request $request, int $companyId, int $projectId, int $projectMessageId, int $commentId): JsonResponse
{
$loggedEmployee = InstanceHelper::getLoggedEmployee();
$company = InstanceHelper::getLoggedCompany();

$data = [
'company_id' => $company->id,
'author_id' => $loggedEmployee->id,
'project_id' => $projectId,
'project_message_id' => $projectMessageId,
'comment_id' => $commentId,
];

(new DestroyProjectMessageComment)->execute($data);

return response()->json([
'data' => true,
], 200);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,6 @@ public function destroy(Request $request, int $companyId, int $projectId, int $p

return response()->json([
'data' => true,
], 201);
], 200);
}
}
41 changes: 38 additions & 3 deletions app/Http/ViewHelpers/Company/Project/ProjectMessagesViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Carbon\Carbon;
use App\Helpers\DateHelper;
use Illuminate\Support\Str;
use App\Helpers\ImageHelper;
use App\Helpers\StringHelper;
use App\Models\Company\Project;
Expand All @@ -28,7 +27,8 @@ public static function index(Project $project, Employee $employee): Collection
$messages = $project->messages()
->select('id', 'title', 'content', 'created_at', 'author_id')
->with('author')
->orderBy('id')
->with('comments')
->orderBy('id', 'desc')
->get();

$messageReadStatuses = DB::table('project_message_read_status')
Expand All @@ -44,12 +44,14 @@ public static function index(Project $project, Employee $employee): Collection

$author = $message->author;

$commentCount = $message->comments->count();

$messagesCollection->push([
'id' => $message->id,
'title' => $message->title,
'content' => Str::words($message->content, 10, '...'),
'read_status' => $readStatus,
'written_at' => $message->created_at->diffForHumans(),
'comment_count' => $commentCount,
'url' => route('projects.messages.show', [
'company' => $company,
'project' => $project,
Expand Down Expand Up @@ -90,6 +92,38 @@ public static function show(ProjectMessage $projectMessage, Employee $employee):
->first();
}

// get comments
$comments = $projectMessage->comments()->orderBy('created_at', 'asc')->get();
$commentsCollection = collect([]);
foreach ($comments as $comment) {
$canDoActionsAgainstComment = false;

if ($comment->author_id == $employee->id) {
$canDoActionsAgainstComment = true;
}
if ($employee->permission_level <= config('officelife.permission_level.hr')) {
$canDoActionsAgainstComment = true;
}

$commentsCollection->push([
'id' => $comment->id,
'content' => StringHelper::parse($comment->content),
'content_raw' => $comment->content,
'written_at' => DateHelper::formatShortDateWithTime($comment->created_at),
'author' => $comment->author ? [
'id' => $comment->author->id,
'name' => $comment->author->name,
'avatar' => ImageHelper::getAvatar($comment->author, 32),
'url' => route('employees.show', [
'company' => $projectMessage->project->company_id,
'employee' => $comment->author,
]),
] : $comment->author_name,
'can_edit' => $canDoActionsAgainstComment,
'can_delete' => $canDoActionsAgainstComment,
]);
}

return [
'id' => $projectMessage->id,
'title' => $projectMessage->title,
Expand Down Expand Up @@ -117,6 +151,7 @@ public static function show(ProjectMessage $projectMessage, Employee $employee):
'employee' => $author,
]),
] : null,
'comments' => $commentsCollection,
];
}

Expand Down
59 changes: 59 additions & 0 deletions app/Models/Company/Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace App\Models\Company;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Comment extends Model
{
use HasFactory;

protected $table = 'comments';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'company_id',
'author_id',
'author_name',
'content',
'commentable_id',
'commentable_type',
];

/**
* Get the Company record associated with the comment object.
*
* @return BelongsTo
*/
public function company()
{
return $this->belongsTo(Company::class);
}

/**
* Get the parent commentable model.
*
* @return MorphTo
*/
public function commentable()
{
return $this->morphTo();
}

/**
* Get the employee record associated with the comment object.
*
* @return BelongsTo
*/
public function author()
{
return $this->belongsTo(Employee::class, 'author_id');
}
}
10 changes: 10 additions & 0 deletions app/Models/Company/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,16 @@ public function candidates()
return $this->hasMany(Candidate::class);
}

/**
* Get all of the comments made in the company.
*
* @return HasMany
*/
public function comments()
{
return $this->hasMany(Comment::class);
}

/**
* Return the PTO policy for the current year.
*
Expand Down
10 changes: 10 additions & 0 deletions app/Models/Company/Employee.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,16 @@ public function jobOpeningsAsSponsor()
return $this->belongsToMany(JobOpening::class, 'job_opening_sponsor')->withTimestamps();
}

/**
* Get all of the comments written by the employee.
*
* @return hasMany
*/
public function comments()
{
return $this->hasMany(Comment::class, 'author_id');
}

/**
* Scope a query to only include unlocked users.
*
Expand Down
15 changes: 13 additions & 2 deletions app/Models/Company/ProjectMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class ProjectMessage extends Model
Expand All @@ -30,7 +31,7 @@ class ProjectMessage extends Model
];

/**
* Get the project decision record associated with the project decision decision.
* Get the project record associated with the project message.
*
* @return BelongsTo
*/
Expand All @@ -40,12 +41,22 @@ public function project()
}

/**
* Get the employee record associated with the project decision.
* Get the employee record associated with the project message.
*
* @return BelongsTo
*/
public function author()
{
return $this->belongsTo(Employee::class, 'author_id');
}

/**
* Get all of the comments associated with the project message.
*
* @return MorphMany
*/
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
}

0 comments on commit f9c57da

Please sign in to comment.