Commit 049a98f2 authored by Ana Rute Mendes's avatar Ana Rute Mendes 🌈

CCU: handle snipe-IT exceptions and ticket transactions

After creating items in Snipe-It we need to save in the ticket
(maniphest task) the created ids and tags. So instead of receiving only
the ids list, the SnipeIt extension will return transactions or
validation errors.

If it returns an errror, append it to the list. If not, it's
an array of transactions so add them to the results array to update the
purchasing request.
parent 10dfb5e9
......@@ -390,7 +390,7 @@ final class ManiphestTransactionEditor
$is_open = !$object->isClosed();
$asset_id = null;
$asset_transactions = [];
$new_status = null;
foreach ($xactions as $xaction) {
switch ($xaction->getTransactionType()) {
......@@ -398,12 +398,12 @@ final class ManiphestTransactionEditor
$new_status = $xaction->getNewValue();
// CCU: create Item in the inventory system if it's a Purchase request and
// its status was changed to "to release"
// its status was changed to "resolved" (Closed)
if (!$this->getIsNewObject()) {
try {
$asset_id = $snipe_api->createAssetItem($new_status, $object, $xactions);
$asset_transactions = $snipe_api->createAssetItem($new_status, $object, $xactions);
} catch (Exception $e) {
$asset_id = null;
$asset_transactions = [];
}
}
break;
......@@ -603,27 +603,30 @@ final class ManiphestTransactionEditor
}
}
// CCU: if there were an asset created in the process, refer it in the
// purchase request.
if ($asset_id) {
$results[] = id(new ManiphestTransaction())
->setTransactionType(PhabricatorTransactions::TYPE_CUSTOMFIELD)
->attachObject($object)
->attachViewer($this->getActor())
->setMetadataValue('customfield:key', 'std:maniphest:purchasing:asset-id')
->setNewValue($asset_id);
// CCU: if there were assets created in snipe-IT, update the ticket with
// their IDs and tags
if ($asset_transactions) {
if ($asset_transactions instanceof PhabricatorApplicationTransactionValidationError) {
$this->moreValidationErrors[] = $asset_transactions;
} else {
foreach ($asset_transactions as $transaction) {
$results[] = $transaction;
}
}
}
// CCU: if it's a Shipping Request, check if it has an asset item
// in Snipe-IT and automatically fill in some fields.
if ($subtype == 'shipping') {
try {
$transactions = $snipe_api->fetchAssetItem($object, $xactions);
$snipe_api->fetchAssetItem($object, $xactions);
} catch (Exception $e) {
$transactions = [];
}
foreach ($transactions as $transaction) {
$results[] = $transaction;
$this->moreValidationErrors[] = new PhabricatorApplicationTransactionValidationError(
PhabricatorTransactions::TYPE_EDGE,
null,
pht('An error ocurred while editing the item in Snipe-IT, please talk with the tools team.'),
null
);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment