Skip to content

Commit

Permalink
Feature: Multiple merchants on voucher sets
Browse files Browse the repository at this point in the history
  • Loading branch information
ok200paul committed Nov 7, 2024
1 parent e1c74ed commit ba62577
Show file tree
Hide file tree
Showing 10 changed files with 972 additions and 808 deletions.
44 changes: 37 additions & 7 deletions app/Http/Controllers/Api/V1/ApiVoucherRedemptionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,48 @@ public function index(): JsonResponse
status : 200,
description: '',
)]
#[Response(
content : '{"meta":{"responseCode":400,"limit":50,"offset":0,"message":"Invalid merchant team."},"data":{}',
status : 400,
description: 'Shown when the team that the redeeming user is not valid for this voucher under redemption. Merchant team might not have approved their involvement yet.',
)]
#[Response(
content : '{"meta":{"responseCode":400,"limit":50,"offset":0,"message":"This voucher has already been fully redeemed, no redemption made this time."},"data":{}',
status : 400,
description: 'Voucher fully redeemed. No redemption takes place',
)]
#[Response(
content : '{"meta":{"responseCode":400,"limit":50,"offset":0,"message":"Requested amount is greater than voucher value remaining, no redemption made this time."},"data":{}',
status : 400,
description: 'Voucher redemption amount too high',
)]
#[Response(
content : '{"meta":{"responseCode":404,"limit":50,"offset":0,"message":"Not found."},"data":{}',
status : 404,
description: '',
)]
#[Response(
content : '{"meta":{"responseCode":429,"limit":50,"offset":0,"message":"Too many redemption attempts, please wait."},"data":{}',
status : 429,
description: 'Too many redemption requests. Only 1 redemption may be made per voucher per minute.',
)]
#[Response(
content : '{"meta":{"responseCode":500,"limit":50,"offset":0,"message":"Error"},"data":{}',
status : 500,
description: 'Server Error',
)]
public function store(): JsonResponse
{
$validationArray = [
'voucher_id' => [
'voucher_id' => [
'required',
Rule::exists('vouchers', 'id'),
],
'voucher_set_id' => [
'required',
Rule::exists('voucher_sets', 'id'),
],
'amount' => [
'amount' => [
'integer',
'min:1',
],
Expand Down Expand Up @@ -109,9 +139,10 @@ public function store(): JsonResponse
* Ensure the users current team is a merchant for the voucher set.
*/
$voucherSetMerchantTeamIds = VoucherSetMerchantTeam::where('voucher_set_id', $voucherSetId)
->pluck('merchant_team_id')
->unique()
->toArray();
->whereNotNull('voucher_set_merchant_team_approval_request_id')
->pluck('merchant_team_id')
->unique()
->toArray();

if (!in_array(Auth::user()->current_team_id, $voucherSetMerchantTeamIds)) {
$this->responseCode = 400;
Expand Down Expand Up @@ -172,8 +203,7 @@ public function store(): JsonResponse
$this->message = ApiResponse::RESPONSE_REDEMPTION_SUCCESSFUL->value . ' ' . $redemptionMessageSuffix;
$this->data = $redemption;

}
catch (Exception $e) {
} catch (Exception $e) {
$this->responseCode = 500;
$this->message = ApiResponse::RESPONSE_ERROR->value . ':' . $e->getMessage();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Events\VoucherSetMerchantTeamApprovalRequest\VoucherSetMerchantTeamApprovalRequestWasApproved;
use App\Models\User;
use App\Models\VoucherSetMerchantTeam;
use App\Notifications\Slack\VoucherSetMerchantTeamApprovalRequest\VoucherSetMerchantTeamApprovalRequestApprovedNotification;
use App\Services\AuditItemService;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand All @@ -15,7 +16,9 @@ class HandleVoucherSetMerchantTeamApprovalRequestWasApprovedEvent implements Sho
/**
* Create the event listener.
*/
public function __construct() {}
public function __construct()
{
}

/**
* Handle the event.
Expand All @@ -24,8 +27,28 @@ public function __construct() {}
*/
public function handle(VoucherSetMerchantTeamApprovalRequestWasApproved $event): void
{
$user = User::first();

/**
* Mark the participating merchant team with the ID of the approval
*/
$voucherSetMerchantTeam = VoucherSetMerchantTeam::where(
column : 'voucher_set_id',
operator: $event->voucherSetMerchantTeamApprovalRequest->voucher_set_id
)->where(
column : 'merchant_team_id',
operator: $event->voucherSetMerchantTeamApprovalRequest->merchant_team_id
)->first();

if($voucherSetMerchantTeam)
{
$voucherSetMerchantTeam->voucher_set_merchant_team_approval_request_id = $event->voucherSetMerchantTeamApprovalRequest->id;
$voucherSetMerchantTeam->save();
}

/**
* Notify the user
*/
$user = User::first();
$user->notify(new VoucherSetMerchantTeamApprovalRequestApprovedNotification($event->voucherSetMerchantTeamApprovalRequest));

AuditItemService::createAuditItemForEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Notifications\Mail\VoucherSetMerchantTeamApprovalRequest;

use App\Models\Team;
use App\Models\User;
use App\Models\VoucherSet;
use App\Models\VoucherSetMerchantTeamApprovalRequest;
Expand All @@ -21,7 +22,9 @@ class VoucherSetMerchantTeamApprovalRequestEmailNotification extends Notificatio
*
* @param VoucherSetMerchantTeamApprovalRequest $voucherSetMerchantTeamApprovalRequest
*/
public function __construct(public VoucherSetMerchantTeamApprovalRequest $voucherSetMerchantTeamApprovalRequest) {}
public function __construct(public VoucherSetMerchantTeamApprovalRequest $voucherSetMerchantTeamApprovalRequest)
{
}

/**
* Get the notification's delivery channels.
Expand Down Expand Up @@ -51,7 +54,7 @@ public function toMail(object $notifiable): MailMessage
expiry : now()->addDays(2),
redirectPath: '/my-voucher-set-merchant-team-approval-request/' . $this->voucherSetMerchantTeamApprovalRequest->id . '?selected=approve'
);
$urlReject = BounceService::generateSignedUrlForUser(
$urlReject = BounceService::generateSignedUrlForUser(
user : $user,
expiry : now()->addDays(2),
redirectPath: '/my-voucher-set-merchant-team-approval-request/' . $this->voucherSetMerchantTeamApprovalRequest->id . '?selected=reject'
Expand All @@ -66,11 +69,14 @@ public function toMail(object $notifiable): MailMessage
]
)->find($this->voucherSetMerchantTeamApprovalRequest->voucher_set_id);

$merchantTeam = Team::find($this->voucherSetMerchantTeamApprovalRequest->merchant_team_id);

return (new MailMessage())
->subject('A Vine voucher set is about to be been generated that may be redeemed at your shop')
->markdown('mail.voucher-set-approval-request', [
'voucherSetId' => $this->voucherSetMerchantTeamApprovalRequest->voucher_set_id,
'voucherSet' => $voucherSet,
'merchantTeam' => $merchantTeam,
'createdBy' => $voucherSet->createdByTeam->name,
'approve' => $urlApprove,
'reject' => $urlReject,
Expand Down
Loading

0 comments on commit ba62577

Please sign in to comment.