Console Commands (Artisan)
Relevant source files
The following files were used as context for generating this wiki page:Artisan console commands in the QashierWise backend are essential for performing background tasks, data synchronization, and automating development workflows. These commands are built using Laravel's Artisan console framework and are executed via the command line.
The primary custom command available is whatsapp:sync, which is responsible for synchronizing WhatsApp data, such as message templates, from the Meta (Facebook) Graph API into the local application database. Additionally, several standard and custom scripts are defined in composer.json to streamline development, setup, and testing processes.
Available Commands
The project utilizes a mix of custom and standard Laravel Artisan commands, often orchestrated through composer.json scripts.
| Command/Script | Signature | Description | Source File |
|---|---|---|---|
whatsapp:sync | whatsapp:sync {--templates : Sync templates only} | Syncs WhatsApp data (templates) from the Meta API to the local database. | app/Console/Commands/SyncWhatsAppData.php |
setup | composer run setup | Installs dependencies, generates an app key, runs migrations, and builds frontend assets. | composer.json |
dev | composer run dev | Concurrently runs the PHP dev server, queue listener, log tailing, and Vite dev server. | composer.json |
test | composer run test | Clears the configuration cache and runs the application's test suite. | composer.json |
Sources: app/Console/Commands/SyncWhatsAppData.php:15, composer.json:42-57
Detailed Command Analysis
whatsapp:sync
This command is the core utility for keeping the local database in sync with the WhatsApp Business Account data hosted by Meta. Its primary function is to fetch message templates.
Sources: app/Console/Commands/SyncWhatsAppData.php
Signature
php artisan whatsapp:sync {--templates : Sync templates only}
--templates: An optional flag that restricts the sync operation to only message templates. The current implementation syncs templates regardless of this flag.
Sources: app/Console/Commands/SyncWhatsAppData.php:15, app/Console/Commands/SyncWhatsAppData.php:44-46
Execution Flow
The command follows a clear sequence to fetch and store data. It ensures a WhatsApp account exists locally before proceeding to sync templates from the Meta Graph API.
This diagram illustrates the execution flow of the whatsapp:sync command.
Key Logic and Components
-
Ensure WhatsApp Account: The
ensureWhatsAppAccountmethod usesfirstOrCreateto guarantee a localWhatsAppAccountrecord exists. It pulls credentials likephone_number_id,business_account_id, andaccess_tokenfrom the application's configuration (config/whatsapp.php). -
Sync Templates: The
syncTemplatesmethod constructs a request to the Meta Graph API endpointhttps://graph.facebook.com/v21.0/{wabaId}/message_templates. It requests fields such asname,status,category, andcomponents. -
Component Parsing: For each template returned by the API, the command iterates through its
componentsarray to extract theHEADER,BODY,FOOTER, andBUTTONS. -
Database Persistence: The parsed template data is saved to the
whatsapp_templatestable usingWhatsAppTemplate::updateOrCreate. This method finds a template bywhatsapp_account_idandnameor creates a new one, ensuring data is always up-to-date without creating duplicates.
User-Facing Trigger
While this is a console command, its functionality can be triggered from the web interface. The "Templates" dashboard contains a "Sync from Meta" button that initiates this process via an API call.
- File:
resources/views/dashboard/templates.blade.php - Logic: The button calls the
refreshTemplates()JavaScript function, which makes a GET request to/api/whatsapp/templates?refresh=true. This suggests a controller action likely invokes the same logic as thewhatsapp:synccommand.
Sources: resources/views/dashboard/templates.blade.php:73-78, resources/views/dashboard/templates.blade.php:1003-1011
Development & Setup Commands
The composer.json file defines several scripts that leverage Artisan commands to simplify and standardize the development environment setup and daily workflow.
Sources: composer.json:42-69
composer run setup
This script automates the initial project setup.
"setup": [
"composer install",
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\"",
"@php artisan key:generate",
"@php artisan migrate --force",
"npm install",
"npm run build"
]
Sources: composer.json:43-50
composer run dev
This script provides a unified command to start all necessary services for local development, running them concurrently.
This diagram shows the concurrent processes started by the dev script.
Sources: composer.json:51-55
Related Services and Models
The console commands interact with various parts of the application, including services and database models.
TemplateService
The TemplateService provides an API-driven way to manage templates, complementing the whatsapp:sync command. While the command is for bulk synchronization, the service handles individual CRUD (Create, Read, Update, Delete) operations, likely triggered by user actions in the dashboard. It contains methods for validating and building template components for API submission.
Sources: app/Services/TemplateService.php
Data Models
The whatsapp:sync command directly interacts with the following Eloquent models. The relationship is that a WhatsAppAccount can have many WhatsAppTemplate records.
This ER diagram shows the relationship between WhatsApp accounts and their templates.