Commit 7fa005ef authored by Bastien Le Querrec's avatar Bastien Le Querrec
Browse files

do not prevent use of existing address

Multiple users should be able to share one address.
parent ad1b9d08
......@@ -288,24 +288,19 @@ class Perso extends Controller
$f3->push('SESSION.message', _("Adresse ajoutée à votre profil"));
break;
case 'UPDATE':
try {
$f3->get('container')['command_handler']->handle(
new AddressUpdateCommand(
\Utils::asl($f3->get('id')),
\Utils::asl($f3->get('SESSION.id')),
\Utils::asl($f3->get('nom')),
\Utils::asl($f3->get('adresse')),
\Utils::asl($f3->get('adresse2')),
\Utils::asl($f3->get('codepostal')),
\Utils::asl($f3->get('ville')),
\Utils::asl($f3->get('pays')),
\Utils::asl($f3->get('state'))
)
);
} catch (AddressAlreadyExistsException $e) {
$f3->set("error", _("Cette adresse existe déjà."));
$f3->error('403');
}
$f3->get('container')['command_handler']->handle(
new AddressUpdateCommand(
\Utils::asl($f3->get('id')),
\Utils::asl($f3->get('SESSION.id')),
\Utils::asl($f3->get('nom')),
\Utils::asl($f3->get('adresse')),
\Utils::asl($f3->get('adresse2')),
\Utils::asl($f3->get('codepostal')),
\Utils::asl($f3->get('ville')),
\Utils::asl($f3->get('pays')),
\Utils::asl($f3->get('state'))
)
);
$f3->push('SESSION.message', _("Adresse du profil modifiée."));
break;
case 'DELETE':
......
......@@ -63,6 +63,25 @@ EOF;
$stmt->execute();
}
/**
* Check if an adress is used.
*
* @param int $addressId
*
* @return bool
*/
private function addressUsed($addressId)
{
$count = (int) $this->connection->fetchColumn(
"SELECT count(1) FROM contreparties, dons WHERE contreparties.adresse_id = :id AND contreparties.status = 1;",
[
'id' => $addressId,
],
0
);
return ($count > 0);
}
/**
* Update an address.
*
......@@ -73,12 +92,6 @@ EOF;
$addressId = $command->getAddressId();
$userId = $command->getUserId();
// Let's check if the address is used
if ($this->addressUsed($addressId) == true) {
// The address is used somehow
throw new AddressUsedException();
};
$query =<<<EOF
UPDATE adresses
SET nom = :name, adresse = :address, adresse2 = :address2, codepostal = :postal_code, ville = :city, etat = :state, pays = :country
......@@ -97,41 +110,4 @@ EOF;
$stmt->bindValue('country', $command->getCountry());
$stmt->execute();
}
/**
* Check if an address already exists.
*
* @param int $userId
*
* @return bool
*/
private function addressExists($userId)
{
return (bool) $this->connection->fetchColumn(
"SELECT 1 FROM adresses WHERE user_id = :user_id",
[
'user_id' => $userId,
],
0
);
}
/**
* Check if an adress is used.
*
* @param int $addressId
*
* @return bool
*/
private function addressUsed($addressId)
{
$count = (int) $this->connection->fetchColumn(
"SELECT count(1) FROM contreparties, dons WHERE contreparties.adresse_id = :id AND contreparties.status = 1;",
[
'id' => $addressId,
],
0
);
return ($count > 0);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment