Merge branch 'issue520_reference_address_update' into 'master'

Add new fields to AddressReference for update procedure

See merge request Chill-Projet/chill-bundles!395
This commit is contained in:
Julien Fastré 2022-05-06 13:32:29 +00:00
commit 66c7504b22
5 changed files with 151 additions and 8 deletions

View File

@ -76,7 +76,8 @@ final class AddressReferenceAPIController extends ApiController
protected function customizeQuery(string $action, Request $request, $qb): void
{
if ($request->query->has('postal_code')) {
$qb->where('e.postcode = :postal_code')
$qb->where($qb->expr()->isNull('e.deletedAt'))
->andWhere('e.postcode = :postal_code')
->setParameter('postal_code', $request->query->get('postal_code'));
}
}

View File

@ -12,13 +12,14 @@ declare(strict_types=1);
namespace Chill\MainBundle\Entity;
use Chill\MainBundle\Doctrine\Model\Point;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ORM\Entity
* @ORM\Table(name="chill_main_address_reference", indexes={
* @ORM\Index(name="address_refid", columns={"refId"}, options={"where": "refid != ''"})
* @ORM\Index(name="address_refid", columns={"refId"})
* })
* @ORM\HasLifecycleCallbacks
*/
@ -33,6 +34,18 @@ class AddressReference
*/
private string $addressCanonical = '';
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
* @groups({"read"})
*/
private ?DateTimeImmutable $createdAt = null;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
* @groups({"read"})
*/
private ?DateTimeImmutable $deletedAt = null;
/**
* @ORM\Id
* @ORM\GeneratedValue
@ -89,6 +102,22 @@ class AddressReference
*/
private $streetNumber;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
* @groups({"read"})
*/
private ?DateTimeImmutable $updatedAt = null;
public function getCreatedAt(): ?DateTimeImmutable
{
return $this->createdAt;
}
public function getDeletedAt(): ?DateTimeImmutable
{
return $this->deletedAt;
}
public function getId(): ?int
{
return $this->id;
@ -134,6 +163,25 @@ class AddressReference
return $this->streetNumber;
}
public function getUpdatedAt(): ?DateTimeImmutable
{
return $this->updatedAt;
}
public function setCreatedAt(?DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function setDeletedAt(?DateTimeImmutable $deletedAt): self
{
$this->deletedAt = $deletedAt;
return $this;
}
public function setMunicipalityCode(?string $municipalityCode): self
{
$this->municipalityCode = $municipalityCode;
@ -189,4 +237,11 @@ class AddressReference
return $this;
}
public function setUpdatedAt(?DateTimeImmutable $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
}

View File

@ -130,12 +130,13 @@ final class AddressReferenceRepository implements ObjectRepository
$query
->setFromClause('chill_main_address_reference cma')
->andWhereClause('postcode_id = ?', [$postalCode->getId()]);
->andWhereClause('postcode_id = ?', [$postalCode->getId()])
->andWhereClause('deletedAt IS NULL', []);
$pertinenceClause = ['STRICT_WORD_SIMILARITY(addresscanonical, UNACCENT(?))'];
$pertinenceArgs = [$pattern];
$orWhere = ['addresscanonical %>> UNACCENT(?)'];
$orWhereArgs = [$pattern];
$andWhere = [];
$andWhereArgs = [];
foreach (explode(' ', $pattern) as $part) {
$part = trim($part);
@ -144,8 +145,8 @@ final class AddressReferenceRepository implements ObjectRepository
continue;
}
$orWhere[] = "addresscanonical LIKE '%' || UNACCENT(LOWER(?)) || '%'";
$orWhereArgs[] = $part;
$andWhere[] = "(addresscanonical LIKE '%' || UNACCENT(LOWER(?)) || '%')";
$andWhereArgs[] = $part;
$pertinenceClause[] =
"(EXISTS (SELECT 1 FROM unnest(string_to_array(addresscanonical, ' ')) AS t WHERE starts_with(t, UNACCENT(LOWER(?)))))::int";
$pertinenceClause[] =
@ -154,7 +155,7 @@ final class AddressReferenceRepository implements ObjectRepository
}
$query
->setSelectPertinence(implode(' + ', $pertinenceClause), $pertinenceArgs)
->andWhereClause(implode(' OR ', $orWhere), $orWhereArgs);
->andWhereClause(implode(' AND ', $andWhere), $andWhereArgs);
return $query;
}

View File

@ -0,0 +1,49 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\Migrations\Main;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Add 3 fields on AddressReference.
*/
final class Version20220325134944 extends AbstractMigration
{
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_main_address_reference DROP createdAt');
$this->addSql('ALTER TABLE chill_main_address_reference DROP deletedAt');
$this->addSql('ALTER TABLE chill_main_address_reference DROP updatedAt');
$this->addSql('DROP INDEX address_refid');
$this->addSql('create index address_refid
on chill_main_address_reference (refid)
where ((refid)::text <> \'\'::text)');
}
public function getDescription(): string
{
return 'Add 3 fields on AddressReference';
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_main_address_reference ADD createdAt TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL');
$this->addSql('ALTER TABLE chill_main_address_reference ADD deletedAt TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL');
$this->addSql('ALTER TABLE chill_main_address_reference ADD updatedAt TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL');
$this->addSql('COMMENT ON COLUMN chill_main_address_reference.createdAt IS \'(DC2Type:datetime_immutable)\'');
$this->addSql('COMMENT ON COLUMN chill_main_address_reference.deletedAt IS \'(DC2Type:datetime_immutable)\'');
$this->addSql('COMMENT ON COLUMN chill_main_address_reference.updatedAt IS \'(DC2Type:datetime_immutable)\'');
$this->addSql('DROP INDEX address_refid');
$this->addSql('CREATE INDEX address_refid ON chill_main_address_reference (refId)');
}
}

View File

@ -0,0 +1,37 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\Migrations\Main;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20220506131307 extends AbstractMigration
{
public function down(Schema $schema): void
{
$this->addSql('DROP INDEX chill_internal_address_reference_canonicalized');
$this->addSql('create index chill_internal_address_reference_canonicalized
on chill_main_address_reference using gist (postcode_id, addresscanonical gist_trgm_ops);');
}
public function getDescription(): string
{
return 'Adapt search index on address reference canonicalized';
}
public function up(Schema $schema): void
{
$this->addSql('DROP INDEX chill_internal_address_reference_canonicalized');
$this->addSql('create index chill_internal_address_reference_canonicalized
on chill_main_address_reference using gist (postcode_id, addresscanonical gist_trgm_ops) WHERE deletedat IS NULL;');
}
}