Ok, so I have this code in the handle() method of a Job called CreateSubscription.php:
{
// Make sure dates provided to Job are chronologically valid
if ($this->data['starts_on'] >= $this->data['ends_on'])
{
dd('End date has to be greater than start date');
}
// Insert records in the database
}
Basically, I want to insert records in the database as long as the dates (supplied by an API call) validation checks out. The store method of my SubscriptionController.php calls the job as follows:
{
// Dispatch synchronous job to create subscription
$subscription = CreateSubscription::dispatchNow($request->validated());
// All is saved to the database, now return the new subscription.
return new SubscriptionResource($subscription);
}
The above is the controller and method that my API calls.
My question is this: I want to replace the dd() in CreateSubscription.php with proper error reporting back to the API/user. What is the best way of doing this since the API is typically expecting the subscription resource (formatted by SubscriptionResource)?
Should I actually be doing this date validation in my Form Request class?
My Form Request class (StoreSubscription.php) is simple. The rule() method is as follows.
{
return [
'name' => 'required | min:3',
'aum' => 'numeric',
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required',
'phone' => 'required',
'starts_on' => 'required',
'ends_on' => 'required',
'plan_id' => 'required'
];
}
I get the two dates in there, and validate that they have been supplied in the API request. I don't do any chronological validation, however, in the Form Request class. Should I? Or is it better relegated to the Job responsible for persisting the subscription with the correct date chronology. The dates I am referring to are "starts_on" and "ends_on". Any help would be appreciated.