Skip to content

Commit

Permalink
Merge pull request #79 from canyongbs/feature/aidapp-77-enhance-way-o…
Browse files Browse the repository at this point in the history
…nline-forms-presented-application

[AIDAPP-77]: Enhance the way Online Forms is presented in the application
  • Loading branch information
Orrison authored May 15, 2024
2 parents 44682c5 + 5dfda90 commit cbe2261
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 248 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,31 @@

namespace AidingApp\Portal\Http\Controllers\KnowledgeManagementPortal;

use App\Enums\Feature;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Validator;
use Symfony\Component\HttpFoundation\Response;
use AidingApp\Form\Actions\GenerateFormKitSchema;
use AidingApp\ServiceManagement\Models\ServiceRequest;
use AidingApp\Form\Actions\GenerateSubmissibleValidation;
use AidingApp\ServiceManagement\Models\ServiceRequestType;
use AidingApp\Form\Actions\ResolveSubmissionAuthorFromEmail;
use AidingApp\Form\Filament\Blocks\EducatableEmailFormFieldBlock;
use AidingApp\ServiceManagement\Models\ServiceRequestFormSubmission;

class CreateServiceRequestController extends Controller
{
public function create(GenerateFormKitSchema $generateSchema, ServiceRequestType $type): JsonResponse
{
return response()->json([
'schema' => $generateSchema($type->form),
'priorities' => $type->priorities()->orderBy('order', 'desc')->get()->pluck('name', 'id')->map(fn ($name, $id) => ['id' => $id, 'name' => $name]),
'schema' => $type->form && Gate::check(Feature::OnlineForms->getGateName()) ? $generateSchema($type->form) : [],
'priorities' => $type->priorities()->orderBy('order', 'desc')->pluck('name', 'id'),
]);
}

Expand All @@ -64,117 +70,143 @@ public function store(
ResolveSubmissionAuthorFromEmail $resolveSubmissionAuthorFromEmail,
ServiceRequestType $type,
): JsonResponse {
$contact = auth('contact')->user() ?? $request->user();
$contact = auth('contact')->user();

$serviceRequestForm = $type->form;
abort_if(is_null($contact), Response::HTTP_UNAUTHORIZED);

if (
is_null($contact)
) {
abort(Response::HTTP_UNAUTHORIZED);
}
$serviceRequestForm = $type->form;

$validator = Validator::make(
$request->all(),
$generateValidation($serviceRequestForm)
);
$validator = Validator::make($request->all(), [
'priority' => ['required', 'exists:service_request_priorities,id'],
'description' => ['required', 'string', 'max:65535'],
...$serviceRequestForm
? Arr::prependKeysWith($generateValidation($serviceRequestForm), 'extra.')
: [],
]);

if ($validator->fails()) {
return response()->json(
[
'errors' => (object) $validator->errors(),
],
Response::HTTP_UNPROCESSABLE_ENTITY
);
return response()->json([
'errors' => (object) $validator->errors(),
], Response::HTTP_UNPROCESSABLE_ENTITY);
}

$submission = $serviceRequestForm->submissions()->make();

$submission
->priority()
->associate(
$serviceRequestForm
->type
->priorities()
->findOrFail(
$request->input('priority')
)
);
$data = $validator->validated();

if ($contact) {
$submission->author()->associate($contact);
}
$priority = $type->priorities()->findOrFail($data['priority']);

$submission->submitted_at = now();
return DB::transaction(function () use (
$resolveSubmissionAuthorFromEmail,
$serviceRequestForm,
$priority,
$contact,
$data,
$type
) {
$serviceRequest = new ServiceRequest([
'title' => $type->name,
'close_details' => $data['description'],
]);

$submission->description = $request->input('description');
$serviceRequest->respondent()->associate($contact);
$serviceRequest->priority()->associate($priority);

$submission->save();
$serviceRequest->save();

$data = $validator->validated();
if (! $serviceRequestForm) {
return response()->json([
'message' => 'Service Request Form submitted successfully.',
]);
}

unset($data['recaptcha-token']);
unset(
$data['description'],
$data['priority'],
);

if ($serviceRequestForm->is_wizard) {
foreach ($serviceRequestForm->steps as $step) {
$stepFields = $step->fields()->pluck('type', 'id')->all();
$submission = $serviceRequestForm->submissions()
->make([
'submitted_at' => now(),
]);

foreach ($data[$step->label] as $fieldId => $response) {
$submission->fields()->attach(
$fieldId,
['id' => Str::orderedUuid(), 'response' => $response],
);
$submission->priority()->associate($priority);

if ($submission->author) {
continue;
}
$submission->save();

if ($stepFields[$fieldId] !== EducatableEmailFormFieldBlock::type()) {
continue;
}
unset($data['recaptcha-token']);

$data = data_get($data, 'extra', []);

$author = $resolveSubmissionAuthorFromEmail($response);
if ($serviceRequestForm->is_wizard) {
foreach ($serviceRequestForm->steps as $step) {
$fields = $step->fields()->pluck('type', 'id')->all();

if (! $author) {
continue;
foreach ($data[$step->label] as $fieldId => $response) {
$this->processSubmissionField(
$submission,
$fieldId,
$response,
$fields,
$resolveSubmissionAuthorFromEmail
);
}
}
} else {
$fields = $serviceRequestForm->fields()->pluck('type', 'id')->all();

$submission->author()->associate($author);
foreach ($data as $fieldId => $response) {
$this->processSubmissionField(
$submission,
$fieldId,
$response,
$fields,
$resolveSubmissionAuthorFromEmail
);
}
}
} else {
$formFields = $serviceRequestForm->fields()->pluck('type', 'id')->all();

foreach ($data as $fieldId => $response) {
$submission->fields()->attach(
$fieldId,
['id' => Str::orderedUuid(), 'response' => $response],
);
$submission->save();

if ($submission->author) {
continue;
}
$serviceRequest->title = $serviceRequestForm->name;
$serviceRequest->serviceRequestFormSubmission()->associate($submission);

if ($formFields[$fieldId] !== EducatableEmailFormFieldBlock::type()) {
continue;
}
if ($submission->author) {
$serviceRequest->respondent()->associate($submission->author);
}

$author = $resolveSubmissionAuthorFromEmail($response);
$serviceRequest->save();

if (! $author) {
continue;
}
return response()->json([
'message' => 'Service Request Form submitted successfully.',
]);
});
}

$submission->author()->associate($author);
}
private function processSubmissionField(
ServiceRequestFormSubmission $submission,
string $fieldId,
mixed $response,
array $fields,
ResolveSubmissionAuthorFromEmail $resolveSubmissionAuthorFromEmail
) {
$submission->fields()->attach($fieldId, [
'id' => Str::orderedUuid(),
'response' => $response,
]);

if ($submission->author) {
return;
}

if ($fields[$fieldId] !== EducatableEmailFormFieldBlock::type()) {
return;
}

$submission->save();
$author = $resolveSubmissionAuthorFromEmail($response);

return response()->json(
[
'message' => 'Service Request Form submitted successfully.',
]
);
if (! $author) {
return;
}

$submission->author()->associate($author);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public function index(): JsonResponse
{
return response()->json([
'types' => ServiceRequestType::query()
->whereHas('form')
->orderBy('name')
->get()
->map(function (ServiceRequestType $type) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ public function table(Table $table): Table
->dateTime(),
TextColumn::make('author.email')
->label('Submitted By')
->url(fn (ServiceRequestFormSubmission $record) => resolve($record->author::filamentResource())->getUrl('view', ['record' => $record->author]))
->url(
fn (ServiceRequestFormSubmission $record) => $record->author
? resolve($record->author::filamentResource())->getUrl('view', ['record' => $record->author])
: null
)
->color('primary'),
TextColumn::make('author_type')
->label('Submitted By Type')
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ public function forceDelete(Authenticatable $authenticatable, ServiceRequestForm

protected function requiredFeatures(): array
{
return [Feature::ServiceManagement];
return [
Feature::ServiceManagement,
Feature::OnlineForms,
];
}
}
Loading

0 comments on commit cbe2261

Please sign in to comment.