Skip to content

Commit

Permalink
Update import and fix scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
Koen Caerels committed May 6, 2024
1 parent b88b8ab commit 313ae41
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/*
* This file is part of the Yoshi-Kan software.
*
* (c) Koen Caerels
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\YoshiKan\Application\Command\Import\FixEmail;

use App\YoshiKan\Domain\Model\Member\MemberRepository;
use App\YoshiKan\Domain\Model\Member\Subscription;
use App\YoshiKan\Domain\Model\Member\SubscriptionRepository;
use Doctrine\ORM\EntityManagerInterface;

class FixEmailHandler
{
public function __construct(
protected EntityManagerInterface $entityManager,
protected MemberRepository $memberRepository,
protected SubscriptionRepository $subscriptionRepository,
) {
}

public function fixSubscriptions(): bool
{
$allSubscriptions = $this->subscriptionRepository->getAll();

/** @var Subscription $subscription */
foreach ($allSubscriptions as $subscription) {
$filteredEmail = str_replace(' ', '', $subscription->getContactEmail());
$filteredEmail = str_replace(';', '.', $filteredEmail);
$filteredEmail = str_replace('?', '.', $filteredEmail);
$filteredEmail = str_replace('²', '@', $filteredEmail);
$filteredEmail = strtolower($filteredEmail);

$subscription->setContactEmail($filteredEmail);
$this->subscriptionRepository->save($subscription);
$this->entityManager->flush();

echo '<br>'.$subscription->getContactFirstname().' '.$subscription->getContactLastname().' -> '.$subscription->getContactEmail();
ob_flush();
flush();
}

return true;
}

public function fixMembers(): bool
{
$allMembers = $this->memberRepository->getAll();

foreach ($allMembers as $member) {
$filteredEmail = str_replace(' ', '', $member->getContactEmail());
$filteredEmail = str_replace(';', '.', $filteredEmail);
$filteredEmail = str_replace('?', '.', $filteredEmail);
$filteredEmail = str_replace('²', '@', $filteredEmail);
$filteredEmail = strtolower($filteredEmail);

if ('' === $filteredEmail) {
$filteredEmail = '[email protected]';
}

$member->setContactInformation(
contactFirstname: $member->getContactFirstname(),
contactLastname: $member->getContactLastname(),
contactEmail: $filteredEmail,
contactPhone: $member->getContactPhone()
);
$this->memberRepository->save($member);
$this->entityManager->flush();

echo '<br>'.$member->getFirstname().' '.$member->getLastname().' -> '.$member->getContactEmail();
ob_flush();
flush();
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,22 @@ public function import(): bool

$dto = new \stdClass();
$dto->firstName = (string) $worksheet->getCellByColumnAndRow(2, $row)->getValue();
$dto->lastName = mb_strtoupper($worksheet->getCellByColumnAndRow(1, $row)->getValue());
$dto->lastName = mb_strtoupper((string) $worksheet->getCellByColumnAndRow(1, $row)->getValue());
$dto->dateOfBirth = Date::excelToDateTimeObject($worksheet->getCellByColumnAndRow(4, $row)->getValue())->format(\DateTimeInterface::ATOM);
$dto->nationalNumber = (string) $worksheet->getCellByColumnAndRow(5, $row)->getValue();
$dto->street = (string) $worksheet->getCellByColumnAndRow(3, $row)->getValue();
$dto->city = $worksheet->getCellByColumnAndRow(6, $row)->getValue();
$dto->city = (string) $worksheet->getCellByColumnAndRow(6, $row)->getValue();
$dto->postalCode = (string) $worksheet->getCellByColumnAndRow(7, $row)->getValue();
$dto->contactPhone = (string) $worksheet->getCellByColumnAndRow(8, $row)->getValue();
$dto->contactEmail = (string) $worksheet->getCellByColumnAndRow(9, $row)->getValue();

$contactEmail = '[email protected]';
if ('' !== (string) $worksheet->getCellByColumnAndRow(9, $row)->getValue()) {
$contactEmail = (string) $worksheet->getCellByColumnAndRow(9, $row)->getValue();
}
$dto->contactEmail = $contactEmail;
$dto->gender = Gender::X;
$dto->location = $this->locationRepository->getById(LocationMapping::getLocationId($worksheet->getCellByColumnAndRow(26, $row)->getValue()));
$dto->grade = $this->gradeRepository->getById(GradeMapping::getGradeId($worksheet->getCellByColumnAndRow(25, $row)->getValue()));
$dto->location = $this->locationRepository->getById(LocationMapping::getLocationId((string) $worksheet->getCellByColumnAndRow(26, $row)->getValue()));
$dto->grade = $this->gradeRepository->getById(GradeMapping::getGradeId((string) $worksheet->getCellByColumnAndRow(25, $row)->getValue()));
$dto->sporta = (bool) $worksheet->getCellByColumnAndRow(23, $row)->getValue();
$dto->jv = (bool) $worksheet->getCellByColumnAndRow(24, $row)->getValue();
if ($dto->sporta) {
Expand Down
5 changes: 5 additions & 0 deletions application/YoshiKan/Domain/Model/Member/MemberRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,9 @@ public function getActiveMembersByFederationAndLocation(Federation $federation,
* @return Member[]
*/
public function getActiveMembersByLocation(Location $location): array;

/**
* @return Member[]
*/
public function getAll(): array;
}
5 changes: 5 additions & 0 deletions application/YoshiKan/Domain/Model/Member/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,11 @@ public function setNewMemberFields(
$this->addressCity = $addressCity;
}

public function setContactEmail(string $contactEmail): void
{
$this->contactEmail = $contactEmail;
}

public function fullChange(
string $contactFirstname,
string $contactLastname,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,15 @@ public function getActiveMembersByLocation(Location $location): array

return $q->getQuery()->getResult();
}

/**
* @return Member[]
*/
public function getAll(): array
{
$q = $this->createQueryBuilder('t')->andWhere('0 = 0');
$q->addOrderBy('t.id', 'DESC');

return $q->getQuery()->getResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@
<tr>
<td><span class="small gray">Gebdatum</span></td>
<td>
° {{ subscription.dateOfBirth |date('d/m/Y') }} ( {{ subscription.gender.value }} )
° {{ subscription.dateOfBirth |date('d/m/Y') }}
{# ( {{ subscription.gender.value }} )#}
</td>
</tr>
<tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace App\YoshiKan\Infrastructure\Web\Controller;

use App\YoshiKan\Application\Command\Import\FixEmail\FixEmailHandler;
use App\YoshiKan\Application\Command\Import\ImportActiveMember\ImportActiveMembersHandler;
use App\YoshiKan\Application\Command\Import\ImportSubscriptionArchive\ImportSubscriptionArchiveHandler;
use App\YoshiKan\Domain\Model\Member\Federation;
Expand Down Expand Up @@ -117,4 +118,36 @@ public function importArchiveMembersGrades(Request $request): Response

return new JsonResponse($response, 200, []);
}

#[IsGranted('ROLE_DEVELOPER')]
#[Route('/mm/fWXCq7sBpYQngXil/import/fix/subscriptions', name: 'import_fix_subscription_emails', methods: ['GET'])]
public function fixSubscriptionEmails(Request $request): Response
{
$fixHandler = new FixEmailHandler(
$this->entityManager,
$this->entityManager->getRepository(Member::class),
$this->entityManager->getRepository(Subscription::class),
);
$fixHandler->fixSubscriptions();

$response = 'Fixed!';

return new JsonResponse($response, 200, []);
}

#[IsGranted('ROLE_DEVELOPER')]
#[Route('/mm/fWXCq7sBpYQngXil/import/fix/members', name: 'import_fix_member_emails', methods: ['GET'])]
public function fixMemberEmails(Request $request): Response
{
$fixHandler = new FixEmailHandler(
$this->entityManager,
$this->entityManager->getRepository(Member::class),
$this->entityManager->getRepository(Subscription::class),
);
$fixHandler->fixMembers();

$response = 'Fixed!';

return new JsonResponse($response, 200, []);
}
}
3 changes: 2 additions & 1 deletion frontends/member_module/.env.development
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
VITE_API_URL=http://localhost.charlesproxy.com:8080/mm/api
# VITE_API_URL=http://localhost.charlesproxy.com:8080/mm/api
VITE_API_URL=http://localhost:8080/mm/api
VITE_VERSION=0.0.2

0 comments on commit 313ae41

Please sign in to comment.