Skip to content

Commit

Permalink
handle reorder errors gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
nateiler committed Jun 10, 2019
1 parent f66f86b commit f977027
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 28 deletions.
31 changes: 25 additions & 6 deletions src/records/OrganizationTypeAssociation.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use Craft;
use flipbox\craft\ember\records\ActiveRecord;
use flipbox\craft\ember\records\SortableTrait;
use flipbox\organizations\Organizations;
use flipbox\organizations\queries\OrganizationTypeAssociationQuery;
use yii\helpers\Json;

/**
* @author Flipbox Factory <[email protected]>
Expand Down Expand Up @@ -103,12 +105,29 @@ public function beforeSave($insert)
*/
public function afterSave($insert, $changedAttributes)
{
$this->autoReOrder(
'typeId',
[
'organizationId' => $this->organizationId
]
);
try {
$this->autoReOrder(
'typeId',
[
'organizationId' => $this->organizationId
]
);
} catch (\Exception $e) {
Organizations::error(
sprintf(
"Exception caught while trying to reorder '%s'. Exception: [%s].",
(string)get_class($this),
(string)Json::encode([
'Trace' => $e->getTraceAsString(),
'File' => $e->getFile(),
'Line' => $e->getLine(),
'Code' => $e->getCode(),
'Message' => $e->getMessage()
])
),
__METHOD__
);
}

parent::afterSave($insert, $changedAttributes);
}
Expand Down
50 changes: 34 additions & 16 deletions src/records/UserAssociation.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use flipbox\organizations\Organizations;
use flipbox\organizations\queries\UserAssociationQuery;
use yii\db\ActiveQueryInterface;
use yii\helpers\Json;

/**
* @author Flipbox Factory <[email protected]>
Expand Down Expand Up @@ -182,23 +183,40 @@ public function beforeSave($insert)
*/
public function afterSave($insert, $changedAttributes)
{
if (Organizations::getInstance()->getSettings()->getEnforceUserSortOrder()) {
$this->autoReOrder(
'userId',
[
'organizationId' => $this->organizationId
],
'userOrder'
);
}
try {
if (Organizations::getInstance()->getSettings()->getEnforceUserSortOrder()) {
$this->autoReOrder(
'userId',
[
'organizationId' => $this->organizationId
],
'userOrder'
);
}

if (Organizations::getInstance()->getSettings()->getEnforceOrganizationSortOrder()) {
$this->autoReOrder(
'organizationId',
[
'userId' => $this->userId
],
'organizationOrder'
if (Organizations::getInstance()->getSettings()->getEnforceOrganizationSortOrder()) {
$this->autoReOrder(
'organizationId',
[
'userId' => $this->userId
],
'organizationOrder'
);
}
} catch (\Exception $e) {
Organizations::error(
sprintf(
"Exception caught while trying to reorder '%s'. Exception: [%s].",
(string)get_class($this),
(string)Json::encode([
'Trace' => $e->getTraceAsString(),
'File' => $e->getFile(),
'Line' => $e->getLine(),
'Code' => $e->getCode(),
'Message' => $e->getMessage()
])
),
__METHOD__
);
}

Expand Down
31 changes: 25 additions & 6 deletions src/records/UserTypeAssociation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
use flipbox\craft\ember\helpers\ModelHelper;
use flipbox\craft\ember\records\ActiveRecord;
use flipbox\craft\ember\records\SortableTrait;
use flipbox\organizations\Organizations;
use flipbox\organizations\queries\UserTypeAssociationQuery;
use yii\db\ActiveQueryInterface;
use yii\helpers\Json;

/**
* @author Flipbox Factory <[email protected]>
Expand Down Expand Up @@ -103,12 +105,29 @@ public function beforeSave($insert)
*/
public function afterSave($insert, $changedAttributes)
{
$this->autoReOrder(
'typeId',
[
'userId' => $this->userId
]
);
try {
$this->autoReOrder(
'typeId',
[
'userId' => $this->userId
]
);
} catch (\Exception $e) {
Organizations::error(
sprintf(
"Exception caught while trying to reorder '%s'. Exception: [%s].",
(string)get_class($this),
(string)Json::encode([
'Trace' => $e->getTraceAsString(),
'File' => $e->getFile(),
'Line' => $e->getLine(),
'Code' => $e->getCode(),
'Message' => $e->getMessage()
])
),
__METHOD__
);
}

parent::afterSave($insert, $changedAttributes);
}
Expand Down

0 comments on commit f977027

Please sign in to comment.