Skip to content

Commit

Permalink
feature/historic-update-requests (#254)
Browse files Browse the repository at this point in the history
* Added new export without actioning_user

* Update 2019_09_16_092204_add_historic_update_requests_to_report_types_table.php

* Update ERD.mwb

* Added actioning_user_id column migration

* Updated API docs

* Added logic for storing actioning user

* Added actioning user to report

* Expecting nullable users for update request report
  • Loading branch information
matthew-inamdar authored Sep 16, 2019
1 parent 5a5dcda commit e514e04
Show file tree
Hide file tree
Showing 14 changed files with 353 additions and 25 deletions.
3 changes: 3 additions & 0 deletions app/Docs/Schemas/UpdateRequest/UpdateRequestSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public static function create(string $objectId = null): BaseObject
Schema::string('user_id')
->format(Schema::FORMAT_UUID)
->nullable(),
Schema::string('actioning_user_id')
->format(Schema::FORMAT_UUID)
->nullable(),
Schema::string('updateable_type')
->enum(
UpdateRequest::EXISTING_TYPE_LOCATION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function update(Request $request, UpdateRequest $updateRequest)
}

return DB::transaction(function () use ($request, $updateRequest) {
$updateRequest->apply();
$updateRequest->apply($request->user('api'));

event(EndpointHit::onUpdate($request, "Approved update request [{$updateRequest->id}]", $updateRequest));

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Core/V1/UpdateRequestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function destroy(DestroyRequest $request, UpdateRequest $updateRequest)
return DB::transaction(function () use ($request, $updateRequest) {
event(EndpointHit::onDelete($request, "Deleted update request [{$updateRequest->id}]", $updateRequest));

$updateRequest->delete();
$updateRequest->delete($request->user('api'));

return new ResourceDeleted('update request');
});
Expand Down
1 change: 1 addition & 0 deletions app/Http/Resources/UpdateRequestResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function toArray($request)
return [
'id' => $this->id,
'user_id' => $this->user_id,
'actioning_user_id' => $this->actioning_user_id,
'updateable_type' => $this->updateable_type,
'updateable_id' => $this->updateable_id,
'data' => $this->data,
Expand Down
8 changes: 8 additions & 0 deletions app/Models/Relationships/UpdateRequestRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ public function user()
return $this->belongsTo(User::class)->withTrashed();
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function actioningUser()
{
return $this->belongsTo(User::class, 'actioning_user_id')->withTrashed();
}

/**
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
Expand Down
8 changes: 8 additions & 0 deletions app/Models/Relationships/UserRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public function updateRequests()
return $this->hasMany(UpdateRequest::class);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function actionedUpdateRequests()
{
return $this->hasMany(UpdateRequest::class, 'actioning_user_id');
}

/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
Expand Down
99 changes: 89 additions & 10 deletions app/Models/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ class Report extends Model
* @throws \Exception
* @return \App\Models\Report
*/
public static function generate(ReportType $type, CarbonImmutable $startsAt = null, CarbonImmutable $endsAt = null): self
{
public static function generate(
ReportType $type,
CarbonImmutable $startsAt = null,
CarbonImmutable $endsAt = null
): self {
// Generate the file name.
$filename = sprintf(
'%s_%s_%s.csv',
Expand Down Expand Up @@ -294,8 +297,10 @@ public function generateLocationsExport(): self
* @param \Carbon\CarbonImmutable|null $endsAt
* @return \App\Models\Report
*/
public function generateReferralsExport(CarbonImmutable $startsAt = null, CarbonImmutable $endsAt = null): self
{
public function generateReferralsExport(
CarbonImmutable $startsAt = null,
CarbonImmutable $endsAt = null
): self {
// Update the date range fields if passed.
if ($startsAt && $endsAt) {
$this->update([
Expand Down Expand Up @@ -355,8 +360,10 @@ public function generateReferralsExport(CarbonImmutable $startsAt = null, Carbon
* @param \Carbon\CarbonImmutable|null $endsAt
* @return \App\Models\Report
*/
public function generateFeedbackExport(CarbonImmutable $startsAt = null, CarbonImmutable $endsAt = null): self
{
public function generateFeedbackExport(
CarbonImmutable $startsAt = null,
CarbonImmutable $endsAt = null
): self {
// Update the date range fields if passed.
if ($startsAt && $endsAt) {
$this->update([
Expand Down Expand Up @@ -401,8 +408,10 @@ public function generateFeedbackExport(CarbonImmutable $startsAt = null, CarbonI
* @param \Carbon\CarbonImmutable|null $endsAt
* @return \App\Models\Report
*/
public function generateAuditLogsExport(CarbonImmutable $startsAt = null, CarbonImmutable $endsAt = null): self
{
public function generateAuditLogsExport(
CarbonImmutable $startsAt = null,
CarbonImmutable $endsAt = null
): self {
// Update the date range fields if passed.
if ($startsAt && $endsAt) {
$this->update([
Expand Down Expand Up @@ -454,8 +463,10 @@ public function generateAuditLogsExport(CarbonImmutable $startsAt = null, Carbon
* @param \Carbon\CarbonImmutable|null $endsAt
* @return \App\Models\Report
*/
public function generateSearchHistoriesExport(CarbonImmutable $startsAt = null, CarbonImmutable $endsAt = null): self
{
public function generateSearchHistoriesExport(
CarbonImmutable $startsAt = null,
CarbonImmutable $endsAt = null
): self {
// Update the date range fields if passed.
if ($startsAt && $endsAt) {
$this->update([
Expand Down Expand Up @@ -504,4 +515,72 @@ public function generateSearchHistoriesExport(CarbonImmutable $startsAt = null,

return $this;
}

/**
* @param \Carbon\CarbonImmutable|null $startsAt
* @param \Carbon\CarbonImmutable|null $endsAt
* @return \App\Models\Report
*/
public function generateHistoricUpdateRequestsExport(
CarbonImmutable $startsAt = null,
CarbonImmutable $endsAt = null
): self {
// Update the date range fields if passed.
if ($startsAt && $endsAt) {
$this->update([
'starts_at' => $startsAt,
'ends_at' => $endsAt,
]);
}

$headings = [
'User Submitted',
'Type',
'Entry',
'Date/Time Request Made',
'Approved/Declined',
'Date Actioned',
'Admin who Actioned',
];

$data = [$headings];

UpdateRequest::withTrashed()
->select('*')
->withEntry()
->whereNotNull('approved_at')
->orWhereNotNull('deleted_at')
->when($startsAt && $endsAt, function (Builder $query) use ($startsAt, $endsAt) {
/*
* When date range provided, filter update requests which were created between the
* date range.
*/
$query->whereBetween(
table(UpdateRequest::class, 'created_at'),
[$startsAt, $endsAt]
);
})
->chunk(200, function (Collection $updateRequests) use (&$data) {
// Loop through each update requests in the chunk.
$updateRequests->each(function (UpdateRequest $updateRequest) use (&$data) {
// Append a row to the data array.
$data[] = [
$updateRequest->user->full_name ?? null,
$updateRequest->updateable_type,
$updateRequest->entry,
$updateRequest->created_at->format(CarbonImmutable::ISO8601),
$updateRequest->isApproved() ? 'Approved' : 'Declined',
$updateRequest->isApproved()
? $updateRequest->approved_at->format(CarbonImmutable::ISO8601)
: $updateRequest->declined_at->format(CarbonImmutable::ISO8601),
$updateRequest->actioningUser->full_name ?? null,
];
});
});

// Upload the report.
$this->file->upload(array_to_csv($data));

return $this;
}
}
8 changes: 8 additions & 0 deletions app/Models/ReportType.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,12 @@ public static function searchHistoriesExport(): self
{
return static::where('name', 'Search Histories Export')->firstOrFail();
}

/**
* @return \App\Models\ReportType
*/
public static function historicUpdateRequestsExport(): self
{
return static::where('name', 'Historic Update Requests Export')->firstOrFail();
}
}
38 changes: 36 additions & 2 deletions app/Models/UpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ public function isExisting(): bool
return !$this->isNew();
}

/**
* @return bool
*/
public function isApproved(): bool
{
return $this->approved_at !== null;
}

/**
* @return bool
*/
public function isDeclined(): bool
{
return $this->deleted_at !== null;
}

/**
* @return \Illuminate\Support\MessageBag
*/
Expand All @@ -73,16 +89,34 @@ public function validate(): bool
}

/**
* @param \App\Models\User|null $user
* @return \App\Models\UpdateRequest
*/
public function apply(): self
public function apply(User $user = null): self
{
$this->getUpdateable()->applyUpdateRequest($this);
$this->update(['approved_at' => Date::now()]);
$this->update([
'actioning_user_id' => $user->id ?? null,
'approved_at' => Date::now(),
]);

return $this;
}

/**
* @param \App\Models\User|null $user
* @throws \Exception
* @return bool|null
*/
public function delete(User $user = null)
{
if ($user) {
$this->update(['actioning_user_id' => $user->id]);
}

return parent::delete();
}

/**
* @return \App\UpdateRequest\AppliesUpdateRequests
*/
Expand Down
Binary file modified database/diagrams/ERD.mwb
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;

class AddHistoricUpdateRequestsToReportTypesTable extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
$now = Date::now();

DB::table('report_types')->insert([
'id' => uuid(),
'name' => 'Historic Update Requests Export',
'created_at' => $now,
'updated_at' => $now,
]);
}

/**
* Reverse the migrations.
*/
public function down()
{
$reportType = DB::table('report_types')
->where('name', '=', 'Historic Update Requests Export')
->first();

$fileIds = DB::table('reports')
->where('report_type_id', '=', $reportType->id)
->pluck('file_id')
->toArray();

DB::table('files')
->whereIn('id', $fileIds)
->delete();

DB::table('reports')
->where('report_type_id', '=', $reportType->id)
->delete();

DB::table('report_types')
->where('name', '=', 'Historic Update Requests Export')
->delete();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddActioningUserIdColumnToUpdateRequestsTable extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::table('update_requests', function (Blueprint $table) {
$table->uuid('actioning_user_id')->nullable()->after('user_id');
$table->foreign('actioning_user_id')->references('id')->on('users');
});
}

/**
* Reverse the migrations.
*/
public function down()
{
Schema::table('update_requests', function (Blueprint $table) {
$table->dropForeign(['actioning_user_id']);
$table->dropColumn('actioning_user_id');
});
}
}
Loading

0 comments on commit e514e04

Please sign in to comment.