addEmailAddress: permit shared email addresses.
An existing email address can exist already. If it does, just map it to the new donor_id as well.
This commit is contained in:
parent
736f022005
commit
6d1825240a
2 changed files with 50 additions and 21 deletions
|
@ -200,28 +200,37 @@ sub addEmailAddress($$$$) {
|
|||
die "addEmailAddress: invalid email address, $emailAddressType"
|
||||
unless defined $emailAddressType and Mail::RFC822::Address::valid($emailAddress);
|
||||
|
||||
my $existingEmail = $self->_lookupEmailAddress($emailAddress);
|
||||
die "addEmailAddress: attempt to add email address that exists, using a different type!"
|
||||
if defined $existingEmail and $existingEmail->{type} ne $emailAddressType;
|
||||
|
||||
my($sth, $addressId);
|
||||
|
||||
$self->_beginWork();
|
||||
|
||||
my $addressTypeId;
|
||||
eval {
|
||||
$addressTypeId = $self->addAddressType($emailAddressType);
|
||||
};
|
||||
if ($@ or not defined $addressTypeId) {
|
||||
my $err = $@;
|
||||
$err = "addEmailAddress: unable to addAddressType" if (not defined $err);
|
||||
$self->_rollback();
|
||||
die $@ if $@;
|
||||
if (defined $existingEmail) {
|
||||
$addressId = $existingEmail->{id};
|
||||
} else {
|
||||
my $addressTypeId;
|
||||
eval {
|
||||
$addressTypeId = $self->addAddressType($emailAddressType);
|
||||
};
|
||||
if ($@ or not defined $addressTypeId) {
|
||||
my $err = $@;
|
||||
$err = "addEmailAddress: unable to addAddressType" if (not defined $err);
|
||||
$self->_rollback();
|
||||
die $@ if $@;
|
||||
}
|
||||
$sth = $self->dbh->prepare("INSERT INTO email_address(email_address, type_id, date_encountered)" .
|
||||
"VALUES( ?, ?, date('now'))");
|
||||
|
||||
$sth->execute($emailAddress, $addressTypeId);
|
||||
$addressId = $self->dbh->last_insert_id("","","","");
|
||||
$sth->finish();
|
||||
}
|
||||
my $sth = $self->dbh->prepare("INSERT INTO email_address(email_address, type_id, date_encountered)" .
|
||||
"VALUES( ?, ?, date('now'))");
|
||||
|
||||
$sth->execute($emailAddress, $addressTypeId);
|
||||
my $addressId = $self->dbh->last_insert_id("","","","");
|
||||
$sth->finish();
|
||||
|
||||
$sth = $self->dbh->prepare("INSERT INTO donor_email_address_mapping" .
|
||||
"(donor_id, email_address_id) " .
|
||||
"VALUES( ?, ?)");
|
||||
"(donor_id, email_address_id) " .
|
||||
"VALUES( ?, ?)");
|
||||
$sth->execute($id, $addressId);
|
||||
$sth->finish();
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Test::More tests => 176;
|
||||
use Test::More tests => 182;
|
||||
use Test::Exception;
|
||||
|
||||
use Scalar::Util qw(looks_like_number reftype);
|
||||
|
@ -131,9 +131,25 @@ ok((not defined $val or not defined $val->{'name'}),
|
|||
my $drapperEmailId;
|
||||
|
||||
lives_ok { $drapperEmailId = $sp->addEmailAddress($drapperId, 'drapper@example.org', 'work') }
|
||||
"addEmailAdress: inserting a valid email address works";
|
||||
"addEmailAddress: inserting a valid email address works";
|
||||
ok((looks_like_number($drapperEmailId) and $drapperEmailId > 0), "addEmailAddress: id returned is sane.");
|
||||
|
||||
my $olsonEmailId2;
|
||||
|
||||
dies_ok { $olsonEmailId2 = $sp->addEmailAddress($olsonId, 'drapper@example.org', 'paypal') }
|
||||
"addEmailAddress: fails when adding the same email address for someone else, but as a different type";
|
||||
|
||||
my $drapperEmailId2;
|
||||
lives_ok { $drapperEmailId2 = $sp->addEmailAddress($drapperId, 'everyone@example.net', 'paypal') }
|
||||
"addEmailAddress: inserting a second valid email address works";
|
||||
ok((looks_like_number($drapperEmailId2) and $drapperEmailId2 > 0 and $drapperEmailId != $drapperEmailId2),
|
||||
"addEmailAddress: id returned is sane and is not same as previous id.");
|
||||
|
||||
lives_ok { $olsonEmailId2 = $sp->addEmailAddress($olsonId, 'everyone@example.net', 'paypal') }
|
||||
"addEmailAddress: binding known email address to another person works...";
|
||||
ok((looks_like_number($olsonEmailId2) and $olsonEmailId2 > 0 and $olsonEmailId2 == $drapperEmailId2),
|
||||
"addEmailAddress: ... and id returned is sane and is same.");
|
||||
|
||||
=item addAddressType
|
||||
|
||||
=cut
|
||||
|
@ -546,7 +562,11 @@ dies_ok { $sp->_lookupEmailAddress(undef); } "_lookupEmailAddressId: dies for un
|
|||
|
||||
is_deeply($sp->_lookupEmailAddress('drapper@example.org'),
|
||||
{ emailAddress => 'drapper@example.org', id => $drapperEmailId, type => 'work', dateEncountered => $today },
|
||||
"_lookupEmailAddressId: returns email Id for known item");
|
||||
"_lookupEmailAddressId: 1 returns email Id for known item");
|
||||
|
||||
is_deeply($sp->_lookupEmailAddress('everyone@example.net'),
|
||||
{ emailAddress => 'everyone@example.net', id => $olsonEmailId2, type => 'paypal', dateEncountered => $today },
|
||||
"_lookupEmailAddressId: 2 returns email id for known item");
|
||||
|
||||
is($sp->_lookupEmailAddress('drapper@example.com'), undef,
|
||||
"_lookupEmailAddressId: returns undef for unknown item.");
|
||||
|
|
Loading…
Reference in a new issue