Skip to content

Commit ae521f5

Browse files
authored
Refactor relationships (#971)
1 parent aa869d5 commit ae521f5

File tree

95 files changed

+3761
-2775
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+3761
-2775
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
UNRELEASED CHANGES:
22

3+
* Add many more relationship types to link contacts together
34
* Fix called_at field in the Call object returned by the API
45
* Add Linkedin URL in the Contact object returned by the API
56
* Improve localization: add plural forms, localize every needed messages

app/Account.php

Lines changed: 140 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -31,52 +31,6 @@ class Account extends Model
3131
'has_access_to_paid_version_for_free' => 'boolean',
3232
];
3333

34-
/**
35-
* Create a new account and associate a new User.
36-
*
37-
* @param string $first_name
38-
* @param string $last_name
39-
* @param string $email
40-
* @param string $password
41-
* @return this
42-
*/
43-
public static function createDefault($first_name, $last_name, $email, $password)
44-
{
45-
// create new account
46-
$account = new self;
47-
$account->api_key = str_random(30);
48-
$account->created_at = now();
49-
$account->save();
50-
51-
$account->populateDefaultFields($account);
52-
53-
// create the first user for this account
54-
User::createDefault($account->id, $first_name, $last_name, $email, $password);
55-
56-
return $account;
57-
}
58-
59-
/**
60-
* Get if any account exists on the database.
61-
*
62-
* @return bool
63-
*/
64-
public static function hasAny()
65-
{
66-
return DB::table('accounts')->count() > 0;
67-
}
68-
69-
/**
70-
* Populates all the default column that should be there when a new account
71-
* is created or reset.
72-
*/
73-
public static function populateDefaultFields($account)
74-
{
75-
$account->populateContactFieldTypeTable();
76-
$account->populateDefaultGendersTable();
77-
$account->populateDefaultReminderRulesTable();
78-
}
79-
8034
/**
8135
* Get the activity records associated with the account.
8236
*
@@ -327,6 +281,26 @@ public function reminderRules()
327281
return $this->hasMany('App\ReminderRule');
328282
}
329283

284+
/**
285+
* Get the relationship types records associated with the account.
286+
*
287+
* @return HasMany
288+
*/
289+
public function relationshipTypes()
290+
{
291+
return $this->hasMany('App\RelationshipType');
292+
}
293+
294+
/**
295+
* Get the relationship type groups records associated with the account.
296+
*
297+
* @return HasMany
298+
*/
299+
public function relationshipTypeGroups()
300+
{
301+
return $this->hasMany('App\RelationshipTypeGroup');
302+
}
303+
330304
/**
331305
* Get the Notifications records associated with the account.
332306
*
@@ -480,12 +454,12 @@ public function timezone()
480454
* Populates the Contact Field Types table right after an account is
481455
* created.
482456
*/
483-
public function populateContactFieldTypeTable($ignoreMigratedTable = false)
457+
public function populateContactFieldTypeTable($ignoreTableAlreadyMigrated = false)
484458
{
485459
$defaultContactFieldTypes = DB::table('default_contact_field_types')->get();
486460

487461
foreach ($defaultContactFieldTypes as $defaultContactFieldType) {
488-
if (! $ignoreMigratedTable || $defaultContactFieldType->migrated == 0) {
462+
if (! $ignoreTableAlreadyMigrated || $defaultContactFieldType->migrated == 0) {
489463
ContactFieldType::create([
490464
'account_id' => $this->id,
491465
'name' => $defaultContactFieldType->name,
@@ -521,6 +495,54 @@ public function populateDefaultReminderRulesTable()
521495
ReminderRule::create(['number_of_days_before' => 30, 'account_id' => $this->id, 'active' => 1]);
522496
}
523497

498+
/**
499+
* Populates the default relationship types in a new account.
500+
*
501+
* @return void
502+
*/
503+
public function populateRelationshipTypeGroupsTable($ignoreTableAlreadyMigrated = false)
504+
{
505+
$defaultRelationshipTypeGroups = DB::table('default_relationship_type_groups')->get();
506+
foreach ($defaultRelationshipTypeGroups as $defaultRelationshipTypeGroup) {
507+
if (! $ignoreTableAlreadyMigrated || $defaultRelationshipTypeGroup->migrated == 0) {
508+
DB::table('relationship_type_groups')->insert([
509+
'account_id' => $this->id,
510+
'name' => $defaultRelationshipTypeGroup->name,
511+
'delible' => $defaultRelationshipTypeGroup->delible,
512+
]);
513+
}
514+
}
515+
}
516+
517+
/**
518+
* Populate the relationship types table based on the default ones.
519+
*
520+
* @param bool $ignoreTableAlreadyMigrated
521+
* @return void
522+
*/
523+
public function populateRelationshipTypesTable($ignoreTableAlreadyMigrated = false)
524+
{
525+
$defaultRelationshipTypes = DB::table('default_relationship_types')->get();
526+
527+
foreach ($defaultRelationshipTypes as $defaultRelationshipType) {
528+
if (! $ignoreTableAlreadyMigrated || $defaultRelationshipType->migrated == 0) {
529+
$defaultRelationshipTypeGroup = DB::table('default_relationship_type_groups')
530+
->where('id', $defaultRelationshipType->relationship_type_group_id)
531+
->first();
532+
533+
$relationshipTypeGroup = $this->getRelationshipTypeGroupByType($defaultRelationshipTypeGroup->name);
534+
535+
RelationshipType::create([
536+
'account_id' => $this->id,
537+
'name' => $defaultRelationshipType->name,
538+
'name_reverse_relationship' => $defaultRelationshipType->name_reverse_relationship,
539+
'relationship_type_group_id' => $relationshipTypeGroup->id,
540+
'delible' => $defaultRelationshipType->delible,
541+
]);
542+
}
543+
}
544+
}
545+
524546
/**
525547
* Get the reminders for the month given in parameter.
526548
* - 0 means current month
@@ -580,6 +602,76 @@ public function replaceGender(Gender $genderToDelete, Gender $genderToReplaceWit
580602
return true;
581603
}
582604

605+
/**
606+
* Get if any account exists on the database.
607+
*
608+
* @return bool
609+
*/
610+
public static function hasAny()
611+
{
612+
return DB::table('accounts')->count() > 0;
613+
}
614+
615+
/**
616+
* Create a new account and associate a new User.
617+
*
618+
* @param string $first_name
619+
* @param string $last_name
620+
* @param string $email
621+
* @param string $password
622+
* @return this
623+
*/
624+
public static function createDefault($first_name, $last_name, $email, $password)
625+
{
626+
// create new account
627+
$account = new self;
628+
$account->api_key = str_random(30);
629+
$account->created_at = now();
630+
$account->save();
631+
632+
$account->populateDefaultFields($account);
633+
634+
// create the first user for this account
635+
User::createDefault($account->id, $first_name, $last_name, $email, $password);
636+
637+
return $account;
638+
}
639+
640+
/**
641+
* Populates all the default column that should be there when a new account
642+
* is created or reset.
643+
*/
644+
public static function populateDefaultFields($account)
645+
{
646+
$account->populateContactFieldTypeTable();
647+
$account->populateDefaultGendersTable();
648+
$account->populateDefaultReminderRulesTable();
649+
$account->populateRelationshipTypeGroupsTable();
650+
$account->populateRelationshipTypesTable();
651+
}
652+
653+
/**
654+
* Gets the RelationshipType object matching the given type.
655+
*
656+
* @param string $relationshipTypeName
657+
* @return RelationshipType
658+
*/
659+
public function getRelationshipTypeByType(string $relationshipTypeName)
660+
{
661+
return $this->relationshipTypes->where('name', $relationshipTypeName)->first();
662+
}
663+
664+
/**
665+
* Gets the RelationshipType object matching the given type.
666+
*
667+
* @param string $relationshipTypeGroupName
668+
* @return RelationshipTypeGroup
669+
*/
670+
public function getRelationshipTypeGroupByType(string $relationshipTypeGroupName)
671+
{
672+
return $this->relationshipTypeGroups->where('name', $relationshipTypeGroupName)->first();
673+
}
674+
583675
/**
584676
* Get the statistics of the number of calls grouped by year.
585677
*

0 commit comments

Comments
 (0)