Skip to content

Commit

Permalink
YK-70 + YK-45
Browse files Browse the repository at this point in the history
  • Loading branch information
koencaerels committed Jan 20, 2024
1 parent e8efe00 commit 3b9fccf
Show file tree
Hide file tree
Showing 14 changed files with 482 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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\Member\ChangeLicense;

class ChangeLicense
{
// —————————————————————————————————————————————————————————————————————————
// Constructor
// —————————————————————————————————————————————————————————————————————————

private function __construct(
protected int $memberId,
protected int $federationId,
) {
}

// —————————————————————————————————————————————————————————————————————————
// Hydrate from a json command
// —————————————————————————————————————————————————————————————————————————

public static function hydrateFromJson(\stdClass $json): self
{
return new self(
$json->memberId,
$json->federationId,
);
}

// —————————————————————————————————————————————————————————————————————————
// Getters
// —————————————————————————————————————————————————————————————————————————

public function getMemberId(): int
{
return $this->memberId;
}

public function getFederationId(): int
{
return $this->federationId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?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\Member\ChangeLicense;

use App\YoshiKan\Application\Command\Common\SubscriptionItemsFactory;
use App\YoshiKan\Application\Query\Member\Readmodel\SettingsReadModel;
use App\YoshiKan\Domain\Model\Member\MemberRepository;
use App\YoshiKan\Domain\Model\Member\SettingsCode;
use App\YoshiKan\Domain\Model\Member\Subscription;
use App\YoshiKan\Domain\Model\Member\SubscriptionItemRepository;
use App\YoshiKan\Domain\Model\Member\SubscriptionStatus;
use App\YoshiKan\Domain\Model\Member\SubscriptionType;
use App\YoshiKan\Infrastructure\Database\Member\FederationRepository;
use App\YoshiKan\Infrastructure\Database\Member\LocationRepository;
use App\YoshiKan\Infrastructure\Database\Member\SettingsRepository;
use App\YoshiKan\Infrastructure\Database\Member\SubscriptionRepository;
use Doctrine\ORM\EntityManagerInterface;

class ChangeLicenseHandler
{
// ———————————————————————————————————————————————————————————————
// Constructor
// ———————————————————————————————————————————————————————————————

public function __construct(
protected MemberRepository $memberRepository,
protected SubscriptionRepository $subscriptionRepository,
protected SubscriptionItemRepository $subscriptionItemRepository,
protected FederationRepository $federationRepository,
protected LocationRepository $locationRepository,
protected SettingsRepository $settingsRepository,
protected EntityManagerInterface $entityManager,
) {
}

// ———————————————————————————————————————————————————————————————
// Handle
// ———————————————————————————————————————————————————————————————

public function change(ChangeLicense $command): \stdClass
{
$member = $this->memberRepository->getById($command->getMemberId());
$federation = $this->federationRepository->getById($command->getFederationId());
$settings = $this->settingsRepository->findByCode(SettingsCode::ACTIVE->value);

$extraTraining = false;
if (3 === $member->getNumberOfTraining()) {
$extraTraining = true;
}

// -- calculate the new license dates -------------------------------

$now = new \DateTimeImmutable();
$licenseStart = $now->setDate((int) $now->format('Y'), (int) $now->format('m'), 1);
$licenseEnd = $licenseStart->modify('+1 year');

// -- create a new subscription -------------------------------------

$subscription = Subscription::make(
$this->subscriptionRepository->nextIdentity(),
$member->getContactFirstname(),
$member->getContactLastname(),
$member->getContactEmail(),
$member->getContactPhone(),
$member->getFirstname(),
$member->getLastname(),
$member->getDateOfBirth(),
$member->getGender(),
SubscriptionType::RENEWAL_LICENSE,
$member->getNumberOfTraining(),
$extraTraining,
false,
false,
false,
'Wijziging vergunning naar '.$federation->getName(),
$member->getLocation(),
json_decode(json_encode(SettingsReadModel::hydrateFromModel($settings)), true),
$federation,
$member->getMemberSubscriptionStart(),
$member->getMemberSubscriptionEnd(),
0,
false,
false,
true,
$licenseStart,
$licenseEnd,
$federation->getYearlySubscriptionFee(),
true,
false,
);

$subscription->setMember($member);
$subscription->changeStatus(SubscriptionStatus::AWAITING_PAYMENT);
$subscription->calculate();
$this->subscriptionRepository->save($subscription);
$this->entityManager->flush();

// -- make some subscription lines ----------------------------------

$subscriptionItemFactory = new SubscriptionItemsFactory(
$this->subscriptionRepository,
$this->subscriptionItemRepository
);
$resultItems = $subscriptionItemFactory->saveItemsFromSubscription(
$subscription,
$federation,
$settings
);

// -- flag new dates in the member ----------------------------------

$member->setLicenseDates(
$subscription->getLicenseStart(),
$subscription->getLicenseEnd()
);
$member->syncFromSubscription(
federation: $federation,
numberOfTraining: $subscription->getNumberOfTraining(),
isHalfYearSubscription: $subscription->isMemberSubscriptionIsHalfYear(),
);

$this->memberRepository->save($member);
$this->entityManager->flush();

// -- compile a result class ----------------------------------------

$subscriptionId = $this->subscriptionRepository->getMaxId();
$result = new \stdClass();
$result->id = $subscriptionId;
$result->reference = 'YKS-'.$subscriptionId.': '.$member->getFirstname().' '.$member->getLastName();

return $result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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\Member\ChangeLicense;

trait ChangeLicenseTrait
{
public function changeLicense(\stdClass $jsonCommand): \stdClass
{
$this->permission->CheckRole(['ROLE_DEVELOPER', 'ROLE_ADMIN', 'ROLE_CHIEF_EDITOR']);

$command = ChangeLicense::hydrateFromJson($jsonCommand);
$handler = new ChangeLicenseHandler(
memberRepository: $this->memberRepository,
subscriptionRepository: $this->subscriptionRepository,
subscriptionItemRepository: $this->subscriptionItemRepository,
federationRepository: $this->federationRepository,
locationRepository: $this->locationRepository,
settingsRepository: $this->settingsRepository,
entityManager: $this->entityManager,
);

$result = $handler->change($command);
$this->entityManager->flush();

return $result;
}
}
2 changes: 2 additions & 0 deletions application/YoshiKan/Application/MemberCommandBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use App\YoshiKan\Application\Command\Member\ChangeFederation\ChangeFederationTrait;
use App\YoshiKan\Application\Command\Member\ChangeGrade\ChangeGradeTrait;
use App\YoshiKan\Application\Command\Member\ChangeGroup\ChangeGroupTrait;
use App\YoshiKan\Application\Command\Member\ChangeLicense\ChangeLicenseTrait;
use App\YoshiKan\Application\Command\Member\ChangeLocation\ChangeLocationTrait;
use App\YoshiKan\Application\Command\Member\ChangeMemberDetails\ChangeMemberDetailsTrait;
use App\YoshiKan\Application\Command\Member\ChangeMemberGrade\ChangeMemberGradeTrait;
Expand Down Expand Up @@ -115,6 +116,7 @@ class MemberCommandBus
use SendPaymentReceivedConfirmationMailTrait;
use NewMemberSubscriptionMailTrait;
use ChangeSubscriptionDetailsTrait;
use ChangeLicenseTrait;

// -- member images ---------------------------------------------------------
use UploadProfileImageTrait;
Expand Down
3 changes: 2 additions & 1 deletion application/YoshiKan/Application/Query/Member/GetMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public function search(MemberSearchModel $searchModel): MemberReadModelCollectio
$location,
$grade,
$minYearOfBirth,
$maxYearOfBirth
$maxYearOfBirth,
$searchModel->isActive(),
);

// -- covert to readmodel collection ------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ private function __construct(
protected int $gradeId,
protected int $yearOfBirth,
protected int $groupId,
protected ?bool $isActive = null,
) {
}

Expand All @@ -39,6 +40,7 @@ public static function hydrateFromJson(\stdClass $json): self
$gradeId = 0;
$yearOfBirth = 0;
$groupId = 0;
$isActive = null;
if (isset($json->locationId)) {
$locationId = intval($json->locationId);
}
Expand All @@ -51,13 +53,17 @@ public static function hydrateFromJson(\stdClass $json): self
if (isset($json->group)) {
$groupId = intval($json->group->id);
}
if (isset($json->isActive)) {
$isActive = boolval($json->isActive);
}

return new self(
$keyword,
$locationId,
$gradeId,
$yearOfBirth,
$groupId
$groupId,
$isActive,
);
}

Expand Down Expand Up @@ -89,4 +95,9 @@ public function getGroupId(): int
{
return $this->groupId;
}

public function isActive(): ?bool
{
return $this->isActive;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,18 @@ public function extendMemberSubscription(int $id, Request $request): JsonRespons
return new JsonResponse($response, 200, $this->apiAccess);
}

#[Route('/mm/api/member/{id}/change-license', requirements: ['id' => '\d+'], methods: ['POST', 'PUT'])]
public function changeMemberLicense(int $id, Request $request): JsonResponse
{
$command = json_decode($request->request->get('command'));
$response = $this->commandBus->changeLicense($command);

$result_mollie = $this->commandBus->createMolliePaymentLink($response->id);
$result_mail = $this->commandBus->sendMemberExtendSubscriptionMail($response->id);

return new JsonResponse($response, 200, $this->apiAccess);
}

/**
* @throws \Exception
*/
Expand Down
22 changes: 22 additions & 0 deletions frontends/member_module/src/api/command/changeMemberLicense.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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.
*/

import axios from "axios";

export interface ChangeMemberLicenseCommand {
memberId: number;
federationId: number;
}

export async function changeMemberLicense(command: ChangeMemberLicenseCommand) {
const formData = new FormData();
formData.append('command', JSON.stringify(command));
const response= await axios.post<boolean>(`/member/${command.memberId}/change-license`, formData);
return response.data;
}
1 change: 1 addition & 0 deletions frontends/member_module/src/api/query/searchMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface MemberSearchModel {
grade?: Grade,
yearOfBirth?: string,
group?: Group,
isActive?: boolean,
}

export async function searchMembers(searchModel: MemberSearchModel) {
Expand Down
4 changes: 2 additions & 2 deletions frontends/member_module/src/components/member/memberBadge.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
:style="'background-color: #'+member.grade.color">
<div v-if="member.profileImage !== ''"
class="text-right rounded-l-xl"
style="width:160px;height:140px;float:right;">
<Image style="width:140px;height:140px;"
style="width:175px;height:160px;float:right;">
<Image style="width:160px;height:160px;"
:src="apiUrl+'/member/'+member.id+'/profile-image?t=' + timestamp"
:alt="member.firstname+' '+member.lastname" preview/>
</div>
Expand Down
Loading

0 comments on commit 3b9fccf

Please sign in to comment.