Part of new API, cleanup code.

This commit is contained in:
James Cole 2024-11-23 16:14:47 +01:00
parent f3a20e14a6
commit fa655f065b
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
5 changed files with 59 additions and 23 deletions

View File

@ -48,8 +48,19 @@ class StoreRequest extends FormRequest
public function rules(): array public function rules(): array
{ {
$roles = [];
foreach(UserRoleEnum::cases() as $role) {
$roles[] = $role->value;
}
$string = implode(',', $roles);
return [ return [
'title' => 'unique:user_groups,title|required|min:1|max:255', 'title' => 'unique:user_groups,title|required|min:1|max:255',
'members' => 'required|min:1',
'members.*.user_email' => 'email|missing_with:members.*.user_id',
'members.*.user_id' => 'integer|exists:users,id|missing_with:members.*.user_email',
'members.*.roles' => 'required|array|min:1',
'members.*.roles.*' => sprintf('required|in:%s',$string),
]; ];
} }
} }

View File

@ -26,6 +26,7 @@ namespace FireflyIII\Services\Internal\Update;
use Carbon\Carbon; use Carbon\Carbon;
use Carbon\Exceptions\InvalidDateException; use Carbon\Exceptions\InvalidDateException;
use Carbon\Exceptions\InvalidFormatException; use Carbon\Exceptions\InvalidFormatException;
use FireflyIII\Enums\TransactionTypeEnum;
use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Events\TriggeredAuditLog;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\TagFactory; use FireflyIII\Factory\TagFactory;
@ -720,15 +721,15 @@ class JournalUpdateService
// if the transaction is a TRANSFER, and the foreign amount and currency are set (like they seem to be) // if the transaction is a TRANSFER, and the foreign amount and currency are set (like they seem to be)
// the correct fields to update in the destination transaction are NOT the foreign amount and currency // the correct fields to update in the destination transaction are NOT the foreign amount and currency
// but rather the normal amount and currency. This is new behavior. // but rather the normal amount and currency. This is new behavior.
$isTransfer = TransactionTypeEnum::TRANSFER->value === $this->transactionJournal->transactionType->type;
if (TransactionType::TRANSFER === $this->transactionJournal->transactionType->type) { if ($isTransfer) {
Log::debug('Switch amounts, store in amount and not foreign_amount'); Log::debug('Switch amounts, store in amount and not foreign_amount');
$dest->transaction_currency_id = $foreignCurrency->id; $dest->transaction_currency_id = $foreignCurrency->id;
$dest->amount = app('steam')->positive($foreignAmount); $dest->amount = app('steam')->positive($foreignAmount);
$dest->foreign_amount = app('steam')->positive($source->amount); $dest->foreign_amount = app('steam')->positive($source->amount);
$dest->foreign_currency_id = $source->transaction_currency_id; $dest->foreign_currency_id = $source->transaction_currency_id;
} }
if (TransactionType::TRANSFER !== $this->transactionJournal->transactionType->type) { if (!$isTransfer) {
$dest->foreign_currency_id = $foreignCurrency->id; $dest->foreign_currency_id = $foreignCurrency->id;
$dest->foreign_amount = app('steam')->positive($foreignAmount); $dest->foreign_amount = app('steam')->positive($foreignAmount);
} }

View File

@ -73,6 +73,7 @@ class UserGroupTransformer extends AbstractTransformer
} }
} }
} }
$this->mergeMemberships();
} }
return $objects; return $objects;
@ -90,8 +91,28 @@ class UserGroupTransformer extends AbstractTransformer
'in_use' => $this->inUse[$userGroup->id] ?? false, 'in_use' => $this->inUse[$userGroup->id] ?? false,
'title' => $userGroup->title, 'title' => $userGroup->title,
'can_see_members' => $this->membershipsVisible[$userGroup->id] ?? false, 'can_see_members' => $this->membershipsVisible[$userGroup->id] ?? false,
'members' => $this->memberships[$userGroup->id] ?? [], 'members' => array_values($this->memberships[$userGroup->id] ?? []),
]; ];
// if the user has a specific role in this group, then collect the memberships. // if the user has a specific role in this group, then collect the memberships.
} }
private function mergeMemberships(): void
{
$new = [];
foreach ($this->memberships as $groupId => $members) {
$new[$groupId] ??= [];
foreach ($members as $member) {
$mail = $member['user_email'];
$new[$groupId][$mail] ??= [
'user_id' => $member['user_id'],
'user_email' => $member['user_email'],
'you' => $member['you'],
'roles' => [],
];
$new[$groupId][$mail]['roles'][] = $member['role'];
}
}
$this->memberships = $new;
}
} }

View File

@ -262,6 +262,7 @@ return [
'gte.file' => 'The :attribute must be greater than or equal to :value kilobytes.', 'gte.file' => 'The :attribute must be greater than or equal to :value kilobytes.',
'gte.string' => 'The :attribute must be greater than or equal to :value characters.', 'gte.string' => 'The :attribute must be greater than or equal to :value characters.',
'gte.array' => 'The :attribute must have :value items or more.', 'gte.array' => 'The :attribute must have :value items or more.',
'missing_with' => 'The :attribute cannot be combined with another field.',
'amount_required_for_auto_budget' => 'The amount is required.', 'amount_required_for_auto_budget' => 'The amount is required.',
'auto_budget_amount_positive' => 'The amount must be more than zero.', 'auto_budget_amount_positive' => 'The amount must be more than zero.',

View File

@ -53,6 +53,24 @@ Route::group(
} }
); );
// USER GROUP ROUTES
Route::group(
[
'namespace' => 'FireflyIII\Api\V2\Controllers\UserGroup',
'prefix' => 'v2/user-groups',
'as' => 'api.v2.user-groups.',
],
static function (): void {
Route::get('', ['uses' => 'IndexController@index', 'as' => 'index']);
Route::post('', ['uses' => 'StoreController@store', 'as' => 'store']);
Route::get('{userGroup}', ['uses' => 'ShowController@show', 'as' => 'show']);
// Route::put('{userGroup}', ['uses' => 'UpdateController@update', 'as' => 'update']);
// Route::post('{userGroup}/use', ['uses' => 'UpdateController@useUserGroup', 'as' => 'use']);
// Route::put('{userGroup}/update-membership', ['uses' => 'UpdateController@updateMembership', 'as' => 'updateMembership']);
// Route::delete('{userGroup}', ['uses' => 'DestroyController@destroy', 'as' => 'destroy']);
}
);
// CHART ROUTES // CHART ROUTES
Route::group( Route::group(
[ [
@ -221,23 +239,7 @@ Route::group(
} }
); );
// V2 API route for user groups (administrations).
Route::group(
[
'namespace' => 'FireflyIII\Api\V2\Controllers\UserGroup',
'prefix' => 'v2/user-groups',
'as' => 'api.v2.user-groups.',
],
static function (): void {
// Route::get('', ['uses' => 'IndexController@index', 'as' => 'index']);
// Route::post('', ['uses' => 'StoreController@store', 'as' => 'store']);
// Route::get('{userGroup}', ['uses' => 'ShowController@show', 'as' => 'show']);
// Route::put('{userGroup}', ['uses' => 'UpdateController@update', 'as' => 'update']);
// Route::post('{userGroup}/use', ['uses' => 'UpdateController@useUserGroup', 'as' => 'use']);
// Route::put('{userGroup}/update-membership', ['uses' => 'UpdateController@updateMembership', 'as' => 'updateMembership']);
// Route::delete('{userGroup}', ['uses' => 'DestroyController@destroy', 'as' => 'destroy']);
}
);
// V2 JSON API ROUTES // V2 JSON API ROUTES
// JsonApiRoute::server('v2')->prefix('v2') // JsonApiRoute::server('v2')->prefix('v2')