From 03350f74b0f7102feca7cc89e44dbe736d9cc9f0 Mon Sep 17 00:00:00 2001 From: Daniel Subiabre Date: Tue, 25 Feb 2025 14:18:04 +0100 Subject: [PATCH] Add organization entity to db --- .../User/OrganizationApiResource.php | 5 +- src/ApiResource/User/UserApiResource.php | 12 ++- src/Entity/User/Organization.php | 84 +++++++++++++++++++ src/Entity/User/User.php | 20 +++++ .../User/OrganizationRepository.php | 43 ++++++++++ 5 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 src/Entity/User/Organization.php create mode 100644 src/Repository/User/OrganizationRepository.php diff --git a/src/ApiResource/User/OrganizationApiResource.php b/src/ApiResource/User/OrganizationApiResource.php index fe32eb9..7eb17db 100644 --- a/src/ApiResource/User/OrganizationApiResource.php +++ b/src/ApiResource/User/OrganizationApiResource.php @@ -2,7 +2,9 @@ namespace App\ApiResource\User; +use ApiPlatform\Doctrine\Orm\State\Options; use ApiPlatform\Metadata as API; +use App\Entity\User\Organization; use App\State\ApiResourceStateProcessor; use App\State\ApiResourceStateProvider; use Symfony\Component\Validator\Constraints as Assert; @@ -18,13 +20,14 @@ use Symfony\Component\Validator\Constraints as Assert; #[API\ApiResource( shortName: 'Organization', security: 'is_granted("ORGANIZATION_VIEW", object)', + stateOptions: new Options(entityClass: Organization::class), provider: ApiResourceStateProvider::class, processor: ApiResourceStateProcessor::class, uriTemplate: '/users/{id}/organization', uriVariables: [ 'id' => new API\Link( fromClass: UserApiResource::class, - fromProperty: 'Organization' + fromProperty: 'organization' ), ] )] diff --git a/src/ApiResource/User/UserApiResource.php b/src/ApiResource/User/UserApiResource.php index 15e32ba..a16940c 100644 --- a/src/ApiResource/User/UserApiResource.php +++ b/src/ApiResource/User/UserApiResource.php @@ -78,11 +78,17 @@ class UserApiResource public string $displayName; /** - * For `individual` User types: personal data about the User themselves, - * for `organization` User types: personal data for the organization representative. + * For `individual` User types: personal data about the User themselves.\ + * For `organization` User types: data for the organization representative or person managing the User. */ #[API\ApiProperty(writable: false)] - public ?PersonApiResource $person = null; + public PersonApiResource $person; + + /** + * For `organization` User types only. Legal entity data. + */ + #[API\ApiProperty(writable: false)] + public ?OrganizationApiResource $organization = null; /** * The Accounting for this User monetary movements. diff --git a/src/Entity/User/Organization.php b/src/Entity/User/Organization.php new file mode 100644 index 0000000..76cbf2b --- /dev/null +++ b/src/Entity/User/Organization.php @@ -0,0 +1,84 @@ +user; + } + + public function setUser(User $user): static + { + $this->user = $user; + + return $this; + } + + public function getTaxId(): ?string + { + return $this->taxId; + } + + public function setTaxId(string $taxId): static + { + $this->taxId = $taxId; + + return $this; + } + + public function getLegalName(): ?string + { + return $this->legalName; + } + + public function setLegalName(string $legalName): static + { + $this->legalName = $legalName; + + return $this; + } + + public function getBusinessName(): ?string + { + return $this->businessName; + } + + public function setBusinessName(?string $businessName): static + { + $this->businessName = $businessName; + + return $this; + } +} diff --git a/src/Entity/User/User.php b/src/Entity/User/User.php index 2a953fa..6ba6692 100644 --- a/src/Entity/User/User.php +++ b/src/Entity/User/User.php @@ -78,6 +78,9 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface, Account #[ORM\OneToOne(mappedBy: 'user', cascade: ['persist', 'remove'])] private ?Person $person = null; + #[ORM\OneToOne(mappedBy: 'user', cascade: ['persist', 'remove'])] + private ?Organization $organization = null; + /** * @var list The user roles. Admin only property. */ @@ -268,6 +271,23 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface, Account return $this; } + public function getOrganization(): ?Organization + { + return $this->organization; + } + + public function setOrganization(Organization $organization): static + { + // set the owning side of the relation if necessary + if ($organization->getUser() !== $this) { + $organization->setUser($this); + } + + $this->organization = $organization; + + return $this; + } + /** * @see UserInterface * diff --git a/src/Repository/User/OrganizationRepository.php b/src/Repository/User/OrganizationRepository.php new file mode 100644 index 0000000..3215719 --- /dev/null +++ b/src/Repository/User/OrganizationRepository.php @@ -0,0 +1,43 @@ + + */ +class OrganizationRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Organization::class); + } + + // /** + // * @return Organization[] Returns an array of Organization objects + // */ + // public function findByExampleField($value): array + // { + // return $this->createQueryBuilder('o') + // ->andWhere('o.exampleField = :val') + // ->setParameter('val', $value) + // ->orderBy('o.id', 'ASC') + // ->setMaxResults(10) + // ->getQuery() + // ->getResult() + // ; + // } + + // public function findOneBySomeField($value): ?Organization + // { + // return $this->createQueryBuilder('o') + // ->andWhere('o.exampleField = :val') + // ->setParameter('val', $value) + // ->getQuery() + // ->getOneOrNullResult() + // ; + // } +}