Commit 05f969f8 authored by okhin's avatar okhin 🚴

Merge branch '59-edition-d-une-adresse' into 'preprod'

Resolve "Edition d'une adresse"

Closes #59

See merge request !43
parents 8e0c6d91 f0d18089
Pipeline #2194 failed with stages
in 1 minute and 21 seconds
......@@ -16,3 +16,8 @@ __MAC
tests/_data
tests/_output
tests/_support/_generated
tests/_output/*
/var/log/*
!/var/log/.gitkeep
/.php_cs.cache
tests/*/_generated/*
......@@ -72,8 +72,8 @@ class Perso extends Controller
foreach ($result->fetchAll(\PDO::FETCH_ASSOC) as $row) {
$pdfs[$row['pdf']] = $row;
}
$addresses = $f3->get('container')['address_finder']->findByUserId((int) $data['id']);
$f3->set('adresses', $addresses);
$addresse = $f3->get('container')['address_finder']->findByUserId((int) $data['id']);
$f3->set('adresse', $addresse);
$f3->set('pdfs', $pdfs);
}
......@@ -292,7 +292,6 @@ class Perso extends Controller
$f3->error('401');
}
// Let's get the email adress of the user
switch ($f3->get('action')) {
case 'ADD':
try {
......@@ -300,14 +299,12 @@ class Perso extends Controller
new AddressCreateCommand(
\Utils::asl($f3->get('SESSION.id')),
\Utils::asl($f3->get('nom')),
\Utils::asl($f3->get('alias')),
\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->exists('defaut')
\Utils::asl($f3->get('state'))
)
);
} catch (AddressAlreadyExistsException $e) {
......@@ -315,22 +312,6 @@ class Perso extends Controller
$f3->error('403');
}
break;
case 'UPDATE':
$f3->get('container')['command_handler']->handle(
new AddressUpdateCommand(
\Utils::asl($f3->get('id')),
\Utils::asl($f3->get('SESSION.id')),
\Utils::asl($f3->get('alias')),
\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->exists('defaut')
)
);
break;
case 'DELETE':
try {
$f3->get('container')['command_handler']->handle(
......@@ -340,7 +321,7 @@ class Perso extends Controller
)
);
} catch (AddressUsedException $e) {
$f3->set("error", _("Cette adresse est utilisée et il vous n'avez pas définit d'adresse par défault."));
$f3->set("error", _("Cette adresse est utilisée pour une de vos contreparties"));
$f3->error('403');
};
break;
......@@ -430,7 +411,7 @@ class Perso extends Controller
FROM dons
INNER JOIN users ON users.id = dons.user_id
LEFT OUTER JOIN adresses ON adresses.user_id = users.id
WHERE users.id=".$f3->get('SESSION.id')." AND adresses.defaut IS true AND dons.id=".\Utils::asl($id).";";
WHERE users.id=".$f3->get('SESSION.id')." AND dons.id=".\Utils::asl($id).";";
$result = $db->query($query);
$logger->write($query);
$don = $result->fetch(\PDO::FETCH_ASSOC);
......
......@@ -32,7 +32,13 @@
<td>
{{ @contrepartie.nom }}<br />
{{ @contrepartie.adresse }}<br />
<check if="{{ @@contrepartie.adresse2 }}">
{{ @contrepartie.adresse2 }}<br />
</check>
{{ @contrepartie.codepostal }} {{ @contrepartie.ville }}<br />
<check if="{{ @@contrepartie.state }}">
{{ @contrepartie.state }}<br />
<check if="{{ @@contrepartie.adresse2 }}">
{{ @contrepartie.pays }}
</td>
<td>
......
......@@ -20,65 +20,36 @@
<!-- Gestion des adresses pour livrer les contreparties -->
<section id="adresses">
<h2>{{ _("Vos adresses :) }}</h2>
<repeat group="{{ @adresses }}" value="{{ @adresse }}" counter="{{ @ctr }}">
<h2>{{ _("Votre adresse postale :) }}</h2>
<form method="post" action="{{ @ALIASES.adresses}}" name="{{ adr.@ctr }}" id="{{ adr.@ctr }}">
<input type="hidden" name="action" id="action" value="UPDATE">
<p><label for="alias">{{ _("Nom de l'adresse (livraison, principale, etc) :") }}</label>
<input name="alias" id="alias" class="{{ Utils::badfield(array(6)) }}" value="{{ @adresse.alias }}" ></p>
<input type="hidden" name="action" id="action" value="ADD">
<p><label for="nom">{{ _("Nom, Prénom, Pseudo (pensez au facteur) :") }}</label>
<input name="nom" id="nom" class="{{ Utils::badfield(array(6)) }}" value="{{ @adresse.nom }}" ></p>
<input name="nom" id="nom" class="{{ Utils::badfield(array(6)) }}" value="{{ @@adresse.nom }}" ></p>
<p><label for="adresse">{{ _("Adresse postale :") }}</label>
<textarea name="adresse" id="adresse" class="{{ Utils::badfield(array(6)) }}">{{ @adresse.adresse }}</textarea></p>
<textarea name="adresse" id="adresse" class="{{ Utils::badfield(array(6)) }}">{{ @@adresse.adresse }}</textarea></p>
<p><label for="adresse2">{{ _("Complément d'adresse :") }}</label>
<textarea name="adresse2" id="adresse2" class="{{ Utils::badfield(array(6)) }}">{{ @adresse.adresse2 }}</textarea></p>
<textarea name="adresse2" id="adresse2" class="{{ Utils::badfield(array(6)) }}">{{ @@adresse.adresse2 }}</textarea></p>
<p><label for="ville">{{ _("Ville :") }}</label>
<input name="ville" id="ville" class="{{ Utils::badfield(array(6)) }}" value="{{ @adresse.ville }}" ></p>
<input name="ville" id="ville" class="{{ Utils::badfield(array(6)) }}" value="{{ @@adresse.ville }}" ></p>
<p><label for="state">{{ _("Province / État :") }}</label>
<input name="state" id="state" class="{{ Utils::badfield(array(6)) }}" value="{{ @@adresse.state }}" ></p>
<p><label for="codepostal">{{ _("Code Postal :") }}</label>
<input name="codepostal" id="codepostal" class="{{ Utils::badfield(array(6)) }}" value="{{ @adresse.codepostal }}" ></p>
<input name="codepostal" id="codepostal" class="{{ Utils::badfield(array(6)) }}" value="{{ @@adresse.codepostal }}" ></p>
<p><label for="pays">{{ _("Pays :") }}</label>
<input name="pays" id="pays" class="{{ Utils::badfield(array(6)) }}" value="{{ @adresse.pays }}" ></p>
<input name="pays" id="pays" class="{{ Utils::badfield(array(6)) }}" value="{{ @@adresse.pays }}" ></p>
<p><input type="submit" class="big_button" value="{{ _('Valider les modifications') }} &gt;&gt;" ></p>
</form>
<form method="post" action="{{ @ALIASES.adresses }}" name="{{ del.@ctr }}" id="{{ del.@ctr }}">
<input type="hidden" name="action" id="action" value="DELETE">
<input type="hidden" name="alias" id="alias" value="{{ @adresse.alias }}">
<p><input type="submit" class="big_button" value="{{ _('Supprimer cette adresse') }}" ></p>
</form>
</repeat>
<form method="post" action="{{ @ALIASES.adresses}}" name="{{ add_adr }}" id="{{ add_adr }}">
<input type="hidden" name="action" id="action" value="ADD">
<p><label for="alias">{{ _("Nom de l'adresse (livraison, principale, etc) :") }}</label>
<input name="alias" id="alias" class="{{ Utils::badfield(array(6)) }}"></p>
<p><label for="nom">{{ _("Nom, Prénom, Pseudo (pensez au facteur) :") }}</label>
<input name="nom" id="nom" class="{{ Utils::badfield(array(6)) }}"></p>
<p><label for="adresse">{{ _("Adresse postale :") }}</label>
<textarea name="adresse" id="adresse" class="{{ Utils::badfield(array(6)) }}"></textarea></p>
<p><label for="adresse2">{{ _("Complément d'adresse :") }}</label>
<textarea name="adresse2" id="adresse2" class="{{ Utils::badfield(array(6)) }}"></textarea></p>
<p><label for="ville">{{ _("Ville :") }}</label>
<input name="ville" id="ville" class="{{ Utils::badfield(array(6)) }}"></p>
<p><label for="codepostal">{{ _("Code Postal :") }}</label>
<input name="codepostal" id="codepostal" class="{{ Utils::badfield(array(6)) }}" ></p>
<p><label for="pays">{{ _("Pays :") }}</label>
<input name="pays" id="pays" class="{{ Utils::badfield(array(6)) }}" ></p>
<p><input type="submit" class="big_button" value="{{ _('Ajouter une adresse postale.') }} &gt;&gt;" /></p>
<p><input type="submit" class="big_button" value="{{ _('Supprimer votre adresse') }}" ></p>
</form>
</section>
<!-- Gestion des demandes de contreparties -->
......@@ -173,13 +144,10 @@
<p id="admin_taille_h"><label for="taille_h">{{ _("Coupe et taille du hoodie:") }}</label>
<select name="taille_h" id="taille_h">{{ Utils::eoption(@atailles, @taille_h) }}</select></p>
<p id="admin_adresses"><label for="alias">{{ _("Adresse de livraison:") }}</label>
<select name="adresse_id" id="adresse_id">
<repeat group="{{ @adresses }}" value="{{ @adresse }}">
<option value="{{ @adresse.id }}">{{@adresse.alias}}</option>
</repeat>
<input type="hidden" name="adresse_id" id="adresse_id" value="{{ @adresse.id }}">
Expédier à&nbsp;: {{@adresse.nom}} {{@adresse.adresse}}
</select></p>
<input type="submit" class="big_button" value="Valider" />
<input type="submit" class="big_button" value="Valider" <check if="{{ @adresse }}"><false>disabled</false>/</check>/>
</div>
<check if="{{ @error!='' }}">
<p class="error">{{ @error }}</p>
......
This diff is collapsed.
<?php
use Phinx\Migration\AbstractMigration;
class UneSeuleAdresse extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* addCustomColumn
* renameColumn
* addIndex
* addForeignKey
*
* Any other destructive changes will result in an error when trying to
* rollback the migration.
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function up()
{
// Removing the alias <> user_id index
$table = $this->table('adresses');
$table->removeIndex(['alias', 'user_id'])
->save();
// Deleting all addresses, to be sure we don't have doubles
$this->execute('TRUNCATE TABLE adresses;');
// Add a unique index on user_id
$table->addIndex(['user_id'], ['unique' => true])
->removeColumn('alias')
->save();
}
public function down() {
$table = $this->table('adresses');
$table->addColumn('alias', 'string')
->removeIndex(['user_id'])
->save();
$table->addIndex(['alias', 'user_id'], ['unique' => true])
->save();
}
}
<?php
use Phinx\Migration\AbstractMigration;
class RemoveAddressDefaut extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* addCustomColumn
* renameColumn
* addIndex
* addForeignKey
*
* Any other destructive changes will result in an error when trying to
* rollback the migration.
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function up()
{
$table = $this->table('adresses');
$table->removeColumn('defaut')
->save();
}
public function down()
{
$table = $this->table('adresses');
$table->addColumn('defaut', 'boolean')
->save();
}
}
......@@ -24,38 +24,10 @@ class AddressSeeder extends AbstractSeed
'ville' => 'Paris',
'etat' => null,
'pays' => 'France',
'alias' => 'Main',
'user_id' => 1,
'defaut' => true,
),
array(
'id' => 2,
'nom' => 'Second address',
'adresse' => '2 rue Ménars',
'adresse2' => null,
'codepostal' => 75002,
'ville' => 'Paris',
'etat' => null,
'pays' => 'France',
'alias' => 'Second address',
'user_id' => 1,
'defaut' => false,
),
array(
'id' => 4,
'nom' => 'Third address',
'adresse' => '3 rue Ménars',
'adresse2' => null,
'codepostal' => 75003,
'ville' => 'Paris',
'etat' => null,
'pays' => 'France',
'alias' => 'Third address',
'user_id' => 1,
'defaut' => false,
),
array(
'id' => 5,
'nom' => 'Bob address',
'adresse' => '4 rue Ménars',
'adresse2' => null,
......@@ -63,9 +35,7 @@ class AddressSeeder extends AbstractSeed
'ville' => 'Paris',
'etat' => null,
'pays' => 'France',
'alias' => 'Third address',
'user_id' => 2,
'defaut' => false,
),
);
......
......@@ -17,7 +17,7 @@ environments:
development:
adapter: mysql
host: localhost
name: soutien
name: don
user: soutien
pass: 'soutien'
port: 3306
......
......@@ -7,27 +7,23 @@ class AddressCreateCommand
private $addressId;
private $userId;
private $name;
private $alias;
private $address;
private $address2;
private $postalCode;
private $city;
private $country;
private $state;
private $isDefault;
public function __construct($userId, $name, $alias, $address, $address2, $postalCode, $city, $country, $state, $isDefault)
public function __construct($userId, $name, $address, $address2, $postalCode, $city, $country, $state)
{
$this->userId = $userId;
$this->name = $name;
$this->alias = $alias;
$this->address = $address;
$this->address2 = $address2;
$this->postalCode = $postalCode;
$this->city = $city;
$this->country = $country;
$this->state = $state;
$this->isDefault = $isDefault;
}
public function getUserId()
......@@ -40,11 +36,6 @@ class AddressCreateCommand
return $this->name;
}
public function getAlias()
{
return $this->alias;
}
public function getAddress()
{
return $this->address;
......@@ -74,9 +65,4 @@ class AddressCreateCommand
{
return $this->state;
}
public function isDefault()
{
return $this->isDefault;
}
}
<?php
namespace LQDN\Command;
class AddressUpdateCommand
{
private $addressId;
private $userId;
private $alias;
private $address;
private $address2;
private $postalCode;
private $city;
private $country;
private $state;
private $isDefault;
public function __construct($addressId, $userId, $alias, $address, $address2, $postalCode, $city, $country, $state, $isDefault)
{
$this->addressId = $addressId;
$this->userId = $userId;
$this->alias = $alias;
$this->address = $address;
$this->address2 = $address2;
$this->postalCode = $postalCode;
$this->city = $city;
$this->country = $country;
$this->state = $state;
$this->isDefault = $isDefault;
}
public function getAddressId()
{
return $this->addressId;
}
public function getUserId()
{
return $this->userId;
}
public function getAlias()
{
return $this->alias;
}
public function getAddress()
{
return $this->address;
}
public function getAddress2()
{
return $this->address2;
}
public function getPostalCode()
{
return $this->postalCode;
}
public function getCity()
{
return $this->city;
}
public function getCountry()
{
return $this->country;
}
public function getState()
{
return $this->state;
}
public function isDefault()
{
return $this->isDefault;
}
}
......@@ -23,12 +23,8 @@ class AddressFinder
public function findByUserId($userId)
{
$userId = (int) $userId;
$addresses = [];
$stmt = $this->connection->executeQuery('SELECT * FROM adresses WHERE user_id = :user_id', ['user_id' => $userId]);
while ($address = $stmt->fetch()) {
$addresses[$address['id']] = $address;
}
return $addresses;
$addresse = $stmt->fetch();
return $addresse;
}
}
......@@ -19,9 +19,7 @@ class AddressHandler
}
/**
* Delete an address. If there's a default adress, we should replace it in
* dons and contreparties. If there's none and the adress is used
* in contreparties, we should raise an error.
* Delete an address.
*
* @param AddressDeleteCommand $command
*/
......@@ -33,63 +31,12 @@ class AddressHandler
$addressId = $command->getAddressId();
// Let's check if the address is used
if ($this->addressUsed($addressId) == true) {
// First lets' check if there is a default Address we could use
$default = $this->getDefaultAddress($user_id);
$params = ['id' => $command->getAddressId(),
'default_id' => $default];
if ($default != '' and $default != $addressId) {
// We have a default address which is not the current one
$this->connection->executeUpdate("UPDATE dons SET adresse_id = :default_id WHERE adresse_id = :id", $params);
$this->connection->executeUpdate("UPDATE contreparties SET adresse_id= :default_id WHERE adresse_id = :id", $params);
} else {
// We do not have a default address and the address is used somehow
throw new AddressUsedException();
};
// The address is used somehow
throw new AddressUsedException();
};
$this->connection->executeUpdate("DELETE FROM adresses WHERE id = :id", ['id' => $addressId]);
}
/**
* Update an address.
*
* @param AddressUpdateCommand $command
*/
public function handleAddressUpdateCommand(AddressUpdateCommand $command)
{
$userId = $command->getUserId();
if ($isDefault = $command->isDefault()) {
$this->undefaultAddresses($userId);
}
$query = <<<EOF
UPDATE adresses
SET
alias = :alias,
adresse = :address,
adresse2 = :address2,
codepostal = :postal_code,
ville = :city,
pays = :country,
etat = :state,
defaut = :default
WHERE id = :address_id
AND user_id = :user_id;
EOF;
$stmt = $this->connection->prepare($query);
$stmt->bindValue('alias', $command->getAlias());
$stmt->bindValue('address', $command->getAddress());
$stmt->bindValue('address2', $command->getAddress2());
$stmt->bindValue('postal_code', $command->getPostalCode());
$stmt->bindValue('city', $command->getCity());
$stmt->bindValue('country', $command->getCountry());
$stmt->bindValue('state', $command->getState());
$stmt->bindValue('default', $command->isDefault());
$stmt->bindValue('address_id', $command->getAddressId());
$stmt->bindValue('user_id', $command->getUserId());
$stmt->execute();
}
/**
* Create an address.
*
......@@ -98,64 +45,24 @@ EOF;
public function handleAddressCreateCommand(AddressCreateCommand $command)
{
$userId = $command->getUserId();
$alias = $command->getAlias();
if ($this->addressExists($userId, $alias)) {
throw new AddressAlreadyExistsException();
}
if ($isDefault = $command->isDefault()) {
$this->undefaultAddresses($userId);
}
$query =<<<EOF
INSERT INTO adresses(user_id, nom, alias, adresse, adresse2, codepostal, ville, etat, pays, defaut)
VALUES (:user_id, :name, :alias, :address, :address2, :postal_code, :city, :state, :country, :default)
REPLACE INTO adresses(user_id, nom, adresse, adresse2, codepostal, ville, etat, pays)
VALUES (:user_id, :name, :address, :address2, :postal_code, :city, :state, :country)
EOF;
$stmt = $this->connection->prepare($query);
$stmt->bindValue('user_id', $command->getUserId());
$stmt->bindValue('name', $command->getName());
$stmt->bindValue('alias', $command->getAlias());
$stmt->bindValue('address', $command->getAddress());
$stmt->bindValue('address2', $command->getAddress2());
$stmt->bindValue('postal_code', $command->getPostalCode());
$stmt->bindValue('city', $command->getCity());
$stmt->bindValue('state', $command->getState());
$stmt->bindValue('country', $command->getCountry());
$stmt->bindValue('default', $isDefault);
$stmt->execute();
}
/**
* Undefault addresses of a given user.
*
* @param int $userId
*/
private function undefaultAddresses($userId)
{
$this->connection->executeUpdate("UPDATE adresses SET defaut = FALSE WHERE user_id = :user_id", [
'user_id' => $userId,
]);
}
/**
* Get the default address of a given user.
*
* @param int $userId
*
* @return int
*/
private function getDefaultAddress($userId)
{
return (int) $this->connection->fetchColumn(
"SELECT id FROM adresses WHERE user_id = :user_id AND defaut = TRUE",
[
'user_id' => $userId,
],
0
);
}
/**
* Check if an address already exists.
*
......@@ -164,13 +71,12 @@ EOF;
*
* @return bool
*/
private function addressExists($userId, $alias)
private function addressExists($userId)
{
return (bool) $this->connection->fetchColumn(
"SELECT 1 FROM adresses WHERE user_id = :user_id AND alias = :alias",
"SELECT 1 FROM adresses WHERE user_id = :user_id",
[
'user_id' => $userId,
'alias' => $alias,
],
0
);
......
......@@ -8,23 +8,21 @@ class AddressFinderTest extends FunctionalTest
{
public function testFindByUserId()
{
$addresses = $this->container['address_finder']->findByUserId(1);
$this->assertCount(3, $addresses);
$addresse = $this->container['address_finder']->findByUserId(1);
$this->assertCount(9, $addresse);
// Check the third address which have an id of 4
// Check the address have an ID of 1
$expectedAddress = [
'id' => '4',
'nom' => 'Third address',
'adresse' => '3 rue Ménars',
'id' => '1',
'nom' => 'Main',
'adresse' => '1 rue Ménars',
'adresse2' => null,
'codepostal' => '75003',
'codepostal' => '75001',
'ville' => 'Paris',
'etat' => null,
'pays' => 'France',
'alias' => 'Third address',
'user_id' => '1',
'defaut' => '0',
];
$this->assertEquals($expectedAddress, $addresses[4]);
$this->assertEquals($expectedAddress, $addresse);
}
}
......@@ -27,25 +27,18 @@ class AddressHandlerTest extends FunctionalTest
$this->assertFalse($this->addressExists(2));
}
public function testAddressUpdate()
public function testAddressCreate()
{
$this->assertTrue($this->addressExists(1));
$this->assertTrue($this->isDefaultAddress(1));
$this->assertTrue($this->addressExists(2));
$this->assertFalse($this->isDefaultAddress(2));
$this->container['command_handler']->handle(new AddressUpdateCommand(2, 1, 'updated', '6 rue Ménars', '', 75002, 'Paris', 'France', 'IDF', true));
$this->assertFalse($this->isDefaultAddress(1));
$this->assertTrue($this->isDefaultAddress(2));
$this->container['command_handler']->handle(new AddressCreateCommand(1, 1, '6 rue Ménars', '', 75002, 'Paris', 'France', 'IDF'));