Skip to content

Commit

Permalink
feat: change how default module table is populated (monicahq#1934)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Oct 23, 2018
1 parent 511b842 commit a46b617
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 54 deletions.
30 changes: 6 additions & 24 deletions app/Models/Account/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use App\Models\Relationship\RelationshipType;
use App\Models\Relationship\RelationshipTypeGroup;
use Illuminate\Database\Eloquent\Relations\HasMany;
use App\Services\Auth\Population\PopulateModulesTable;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Services\Auth\Population\PopulateLifeEventsTable;

Expand Down Expand Up @@ -687,29 +688,6 @@ public function populateRelationshipTypesTable($migrateOnlyNewTypes = false)
}
}

/**
* Populate the account modules table based on the default ones.
*
* @param bool $ignoreTableAlreadyMigrated
* @return void
*/
public function populateModulesTable($ignoreTableAlreadyMigrated = false)
{
$defaultModules = DB::table('default_contact_modules')->get();

foreach ($defaultModules as $defaultModule) {
if (! $ignoreTableAlreadyMigrated || $defaultModule->migrated == 0) {
Module::create([
'account_id' => $this->id,
'key' => $defaultModule->key,
'translation_key' => $defaultModule->translation_key,
'delible' => $defaultModule->delible,
'active' => $defaultModule->active,
]);
}
}
}

/**
* Get the reminders for the month given in parameter.
* - 0 means current month
Expand Down Expand Up @@ -836,14 +814,18 @@ public function populateDefaultFields()
$this->populateDefaultReminderRulesTable();
$this->populateRelationshipTypeGroupsTable();
$this->populateRelationshipTypesTable();
$this->populateModulesTable();
$this->populateChangelogsTable();
$this->populateActivityTypeTable();

(new PopulateLifeEventsTable)->execute([
'account_id' => $this->id,
'migrate_existing_data' => true,
]);

(new PopulateModulesTable)->execute([
'account_id' => $this->id,
'migrate_existing_data' => true,
]);
}

/**
Expand Down
115 changes: 115 additions & 0 deletions app/Services/Auth/Population/PopulateModulesTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace App\Services\Auth\Population;

use App\Models\User\Module;
use App\Services\BaseService;
use App\Models\Account\Account;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\QueryException;
use App\Exceptions\MissingParameterException;

/**
* Populate the modules table for a given account.
*/
class PopulateModulesTable extends BaseService
{
/**
* The structure that the method expects to receive as parameter.
*
* @var array
*/
private $structure = [
'account_id',
'migrate_existing_data',
];

/**
* The data needed for the query to be executed.
*
* @var array
*/
private $data;

/**
* Execute the service.
*
* @param array $data
* @return void
*/
public function execute(array $givenData)
{
$this->data = $givenData;

if (! $this->validateDataStructure($this->data, $this->structure)) {
throw new MissingParameterException('Missing parameters');
}

$this->createEntries();

$this->markTableAsMigrated();
}

/**
* Create modules entries.
*
* @return void
*/
private function createEntries()
{
$defaultModules = $this->getDefaultModules();

foreach ($defaultModules as $defaultModule) {
$this->feedModule($defaultModule);
}
}

/**
* Get the default modules.
*
* @throws QueryException if the query does not run for some reasons.
* @return Collection
*/
private function getDefaultModules()
{
if ($this->data['migrate_existing_data'] == 1) {
$defaultModules = DB::table('default_contact_modules')
->get();
} else {
$defaultModules = DB::table('default_contact_modules')
->where('migrated', 0)
->get();
}

return $defaultModules;
}

/**
* Create an entry in the module table.
*
* @param object $defaultModule
* @return void
*/
private function feedModule($defaultModule)
{
Module::create([
'account_id' => $this->data['account_id'],
'key' => $defaultModule->key,
'translation_key' => $defaultModule->translation_key,
'delible' => $defaultModule->delible,
'active' => $defaultModule->active,
]);
}

/**
* Mark the table as migrated.
*
* @return void
*/
private function markTableAsMigrated()
{
DB::table('default_contact_modules')
->update(['migrated' => 1]);
}
}
30 changes: 0 additions & 30 deletions tests/Unit/Models/AccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -701,36 +701,6 @@ public function test_it_retrieves_yearly_activities_statistics()
);
}

public function test_it_populates_default_account_modules_table_if_tables_havent_been_migrated_yet()
{
$account = factory(Account::class)->create();
DB::table('default_contact_modules')->insert([
'key' => 'work_information',
]);

$account->populateModulesTable();

$this->assertDatabaseHas('modules', [
'key' => 'work_information',
]);
}

public function test_it_skips_default_account_modules_table_for_types_already_migrated()
{
$account = factory(Account::class)->create();
DB::table('default_contact_modules')->insert([
'key' => 'awesome',
'migrated' => 1,
]);

$account->populateModulesTable(true);

$this->assertDatabaseMissing('modules', [
'account_id' => $account->id,
'key' => 'awesome',
]);
}

public function test_it_adds_an_unread_changelog_entry_to_all_users()
{
$account = factory(Account::class)->create();
Expand Down
100 changes: 100 additions & 0 deletions tests/Unit/Services/Auth/PopulateModulesTableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Tests\Unit\Services\Contact\Conversation;

use Tests\TestCase;
use App\Models\User\User;
use App\Models\Account\Account;
use Illuminate\Support\Facades\DB;
use App\Exceptions\MissingParameterException;
use App\Services\Auth\Population\PopulateModulesTable;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class PopulateModulesTableTest extends TestCase
{
use DatabaseTransactions;

public function test_it_fails_if_wrong_parameters_are_given()
{
$request = [
'account_id' => 1,
];

$this->expectException(\Exception::class);

$populateModulesService = new PopulateModulesTable;
$populateModulesService->execute($request);

$request = [
'migrate_existing_data' => false,
];

$this->expectException(MissingParameterException::class);

$populateModulesService = new PopulateModulesTable;
$populateModulesService->execute($request);
}

public function test_it_populate_modules_tables()
{
$account = factory(Account::class)->create([]);
$user = factory(User::class)->create([
'account_id' => $account->id,
]);

DB::table('default_contact_modules')
->where('key', 'work_education')
->update(['migrated' => 0]);

$request = [
'account_id' => $account->id,
'migrate_existing_data' => 1,
];

$populateModulesService = new PopulateModulesTable;
$populateModulesService->execute($request);

// by defauult there is 17 columns in the default table.
// therefore, we need 17 entries for the new account.
$this->assertEquals(
17,
DB::table('modules')->where('account_id', $account->id)->get()->count()
);

// make sure tables have been set to migrated = 1
$this->assertDatabaseMissing('default_contact_modules', [
'migrated' => 0,
]);
}

public function test_it_only_populates_module_tables_partially()
{
$account = factory(Account::class)->create([]);
$user = factory(User::class)->create([
'account_id' => $account->id,
]);

DB::table('default_contact_modules')
->update(['migrated' => 0]);

DB::table('default_contact_modules')
->where('key', 'love_relationships')
->update(['migrated' => 1]);

// we will only migrate the ones that haven't been populated yet
$request = [
'account_id' => $account->id,
'migrate_existing_data' => 0,
];

$populateModulesService = new PopulateModulesTable;
$populateModulesService->execute($request);

// by defauult there is 17 columns in the default table.
// therefore, we need 16 entries for the new account.
$this->assertEquals(
16,
DB::table('modules')->where('account_id', $account->id)->get()->count()
);
}
}

0 comments on commit a46b617

Please sign in to comment.