Skip to content

Commit

Permalink
Merge pull request #7 from APIBrasil/beta
Browse files Browse the repository at this point in the history
Beta
  • Loading branch information
jhowbhz authored Aug 22, 2024
2 parents 17a0546 + 964dc59 commit 8d19f7b
Show file tree
Hide file tree
Showing 20 changed files with 320 additions and 218 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Esse é o roadmap planejado para a plataforma
| OK | Editar dispositivo |
| OK | Deletar dispositivo |
| OK | Envia Texto, Imagens e arquivos |
| Em andamento | Criar usuário |
| OK | Criar usuário |
| Em andamento | Editar usuário |
| Em andamento | Deletar usuário |
-----------------------------------------------------
Expand Down
2 changes: 0 additions & 2 deletions app/Console/Commands/SeedCreateFirstUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ public function handle()
return 1;
}

// Verifica se o usuário já existe
if (User::where('email', $email)->exists()) {
$this->error('Usuário com este email já existe.');
return 1;
}

// Cria o usuário
User::create([
'name' => 'Admin',
'email' => $email,
Expand Down
75 changes: 23 additions & 52 deletions app/Console/Commands/SendMessagesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ public function handle()
}

$random_device = $devices_online[array_rand($devices_online)];
$messages_pending = $disparos->messagesPending;
$messagesPending = $disparos->messagesPending;

if (count($messages_pending) == 0) {
if (count($messagesPending) == 0) {
echo "No messages pending for user {$user->name}\n";
continue;
}

$qt_disparo = 0;

foreach ($messages_pending as $message) {
foreach ($messagesPending as $message) {

$messageParsed = $this->parseMessage($message);

Expand Down Expand Up @@ -86,6 +86,7 @@ public function handle()
}

if($message->template->type == 'text') {

$sendText = Service::WhatsApp("sendText", [
"Bearer" => $token,
"DeviceToken" => $random_device->device_token,
Expand All @@ -95,50 +96,22 @@ public function handle()
]
]);

if(!isset($sendText->response->result)) {
$message->status = 'error';
$message->save();
continue;
}

if (!$sendText or $sendText->response->result != 200) {
$message->status = 'error';
$message->save();
continue;
}
}

if($message->template->type == 'image') {

$sendFile = Service::WhatsApp("sendFile", [
"Bearer" => $token,
"DeviceToken" => $random_device->device_token,
"body" => [
"number" => $message->contato->number,
"path" => $message->template->path,
"options" => [
"caption" => $messageParsed,
"createChat" > true,
"filename" => basename($message->template->path)
]
]
]);

if(!isset($sendFile->response->result)) {
$message->status = 'error';
$message->save();
continue;
}

if (!$sendFile or $sendFile->response->result != 200) {
if (!$sendText or !isset($sendText->response->result) or $sendText->response->result != 200) {
$message->status = 'error';
$message->log = json_encode($sendText);
$message->save();
continue;
}

$qt_disparo++;

$message->status = 'sent';
$message->send_at = now();
$message->save();

}

if($message->template->type == 'file') {
if( $message->template->type == 'file' or $message->template->type == 'image' ) {

$sendFile = Service::WhatsApp("sendFile", [
"Bearer" => $token,
Expand All @@ -154,28 +127,26 @@ public function handle()
]
]);

if(!isset($sendFile->response->result)) {
if (!$sendFile or !isset($sendFile->response->result) or $sendFile->response->result != 200) {
$message->status = 'error';
$message->log = json_encode($sendFile);
$message->save();
continue;
}

$qt_disparo++;

if (!$sendFile or $sendFile->response->result != 200) {
$message->status = 'error';
$message->save();
continue;
}
$message->status = 'sent';
$message->send_at = now();
$message->save();

}

$qt_disparo++;

$message->status = 'sent';
$message->send_at = now();
$message->save();

}

$disparos->status = 'finish';
$disparos->save();

}
}

Expand Down
21 changes: 15 additions & 6 deletions app/Http/Controllers/Admin/ContatosController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,40 @@
use App\Models\Tags;
use App\Models\Contatos;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Jobs\UploadContactsJob;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Yajra\DataTables\Facades\DataTables;
use Illuminate\Support\Facades\Validator;

class ContatosController extends Controller
{

public function index()
{
$contatos = Contatos::where('user_id', Auth::user()->id)
->orderBy('id', 'desc')
->get();


$tags = Tags::orderBy('id', 'desc')
->where('status', 'active')
->where('user_id', Auth::user()->id)
->get();

return view('admin.contatos')
->with('contatos', $contatos)
->with('tags', $tags);

}

public function datatables()
{

$contatos = Contatos::where('user_id', Auth::user()->id)
->with('tag')
->orderBy('id', 'desc')
->get();

return DataTables::of($contatos)->make(true);

}

public function store(Request $request)
{
try {
Expand Down
20 changes: 16 additions & 4 deletions app/Http/Controllers/Admin/DisparosController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Yajra\DataTables\Facades\DataTables;

class DisparosController extends Controller
{

public function index()
{
$disparos = Disparos::orderBy('id', 'desc')
->where('user_id', Auth::user()->id)
->get();

$templates = Templates::orderBy('id', 'desc')
->where('status', 'active')
Expand All @@ -30,11 +28,25 @@ public function index()
->get();

return view('admin.disparos')
->with('disparos', $disparos)
->with('templates', $templates)
->with('tags', $tags);
}

public function datatables()
{

$disparos = Disparos::orderBy('id', 'desc')
->with('tag')
->with('templates')
->withCount('messagesPending')
->withCount('messagesSent')
->where('user_id', Auth::user()->id)
->get();

return DataTables::of($disparos)->make(true);

}

public function store(Request $request)
{
try {
Expand Down
22 changes: 16 additions & 6 deletions app/Http/Controllers/Admin/DispositivosController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,17 @@
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Yajra\DataTables\Facades\DataTables;
use GuzzleHttp\Psr7\Request as RequestGuzzle;

class DispositivosController extends Controller
{
public function index()
{

$dispositivos = Dispositivos::getAll();
$servidores = Servidores::getAll();
$apis = API::getAll();

$dispositivos = array_filter($dispositivos, function($dispositivo) {
return $dispositivo->type == 'cellphone' or $dispositivo->type == 'tablet';
});

$apis = array_filter($apis, function($api) {
return $api->type == 'whatsapp' or $api->type == 'baileys';
Expand All @@ -36,8 +33,21 @@ public function index()

return view('admin.dispositivos')
->with('servidores', $servidores)
->with('apis', $apis)
->with('dispositivos', $dispositivos);
->with('apis', $apis);
}

public function datatables()
{

$dispositivos = Dispositivos::getAll();

$dispositivos = array_filter($dispositivos, function($dispositivo) {
return $dispositivo->type == 'cellphone' or $dispositivo->type == 'tablet';
});


return DataTables::of($dispositivos)->make(true);

}

public function start(string $device_token)
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Admin/HistoricoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function dataTables()
->with(['contato' => function($query){
$query->select('id', 'number'); // Include 'id' to avoid issues with missing columns
}, 'tag' => function($query){
$query->select('id', 'name'); // Include 'id' to avoid issues with missing columns
$query->select('id', 'color', 'name'); // Include 'id' to avoid issues with missing columns
}])
->with('template', function($query){
$query->select('id', 'name'); // Include 'id' to avoid issues with missing columns
Expand Down
3 changes: 1 addition & 2 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public function register(Request $request): RedirectResponse
}

if (User::where('email', $email)->exists()) {
return redirect('register')->with('error', 'Este email já está em uso.');
return 1;
return redirect('register')->with('error', "O e-mail {$email} já está em uso.");
}

$client = new Client(['http_errors' => false, 'verify' => false]);
Expand Down
6 changes: 6 additions & 0 deletions app/Models/Disparos.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ public function messagesPending()
->with('tag');
}

//templates
public function templates()
{
return $this->hasMany(Templates::class, 'id', 'templates_id');
}

public function messagesSent()
{
return $this->hasMany(Mensagens::class, 'disparo_id', 'id')
Expand Down
3 changes: 0 additions & 3 deletions app/Models/Dispositivos.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
namespace App\Models;

use ApiBrasil\Service;
use GuzzleHttp\Client;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Database\Eloquent\Model;
use GuzzleHttp\Psr7\Request as RequestGuzzle;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Dispositivos extends Model
Expand Down
1 change: 1 addition & 0 deletions app/Models/Mensagens.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Mensagens extends Model
'user_id',
'disparo_id',
'send_at',
'log',
'status',
];

Expand Down
17 changes: 6 additions & 11 deletions app/Models/Servidores.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace App\Models;

use GuzzleHttp\Client;
use ApiBrasil\Service;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

use GuzzleHttp\Psr7\Request as RequestGuzzle;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Servidores extends Model
{
Expand All @@ -17,18 +16,14 @@ public static function getAll()
{
try {

$client = new Client(['http_errors' => false, 'verify' => false]);
$token = Auth::user()->bearer_token_api_brasil;

$request = new RequestGuzzle('GET', env("API_URL").'/v2/servers', [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $token
$servers = Service::Server("", [
"Bearer" => $token,
"method" => "GET",
]);

$res = $client->sendAsync($request)->wait();
$response = json_decode($res->getBody()->getContents());

return $response;
return $servers;

} catch (\GuzzleHttp\Exception\GuzzleException $th) {
return response()->json(['error' => $th->getMessage()]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public function up(): void
$table->unsignedBigInteger('disparo_id')->nullable();
$table->foreign('disparo_id')->references('id')->on('disparos');

$table->timestamp('send_at')->nullable();

$table->enum('status', ['pending', 'sent', 'error'])->default('pending');

$table->timestamp('send_at')->nullable();

$table->timestamps();
});
}
Expand Down
Loading

0 comments on commit 8d19f7b

Please sign in to comment.