Fixed some tests.

This commit is contained in:
James Cole
2015-05-08 07:27:29 +02:00
parent cc01592085
commit cefbbcd1df
4 changed files with 153 additions and 6 deletions

View File

@@ -60,6 +60,31 @@ class Account extends Model
}
/**
* @param array $fields
*
* @return Account|null
*/
public static function firstOrNullEncrypted(array $fields)
{
// everything but the name:
$query = Account::orderBy('id');
foreach ($fields as $name => $value) {
if ($name != 'name') {
$query->where($name, $value);
}
}
$set = $query->get(['accounts.*']);
/** @var Account $account */
foreach ($set as $account) {
if ($account->name == $fields['name']) {
return $account;
}
}
return null;
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/

View File

@@ -443,11 +443,15 @@ class AccountRepository implements AccountRepositoryInterface
if (!$newAccount->isValid()) {
// does the account already exist?
$existingAccount = Account::where('user_id', $data['user'])->where('account_type_id', $accountType->id)->where('name', $data['name'])->first();
$searchData = [
'user_id' => $data['user'],
'account_type_id' => $accountType->id,
'name' => $data['name']
];
$existingAccount = Account::firstOrNullEncrypted($searchData);
if (!$existingAccount) {
Log::error('Account create error: ' . $newAccount->getErrors()->toJson());
App::abort(500);
}
$newAccount = $existingAccount;
}