diff --git a/app/Factory/TagFactory.php b/app/Factory/TagFactory.php new file mode 100644 index 0000000000..4cfe38e9b7 --- /dev/null +++ b/app/Factory/TagFactory.php @@ -0,0 +1,72 @@ +. + */ + +declare(strict_types=1); + +namespace FireflyIII\Factory; + +use FireflyIII\Models\Tag; +use FireflyIII\User; + +/** + * Class TagFactory + */ +class TagFactory +{ + /** @var User */ + private $user; + + /** + * TagFactory constructor. + */ + public function __construct() + { + + } + + /** + * @param User $user + */ + public function setUser(User $user): void + { + $this->user = $user; + } + + /** + * @param array $data + * + * @return Tag|null + */ + public function create(array $data): ?Tag + { + return Tag::create( + [ + 'user_id' => $data['user']->id, + 'tag' => $data['tag'], + 'tagMode' => 'nothing', + 'date' => $data['date'], + 'description'=> $data['description'], + + ] + ); + } + +} \ No newline at end of file diff --git a/app/Factory/TransactionJournalFactory.php b/app/Factory/TransactionJournalFactory.php index e60ac8cb9d..983e71d20b 100644 --- a/app/Factory/TransactionJournalFactory.php +++ b/app/Factory/TransactionJournalFactory.php @@ -23,7 +23,6 @@ declare(strict_types=1); namespace FireflyIII\Factory; -use FireflyIII\Events\StoredTransactionJournal; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Bill; use FireflyIII\Models\PiggyBank; @@ -74,8 +73,8 @@ class TransactionJournalFactory public function create(array $data): TransactionJournal { $type = $this->findTransactionType($data['type']); - $bill = $this->findBill($data['bill_id'], $data['bill_name']); - $piggyBank = $this->findPiggyBank($data['piggy_bank_id'], $data['piggy_bank_name']); + //$bill = $this->findBill($data['bill_id'], $data['bill_name']); + //$piggyBank = $this->findPiggyBank($data['piggy_bank_id'], $data['piggy_bank_name']); $defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user); $values = [ 'user_id' => $data['user'], @@ -91,10 +90,6 @@ class TransactionJournalFactory $journal = $this->repository->storeBasic($values); - // todo link other stuff to journal (meta-data etc). tags - // todo - // start creating transactions: - $factory = app(TransactionFactory::class); $factory->setUser($this->user); @@ -112,6 +107,24 @@ class TransactionJournalFactory $factory->create($journal, $piggyBank); } + // store tags and link them: + /** @var TagFactory $factory */ + $factory = app(TagFactory::class); + $factory->setUser($journal->user); + foreach ($data['tags'] as $string) { + $tagData = [ + 'tag' => $string, + 'date' => null, + 'description' => null, + 'latitude' => null, + 'longitude' => null, + 'zoom_level' => null, + 'user' => $journal->user, + ]; + $tag = $factory->create($tagData); + $this->repository->addTag($journal, $tag); + } + return $journal; } diff --git a/tests/Api/V1/Controllers/TransactionControllerTest.php b/tests/Api/V1/Controllers/TransactionControllerTest.php index 6d7bca988e..dfc2d09b34 100644 --- a/tests/Api/V1/Controllers/TransactionControllerTest.php +++ b/tests/Api/V1/Controllers/TransactionControllerTest.php @@ -546,4 +546,62 @@ class TransactionControllerTest extends TestCase ); } + /** + * Submit the minimum amount of data required to create a withdrawal. + * Add some tags as well. Expect to see them in the result. + * + * @covers \FireflyIII\Api\V1\Controllers\TransactionController::store + */ + public function testSuccessStoreTags() + { + $tags = [ + 'TagOne' . rand(1, 1000), + 'TagTwoBlarg' . rand(1, 1000), + 'SomeThreeTag' . rand(1, 1000), + ]; + $account = auth()->user()->accounts()->where('account_type_id', 3)->first(); + $data = [ + 'description' => 'Some transaction #' . rand(1, 1000), + 'date' => '2018-01-01', + 'type' => 'withdrawal', + 'tags' => join(',', $tags), + 'transactions' => [ + [ + 'amount' => '10', + 'currency_id' => 1, + 'source_id' => $account->id, + ], + + + ], + ]; + + // test API + $response = $this->post('/api/v1/transactions?include=tags', $data, ['Accept' => 'application/json']); + $response->assertStatus(200); + foreach ($tags as $tag) { + $response->assertSee($tag); + } + $response->assertJson( + [ + 'data' => [ + 'type' => 'transactions', + 'attributes' => [ + 'description' => $data['description'], + 'date' => $data['date'], + 'source_id' => $account->id, + 'source_name' => $account->name, + 'type' => 'Withdrawal', + 'source_type' => 'Asset account', + 'destination_name' => 'Cash account', + 'destination_type' => 'Cash account', + 'amount' => -10, + ], + 'links' => true, + 'includes' => [], + ], + ] + ); + } + } \ No newline at end of file