forked from monicahq/monica
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: change how default module table is populated (monicahq#1934)
- Loading branch information
Showing
4 changed files
with
221 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} | ||
} |