Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
SnipeIt
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OskarStenberg
SnipeIt
Commits
868419b3
Commit
868419b3
authored
4 years ago
by
snipe
Browse files
Options
Downloads
Patches
Plain Diff
Components checkin/checkout via API
Signed-off-by:
snipe
<
snipe@snipe.net
>
parent
df7e0e56
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
app/Http/Controllers/Api/ComponentsController.php
+118
-0
118 additions, 0 deletions
app/Http/Controllers/Api/ComponentsController.php
routes/api.php
+15
-0
15 additions, 0 deletions
routes/api.php
with
133 additions
and
0 deletions
app/Http/Controllers/Api/ComponentsController.php
+
118
−
0
View file @
868419b3
...
...
@@ -8,6 +8,9 @@ use App\Http\Transformers\ComponentsTransformer;
use
App\Models\Company
;
use
App\Models\Component
;
use
Illuminate\Http\Request
;
use
App\Events\CheckoutableCheckedIn
;
use
App\Events\ComponentCheckedIn
;
use
App\Models\Asset
;
class
ComponentsController
extends
Controller
{
...
...
@@ -172,4 +175,119 @@ class ComponentsController extends Controller
$assets
=
$assets
->
skip
(
$offset
)
->
take
(
$limit
)
->
get
();
return
(
new
ComponentsTransformer
)
->
transformCheckedoutComponents
(
$assets
,
$total
);
}
/**
* Validate and checkout the component.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* t
* @since [v5.1.8]
* @param Request $request
* @param int $componentId
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public
function
checkout
(
Request
$request
,
$componentId
)
{
// Check if the component exists
if
(
is_null
(
$component
=
Component
::
find
(
$componentId
)))
{
return
response
()
->
json
(
Helper
::
formatStandardApiResponse
(
'error'
,
null
,
trans
(
'admin/components/message.does_not_exist'
)));
}
$this
->
authorize
(
'checkout'
,
$component
);
if
(
$component
->
numRemaining
()
>
$request
->
get
(
'assigned_qty'
))
{
if
(
!
$asset
=
Asset
::
find
(
$request
->
input
(
'assigned_to'
)))
{
return
response
()
->
json
(
Helper
::
formatStandardApiResponse
(
'error'
,
null
,
trans
(
'admin/hardware/message.does_not_exist'
)));
}
// Update the accessory data
$component
->
assigned_to
=
$request
->
input
(
'assigned_to'
);
$component
->
assets
()
->
attach
(
$component
->
id
,
[
'component_id'
=>
$component
->
id
,
'created_at'
=>
\Carbon
::
now
(),
'assigned_qty'
=>
$request
->
get
(
'assigned_qty'
),
'user_id'
=>
\Auth
::
id
(),
'asset_id'
=>
$request
->
get
(
'assigned_to'
)
]);
$component
->
logCheckout
(
$request
->
input
(
'note'
),
$asset
);
return
response
()
->
json
(
Helper
::
formatStandardApiResponse
(
'success'
,
null
,
trans
(
'admin/components/message.checkout.success'
)));
}
return
response
()
->
json
(
Helper
::
formatStandardApiResponse
(
'error'
,
null
,
'Not enough components remaining: '
.
$component
->
numRemaining
()
.
' remaining, '
.
$request
->
get
(
'assigned_qty'
)
.
' requested.'
));
}
/**
* Validate and store checkin data.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v5.1.8]
* @param Request $request
* @param $component_asset_id
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public
function
checkin
(
Request
$request
,
$component_asset_id
)
{
if
(
$component_assets
=
\DB
::
table
(
'components_assets'
)
->
find
(
$component_asset_id
))
{
if
(
is_null
(
$component
=
Component
::
find
(
$component_assets
->
component_id
)))
{
return
response
()
->
json
(
Helper
::
formatStandardApiResponse
(
'error'
,
null
,
trans
(
'admin/components/message.not_found'
)));
}
$this
->
authorize
(
'checkin'
,
$component
);
$max_to_checkin
=
$component_assets
->
assigned_qty
;
if
(
$max_to_checkin
>
1
)
{
$validator
=
\Validator
::
make
(
$request
->
all
(),
[
"checkin_qty"
=>
"required|numeric|between:1,
$max_to_checkin
"
]);
if
(
$validator
->
fails
())
{
return
response
()
->
json
(
Helper
::
formatStandardApiResponse
(
'error'
,
null
,
'Checkin quantity must be between 1 and '
.
$max_to_checkin
));
}
}
// Validation passed, so let's figure out what we have to do here.
$qty_remaining_in_checkout
=
(
$component_assets
->
assigned_qty
-
(
int
)
$request
->
input
(
'checkin_qty'
,
1
));
// We have to modify the record to reflect the new qty that's
// actually checked out.
$component_assets
->
assigned_qty
=
$qty_remaining_in_checkout
;
\Log
::
debug
(
$component_asset_id
.
' - '
.
$qty_remaining_in_checkout
.
' remaining in record '
.
$component_assets
->
id
);
\DB
::
table
(
'components_assets'
)
->
where
(
'id'
,
$component_asset_id
)
->
update
([
'assigned_qty'
=>
$qty_remaining_in_checkout
]);
// If the checked-in qty is exactly the same as the assigned_qty,
// we can simply delete the associated components_assets record
if
(
$qty_remaining_in_checkout
==
0
)
{
\DB
::
table
(
'components_assets'
)
->
where
(
'id'
,
'='
,
$component_asset_id
)
->
delete
();
}
$asset
=
Asset
::
find
(
$component_assets
->
asset_id
);
event
(
new
CheckoutableCheckedIn
(
$component
,
$asset
,
\Auth
::
user
(),
$request
->
input
(
'note'
),
\Carbon
::
now
()));
return
response
()
->
json
(
Helper
::
formatStandardApiResponse
(
'success'
,
null
,
trans
(
'admin/components/message.checkin.success'
)));
}
return
response
()
->
json
(
Helper
::
formatStandardApiResponse
(
'error'
,
null
,
'No matching checkouts for that component join record'
));
}
}
This diff is collapsed.
Click to expand it.
routes/api.php
+
15
−
0
View file @
868419b3
...
...
@@ -220,6 +220,21 @@ Route::group(['prefix' => 'v1','namespace' => 'Api', 'middleware' => 'auth:api']
'uses'
=>
'ComponentsController@getAssets'
,
]
);
Route
::
post
(
'{component}/checkout'
,
[
'as'
=>
'api.components.checkout'
,
'uses'
=>
'ComponentsController@checkout'
,
]
);
Route
::
post
(
'{component}/checkin'
,
[
'as'
=>
'api.components.checkin'
,
'uses'
=>
'ComponentsController@checkin'
,
]
);
});
// Components group
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment