add whatsup logic
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Messaging;
|
||||
|
||||
use App\Http\Controllers\Api\BaseApiController;
|
||||
use App\Http\Requests\Whatsapp\WhatsappInviteSendRequest;
|
||||
use App\Http\Requests\Whatsapp\WhatsappLinkIndexRequest;
|
||||
use App\Http\Requests\Whatsapp\WhatsappLinkStoreRequest;
|
||||
use App\Http\Requests\Whatsapp\WhatsappLinkUpdateRequest;
|
||||
use App\Http\Requests\Whatsapp\WhatsappMembershipUpdateRequest;
|
||||
use App\Http\Requests\Whatsapp\WhatsappParentContactsByClassRequest;
|
||||
use App\Http\Requests\Whatsapp\WhatsappParentContactsRequest;
|
||||
use App\Http\Resources\Whatsapp\WhatsappClassSectionContactResource;
|
||||
use App\Http\Resources\Whatsapp\WhatsappGroupLinkCollection;
|
||||
use App\Http\Resources\Whatsapp\WhatsappGroupLinkResource;
|
||||
use App\Http\Resources\Whatsapp\WhatsappParentContactResource;
|
||||
use App\Services\Whatsapp\WhatsappContactService;
|
||||
use App\Services\Whatsapp\WhatsappContextService;
|
||||
use App\Services\Whatsapp\WhatsappInviteService;
|
||||
use App\Services\Whatsapp\WhatsappLinkService;
|
||||
use App\Services\Whatsapp\WhatsappMembershipService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class WhatsappController extends BaseApiController
|
||||
{
|
||||
public function __construct(
|
||||
private WhatsappLinkService $linkService,
|
||||
private WhatsappContactService $contactService,
|
||||
private WhatsappInviteService $inviteService,
|
||||
private WhatsappMembershipService $membershipService,
|
||||
private WhatsappContextService $context
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function index(WhatsappLinkIndexRequest $request): JsonResponse
|
||||
{
|
||||
$links = $this->linkService->paginate($request->validated());
|
||||
$collection = new WhatsappGroupLinkCollection($links);
|
||||
|
||||
return $this->success([
|
||||
'links' => $collection->toArray($request),
|
||||
'meta' => $collection->with($request)['meta'],
|
||||
]);
|
||||
}
|
||||
|
||||
public function show(int $linkId): JsonResponse
|
||||
{
|
||||
$link = $this->linkService->findOrFail($linkId);
|
||||
|
||||
return $this->success([
|
||||
'link' => new WhatsappGroupLinkResource($link),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(WhatsappLinkStoreRequest $request): JsonResponse
|
||||
{
|
||||
$link = $this->linkService->upsert($request->validated());
|
||||
|
||||
$code = $link->wasRecentlyCreated ? Response::HTTP_CREATED : Response::HTTP_OK;
|
||||
|
||||
return $this->success([
|
||||
'link' => new WhatsappGroupLinkResource($link),
|
||||
], 'WhatsApp group link saved.', $code);
|
||||
}
|
||||
|
||||
public function update(WhatsappLinkUpdateRequest $request, int $linkId): JsonResponse
|
||||
{
|
||||
$link = $this->linkService->update($linkId, $request->validated());
|
||||
|
||||
return $this->success([
|
||||
'link' => new WhatsappGroupLinkResource($link),
|
||||
], 'WhatsApp group link updated.');
|
||||
}
|
||||
|
||||
public function destroy(int $linkId): JsonResponse
|
||||
{
|
||||
$this->linkService->delete($linkId);
|
||||
|
||||
return $this->success(null, 'WhatsApp group link deleted.');
|
||||
}
|
||||
|
||||
public function parentContacts(WhatsappParentContactsRequest $request): JsonResponse
|
||||
{
|
||||
$payload = $request->validated();
|
||||
$schoolYear = $this->context->schoolYear($payload['school_year'] ?? null);
|
||||
$semester = array_key_exists('semester', $payload)
|
||||
? (string) ($payload['semester'] ?? '')
|
||||
: $this->context->semester();
|
||||
|
||||
$contacts = $this->contactService->listParentContacts($schoolYear, $semester);
|
||||
|
||||
return $this->success([
|
||||
'contacts' => WhatsappParentContactResource::collection($contacts),
|
||||
]);
|
||||
}
|
||||
|
||||
public function parentContactsByClass(WhatsappParentContactsByClassRequest $request): JsonResponse
|
||||
{
|
||||
$payload = $request->validated();
|
||||
$schoolYear = $this->context->schoolYear($payload['school_year'] ?? null);
|
||||
$semester = array_key_exists('semester', $payload)
|
||||
? (string) ($payload['semester'] ?? '')
|
||||
: $this->context->semester();
|
||||
|
||||
$sections = $this->contactService->listParentContactsByClass($schoolYear, $semester);
|
||||
|
||||
return $this->success([
|
||||
'class_sections' => WhatsappClassSectionContactResource::collection($sections),
|
||||
]);
|
||||
}
|
||||
|
||||
public function sendInvites(WhatsappInviteSendRequest $request): JsonResponse
|
||||
{
|
||||
try {
|
||||
$result = $this->inviteService->send($request->validated());
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('WhatsApp invite send failed: ' . $e->getMessage());
|
||||
return $this->error('Invite processing failed.', Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
if (!($result['ok'] ?? false)) {
|
||||
return $this->error($result['message'] ?? 'Invite processing failed.', Response::HTTP_UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
return $this->success([
|
||||
'triggered' => (int) ($result['triggered'] ?? 0),
|
||||
], 'Invite processing started.');
|
||||
}
|
||||
|
||||
public function updateMembership(WhatsappMembershipUpdateRequest $request): JsonResponse
|
||||
{
|
||||
try {
|
||||
$userId = (int) (auth()->id() ?? 0) ?: null;
|
||||
$result = $this->membershipService->updateMembership($request->validated(), $userId);
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('WhatsApp membership update failed: ' . $e->getMessage());
|
||||
return $this->error('Failed to save membership.', Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
if (!($result['ok'] ?? false)) {
|
||||
$code = str_contains((string) ($result['message'] ?? ''), 'missing')
|
||||
? Response::HTTP_INTERNAL_SERVER_ERROR
|
||||
: Response::HTTP_UNPROCESSABLE_ENTITY;
|
||||
|
||||
return $this->error($result['message'] ?? 'Failed to save membership.', $code);
|
||||
}
|
||||
|
||||
return $this->success([
|
||||
'saved' => $result['saved'] ?? [],
|
||||
'term' => $result['term'] ?? [],
|
||||
], 'Membership saved.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Whatsapp;
|
||||
|
||||
use App\Http\Requests\ApiFormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class WhatsappInviteSendRequest extends ApiFormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return auth()->check();
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'mode' => ['required', 'string', Rule::in(['parents', 'class', 'all'])],
|
||||
'class_section_id' => ['required_if:mode,class', 'integer', 'min:1'],
|
||||
'parent_ids' => ['required_if:mode,parents', 'array'],
|
||||
'parent_ids.*' => ['integer', 'min:1'],
|
||||
'school_year' => ['nullable', 'string', 'max:20'],
|
||||
'semester' => ['nullable', 'string', 'max:20'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Whatsapp;
|
||||
|
||||
use App\Http\Requests\ApiFormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class WhatsappLinkIndexRequest extends ApiFormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return auth()->check();
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'school_year' => ['nullable', 'string', 'max:20'],
|
||||
'semester' => ['nullable', 'string', 'max:20'],
|
||||
'class_section_id' => ['nullable', 'integer', 'min:1'],
|
||||
'active' => ['nullable', 'boolean'],
|
||||
'search' => ['nullable', 'string', 'max:2000'],
|
||||
'per_page' => ['nullable', 'integer', 'min:1', 'max:200'],
|
||||
'page' => ['nullable', 'integer', 'min:1'],
|
||||
'sort_by' => ['nullable', 'string', Rule::in([
|
||||
'class_section_id',
|
||||
'class_section_name',
|
||||
'school_year',
|
||||
'semester',
|
||||
'active',
|
||||
'updated_at',
|
||||
])],
|
||||
'sort_dir' => ['nullable', 'string', Rule::in(['asc', 'desc'])],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Whatsapp;
|
||||
|
||||
use App\Http\Requests\ApiFormRequest;
|
||||
|
||||
class WhatsappLinkStoreRequest extends ApiFormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return auth()->check();
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'class_section_id' => ['required', 'integer', 'min:1'],
|
||||
'class_section_name' => ['nullable', 'string', 'max:255'],
|
||||
'school_year' => ['nullable', 'string', 'max:20'],
|
||||
'semester' => ['nullable', 'string', 'max:20'],
|
||||
'invite_link' => ['required', 'string', 'max:2000'],
|
||||
'active' => ['nullable', 'boolean'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Whatsapp;
|
||||
|
||||
use App\Http\Requests\ApiFormRequest;
|
||||
|
||||
class WhatsappLinkUpdateRequest extends ApiFormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return auth()->check();
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'class_section_id' => ['sometimes', 'integer', 'min:1'],
|
||||
'class_section_name' => ['nullable', 'string', 'max:255'],
|
||||
'school_year' => ['nullable', 'string', 'max:20'],
|
||||
'semester' => ['nullable', 'string', 'max:20'],
|
||||
'invite_link' => ['nullable', 'string', 'max:2000'],
|
||||
'active' => ['nullable', 'boolean'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Whatsapp;
|
||||
|
||||
use App\Http\Requests\ApiFormRequest;
|
||||
|
||||
class WhatsappMembershipUpdateRequest extends ApiFormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return auth()->check();
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'class_section_id' => ['required', 'integer', 'min:1'],
|
||||
'primary_id' => ['nullable', 'integer', 'min:1'],
|
||||
'second_id' => ['nullable', 'integer', 'min:1'],
|
||||
'primary_member' => ['nullable', 'boolean'],
|
||||
'second_member' => ['nullable', 'boolean'],
|
||||
'school_year' => ['nullable', 'string', 'max:20'],
|
||||
'semester' => ['nullable', 'string', 'max:20'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Whatsapp;
|
||||
|
||||
use App\Http\Requests\ApiFormRequest;
|
||||
|
||||
class WhatsappParentContactsByClassRequest extends ApiFormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return auth()->check();
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'school_year' => ['nullable', 'string', 'max:20'],
|
||||
'semester' => ['nullable', 'string', 'max:20'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Whatsapp;
|
||||
|
||||
use App\Http\Requests\ApiFormRequest;
|
||||
|
||||
class WhatsappParentContactsRequest extends ApiFormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return auth()->check();
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'school_year' => ['nullable', 'string', 'max:20'],
|
||||
'semester' => ['nullable', 'string', 'max:20'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Whatsapp;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class WhatsappClassSectionContactResource extends JsonResource
|
||||
{
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
'class_section_id' => (int) ($this['class_section_id'] ?? 0),
|
||||
'class_section_name' => (string) ($this['class_section_name'] ?? ''),
|
||||
'semester' => (string) ($this['semester'] ?? ''),
|
||||
'school_year' => (string) ($this['school_year'] ?? ''),
|
||||
'description' => (string) ($this['description'] ?? ''),
|
||||
'main_teachers' => array_values($this['main_teachers'] ?? []),
|
||||
'teacher_assistants' => array_values($this['teacher_assistants'] ?? []),
|
||||
'parents' => WhatsappClassSectionParentResource::collection($this['parents'] ?? []),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Whatsapp;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class WhatsappClassSectionParentResource extends JsonResource
|
||||
{
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
'class_section_id' => (int) ($this['class_section_id'] ?? 0),
|
||||
'primary_id' => (int) ($this['primary_id'] ?? 0),
|
||||
'primary_name' => (string) ($this['primary_name'] ?? ''),
|
||||
'primary_phone' => (string) ($this['primary_phone'] ?? ''),
|
||||
'primary_email' => (string) ($this['primary_email'] ?? ''),
|
||||
'second_id' => (int) ($this['second_id'] ?? 0),
|
||||
'second_name' => (string) ($this['second_name'] ?? ''),
|
||||
'second_phone' => (string) ($this['second_phone'] ?? ''),
|
||||
'second_email' => (string) ($this['second_email'] ?? ''),
|
||||
'primary_member' => $this['primary_member'] ?? null,
|
||||
'second_member' => $this['second_member'] ?? null,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Whatsapp;
|
||||
|
||||
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||
|
||||
class WhatsappGroupLinkCollection extends ResourceCollection
|
||||
{
|
||||
public $collects = WhatsappGroupLinkResource::class;
|
||||
|
||||
public function toArray($request): array
|
||||
{
|
||||
return parent::toArray($request);
|
||||
}
|
||||
|
||||
public function with($request): array
|
||||
{
|
||||
return [
|
||||
'meta' => [
|
||||
'total' => $this->resource->total() ?? null,
|
||||
'per_page' => $this->resource->perPage() ?? null,
|
||||
'current_page' => $this->resource->currentPage() ?? null,
|
||||
'last_page' => $this->resource->lastPage() ?? null,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Whatsapp;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class WhatsappGroupLinkResource extends JsonResource
|
||||
{
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
'id' => (int) ($this->id ?? 0),
|
||||
'class_section_id' => (int) ($this->class_section_id ?? 0),
|
||||
'class_section_name' => (string) ($this->class_section_name ?? ''),
|
||||
'school_year' => (string) ($this->school_year ?? ''),
|
||||
'semester' => (string) ($this->semester ?? ''),
|
||||
'invite_link' => (string) ($this->invite_link ?? ''),
|
||||
'active' => (bool) ($this->active ?? false),
|
||||
'created_at' => $this->created_at ?? null,
|
||||
'updated_at' => $this->updated_at ?? null,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Whatsapp;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class WhatsappParentContactResource extends JsonResource
|
||||
{
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
'firstname' => (string) ($this['firstname'] ?? ''),
|
||||
'lastname' => (string) ($this['lastname'] ?? ''),
|
||||
'email' => (string) ($this['email'] ?? ''),
|
||||
'phone' => (string) ($this['phone'] ?? ''),
|
||||
'type' => (string) ($this['type'] ?? ''),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user