Skip to content

Commit

Permalink
Add Api customer family code (#27)
Browse files Browse the repository at this point in the history
* Add Api customer family code

* remove extend query
  • Loading branch information
ThomasDaSilva authored May 28, 2024
1 parent f91099f commit cf001f5
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
107 changes: 107 additions & 0 deletions Api/Resource/CustomerCustomerFamily.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace CustomerFamily\Api\Resource;

use CustomerFamily\Model\CustomerCustomerFamilyQuery;
use CustomerFamily\Model\CustomerFamilyQuery;
use CustomerFamily\Model\Map\CustomerCustomerFamilyTableMap;
use Propel\Runtime\ActiveRecord\ActiveRecordInterface;
use Propel\Runtime\Exception\PropelException;
use Propel\Runtime\Map\TableMap;
use Symfony\Component\Serializer\Annotation\Ignore;
use Symfony\Component\Serializer\Attribute\Groups;
use Thelia\Api\Resource\Customer as CustomerRessource;
use Thelia\Api\Resource\Order as OrderRessource;
use Thelia\Api\Resource\PropelResourceInterface;
use Thelia\Api\Resource\ResourceAddonInterface;
use Thelia\Api\Resource\ResourceAddonTrait;

class CustomerCustomerFamily implements ResourceAddonInterface
{
use ResourceAddonTrait;

public ?int $id = null;

#[Groups([CustomerRessource::GROUP_ADMIN_READ, CustomerRessource::GROUP_ADMIN_WRITE, CustomerRessource::GROUP_FRONT_READ_SINGLE, OrderRessource::GROUP_ADMIN_READ])]
public ?string $code = null;

#[Ignore] public static function getResourceParent(): string
{
return \Thelia\Api\Resource\Customer::class;
}

#[Ignore] public static function getPropelRelatedTableMap(): ?TableMap
{
return new CustomerCustomerFamilyTableMap();
}

/**
* @throws PropelException
*/
public function buildFromModel(ActiveRecordInterface $activeRecord, PropelResourceInterface $abstractPropelResource): ResourceAddonInterface
{
if (null === $customerCustomerFamily = CustomerCustomerFamilyQuery::create()->filterByCustomerId($activeRecord->getId())->findOne()) {
return $this;
}

$this->setCode(
$activeRecord->hasVirtualColumn('CustomerCustomerFamily_code')
? $activeRecord->getVirtualColumn('CustomerCustomerFamily_code')
: $customerCustomerFamily->getCustomerFamily()->getCode()
);


return $this;
}

public function buildFromArray(array $data, PropelResourceInterface $abstractPropelResource): ResourceAddonInterface
{
$this->setCode($data['code'] ?? null);

return $this;
}

/**
* @throws PropelException
*/
public function doSave(ActiveRecordInterface $activeRecord, PropelResourceInterface $abstractPropelResource): void
{
$model = new \CustomerFamily\Model\CustomerCustomerFamily();

if ($activeRecord->getCustomerCustomerFamily()) {
$id = $activeRecord->getCustomerCustomerFamily()->getCustomerId();
$model = CustomerCustomerFamilyQuery::create()->filterByCustomerId($id)->findOne();
}

$model->setCustomerId($activeRecord->getId());
$model->setCustomerFamilyId(CustomerFamilyQuery::create()->findOneByCode($this->getCode())?->getId());
$model->save();
}

public function doDelete(ActiveRecordInterface $activeRecord, PropelResourceInterface $abstractPropelResource): void
{
$activeRecord->getCustomerCustomerFamily()?->delete();
}

public function getId(): ?int
{
return $this->id;
}

public function setId(?int $id): CustomerCustomerFamily
{
$this->id = $id;
return $this;
}

public function getCode(): ?string
{
return $this->code;
}

public function setCode(?string $code): CustomerCustomerFamily
{
$this->code = $code;
return $this;
}
}
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<descriptive locale="fr_FR">
<title>Famille de clients et prix d'achat</title>
</descriptive>
<version>3.1.6</version>
<version>3.1.7</version>
<author>
<name>Guillaume Barral / Etienne Perriere</name>
<email>gbarral@openstudio.fr / eperriere@openstudio.fr</email>
Expand Down

0 comments on commit cf001f5

Please sign in to comment.