Skip to content

Commit

Permalink
migrate some unit tests for the models
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilReinking committed Sep 18, 2021
1 parent df77ae1 commit e6ff3ec
Show file tree
Hide file tree
Showing 19 changed files with 524 additions and 51 deletions.
52 changes: 52 additions & 0 deletions app/Jobs/CallWebhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Jobs;

use App\Models\FormSession;
use Illuminate\Bus\Queueable;
use Illuminate\Support\Facades\Http;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class CallWebhook implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public $session;
public $webhookUrl;

/**
* Create a new job instance.
*
* @return void
*/
public function __construct(FormSession $session, String $webhookUrl)
{
$this->session = $session;
$this->webhookUrl = $webhookUrl;
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$responses = $this->session->responses->mapWithKeys(function ($response) {
return [$response->block->uuid => $response->value];
});

$request = array_merge([
'_id' => $this->session->token,
], $responses->toArray());

info('send snippet webhook to ' . $this->webhookUrl, [
'payload' => $request,
]);

Http::post($this->webhookUrl, $request);
}
}
53 changes: 27 additions & 26 deletions app/Models/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace App\Models;

use App\Snippet;
use Hashids\Hashids;
use Ramsey\Uuid\Uuid;
use App\Models\FormBlock;
use App\Models\FormSession;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -75,19 +76,19 @@ public function scopePublished($query)
return $query->whereNotNull('published_at')->whereDate('published_at', '<=', Carbon::now());
}

public function snippets()
public function blocks()
{
return $this->hasMany(Snippet::class);
return $this->hasMany(FormBlock::class);
}

public function conversations()
public function sessions()
{
return $this->hasMany(Conversation::class);
return $this->hasMany(FormSession::class);
}

public function results()
public function responses()
{
return $this->hasManyThrough(FormSessionResponse::class, Snippet::class);
return $this->hasManyThrough(FormSessionResponse::class, FormBlock::class);
}

public function user()
Expand All @@ -97,31 +98,31 @@ public function user()

public function route()
{
return route('chatbot.show', $this->uuid);
return route('forms.show', $this->uuid);
}

public function isEmpty()
{
return $this->snippetsCount() <= 0;
return $this->blocksCount() <= 0;
}

public function snippetsCount()
public function blocksCount()
{
return $this->snippets->count();
return $this->blocks->count();
}

public function actionSnippetsCount()
public function actionBlocksCount()
{
return $this->snippets->filter(function ($item) {
return $this->blocks->filter(function ($item) {
return $item->hasResponseAction();
})->count();
}

public function resultsCount()
public function responsesCount()
{
$result = $this->results()
$result = $this->responses()
->select(DB::raw('count(*) as response_count'))
->groupBy('responses.snippet_id')
->groupBy('form_block_id')
->orderBy('response_count', 'DESC')
->limit(1)
->first();
Expand Down Expand Up @@ -207,23 +208,23 @@ public function getInitialsAttribute()

public function countSessions()
{
$snippets = $this->snippets->pluck('id')->toArray();
$blocks = $this->blocks->pluck('id')->toArray();

return FormSessionResponse::select('session')
->whereIn('snippet_id', $snippets)
->whereIn('form_block_id', $blocks)
->groupBy('session')
->get()
->count();
}

public function totalConversations()
public function totalSessions()
{
return $this->conversations()->whereHas('responses')->count();
return $this->sessions()->whereHas('responses')->count();
}

public function completedConversations()
public function completedSessions()
{
return $this->conversations()
return $this->sessions()
->whereHas('responses')
->get()
->where('is_completed', true)
Expand All @@ -233,7 +234,7 @@ public function completedConversations()
public function completionRate()
{
try {
return round(($this->completedConversations() / $this->totalConversations()) * 100, 2);
return round(($this->completedSessions() / $this->totalSessions()) * 100, 2);
} catch (\Throwable $th) {
return 0;
}
Expand All @@ -246,12 +247,12 @@ public function isOwner(User $user = null)

public function countSessionsForCurrentMonth()
{
$snippets = $this->snippets->pluck('id')->toArray();
$blocks = $this->blocks->pluck('id')->toArray();

return FormSessionResponse::select('*')
->whereYear('created_at', '=', Carbon::now())
->whereMonth('created_at', '=', Carbon::now())
->whereIn('snippet_id', $snippets)
->whereIn('form_block_id', $blocks)
->groupBy('session')
->get()
->count();
Expand All @@ -261,7 +262,7 @@ public function createDefaultConsent()
{
FormBlock::create([
'type' => FormBlock::CONSENT,
'chatbot_id' => $this->id,
'form_id' => $this->id,
]);
}
}
11 changes: 7 additions & 4 deletions app/Models/FormBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace App\Models;

use App\Models\Form;
use Hashids\Hashids;
use App\Scopes\Sequence;
use Webpatser\Uuid\Uuid;
use App\Models\FormSessionResponse;
use App\Models\FormBlockInteraction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

Expand Down Expand Up @@ -73,17 +76,17 @@ public function hasInput()

public function interactions()
{
return $this->hasMany(Interaction::class);
return $this->hasMany(FormBlockInteraction::class);
}

public function responses()
{
return $this->hasMany(Response::class);
return $this->hasMany(FormSessionResponse::class);
}

public function chatbot()
public function form()
{
return $this->belongsTo(Chatbot::class);
return $this->belongsTo(Form::class);
}

public function getTypingDelayAttribute()
Expand Down
2 changes: 1 addition & 1 deletion app/Models/FormBlockInteraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class FormBlockInteraction extends Model
const TYPE_CONSENT = 'consent';

protected $casts = [
'block_id' => 'integer',
'form_block_id' => 'integer',
];

protected $hidden = [
Expand Down
6 changes: 4 additions & 2 deletions app/Models/FormSessionResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Models\FormBlock;
use App\Models\FormSession;
use App\Scopes\WithoutChildren;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -27,12 +28,13 @@ public function scopeOfSession($query, $token)

public function block()
{
return $this->belongsTo(Block::class)->withoutGlobalScope(WithoutChildren::class);
return $this->belongsTo(FormBlock::class, 'form_block_id')
->withoutGlobalScope(WithoutChildren::class);
}

public function session()
{
return $this->belongsTo(FormSession::class);
return $this->belongsTo(FormSession::class, 'form_session_id');
}


Expand Down
24 changes: 18 additions & 6 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace App\Models;

use Illuminate\Support\Str;
use Laravel\Jetstream\HasTeams;
use Laravel\Sanctum\HasApiTokens;
use Laravel\Jetstream\HasProfilePhoto;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Jetstream\HasTeams;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
Expand All @@ -26,7 +27,7 @@ class User extends Authenticatable
* @var string[]
*/
protected $fillable = [
'name', 'email', 'password',
'name', 'email', 'password', 'salt'
];

/**
Expand Down Expand Up @@ -58,4 +59,15 @@ class User extends Authenticatable
protected $appends = [
'profile_photo_url',
];

protected static function boot()
{
parent::boot();

self::created(function ($model) {
$model->update([
'salt' => Str::random(32),
]);
});
}
}
9 changes: 8 additions & 1 deletion config/filesystems.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,17 @@
'root' => storage_path('app'),
],

'avatars' => [
'driver' => 'local',
'root' => storage_path('app/avatars'),
'url' => env('APP_URL') . '/avatars',
'visibility' => 'public',
],

'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'url' => env('APP_URL') . '/storage',
'visibility' => 'public',
],

Expand Down
2 changes: 1 addition & 1 deletion database/factories/FormBlockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use App\Models\FormBlock;
use Illuminate\Database\Eloquent\Factories\Factory;

class BlockFactory extends Factory
class FormBlockFactory extends Factory
{
/**
* The name of the factory's corresponding model.
Expand Down
4 changes: 2 additions & 2 deletions database/factories/FormBlockInteractionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use App\Models\FormBlockInteraction;
use Illuminate\Database\Eloquent\Factories\Factory;

class InteractionFactory extends Factory
class FormBlockInteractionFactory extends Factory
{
/**
* The name of the factory's corresponding model.
Expand All @@ -26,7 +26,7 @@ public function definition()
return [
'uuid' => $this->faker->uuid(),
'type' => FormBlockInteraction::TYPE_CLICK,
'block_id' => FormBlock::factory(),
'form_block_id' => FormBlock::factory(),
];
}
}
2 changes: 1 addition & 1 deletion database/factories/FormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Database\Factories;

use App\Models\Form;
use App\Models\User;
use App\Models\Model;
use Illuminate\Support\Carbon;
use Illuminate\Database\Eloquent\Factories\Factory;

Expand Down
1 change: 1 addition & 0 deletions database/factories/FormSessionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Form;
use App\Models\Model;
use App\Models\FormSession;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Factories\Factory;

Expand Down
6 changes: 3 additions & 3 deletions database/factories/FormSessionResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use App\Models\FormBlockInteraction;
use Illuminate\Database\Eloquent\Factories\Factory;

class ResponseFactory extends Factory
class FormSessionResponseFactory extends Factory
{
/**
* The name of the factory's corresponding model.
Expand All @@ -27,8 +27,8 @@ public function definition()
{
return [
'value' => $this->faker->word(),
'block_id' => FormBlock::factory(),
'interaction_id' => FormBlockInteraction::factory(),
'form_block_id' => FormBlock::factory(),
'form_block_interaction_id' => FormBlockInteraction::factory(),
'form_session_id' => FormSession::factory(),
];
}
Expand Down
Loading

0 comments on commit e6ff3ec

Please sign in to comment.