I'm working on a Laravel/ Angular application, and have just added a function to allow the user to set a custom addressee name when generating a PDF letter/ email via the provisional payment reminders page.
The Angular function is defined in provisional-reminders.ts with:
updatePreferredAddresseeDetails($event, payer) {
console.log("updatePreferredAddresseeDetails() called ");
console.log("$event: ", $event);
console.log("taxpayer: ", payer);
console.log("account.preferredAddressee: ", payer.preferredAddresseeName);
//$event.currentTarget.cancelBubble = true;
const contact = payer['contacts'][$event.currentTarget.selectedIndex]; /* Need to ensure contact is defined here, so it can be used below- currently null here */
console.log("contact: ", contact);
//const data = (<any>Object).assign({}, payer, { transactionContactId: contact.userId });
//console.log("data: ", data);
payer.loading = true;
payer.originalAddresseeName = payer.addresseename;
payer.originalAddresseeNamePdf = payer.addresseenamepdf;
payer.ADDRESSEENAME = $event.contactPreferredName;
payer.ADDRESSEENAMEPDF = $event.contactPreferredAddresseeName;
/*contact.originalAddresseeName = payer.addresseename;
contact.originalAddresseeNamePdf = payer.addresseenamepdf;
contact.ADDRESSEENAME = $event.contactPreferredName;
contact.ADDRESSEENAMEPDF = $event.contactPreferredAddresseeName; */
console.log("payer.addresseename: ", payer.ADDRESSEENAME);
console.log("payer.addresseenamepdf: ", payer.ADDRESSEENAMEPDF);
//this.provService.updatePreferredAddresseeDetails(data).subscribe(
this.provService.updateTransactionContact(contact).subscribe(
(response:any) => {
payer.addresseename = response.addresseename;
payer.addresseenamepdf = response.addresseenamepdf;
const message = new Message();
message.type = MessageType.SUCCESS;
message.message = 'Preferred Addressee details have been updated. ';
this.messagingService.emitMessage(message);
payer.loading = false;
},
(error:any) => {
//reset the names back to what they were originally because saving failed
payer.addresseename = payer.originalAddresseeName;
const message = new Message();
message.type = MessageType.ERROR;
message.message = error.message || 'There was a problem updaing the preferred addressee details. If the problem persists, please contact us.';
this.messagingService.emitMessage(message);
payer.loading = false;
}
);
}
In the PHP controller, I have defined the function with:
public function updatePreferredAddresseeDetails(Request $request)
{
try
{
DB::beginTransaction();
$transactionContactId = $request->input('transactionContactId');
$transactionItemId = $request->input('transactionItemId');
if ($transactionItem = transactionItem::find($transactionItemId))
{
$transaction = $transactionItem->transaction;
if (User::canAccessTransaction( auth()->user()->user, $transaction))
{
$transaction->savePropertyValueByPropertyTag('TRANSACTIONCONTACT', $transactionContactId);
$account = Account::find($transaction->accountId);
$account->savePropertyValueByPropertyTag('ADDRESSEENAME', $request->input('contactPreferredName'));
$account->savePropertyValueByPropertyTag('ADDRESSEENAMEPDF', $request->input('contactPreferredAddresseeName'));
$trasaction->save();
$account->save();
/*$newContact = User::find($transactionContactId); /*shouldn't need this line, as it's not a new contact- just updating an
existing one*/
DB::commit();
return response()->json([
'success' => true,
'transactionItemId' => $transactionItem->transactionItemId,
'transactionId' => $transactionItem->transactionId,
'transactionContactId' => $transactionContactId,
'addresseeName' => $account->ADDRESSEENAME,
'addresseeNamePdf' => $account->ADDRESSEENAMEPDF,
//'transactionContactName' => $newContact,
//dd(response);
]);
}
dd("transactionItem: ", $transactionItem);
}
else
{
dd("transactionItem could not be found ");
}
}
catch(Excetpion $e)
{
dd("exception caught: ", $e);
}
}
In the routes/web.php
file, I have added the route to the prov
group:
Route::group(['prefix' => 'prov'], function() {
...
Route::post('/preferredAddressee', 'WebApi\ProvController@updatePreferredAddresseeDetails');
});
Currently, when the page loads, and I enter values into the fields that I have added to the form to be able to set/ amend the addresseename
& addresseenamepdf
values, when I tab out of those fields, this updatePreferredAddresseeDetails()
function is called, but I get a message in the console that says:
POST ... 404 (Not Found)
unparsable response
and the Network-> Preview tab shows the message:
You've tried to access a page that doesn't exist.
Why is this? What am I doing wrong with the routing here?