Coalesce duplicate cache calls into a single one per method

This commit is contained in:
David Bomba 2025-01-24 16:27:51 +11:00
parent 1a5737478f
commit ffd26b91a6
6 changed files with 55 additions and 37 deletions

View File

@ -31,40 +31,49 @@ class InvoiceSummary extends Component
public function mount() public function mount()
{ {
$contact = $this->getContext()['contact'] ?? auth()->guard('contact')->user(); $_context = $this->getContext();
$this->invoices = $this->getContext()['payable_invoices'];
$this->amount = Number::formatMoney($this->getContext()['amount'], $contact->client); $contact = $_context['contact'] ?? auth()->guard('contact')->user();
$this->gateway_fee = isset($this->getContext()['gateway_fee']) ? Number::formatMoney($this->getContext()['gateway_fee'], $contact->client) : false; $this->invoices = $_context['payable_invoices'];
$this->amount = Number::formatMoney($_context['amount'], $contact->client);
$this->gateway_fee = isset($_context['gateway_fee']) ? Number::formatMoney($_context['gateway_fee'], $contact->client) : false;
} }
#[On(self::CONTEXT_UPDATE)] #[On(self::CONTEXT_UPDATE)]
public function onContextUpdate(): void public function onContextUpdate(): void
{ {
$_context = $this->getContext();
// refactor logic for updating the price for eg if it changes with under/over pay // refactor logic for updating the price for eg if it changes with under/over pay
$contact = $this->getContext()['contact'] ?? auth()->guard('contact')->user(); $contact = $_context['contact'] ?? auth()->guard('contact')->user();
$this->invoices = $this->getContext()['payable_invoices']; $this->invoices = $_context['payable_invoices'];
$this->amount = Number::formatMoney($this->getContext()['amount'], $contact->client); $this->amount = Number::formatMoney($_context['amount'], $contact->client);
$this->gateway_fee = isset($this->getContext()['gateway_fee']) ? Number::formatMoney($this->getContext()['gateway_fee'], $contact->client) : false; $this->gateway_fee = isset($_context['gateway_fee']) ? Number::formatMoney($_context['gateway_fee'], $contact->client) : false;
} }
#[On('payment-view-rendered')] #[On('payment-view-rendered')]
public function handlePaymentViewRendered() public function handlePaymentViewRendered()
{ {
$_context = $this->getContext();
$contact = $this->getContext()['contact'] ?? auth()->guard('contact')->user(); $contact = $_context['contact'] ?? auth()->guard('contact')->user();
$this->amount = Number::formatMoney($this->getContext()['amount'], $contact->client); $this->amount = Number::formatMoney($_context['amount'], $contact->client);
$this->gateway_fee = isset($this->getContext()['gateway_fee']) ? Number::formatMoney($this->getContext()['gateway_fee'], $contact->client) : false; $this->gateway_fee = isset($_context['gateway_fee']) ? Number::formatMoney($_context['gateway_fee'], $contact->client) : false;
} }
public function downloadDocument($invoice_hashed_id) public function downloadDocument($invoice_hashed_id)
{ {
$invitation_id = $this->getContext()['invitation_id']; $_context = $this->getContext();
$db = $this->getContext()['db']; $invitation_id = $_context['invitation_id'];
$db = $_context['db'];
$invite = \App\Models\InvoiceInvitation::on($db)->withTrashed()->find($invitation_id); $invite = \App\Models\InvoiceInvitation::on($db)->withTrashed()->find($invitation_id);

View File

@ -56,12 +56,13 @@ class PaymentMethod extends Component
public function mount() public function mount()
{ {
$this->variables = $this->getContext()['variables']; $_context = $this->getContext();
$this->amount = array_sum(array_column($this->getContext()['payable_invoices'], 'amount')); $this->variables = $_context['variables'];
$this->amount = array_sum(array_column($_context['payable_invoices'], 'amount'));
MultiDB::setDb($this->getContext()['db']); MultiDB::setDb($_context['db']);
$contact = $this->getContext()['contact'] ?? auth()->guard('contact')->user(); $contact = $_context['contact'] ?? auth()->guard('contact')->user();
$this->methods = $contact->client->service()->getPaymentMethods($this->amount); $this->methods = $contact->client->service()->getPaymentMethods($this->amount);

View File

@ -37,12 +37,14 @@ class ProcessPayment extends Component
$invitation = InvoiceInvitation::find($this->getContext()['invitation_id']); $invitation = InvoiceInvitation::find($this->getContext()['invitation_id']);
$_context = $this->getContext();
$data = [ $data = [
'company_gateway_id' => $this->getContext()['company_gateway_id'], 'company_gateway_id' => $_context['company_gateway_id'],
'payment_method_id' => $this->getContext()['gateway_type_id'], 'payment_method_id' => $_context['gateway_type_id'],
'payable_invoices' => $this->getContext()['payable_invoices'], 'payable_invoices' => $_context['payable_invoices'],
'signature' => isset($this->getContext()['signature']) ? $this->getContext()['signature'] : false, 'signature' => isset($_context['signature']) ? $_context['signature'] : false,
'signature_ip' => isset($this->getContext()['signature_ip']) ? $this->getContext()['signature_ip'] : false, 'signature_ip' => isset($_context['signature_ip']) ? $_context['signature_ip'] : false,
'pre_payment' => false, 'pre_payment' => false,
'frequency_id' => false, 'frequency_id' => false,
'remaining_cycles' => false, 'remaining_cycles' => false,
@ -52,7 +54,7 @@ class ProcessPayment extends Component
$responder_data = (new LivewireInstantPayment($data))->run(); $responder_data = (new LivewireInstantPayment($data))->run();
$company_gateway = CompanyGateway::find($this->getContext()['company_gateway_id']); $company_gateway = CompanyGateway::find($_context['company_gateway_id']);
if (!$responder_data['success']) { if (!$responder_data['success']) {
throw new PaymentFailed($responder_data['error'], 400); throw new PaymentFailed($responder_data['error'], 400);

View File

@ -56,17 +56,19 @@ class RequiredFields extends Component
public function mount(): void public function mount(): void
{ {
$_context = $this->getContext();
MultiDB::setDB( MultiDB::setDB(
$this->getContext()['db'], $_context['db'],
); );
$this->fields = $this->getContext()['fields']; $this->fields = $_context['fields'];
$contact = auth()->guard('contact')->user(); $contact = auth()->guard('contact')->user();
$this->company_gateway = CompanyGateway::withTrashed() $this->company_gateway = CompanyGateway::withTrashed()
->with('company') ->with('company')
->find($this->getContext()['company_gateway_id']); ->find($_context['company_gateway_id']);
$this->client_name = $contact->client->name; $this->client_name = $contact->client->name;
$this->contact_first_name = $contact->first_name; $this->contact_first_name = $contact->first_name;
@ -89,8 +91,8 @@ class RequiredFields extends Component
$this->client_custom_value4 = $contact->client->custom_value4; $this->client_custom_value4 = $contact->client->custom_value4;
$rff = new RFFService( $rff = new RFFService(
fields: $this->getContext()['fields'], fields: $_context['fields'],
database: $this->getContext()['db'], database: $_context['db'],
company_gateway_id: (string)$this->company_gateway->id, company_gateway_id: (string)$this->company_gateway->id,
); );

View File

@ -30,10 +30,11 @@ class Terms extends Component
#[Computed()] #[Computed()]
public function invoice() public function invoice()
{ {
$_context = $this->getContext();
$invitation_id = $this->getContext()['invitation_id'];
$db = $this->getContext()['db']; $invitation_id = $_context['invitation_id'];
$db = $_context['db'];
$invite = \App\Models\InvoiceInvitation::on($db)->withTrashed()->find($invitation_id); $invite = \App\Models\InvoiceInvitation::on($db)->withTrashed()->find($invitation_id);

View File

@ -32,20 +32,23 @@ class UnderOverPayment extends Component
public function mount() public function mount()
{ {
$contact = $this->getContext()['contact'] ?? auth()->guard('contact')->user();
$_context = $this->getContext();
$this->invoice_amount = array_sum(array_column($this->getContext()['payable_invoices'], 'amount')); $contact = $_context['contact'] ?? auth()->guard('contact')->user();
$this->invoice_amount = array_sum(array_column($_context['payable_invoices'], 'amount'));
$this->currency = $contact->client->currency(); $this->currency = $contact->client->currency();
$this->payableInvoices = $this->getContext()['payable_invoices']; $this->payableInvoices = $_context['payable_invoices'];
} }
public function checkValue(array $payableInvoices) public function checkValue(array $payableInvoices)
{ {
$this->errors = ''; $this->errors = '';
$_context = $this->getContext();
$settings = $_context['settings'];
$settings = $this->getContext()['settings']; $contact = $_context['contact'] ?? auth()->guard('contact')->user();
$contact = $this->getContext()['contact'] ?? auth()->guard('contact')->user();
foreach ($payableInvoices as $key => $invoice) { foreach ($payableInvoices as $key => $invoice) {
$payableInvoices[$key]['amount'] = Number::parseFloat($invoice['formatted_amount']); $payableInvoices[$key]['amount'] = Number::parseFloat($invoice['formatted_amount']);