Skip to content

Commit 10f2487

Browse files
authored
Add customer account order and complaint filtering (#4641)
2 parents 8c9e333 + dd45506 commit 10f2487

14 files changed

Lines changed: 270 additions & 11 deletions

src/Migrations/Version20240816221930.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ public function up(Schema $schema): void
3737

3838
$this->createComplaintStatus(1, ComplaintStatusTypeEnum::STATUS_TYPE_NEW);
3939
$this->createComplaintStatus(2, ComplaintStatusTypeEnum::STATUS_TYPE_RESOLVED);
40+
$this->createComplaintStatus(3, ComplaintStatusTypeEnum::STATUS_TYPE_IN_PROGRESS);
4041

4142
foreach ($this->getAllLocales() as $locale) {
4243
$this->createComplaintStatusTranslations(1, t('New [adjective]', [], Translator::DEFAULT_TRANSLATION_DOMAIN, $locale), $locale);
4344
$this->createComplaintStatusTranslations(2, t('Resolved', [], Translator::DEFAULT_TRANSLATION_DOMAIN, $locale), $locale);
45+
$this->createComplaintStatusTranslations(3, t('In Progress', [], Translator::DEFAULT_TRANSLATION_DOMAIN, $locale), $locale);
4446
}
4547

46-
$this->sql('ALTER SEQUENCE complaint_statuses_id_seq RESTART WITH 3');
48+
$this->sql('ALTER SEQUENCE complaint_statuses_id_seq RESTART WITH 4');
4749

4850
$this->sql('ALTER TABLE complaints ADD status_id INT NULL');
4951
$this->sql('

src/Migrations/Version20240910054629.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@ final class Version20240910054629 extends AbstractMigration implements DomainAwa
1616

1717
private const string COMPLAINT_STATUS_1 = 'complaint_status_1';
1818
private const string COMPLAINT_STATUS_2 = 'complaint_status_2';
19+
private const string COMPLAINT_STATUS_3 = 'complaint_status_3';
1920

2021
#[Override]
2122
public function up(Schema $schema): void
2223
{
2324
$this->insertMailTemplateIfNotExist(self::COMPLAINT_STATUS_1);
2425
$this->insertMailTemplateIfNotExist(self::COMPLAINT_STATUS_2);
26+
$this->insertMailTemplateIfNotExist(self::COMPLAINT_STATUS_3);
2527

2628
$this->sql('UPDATE mail_templates SET complaint_status_id = 1 WHERE name = :mailTemplateName', ['mailTemplateName' => self::COMPLAINT_STATUS_1]);
2729
$this->sql('UPDATE mail_templates SET complaint_status_id = 2 WHERE name = :mailTemplateName', ['mailTemplateName' => self::COMPLAINT_STATUS_2]);
30+
$this->sql('UPDATE mail_templates SET complaint_status_id = 3 WHERE name = :mailTemplateName', ['mailTemplateName' => self::COMPLAINT_STATUS_3]);
2831

2932
foreach ($this->getAllDomainIds() as $domainId) {
3033
$domainLocale = $this->getDomainLocale($domainId);
@@ -52,6 +55,18 @@ public function up(Schema $schema): void
5255
'domainId' => $domainId,
5356
],
5457
);
58+
59+
$this->sql(
60+
'UPDATE mail_templates SET subject = :subject, body = :body WHERE name = :mailTemplateName AND domain_id = :domainId',
61+
[
62+
'mailTemplateName' => self::COMPLAINT_STATUS_3,
63+
'subject' => t('Status of complaint with number {complaint_number} from order number {order_number} created on {date} has changed', [], Translator::DEFAULT_TRANSLATION_DOMAIN, $domainLocale),
64+
'body' => $this->wrapMailTemplateBodyForGrapesJs(
65+
t('Dear customer, <br /><br />Your complaint with number {complaint_number} from order number {order_number} created {date} with preferred resolution {complaint_resolution} is being processed. For more information, visit <a href="{complaint_detail_url}" tabindex="0">complaint detail</a>.<br /><br />Do you need anything else? Visit our <a href="{url}" tabindex="0">website</a>.', [], Translator::DEFAULT_TRANSLATION_DOMAIN, $domainLocale),
66+
),
67+
'domainId' => $domainId,
68+
],
69+
);
5570
}
5671
}
5772
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shopsys\FrameworkBundle\Migrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Override;
9+
use Shopsys\MigrationBundle\Component\Doctrine\Migrations\AbstractMigration;
10+
11+
final class Version20260608120000 extends AbstractMigration
12+
{
13+
#[Override]
14+
public function up(Schema $schema): void
15+
{
16+
$this->sql('ALTER TABLE order_statuses ADD code VARCHAR(255) DEFAULT NULL');
17+
$this->sql('UPDATE order_statuses SET code = \'new\' WHERE id = 1');
18+
$this->sql('UPDATE order_statuses SET code = \'in-progress\' WHERE id = 2');
19+
$this->sql('UPDATE order_statuses SET code = \'done\' WHERE id = 3');
20+
$this->sql('UPDATE order_statuses SET code = \'canceled\' WHERE id = 4');
21+
$this->sql('UPDATE order_statuses SET code = \'withdrawn\' WHERE type = \'withdrawn\'');
22+
$this->sql('
23+
WITH generated_codes AS (
24+
SELECT
25+
os.id,
26+
COALESCE(
27+
NULLIF(TRIM(BOTH \'-\' FROM REGEXP_REPLACE(LOWER(ost.name), \'[^a-z0-9]+\', \'-\', \'g\')), \'\'),
28+
CONCAT(\'order-status-\', os.id)
29+
) AS base_code
30+
FROM order_statuses os
31+
JOIN order_status_translations ost ON ost.id = (
32+
SELECT MIN(ost2.id)
33+
FROM order_status_translations ost2
34+
WHERE ost2.translatable_id = os.id
35+
)
36+
WHERE os.code IS NULL
37+
),
38+
unique_codes AS (
39+
SELECT
40+
id,
41+
CASE
42+
WHEN ROW_NUMBER() OVER (PARTITION BY base_code ORDER BY id) = 1 THEN base_code
43+
ELSE CONCAT(base_code, \'-\', ROW_NUMBER() OVER (PARTITION BY base_code ORDER BY id))
44+
END AS code
45+
FROM generated_codes
46+
)
47+
UPDATE order_statuses os
48+
SET code = unique_codes.code
49+
FROM unique_codes
50+
WHERE unique_codes.id = os.id
51+
');
52+
$this->sql('UPDATE order_statuses SET code = CONCAT(\'order-status-\', id) WHERE code IS NULL OR code = \'\'');
53+
$this->sql('ALTER TABLE order_statuses ALTER code SET NOT NULL');
54+
$this->sql('CREATE UNIQUE INDEX UNIQ_AA08FCA077153098 ON order_statuses (code)');
55+
56+
$this->sql('ALTER TABLE complaint_statuses ADD code VARCHAR(255) DEFAULT NULL');
57+
$this->sql('UPDATE complaint_statuses SET code = \'new\' WHERE id = 1');
58+
$this->sql('UPDATE complaint_statuses SET code = \'resolved\' WHERE id = 2');
59+
$this->sql('UPDATE complaint_statuses SET code = \'in-progress\' WHERE id = 3');
60+
$this->sql('
61+
WITH generated_codes AS (
62+
SELECT
63+
cs.id,
64+
COALESCE(
65+
NULLIF(TRIM(BOTH \'-\' FROM REGEXP_REPLACE(LOWER(cst.name), \'[^a-z0-9]+\', \'-\', \'g\')), \'\'),
66+
CONCAT(\'complaint-status-\', cs.id)
67+
) AS base_code
68+
FROM complaint_statuses cs
69+
JOIN complaint_status_translations cst ON cst.id = (
70+
SELECT MIN(cst2.id)
71+
FROM complaint_status_translations cst2
72+
WHERE cst2.translatable_id = cs.id
73+
)
74+
WHERE cs.code IS NULL
75+
),
76+
unique_codes AS (
77+
SELECT
78+
id,
79+
CASE
80+
WHEN ROW_NUMBER() OVER (PARTITION BY base_code ORDER BY id) = 1 THEN base_code
81+
ELSE CONCAT(base_code, \'-\', ROW_NUMBER() OVER (PARTITION BY base_code ORDER BY id))
82+
END AS code
83+
FROM generated_codes
84+
)
85+
UPDATE complaint_statuses cs
86+
SET code = unique_codes.code
87+
FROM unique_codes
88+
WHERE unique_codes.id = cs.id
89+
');
90+
$this->sql('UPDATE complaint_statuses SET code = CONCAT(\'complaint-status-\', id) WHERE code IS NULL OR code = \'\'');
91+
$this->sql('ALTER TABLE complaint_statuses ALTER code SET NOT NULL');
92+
$this->sql('CREATE UNIQUE INDEX UNIQ_57E9379377153098 ON complaint_statuses (code)');
93+
}
94+
}

src/Model/Complaint/Status/ComplaintStatus.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ class ComplaintStatus extends AbstractTranslatableEntity
3232
#[Override]
3333
protected $id;
3434

35+
/**
36+
* @var string
37+
*/
38+
#[AsMcpColumn]
39+
#[ORM\Column(type: 'string', length: 255, unique: true)]
40+
protected $code;
41+
3542
/**
3643
* @var \Doctrine\Common\Collections\Collection<string, \Shopsys\FrameworkBundle\Model\Complaint\Status\ComplaintStatusTranslation>
3744
*/
@@ -46,10 +53,11 @@ class ComplaintStatus extends AbstractTranslatableEntity
4653
#[ORM\Column(type: 'string', length: 25)]
4754
protected $statusType;
4855

49-
public function __construct(ComplaintStatusData $complaintStatusData, string $statusType)
56+
public function __construct(ComplaintStatusData $complaintStatusData, string $statusType, string $code)
5057
{
5158
$this->translations = new ArrayCollection();
5259
$this->statusType = $statusType;
60+
$this->code = $code;
5361
$this->setData($complaintStatusData);
5462
}
5563

@@ -71,6 +79,14 @@ public function getId()
7179
return $this->id;
7280
}
7381

82+
/**
83+
* @return string
84+
*/
85+
public function getCode()
86+
{
87+
return $this->code;
88+
}
89+
7490
/**
7591
* @param string|null $locale
7692
* @return string

src/Model/Complaint/Status/ComplaintStatusFacade.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Shopsys\FrameworkBundle\Model\Complaint\Status;
66

77
use Doctrine\ORM\EntityManagerInterface;
8+
use Shopsys\FrameworkBundle\Component\String\TransformStringHelper;
89
use Shopsys\FrameworkBundle\Model\Complaint\Mail\ComplaintMail;
910
use Shopsys\FrameworkBundle\Model\Mail\MailTemplateFacade;
1011

@@ -16,6 +17,7 @@ public function __construct(
1617
protected readonly ComplaintStatusRepository $complaintStatusRepository,
1718
protected readonly MailTemplateFacade $mailTemplateFacade,
1819
protected readonly ComplaintMail $complaintMail,
20+
protected readonly TransformStringHelper $transformStringHelper,
1921
) {
2022
}
2123

@@ -24,6 +26,7 @@ public function create(ComplaintStatusData $complaintStatusData): ComplaintStatu
2426
$complaintStatus = $this->complaintStatusFactory->create(
2527
$complaintStatusData,
2628
ComplaintStatusTypeEnum::STATUS_TYPE_IN_PROGRESS,
29+
$this->createUniqueCode($complaintStatusData),
2730
);
2831
$this->em->persist($complaintStatus);
2932
$this->em->flush();
@@ -86,8 +89,46 @@ public function getAll(): array
8689
return $this->complaintStatusRepository->getAll();
8790
}
8891

92+
/**
93+
* @param string[] $codes
94+
* @return \Shopsys\FrameworkBundle\Model\Complaint\Status\ComplaintStatus[]
95+
*/
96+
public function getAllByCodes(array $codes): array
97+
{
98+
return $this->complaintStatusRepository->getAllByCodes($codes);
99+
}
100+
89101
public function getDefault(): ComplaintStatus
90102
{
91103
return $this->complaintStatusRepository->getDefault();
92104
}
105+
106+
protected function createUniqueCode(ComplaintStatusData $complaintStatusData): string
107+
{
108+
$baseCode = $this->createCodeFromComplaintStatusData($complaintStatusData);
109+
$code = $baseCode;
110+
$suffix = 2;
111+
112+
while ($this->complaintStatusRepository->findByCode($code) !== null) {
113+
$code = $baseCode . '-' . $suffix;
114+
$suffix++;
115+
}
116+
117+
return $code;
118+
}
119+
120+
protected function createCodeFromComplaintStatusData(ComplaintStatusData $complaintStatusData): string
121+
{
122+
foreach ($complaintStatusData->name as $name) {
123+
if ($name !== null && trim($name) !== '') {
124+
$code = $this->transformStringHelper->stringToFriendlyUrlSlug($name);
125+
126+
if ($code !== '') {
127+
return $code;
128+
}
129+
}
130+
}
131+
132+
return 'complaint-status';
133+
}
93134
}

src/Model/Complaint/Status/ComplaintStatusFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ public function __construct(
1414
) {
1515
}
1616

17-
public function create(ComplaintStatusData $data, string $statusType): ComplaintStatus
17+
public function create(ComplaintStatusData $data, string $statusType, string $code): ComplaintStatus
1818
{
1919
$this->complaintStatusTypeEnum->validateCase($statusType);
2020
$entityClassName = $this->entityNameResolver->resolve(ComplaintStatus::class);
2121

22-
return new $entityClassName($data, $statusType);
22+
return new $entityClassName($data, $statusType, $code);
2323
}
2424
}

src/Model/Complaint/Status/ComplaintStatusRepository.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ public function getById(int $complaintStatusId): ComplaintStatus
4040
return $complaintStatus;
4141
}
4242

43+
public function findByCode(string $code): ?ComplaintStatus
44+
{
45+
return $this->getComplaintStatusRepository()->findOneBy(['code' => $code]);
46+
}
47+
48+
/**
49+
* @param string[] $codes
50+
* @return \Shopsys\FrameworkBundle\Model\Complaint\Status\ComplaintStatus[]
51+
*/
52+
public function getAllByCodes(array $codes): array
53+
{
54+
return $this->getComplaintStatusRepository()->findBy(['code' => $codes]);
55+
}
56+
4357
public function getDefault(): ComplaintStatus
4458
{
4559
$complaintStatus = $this->getComplaintStatusRepository()->findOneBy(['statusType' => ComplaintStatusTypeEnum::STATUS_TYPE_NEW]);

src/Model/Order/Status/OrderStatus.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ class OrderStatus extends AbstractTranslatableEntity
3333
#[Override]
3434
protected $id;
3535

36+
/**
37+
* @var string
38+
*/
39+
#[AsMcpColumn]
40+
#[ORM\Column(type: 'string', length: 255, unique: true)]
41+
protected $code;
42+
3643
/**
3744
* @var \Doctrine\Common\Collections\Collection<string, \Shopsys\FrameworkBundle\Model\Order\Status\OrderStatusTranslation>
3845
*/
@@ -49,11 +56,13 @@ class OrderStatus extends AbstractTranslatableEntity
4956

5057
/**
5158
* @param string $type
59+
* @param string $code
5260
*/
53-
public function __construct(OrderStatusData $orderStatusData, $type)
61+
public function __construct(OrderStatusData $orderStatusData, $type, $code)
5462
{
5563
$this->translations = new ArrayCollection();
5664
$this->type = $type;
65+
$this->code = $code;
5766
$this->setData($orderStatusData);
5867
}
5968

@@ -75,6 +84,14 @@ public function getId()
7584
return $this->id;
7685
}
7786

87+
/**
88+
* @return string
89+
*/
90+
public function getCode()
91+
{
92+
return $this->code;
93+
}
94+
7895
#[EntityLogIdentify(EntityLogIdentify::IS_LOCALIZED)]
7996
/**
8097
* @param string|null $locale

0 commit comments

Comments
 (0)