Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find email duplicates API #3828

Merged
merged 16 commits into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 50 additions & 9 deletions demo/ChurchCRM-Database.sql

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/api/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@

require __DIR__.'/routes/dashboard.php';

require __DIR__.'/routes/email.php';
require __DIR__.'/routes/geocoder.php';

require __DIR__.'/routes/public.php';
Expand Down
50 changes: 50 additions & 0 deletions src/api/routes/email.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

use Propel\Runtime\Propel;
use ChurchCRM\PersonQuery;
use ChurchCRM\FamilyQuery;
use Slim\Http\Request;
use Slim\Http\Response;

$app->group('/emails', function () {
$this->get('/duplicates', 'getEmailDupes');
});


/**
* A method that review dup emails in the db and returns families and people where that email is used.
*
* @param \Slim\Http\Request $p_request The request.
* @param \Slim\Http\Response $p_response The response.
* @param array $p_args Arguments
* @return \Slim\Http\Response The augmented response.
*/
function getEmailDupes(Request $request, Response $response, array $p_args)
{
$connection = Propel::getConnection();
$dupEmailsSQL = "SELECT email, total FROM email_count where total > 1";
$statement = $connection->prepare($dupEmailsSQL);
$statement->execute();
$dupEmails = $statement->fetchAll();

$emails = [];
foreach ($dupEmails as $dbEmail) {
$email = $dbEmail['email'];
$dbPeople = PersonQuery::create()->filterByEmail($email)->_or()->filterByWorkEmail($email)->find();
$people = [];
foreach ($dbPeople as $person) {
array_push($people, ["id" => $person->getId(), "name" => $person->getFullName()]);
}
$families = [];
$dbFamilies = FamilyQuery::create()->findByEmail($email);
foreach ($dbFamilies as $family) {
array_push($families, ["id" => $family->getId(), "nane" => $family->getName()]);
}
$emails[$email] = [
"people" => $people,
"families" => $families
];
}

return $response->withJson($emails);
}
10 changes: 10 additions & 0 deletions src/mysql/install/Install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1321,4 +1321,14 @@ CREATE TABLE `church_location_role` (
PRIMARY KEY (`location_id`, `role_id`)
) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_unicode_ci;

CREATE VIEW email_list AS
SELECT fam_Email AS email, 'family' AS type, fam_id AS id FROM family_fam WHERE fam_email IS NOT NULL AND fam_email != ''
UNION
SELECT per_email AS email, 'person_home' AS type, per_id AS id FROM person_per WHERE per_email IS NOT NULL AND per_email != ''
UNION
SELECT per_WorkEmail AS email, 'person_work' AS type, per_id AS id FROM person_per WHERE per_WorkEmail IS NOT NULL AND per_WorkEmail != '';

CREATE VIEW email_count AS
SELECT email, COUNT(*) AS total FROM email_list group by email;

update version_ver set ver_update_end = now();
3 changes: 2 additions & 1 deletion src/mysql/upgrade.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@
"2.9.3",
"2.9.4"
],
"scripts": [],
"scripts": [
"/mysql/upgrade/2.10.0-email-dup.sql"],
"dbVersion": "2.10.0"
}
}
13 changes: 13 additions & 0 deletions src/mysql/upgrade/2.10.0-email-dup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

DROP VIEW email_list;
CREATE VIEW email_list AS
SELECT fam_Email AS email, 'family' AS type, fam_id AS id FROM family_fam WHERE fam_email IS NOT NULL AND fam_email != ''
UNION
SELECT per_email AS email, 'person_home' AS type, per_id AS id FROM person_per WHERE per_email IS NOT NULL AND per_email != ''
UNION
SELECT per_WorkEmail AS email, 'person_work' AS type, per_id AS id FROM person_per WHERE per_WorkEmail IS NOT NULL AND per_WorkEmail != '';


DROP VIEW email_count;
CREATE VIEW email_count AS
SELECT email, COUNT(*) AS total FROM email_list group by email;