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 4 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
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 from emails_duplicate;";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make the SQL statements CAPITALIZED to keep with standard SQL convention.

$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);
}
29 changes: 29 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,29 @@
CREATE VIEW emails_duplicate AS
SELECT
email, total
FROM
(SELECT
email, COUNT(*) AS total
FROM
(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 != '') emails
GROUP BY email) AS emailTotals
WHERE
total > 1