'Update Duplicate Bank Statement Details Status', 'message' => 'You have successfully updated the Statement Transation Status' ]; } /** @var UpdatesBankStatementTransactionOwnerStatus */ private $updatesBankStatementTransactionOwnerStatus; /** * @param UpdatesBankStatementTransactionOwnerStatus $updatesBankStatementTransactionOwnerStatus */ public function __construct(UpdatesBankStatementTransactionOwnerStatus $updatesBankStatementTransactionOwnerStatus) { $this->updatesBankStatementTransactionOwnerStatus = $updatesBankStatementTransactionOwnerStatus; } // /** // * Perform logic for approving or rejecting a statement transaction owner and handle matching records. // * // * @param Request $request The request object containing route parameters and data. // * @return JsonResponse The JSON response indicating the result of the logic. // */ // public function logic(Request $request): JsonResponse // { // // Determine the status based on the 'status' route parameter // $status = $request->route('status') == 'approve' ? ApprovalStatus::APPROVED : ApprovalStatus::REJECTED; // // // Find the statement transaction owner based on the 'id' route parameter // $owner = StatementTransactionOwner::find($request->route('id')); // // // Execute an update action to change the status of the owner // $this->updatesBankStatementTransactionOwnerStatus->execute($owner, $status); // // // If the status is 'approved', reject all other owners with the same system, owner type, and owner ID // if ($status === ApprovalStatus::APPROVED) { // StatementTransactionOwner::where('system', $owner->system) // ->where('owner_type', $owner->owner_type) // ->where('owner_id', $owner->owner_id) // ->where('id', '!=', $owner->id) // ->update(['status' => ApprovalStatus::REJECTED]); // } // // // Find all siblings (owners with the same statement transaction ID) // $siblings = StatementTransactionOwner::where('statement_transaction_id', $owner->statement_transaction_id) // ->where('id', '!=', $owner->id) // ->get(); // // // Process each sibling // foreach ($siblings as $sibling) { // // // If the status is 'approved', reject the sibling and save the changes // if ($status === ApprovalStatus::APPROVED) { // $sibling->status = ApprovalStatus::REJECTED; // $sibling->save(); // } // // // Find all twins (owners with the same system, owner type, and owner ID) // $twins = StatementTransactionOwner::where('system', $sibling->system) // ->where('owner_type', $sibling->owner_type) // ->where('owner_id', $sibling->owner_id) // ->where('id', '!=', $sibling->id) // ->get(); // // // Process each twin // foreach ($twins as $twin) { // // Find all owners with the same statement transaction ID as the twin // $owners = StatementTransactionOwner::where('statement_transaction_id', $twin->statement_transaction_id) // ->where('id', '!=', $twin->id) // ->get(); // // // If there is only one owner (the twin itself), execute an update action to change its status to 'approved' // if (count($owners) === 1) { // $this->updatesBankStatementTransactionOwnerStatus->execute($twin, ApprovalStatus::APPROVED); // } // } // } // // // Find all remaining matching owners for the related transaction // $remainingMatches = StatementTransactionOwner::where('system', $owner->system) // ->where('owner_type', $owner->owner_type) // ->where('owner_id', $owner->owner_id) // ->where('status', ApprovalStatus::PENDING_VERIFICATION) // Consider only pending owners // ->get(); // // // Process each remaining match // foreach ($remainingMatches as $match) { // // Find all owners with the same statement transaction ID as the match // $owners = StatementTransactionOwner::where('statement_transaction_id', $match->statement_transaction_id) // ->where('id', '!=', $match->id) // ->get(); // // // If there is only one owner (the match itself), execute an update action to change its status to 'approved' // if (count($owners) === 1) { // $this->updatesBankStatementTransactionOwnerStatus->execute($match, ApprovalStatus::APPROVED); // } // } // // // Return an empty response // return $this->response([]); // } /** * Perform logic for approving or rejecting a statement transaction owner and handle matching records. * * @param Request $request The request object containing route parameters and data. * @return JsonResponse The JSON response indicating the result of the logic. */ public function logic(Request $request): JsonResponse { // Determine the approval status $status = $this->getApprovalStatus($request); // Find the statement transaction owner $owner = $this->getOwner($request); // Update owner status $this->updateOwnerStatus($owner, $status); // If the status is 'approved', handle the approval process if ($status === ApprovalStatus::APPROVED) { $this->handleApprovedStatus($owner); } // Check and approve remaining matches if any $this->checkAndApproveRemainingMatches($owner); // Return an empty response return $this->response([]); } private function getApprovalStatus(Request $request): int { return $request->route('status') == 'approve' ? ApprovalStatus::APPROVED : ApprovalStatus::REJECTED; } private function getOwner(Request $request): StatementTransactionOwner { return StatementTransactionOwner::find($request->route('id')); } private function updateOwnerStatus(StatementTransactionOwner $owner, int $status): void { $this->updatesBankStatementTransactionOwnerStatus->execute($owner, $status); } private function handleApprovedStatus(StatementTransactionOwner $owner): void { // Reject all other owners with the same system, owner type, and owner ID $this->rejectOtherOwners($owner); // Find all siblings and process them $siblings = $this->getSiblings($owner); $this->processSiblings($siblings); } private function rejectOtherOwners(StatementTransactionOwner $owner): void { StatementTransactionOwner::where('system', $owner->system) ->where('owner_type', $owner->owner_type) ->where('owner_id', $owner->owner_id) ->where('id', '!=', $owner->id) ->update(['status' => ApprovalStatus::REJECTED]); } private function getSiblings(StatementTransactionOwner $owner): Collection { return StatementTransactionOwner::where('statement_transaction_id', $owner->statement_transaction_id) ->where('id', '!=', $owner->id) ->get(); } private function processSiblings(Collection $siblings): void { foreach ($siblings as $sibling) { $this->processSibling($sibling); } } private function processSibling(StatementTransactionOwner $sibling): void { // Reject the sibling and save the changes $sibling->status = ApprovalStatus::REJECTED; $sibling->save(); // Find all twins and process them $twins = $this->getTwins($sibling); $this->processTwins($twins); } private function getTwins(StatementTransactionOwner $sibling): Collection { return StatementTransactionOwner::where('system', $sibling->system) ->where('owner_type', $sibling->owner_type) ->where('owner_id', $sibling->owner_id) ->where('id', '!=', $sibling->id) ->get(); } private function processTwins(Collection $twins): void { foreach ($twins as $twin) { $this->processTwin($twin); } } private function processTwin(StatementTransactionOwner $twin): void { // Find all owners with the same statement transaction ID as the twin $owners = $this->getOwners($twin); // If there is only one owner (the twin itself), approve it if ($owners->count() === 1) { $this->updateOwnerStatus($twin, ApprovalStatus::APPROVED); } } private function getOwners(StatementTransactionOwner $transactionOwner): Collection { return StatementTransactionOwner::where('statement_transaction_id', $transactionOwner->statement_transaction_id) ->where('id', '!=', $transactionOwner->id) ->get(); } private function checkAndApproveRemainingMatches(StatementTransactionOwner $owner): void { // Find all remaining matching owners for the related transaction $remainingMatches = $this->getRemainingMatches($owner); // Process each remaining match foreach ($remainingMatches as $match) { $this->processRemainingMatch($match); } } private function getRemainingMatches(StatementTransactionOwner $owner): Collection { return StatementTransactionOwner::where('system', $owner->system) ->where('owner_type', $owner->owner_type) ->where('owner_id', $owner->owner_id) ->where('status', ApprovalStatus::PENDING_VERIFICATION) ->get(); } private function processRemainingMatch(StatementTransactionOwner $match): void { // Find all owners with the same statement transaction ID as the match $owners = $this->getOwners($match); // If there is only one owner (the match itself), approve it if ($owners->count() === 1) { $this->updateOwnerStatus($match, ApprovalStatus::APPROVED); } } }