Skip to content

WIP for #16883 - bulk audit via API for RFID #16904

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

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
53 changes: 41 additions & 12 deletions app/Http/Controllers/Api/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1072,24 +1072,40 @@ public function checkinByTag(Request $request, $tag = null): JsonResponse
* @since [v4.0]
*/
public function audit(Request $request, Asset $asset): JsonResponse

{

$this->authorize('audit', Asset::class);

$settings = Setting::getSettings();
$dt = Carbon::now()->addMonths($settings->audit_interval)->toDateString();

// Allow the asset tag to be passed in the payload (legacy method)

// Allow the asset tag to be passed in the form payload
if ($request->filled('asset_tag')) {
$asset = Asset::where('asset_tag', '=', $request->input('asset_tag'))->first();

// Check if it's an array or a string
if (is_array($request->input('asset_tag'))) {
$asset_tag = $request->input('asset_tag');
$multi_audit = true;
\Log::error('Multi-audit');
} else {
// If it's a string, make it into an array so we can use it in the whereIn query
$asset_tag = [$request->input('asset_tag')];
$multi_audit = false;
\Log::error('Single audit');
}

$assets = Asset::whereIn('asset_tag', $asset_tag)->get();
}

if ($asset) {
foreach ($assets as $asset) {

$originalValues = $asset->getRawOriginal();

$asset->next_audit_date = $dt;
$asset->last_audit_date = date('Y-m-d H:i:s');

// Overwrite next_audit_date if it was specified in the request
if ($request->filled('next_audit_date')) {
$asset->next_audit_date = $request->input('next_audit_date');
}
Expand All @@ -1100,15 +1116,25 @@ public function audit(Request $request, Asset $asset): JsonResponse
$asset->location_id = $request->input('location_id');
}

$asset->last_audit_date = date('Y-m-d H:i:s');

// Set up the payload for re-display in the API response
$payload = [
'id' => $asset->id,
'asset_tag' => $asset->asset_tag,
'note' => $request->input('note'),
'next_audit_date' => Helper::getFormattedDateObject($asset->next_audit_date),
];

if ($multi_audit === true) {
$payload = [
'id' => $asset->id,
'asset_tag' => $asset->asset_tag,
'note' => $request->input('note'),
'next_audit_date' => Helper::getFormattedDateObject($asset->next_audit_date),
];
} else {
$payload[] = [
'id' => $asset->id,
'asset_tag' => $asset->asset_tag,
'note' => $request->input('note'),
'next_audit_date' => Helper::getFormattedDateObject($asset->next_audit_date),
];
}



/**
Expand Down Expand Up @@ -1176,11 +1202,14 @@ public function audit(Request $request, Asset $asset): JsonResponse
*/
if ($asset->isValid() && $asset->save()) {
$asset->logAudit(request('note'), request('location_id'), null, $originalValues);
return response()->json(Helper::formatStandardApiResponse('success', $payload, trans('admin/hardware/message.audit.success')));
}

}

if (count($payload) > 0) {
return response()->json(Helper::formatStandardApiResponse('success', $payload, trans('admin/hardware/message.audit.success')));
}


// No matching asset for the asset tag that was passed.
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist')), 200);
Expand Down
2 changes: 1 addition & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@
->where(['action' => 'audit|audits|checkins', 'upcoming_status' => 'due|overdue|due-or-overdue']);


// Legacy URL for audit
// Bulk audit for RFID, also works as a legacy endpoint
Route::post('audit',
[
Api\AssetsController::class,
Expand Down
30 changes: 30 additions & 0 deletions tests/Feature/Assets/Api/AuditAssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,36 @@ public function testLegacyAssetAuditIsSaved()
}


public function testAssetAuditWithTagsArrayIsSaved()
{
$asset1 = Asset::factory()->create();
$asset2 = Asset::factory()->create();
$asset3 = Asset::factory()->create();

$this->actingAsForApi(User::factory()->auditAssets()->create())
->postJson(route('api.asset.audit.legacy'), [
'asset_tag' => [
$asset1->asset_tag,
$asset2->asset_tag,
$asset3->asset_tag
],
'note' => 'test',
])
->assertStatusMessageIs('success')
->assertJson(
[
'messages' =>trans('admin/hardware/message.audit.success'),
'payload' => [
'id' => $asset1->id,
'asset_tag' => $asset1->asset_tag,
'note' => 'test'
],
])
->assertStatus(200);

}


public function testAssetAuditIsSaved()
{
$asset = Asset::factory()->create();
Expand Down
Loading