v0.8.0
This commit is contained in:
commit
bf5c31169a
221 files changed
+16292
No files matched your search
@@ -0,0 +1,209 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Modules\Leads;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Validation\Rule;
|
||||
use IriksIT\Manager\Models\Lead;
|
||||
use IriksIT\Manager\Models\LeadBlock;
|
||||
use IriksIT\Manager\Support\Leads\Intake;
|
||||
use IriksIT\Manager\Support\Leads\Phone;
|
||||
use IriksIT\Manager\Support\Leads\Report;
|
||||
|
||||
/**
|
||||
* The requests. Whoever may see them sees what is waiting and what was handled, and calls, apps or mails back. Whoever may manage
|
||||
* them also sees what the screening kept away ("other" and spam), with the reasons, and can move requests or block senders.
|
||||
*/
|
||||
class LeadsController
|
||||
{
|
||||
private const VIEWS = ['waiting', 'handled'];
|
||||
|
||||
public function index(Request $request, Report $report): JsonResponse
|
||||
{
|
||||
$manages = Gate::allows('leads.manage');
|
||||
$view = $request->string('view')->toString();
|
||||
$view = in_array($view, $manages ? [...self::VIEWS, Lead::OTHER, Lead::SPAM] : self::VIEWS, true) ? $view : 'waiting';
|
||||
|
||||
$query = Lead::query()->latest('id');
|
||||
match ($view) {
|
||||
'waiting' => $query->waiting(),
|
||||
'handled' => $query->handled(),
|
||||
default => $query->where('folder', $view),
|
||||
};
|
||||
|
||||
return response()->json([
|
||||
'view' => $view,
|
||||
'items' => $query->limit(200)->get()->map(fn (Lead $lead) => $this->present($lead, $manages))->values(),
|
||||
'counts' => [
|
||||
'waiting' => Lead::query()->waiting()->count(),
|
||||
'handled' => Lead::query()->handled()->count(),
|
||||
// How much was kept away; the folders themselves are only for who manages.
|
||||
'other' => Lead::query()->where('folder', Lead::OTHER)->count(),
|
||||
'spam' => Lead::query()->where('folder', Lead::SPAM)->count(),
|
||||
],
|
||||
'open' => $request->integer('open') ?: null,
|
||||
'blocks' => $manages ? LeadBlock::query()->latest('id')->get()->map(fn (LeadBlock $block) => [
|
||||
'id' => $block->id, 'type' => $block->type, 'value' => $block->value, 'reason' => $block->reason, 'expires_at' => $block->expires_at?->toIso8601String(),
|
||||
])->values() : null,
|
||||
// What the forms and the contact buttons brought in, the last 90 days.
|
||||
'report' => $manages ? $report->over(90) : null,
|
||||
'can' => ['handle' => Gate::allows('leads.handle'), 'manage' => $manages, 'delete' => Gate::allows('leads.delete')],
|
||||
]);
|
||||
}
|
||||
|
||||
/** The first time a request is opened: it is no longer new. */
|
||||
public function opened(int $lead): JsonResponse
|
||||
{
|
||||
$lead = Lead::findOrFail($lead);
|
||||
$lead->opened_at ??= now();
|
||||
$lead->save();
|
||||
|
||||
return response()->json(['ok' => true]);
|
||||
}
|
||||
|
||||
/** "Handled", and back; with whether it became a job, when that is said. */
|
||||
public function handled(Request $request, int $lead): JsonResponse
|
||||
{
|
||||
$lead = Lead::findOrFail($lead);
|
||||
$request->validate(['handled' => ['required', 'boolean'], 'outcome' => ['nullable', Rule::in([Lead::WON, Lead::LOST])]]);
|
||||
|
||||
$handled = $request->boolean('handled');
|
||||
|
||||
$lead->update([
|
||||
'handled_at' => $handled ? ($lead->handled_at ?? now()) : null,
|
||||
'outcome' => $handled && $request->filled('outcome') ? $request->string('outcome')->toString() : ($handled ? $lead->outcome : null),
|
||||
'opened_at' => $lead->opened_at ?? now(),
|
||||
]);
|
||||
|
||||
return response()->json($this->present($lead->refresh(), Gate::allows('leads.manage')));
|
||||
}
|
||||
|
||||
/** A note of the owner to themselves. */
|
||||
public function note(Request $request, int $lead): JsonResponse
|
||||
{
|
||||
$lead = Lead::findOrFail($lead);
|
||||
$request->validate(['text' => ['required', 'string', 'max:2000']]);
|
||||
|
||||
$lead->update(['notes' => [
|
||||
...($lead->notes ?? []),
|
||||
['at' => now()->format('Y-m-d H:i:s'), 'by' => Auth::user()?->name, 'text' => $request->string('text')->trim()->toString()],
|
||||
]]);
|
||||
|
||||
return response()->json($this->present($lead->refresh(), Gate::allows('leads.manage')), 201);
|
||||
}
|
||||
|
||||
/** Moves a request to another folder: the screening made the wrong call, or let something through. */
|
||||
public function folder(Request $request, Intake $intake, int $lead): JsonResponse
|
||||
{
|
||||
$lead = Lead::findOrFail($lead);
|
||||
$request->validate(['folder' => ['required', Rule::in(Lead::FOLDERS)]]);
|
||||
|
||||
$folder = $request->string('folder')->toString();
|
||||
$was = $lead->folder;
|
||||
|
||||
$lead->update(['folder' => $folder, 'reasons' => [...($lead->reasons ?? []), 'Moved by hand from '.$was.' to '.$folder]]);
|
||||
|
||||
// A real request after all: the owner hears about it like about any new one.
|
||||
if ($folder === Lead::INBOX && $was !== Lead::INBOX && $lead->notified_at === null) {
|
||||
$intake->tellTheOwner($lead);
|
||||
}
|
||||
|
||||
return response()->json($this->present($lead->refresh(), true));
|
||||
}
|
||||
|
||||
/** Keeps a sender out: what they send from now on goes to the spam folder. */
|
||||
public function block(Request $request, int $lead): JsonResponse
|
||||
{
|
||||
$lead = Lead::findOrFail($lead);
|
||||
$request->validate([
|
||||
'type' => ['required', Rule::in(LeadBlock::TYPES)],
|
||||
'days' => ['nullable', 'integer', 'min:1', 'max:3650'],
|
||||
'reason' => ['nullable', 'string', 'max:255'],
|
||||
]);
|
||||
|
||||
$type = $request->string('type')->toString();
|
||||
$email = mb_strtolower((string) $lead->email);
|
||||
|
||||
$value = match ($type) {
|
||||
LeadBlock::EMAIL => $email,
|
||||
LeadBlock::DOMAIN => substr((string) strrchr($email, '@'), 1),
|
||||
LeadBlock::PHONE => (string) Phone::international((string) $lead->phone),
|
||||
default => (string) $lead->ip,
|
||||
};
|
||||
|
||||
if ($value === '') {
|
||||
return response()->json(['message' => __('This request has nothing of that kind to block.')], 422);
|
||||
}
|
||||
|
||||
// Half the country mails from these: blocking one would keep customers out.
|
||||
if ($type === LeadBlock::DOMAIN && in_array($value, LeadBlock::SHARED_DOMAINS, true)) {
|
||||
return response()->json(['message' => __('Everyone uses :domain: block the mail address instead of the whole domain.', ['domain' => $value])], 422);
|
||||
}
|
||||
|
||||
$block = LeadBlock::query()->updateOrCreate(['type' => $type, 'value' => $value], [
|
||||
'reason' => $request->filled('reason') ? $request->string('reason')->toString() : null,
|
||||
'expires_at' => $request->filled('days') ? now()->addDays($request->integer('days')) : null,
|
||||
'user_id' => Auth::id(),
|
||||
'user_name' => Auth::user()?->name,
|
||||
]);
|
||||
|
||||
return response()->json(['id' => $block->id, 'type' => $type, 'value' => $value], 201);
|
||||
}
|
||||
|
||||
public function unblock(int $block): JsonResponse
|
||||
{
|
||||
LeadBlock::findOrFail($block)->delete();
|
||||
|
||||
return response()->json(['ok' => true]);
|
||||
}
|
||||
|
||||
public function destroy(int $lead): JsonResponse
|
||||
{
|
||||
Lead::findOrFail($lead)->delete();
|
||||
|
||||
return response()->json(['ok' => true]);
|
||||
}
|
||||
|
||||
/**
|
||||
* A request as the screen shows it. The reasons of the screening, the address it came from and where the visit came from are
|
||||
* only for whoever manages the requests.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function present(Lead $lead, bool $manages): array
|
||||
{
|
||||
$digits = $lead->phone ? Phone::international($lead->phone) : null;
|
||||
|
||||
return [
|
||||
'id' => $lead->id,
|
||||
'kind' => $lead->kindLabel(),
|
||||
'subject' => $lead->aboutLine(),
|
||||
'name' => $lead->name,
|
||||
'phone' => $lead->phone,
|
||||
'email' => $lead->email,
|
||||
'prefers' => $lead->prefers,
|
||||
'message' => $lead->message,
|
||||
'answers' => $lead->answers ?? [],
|
||||
'location' => $lead->location,
|
||||
'page' => $lead->page,
|
||||
'call_link' => $digits ? 'tel:+'.$digits : null,
|
||||
'whatsapp_link' => $digits ? 'https://wa.me/'.$digits : null,
|
||||
'mail_link' => $lead->email ? 'mailto:'.$lead->email : null,
|
||||
'is_new' => $lead->opened_at === null,
|
||||
'handled' => $lead->handled_at !== null,
|
||||
'outcome' => $lead->outcome,
|
||||
'notes' => $lead->notes ?? [],
|
||||
'created_at' => $lead->created_at?->toIso8601String(),
|
||||
'notify_failed' => $lead->notify_failed,
|
||||
...($manages ? [
|
||||
'folder' => $lead->folder,
|
||||
'reasons' => $lead->reasons ?? [],
|
||||
'source' => trim(($lead->channel ?? '').($lead->source ? ': '.$lead->source : '')) ?: null,
|
||||
'ip' => $lead->ip,
|
||||
] : []),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user