Skip to content

Commit 98f6c69

Browse files
committed
feat: add Employee API endpoints
1 parent a8772f1 commit 98f6c69

File tree

2 files changed

+196
-0
lines changed

2 files changed

+196
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright since 2007 PrestaShop SA and Contributors
4+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5+
*
6+
* NOTICE OF LICENSE
7+
*
8+
* This source file is subject to the Academic Free License version 3.0
9+
* that is bundled with this package in the file LICENSE.md.
10+
* It is also available through the world-wide-web at this URL:
11+
* https://opensource.org/licenses/AFL-3.0
12+
* If you did not receive a copy of the license and are unable to
13+
* obtain it through the world-wide-web, please send an email
14+
* to license@prestashop.com so we can send you a copy immediately.
15+
*
16+
* @author PrestaShop SA and Contributors <contact@prestashop.com>
17+
* @copyright Since 2007 PrestaShop SA and Contributors
18+
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\Employee;
24+
25+
use ApiPlatform\Metadata\ApiProperty;
26+
use ApiPlatform\Metadata\ApiResource;
27+
use PrestaShop\PrestaShop\Core\Domain\Employee\Command\BulkDeleteEmployeeCommand;
28+
use PrestaShop\PrestaShop\Core\Domain\Employee\Command\BulkUpdateEmployeeStatusCommand;
29+
use PrestaShop\PrestaShop\Core\Domain\Employee\Exception\CannotDeleteEmployeeException;
30+
use PrestaShop\PrestaShop\Core\Domain\Employee\Exception\EmployeeConstraintException;
31+
use PrestaShop\PrestaShop\Core\Domain\Employee\Exception\EmployeeNotFoundException;
32+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSDelete;
33+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate;
34+
use Symfony\Component\HttpFoundation\Response;
35+
use Symfony\Component\Validator\Constraints as Assert;
36+
37+
#[ApiResource(
38+
operations: [
39+
new CQRSDelete(
40+
uriTemplate: '/employees/bulk-delete',
41+
CQRSCommand: BulkDeleteEmployeeCommand::class,
42+
scopes: ['employee_write'],
43+
),
44+
new CQRSUpdate(
45+
uriTemplate: '/employees/bulk-update-status',
46+
output: false,
47+
CQRSCommand: BulkUpdateEmployeeStatusCommand::class,
48+
scopes: ['employee_write'],
49+
CQRSCommandMapping: [
50+
'[enabled]' => '[status]',
51+
],
52+
),
53+
],
54+
exceptionToStatus: [
55+
EmployeeConstraintException::class => Response::HTTP_UNPROCESSABLE_ENTITY,
56+
EmployeeNotFoundException::class => Response::HTTP_NOT_FOUND,
57+
CannotDeleteEmployeeException::class => Response::HTTP_UNPROCESSABLE_ENTITY,
58+
],
59+
)]
60+
class BulkEmployees
61+
{
62+
/**
63+
* @var int[]
64+
*/
65+
#[ApiProperty(openapiContext: ['type' => 'array', 'items' => ['type' => 'integer'], 'example' => [1, 3]])]
66+
#[Assert\NotBlank]
67+
public array $employeeIds;
68+
69+
public bool $enabled;
70+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
/**
3+
* Copyright since 2007 PrestaShop SA and Contributors
4+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5+
*
6+
* NOTICE OF LICENSE
7+
*
8+
* This source file is subject to the Academic Free License version 3.0
9+
* that is bundled with this package in the file LICENSE.md.
10+
* It is also available through the world-wide-web at this URL:
11+
* https://opensource.org/licenses/AFL-3.0
12+
* If you did not receive a copy of the license and are unable to
13+
* obtain it through the world-wide-web, please send an email
14+
* to license@prestashop.com so we can send you a copy immediately.
15+
*
16+
* @author PrestaShop SA and Contributors <contact@prestashop.com>
17+
* @copyright Since 2007 PrestaShop SA and Contributors
18+
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\Employee;
24+
25+
use ApiPlatform\Metadata\ApiProperty;
26+
use ApiPlatform\Metadata\ApiResource;
27+
use PrestaShop\PrestaShop\Core\Domain\Employee\Command\DeleteEmployeeCommand;
28+
use PrestaShop\PrestaShop\Core\Domain\Employee\Command\EditEmployeeCommand;
29+
use PrestaShop\PrestaShop\Core\Domain\Employee\Command\ToggleEmployeeStatusCommand;
30+
use PrestaShop\PrestaShop\Core\Domain\Employee\Exception\CannotDeleteEmployeeException;
31+
use PrestaShop\PrestaShop\Core\Domain\Employee\Exception\EmailAlreadyUsedException;
32+
use PrestaShop\PrestaShop\Core\Domain\Employee\Exception\EmployeeCannotChangeItselfException;
33+
use PrestaShop\PrestaShop\Core\Domain\Employee\Exception\EmployeeConstraintException;
34+
use PrestaShop\PrestaShop\Core\Domain\Employee\Exception\EmployeeNotFoundException;
35+
use PrestaShop\PrestaShop\Core\Domain\Employee\Exception\InvalidProfileException;
36+
use PrestaShop\PrestaShop\Core\Domain\Employee\Exception\MissingShopAssociationException;
37+
use PrestaShop\PrestaShop\Core\Domain\Employee\Query\GetEmployeeForEditing;
38+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSDelete;
39+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet;
40+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSPartialUpdate;
41+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate;
42+
use Symfony\Component\HttpFoundation\Response;
43+
use Symfony\Component\Validator\Constraints as Assert;
44+
45+
#[ApiResource(
46+
operations: [
47+
new CQRSGet(
48+
uriTemplate: '/employees/{employeeId}',
49+
requirements: ['employeeId' => '\d+'],
50+
CQRSQuery: GetEmployeeForEditing::class,
51+
scopes: ['employee_read'],
52+
CQRSQueryMapping: self::QUERY_MAPPING,
53+
),
54+
new CQRSPartialUpdate(
55+
uriTemplate: '/employees/{employeeId}',
56+
requirements: ['employeeId' => '\d+'],
57+
CQRSCommand: EditEmployeeCommand::class,
58+
CQRSQuery: GetEmployeeForEditing::class,
59+
scopes: ['employee_write'],
60+
CQRSQueryMapping: self::QUERY_MAPPING,
61+
CQRSCommandMapping: self::EDIT_COMMAND_MAPPING,
62+
),
63+
new CQRSDelete(
64+
uriTemplate: '/employees/{employeeId}',
65+
requirements: ['employeeId' => '\d+'],
66+
CQRSCommand: DeleteEmployeeCommand::class,
67+
scopes: ['employee_write'],
68+
),
69+
new CQRSUpdate(
70+
uriTemplate: '/employees/{employeeId}/toggle-status',
71+
requirements: ['employeeId' => '\d+'],
72+
output: false,
73+
CQRSCommand: ToggleEmployeeStatusCommand::class,
74+
scopes: ['employee_write'],
75+
),
76+
],
77+
exceptionToStatus: [
78+
EmployeeConstraintException::class => Response::HTTP_UNPROCESSABLE_ENTITY,
79+
EmployeeNotFoundException::class => Response::HTTP_NOT_FOUND,
80+
CannotDeleteEmployeeException::class => Response::HTTP_UNPROCESSABLE_ENTITY,
81+
EmailAlreadyUsedException::class => Response::HTTP_CONFLICT,
82+
InvalidProfileException::class => Response::HTTP_UNPROCESSABLE_ENTITY,
83+
MissingShopAssociationException::class => Response::HTTP_UNPROCESSABLE_ENTITY,
84+
EmployeeCannotChangeItselfException::class => Response::HTTP_FORBIDDEN,
85+
],
86+
)]
87+
class Employee
88+
{
89+
#[ApiProperty(identifier: true)]
90+
public int $employeeId;
91+
92+
#[Assert\NotBlank]
93+
public string $firstName;
94+
95+
#[Assert\NotBlank]
96+
public string $lastName;
97+
98+
#[Assert\Email]
99+
public string $email;
100+
101+
public int $defaultPageId;
102+
103+
public int $languageId;
104+
105+
public bool $enabled;
106+
107+
public int $profileId;
108+
109+
/**
110+
* @var int[]
111+
*/
112+
public array $shopIds;
113+
114+
public string $avatarUrl;
115+
116+
public bool $hasEnabledGravatar;
117+
118+
public const QUERY_MAPPING = [
119+
'[isActive]' => '[active]',
120+
'[shopAssociation]' => '[shopIds]',
121+
];
122+
123+
public const EDIT_COMMAND_MAPPING = [
124+
'[shopIds]' => '[shopAssociation]',
125+
];
126+
}

0 commit comments

Comments
 (0)