Separated laravel app to its own folder (#540)

This commit is contained in:
Julien Nahum
2024-08-26 18:24:56 +02:00
committed by GitHub
parent 39b8df5eed
commit 5bd1dda504
546 changed files with 124 additions and 143 deletions

View File

@@ -0,0 +1,28 @@
<?php
namespace App\Models\Billing;
use App\Events\Billing\SubscriptionCreated;
use App\Events\Billing\SubscriptionUpdated;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Laravel\Cashier\Subscription as CashierSubscription;
class Subscription extends CashierSubscription
{
use HasFactory;
protected $dispatchesEvents = [
'created' => SubscriptionCreated::class,
'updated' => SubscriptionUpdated::class,
];
public static function booted(): void
{
static::saved(function (Subscription $sub) {
$sub->user->flushCache();
});
static::deleted(function (Subscription $sub) {
$sub->user->flushCache();
});
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Models\Forms\AI;
use App\Jobs\Form\GenerateAiForm;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class AiFormCompletion extends Model
{
use HasFactory;
public const STATUS_PENDING = 'pending';
public const STATUS_PROCESSING = 'processing';
public const STATUS_COMPLETED = 'completed';
public const STATUS_FAILED = 'failed';
protected $table = 'ai_form_completions';
protected $fillable = [
'form_prompt',
'status',
'result',
'ip',
];
protected $attributes = [
'status' => self::STATUS_PENDING,
];
protected static function booted()
{
// Dispatch completion job on creation
static::created(function (self $completion) {
GenerateAiForm::dispatch($completion);
});
}
}

View File

@@ -0,0 +1,320 @@
<?php
namespace App\Models\Forms;
use App\Events\Models\FormCreated;
use App\Models\Integration\FormIntegration;
use App\Models\Integration\FormZapierWebhook;
use App\Models\Traits\CachableAttributes;
use App\Models\Traits\CachesAttributes;
use App\Models\User;
use App\Models\Workspace;
use Database\Factories\FormFactory;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
use Stevebauman\Purify\Facades\Purify;
use Carbon\Carbon;
class Form extends Model implements CachableAttributes
{
use CachesAttributes;
use HasFactory;
use HasSlug;
use SoftDeletes;
public const DARK_MODE_VALUES = ['auto', 'light', 'dark'];
public const SIZES = ['sm', 'md', 'lg'];
public const BORDER_RADIUS = ['none', 'small', 'full'];
public const THEMES = ['default', 'simple', 'notion'];
public const WIDTHS = ['centered', 'full'];
public const VISIBILITY = ['public', 'draft', 'closed'];
protected $fillable = [
'workspace_id',
'creator_id',
'properties',
'removed_properties',
'title',
'description',
'tags',
'visibility',
// Customization
'font_family',
'custom_domain',
'size',
'border_radius',
'theme',
'width',
'cover_picture',
'logo_picture',
'dark_mode',
'color',
'uppercase_labels',
'no_branding',
'hide_title',
'transparent_background',
// Custom Code
'custom_code',
// Submission
'submit_button_text',
'database_fields_update',
're_fillable',
're_fill_button_text',
'submitted_text',
'redirect_url',
'use_captcha',
'closes_at',
'closed_text',
'max_submissions_count',
'max_submissions_reached_text',
'editable_submissions',
'editable_submissions_button_text',
'confetti_on_submission',
'show_progress_bar',
'auto_save',
'auto_focus',
// Security & Privacy
'can_be_indexed',
'password',
// Custom SEO
'seo_meta',
];
protected function casts(): array
{
return [
'properties' => 'array',
'database_fields_update' => 'array',
'closes_at' => 'datetime',
'tags' => 'array',
'removed_properties' => 'array',
'seo_meta' => 'object'
];
}
protected $appends = [
'share_url',
];
protected $hidden = [
'workspace_id',
'redirect_url',
'database_fields_update',
'password',
'tags',
'removed_properties',
];
protected $cachableAttributes = [
'is_pro',
'views_count',
'max_file_size',
];
/**
* The event map for the model.
*
* @var array
*/
protected $dispatchesEvents = [
'created' => FormCreated::class,
];
public function getIsProAttribute()
{
return $this->remember('is_pro', 15 * 60, function (): ?bool {
return $this->workspace?->is_pro === true;
});
}
public function getShareUrlAttribute()
{
if ($this->custom_domain) {
return 'https://' . $this->custom_domain . '/forms/' . $this->slug;
}
return front_url('/forms/' . $this->slug);
}
public function getEditUrlAttribute()
{
return front_url('/forms/' . $this->slug . '/show');
}
public function getSubmissionsCountAttribute()
{
return $this->submissions()->count();
}
public function getViewsCountAttribute()
{
return $this->remember('views_count', 15 * 60, function (): int {
if (env('DB_CONNECTION') == 'mysql') {
return (int) ($this->views()->count() +
$this->statistics()->sum(DB::raw("json_extract(data, '$.views')")));
}
return $this->views()->count() +
$this->statistics()->sum(DB::raw("cast(data->>'views' as integer)"));
});
}
public function setDescriptionAttribute($value)
{
// Strip out unwanted html
$this->attributes['description'] = Purify::clean($value);
}
public function setSubmittedTextAttribute($value)
{
// Strip out unwanted html
$this->attributes['submitted_text'] = Purify::clean($value);
}
public function setTagsAttribute($value)
{
if ($value == '') {
$value = null;
}
$this->attributes['tags'] = json_encode($value);
}
public function setClosesAtAttribute($value)
{
$this->attributes['closes_at'] = ($value) ? Carbon::parse($value)->setTimezone('UTC') : null;
}
public function getClosesAtAttribute($value)
{
if (!$value) {
return $value;
}
// Retrieve the desired timezone from the request or default to 'UTC'
$timezone = request()->get('timezone', 'UTC');
return Carbon::parse($value)->setTimezone($timezone)->toIso8601String();
}
public function getIsClosedAttribute()
{
return $this->closes_at && now()->gt($this->closes_at);
}
public function getFormPendingSubmissionKeyAttribute()
{
if ($this->updated_at?->timestamp) {
return 'openform-' . $this->id . '-pending-submission-' . substr($this->updated_at?->timestamp, -6);
}
return null;
}
public function getMaxNumberOfSubmissionsReachedAttribute()
{
return $this->max_submissions_count && $this->max_submissions_count <= $this->submissions_count;
}
public function setClosedTextAttribute($value)
{
$this->attributes['closed_text'] = Purify::clean($value);
}
public function setMaxSubmissionsReachedTextAttribute($value)
{
$this->attributes['max_submissions_reached_text'] = Purify::clean($value);
}
public function getHasPasswordAttribute()
{
return !empty($this->password);
}
public function getMaxFileSizeAttribute()
{
return $this->remember('max_file_size', 15 * 60, function (): int {
return $this->workspace->max_file_size;
});
}
protected function removedProperties(): Attribute
{
return Attribute::make(
get: function ($value) {
return $value ? json_decode($value, true) : [];
}
);
}
/**
* Relationships
*/
public function workspace()
{
return $this->belongsTo(Workspace::class);
}
public function creator()
{
return $this->belongsTo(User::class, 'creator_id');
}
public function submissions()
{
return $this->hasMany(FormSubmission::class);
}
public function views()
{
return $this->hasMany(FormView::class);
}
public function statistics()
{
return $this->hasMany(FormStatistic::class);
}
public function zappierHooks()
{
return $this->hasMany(FormZapierWebhook::class);
}
public function integrations()
{
return $this->hasMany(FormIntegration::class);
}
/**
* Config/options
*/
public function getSlugOptions(): SlugOptions
{
return SlugOptions::create()
->doNotGenerateSlugsOnUpdate()
->generateSlugsFrom(function (Form $form) {
return $form->title . ' ' . Str::random(6);
})
->saveSlugsTo('slug');
}
public static function newFactory()
{
return FormFactory::new();
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace App\Models\Forms;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class FormStatistic extends Model
{
use HasFactory;
public $timestamps = false;
protected $fillable = [
'form_id',
'data',
'date',
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected function casts(): array
{
return [
'data' => 'array',
];
}
/**
* Relationships
*/
public function form()
{
return $this->belongsTo(Form::class);
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Models\Forms;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class FormSubmission extends Model
{
use HasFactory;
protected $fillable = [
'data',
];
protected function casts(): array
{
return [
'data' => 'array',
];
}
/**
* RelationShips
*/
public function form()
{
return $this->belongsTo(Form::class);
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Models\Forms;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class FormView extends Model
{
use HasFactory;
/**
* RelationShips
*/
public function form()
{
return $this->belongsTo(Form::class);
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace App\Models\Integration;
use App\Events\Models\FormIntegrationCreated;
use App\Models\Forms\Form;
use App\Models\OAuthProvider;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class FormIntegration extends Model
{
use HasFactory;
public const STATUS_ACTIVE = 'active';
public const STATUS_INACTIVE = 'inactive';
protected $fillable = [
'form_id',
'status',
'integration_id',
'logic',
'data',
'oauth_id'
];
protected function casts(): array
{
return [
'data' => 'object',
'logic' => 'object'
];
}
protected $dispatchesEvents = [
'created' => FormIntegrationCreated::class,
];
/**
* Relationships
*/
public function form()
{
return $this->belongsTo(Form::class);
}
public function events()
{
return $this->hasMany(FormIntegrationsEvent::class, 'integration_id');
}
public function provider()
{
return $this->belongsTo(OAuthProvider::class, 'oauth_id');
}
public static function getAllIntegrations()
{
return json_decode(file_get_contents(resource_path('data/forms/integrations.json')), true);
}
public static function getIntegration($key)
{
return self::getAllIntegrations()[$key] ?? null;
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace App\Models\Integration;
use App\Events\Models\FormIntegrationsEventCreated;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class FormIntegrationsEvent extends Model
{
use HasFactory;
public const STATUS_SUCCESS = 'success';
public const STATUS_ERROR = 'error';
protected $fillable = [
'integration_id',
'status',
'data'
];
protected function casts()
{
return [
'data' => 'object'
];
}
/**
* The event map for the model.
*
* @var array
*/
protected $dispatchesEvents = [
'created' => FormIntegrationsEventCreated::class,
];
public function integration()
{
return $this->belongsTo(FormIntegration::class, 'integration_id');
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace App\Models\Integration;
use App\Models\Forms\Form;
use App\Service\Forms\Webhooks\WebhookHandlerProvider;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class FormZapierWebhook extends Model
{
use HasFactory;
use SoftDeletes;
protected $table = 'form_zapier_webhooks';
protected $fillable = [
'form_id',
'hook_url',
];
/**
* Relationships
*/
public function form()
{
return $this->belongsTo(Form::class);
}
public function triggerHook(array $data)
{
WebhookHandlerProvider::getProvider(
$this->form,
$data,
WebhookHandlerProvider::ZAPIER_PROVIDER,
$this->hook_url
)->handle();
}
}

View File

@@ -0,0 +1,85 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class License extends Model
{
use HasFactory;
public const STATUS_ACTIVE = 'active';
public const STATUS_INACTIVE = 'inactive';
protected $fillable = [
'license_key',
'user_id',
'license_provider',
'status',
'meta',
];
protected function casts()
{
return [
'meta' => 'array',
];
}
public function user()
{
return $this->belongsTo(User::class);
}
public function isActive()
{
return $this->status === self::STATUS_ACTIVE;
}
public function scopeActive($query)
{
return $query->where('status', self::STATUS_ACTIVE);
}
public function getMaxFileSizeAttribute(): int
{
return [
1 => 25000000, // 25 MB,
2 => 50000000, // 50 MB,
3 => 75000000, // 75 MB,
][$this->meta['tier']];
}
public function getCustomDomainLimitCountAttribute(): ?int
{
return [
1 => 1,
2 => 10,
3 => null,
][$this->meta['tier']];
}
public function getMaxUsersLimitCountAttribute(): ?int
{
return [
1 => 1,
2 => 5,
3 => 20,
][$this->meta['tier']];
}
public static function booted(): void
{
static::saved(function (License $license) {
if ($license->user) {
$license->user->flushCache();
}
});
static::deleted(function (License $license) {
if ($license->user) {
$license->user->flushCache();
}
});
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace App\Models;
use App\Integrations\OAuth\OAuthProviderService;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class OAuthProvider extends Model
{
use HasFactory;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'oauth_providers';
/**
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $guarded = ['id'];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'access_token', 'refresh_token',
];
protected function casts()
{
return [
'provider' => OAuthProviderService::class,
'token_expires_at' => 'datetime',
];
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class);
}
}

117
api/app/Models/Template.php Normal file
View File

@@ -0,0 +1,117 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
use Stevebauman\Purify\Facades\Purify;
class Template extends Model
{
use HasFactory;
use HasSlug;
protected $fillable = [
'creator_id',
'name',
'slug',
'description',
'short_description',
'image_url',
'structure',
'questions',
'publicly_listed',
'industries',
'types',
'related_templates',
];
protected function casts()
{
return [
'structure' => 'array',
'questions' => 'array',
'industries' => 'array',
'types' => 'array',
'related_templates' => 'array',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
}
protected $attributes = [
'publicly_listed' => false,
];
protected $appends = [
'share_url',
];
public function getShareUrlAttribute()
{
return front_url('/form-templates/' . $this->slug);
}
public function setDescriptionAttribute($value)
{
// Strip out unwanted html
$this->attributes['description'] = Purify::clean($value);
}
public function scopePubliclyListed($query)
{
return $this->where('publicly_listed', true);
}
/**
* Config/options
*/
public function getSlugOptions(): SlugOptions
{
return SlugOptions::create()
->doNotGenerateSlugsOnUpdate()
->generateSlugsFrom('name')
->saveSlugsTo('slug');
}
public function getTypes(): Collection
{
return self::getAllTypes()->filter(function ($type) {
return in_array($type['slug'], $this->types);
});
}
public function getIndustries(): Collection
{
return self::getAllIndustries()->filter(function ($type) {
return in_array($type['slug'], $this->industries);
});
}
public static function getAllTypes(): Collection
{
return collect(
array_values(
json_decode(
file_get_contents(resource_path('data/forms/templates/types.json')),
true
)
)
)->values();
}
public static function getAllIndustries(): Collection
{
return collect(
array_values(
json_decode(
file_get_contents(resource_path('data/forms/templates/industries.json')),
true
)
)
)->values();
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace App\Models\Traits;
use Closure;
interface CachableAttributes
{
/**
* Get an item from the cache, or execute the given Closure and store the result.
*
*
* @return mixed
*/
public function remember(string $key, ?int $ttl, Closure $callback);
/**
* Get an item from the cache, or execute the given Closure and store the result forever.
*
*
* @return mixed
*/
public function rememberForever(string $key, Closure $callback);
/**
* Remove an item from the cache.
*/
public function forget(string $key): bool;
/**
* Remove all items from the cache.
*/
public function flush(): bool;
}

View File

@@ -0,0 +1,100 @@
<?php
namespace App\Models\Traits;
use Closure;
use Illuminate\Contracts\Cache\Factory as CacheFactoryContract;
use Illuminate\Contracts\Cache\Repository as CacheRepository;
use Illuminate\Database\Eloquent\Model;
use InvalidArgumentException;
/**
* @property string|null $attributeCachePrefix
* @property string|null $attributeCacheStore
* @property string[]|null $cachableAttributes
*
* @mixin Model
*/
trait CachesAttributes
{
/** @var array<string, mixed> */
protected $attributeCache = [];
public static function bootCachesAttributes(): void
{
static::deleting(function (Model $model): void {
/** @var Model|CachableAttributes $model */
$model->flush();
});
}
public function remember(string $attribute, ?int $ttl, Closure $callback)
{
if ($ttl === 0 || ! $this->exists) {
if (! isset($this->attributeCache[$attribute])) {
$this->attributeCache[$attribute] = value($callback);
}
return $this->attributeCache[$attribute];
}
if ($ttl === null) {
return $this->getCacheRepository()->rememberForever($this->getCacheKey($attribute), $callback);
}
if ($ttl < 0) {
throw new InvalidArgumentException("The TTL has to be null, 0 or any positive number - you provided `{$ttl}`.");
}
return $this->getCacheRepository()->remember($this->getCacheKey($attribute), $ttl, $callback);
}
public function rememberForever(string $attribute, Closure $callback)
{
return $this->remember($attribute, null, $callback);
}
public function forget(string $attribute): bool
{
unset($this->attributeCache[$attribute]);
if (! $this->exists) {
return true;
}
return $this->getCacheRepository()->forget($this->getCacheKey($attribute));
}
public function flush(): bool
{
$result = true;
foreach ($this->cachableAttributes ?? [] as $attribute) {
$result = $this->forget($attribute) ? $result : false;
}
return $result;
}
protected function getCacheKey(string $attribute): string
{
return implode('.', [
$this->attributeCachePrefix ?? 'model_attribute_cache',
$this->getConnectionName() ?? 'connection',
$this->getTable(),
$this->getKey(),
$attribute,
$this->updated_at?->timestamp ?? '0',
]);
}
protected function getCacheRepository(): CacheRepository
{
return $this->getCacheFactory()->store($this->attributeCacheStore);
}
protected function getCacheFactory(): CacheFactoryContract
{
return app('cache');
}
}

259
api/app/Models/User.php Normal file
View File

@@ -0,0 +1,259 @@
<?php
namespace App\Models;
use App\Models\Forms\Form;
use App\Notifications\ResetPassword;
use App\Notifications\VerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Cashier\Billable;
use Laravel\Sanctum\HasApiTokens;
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Authenticatable implements JWTSubject
{
use Billable;
use HasFactory;
use Notifiable;
use HasApiTokens;
public const ROLE_ADMIN = 'admin';
public const ROLE_USER = 'user';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
'hear_about_us',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
'hear_about_us',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected function casts()
{
return [
'email_verified_at' => 'datetime',
];
}
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = [
'photo_url',
];
public function ownsForm(Form $form)
{
return $this->workspaces()->where('workspaces.id', $form->workspace_id)->exists();
}
public function ownsWorkspace(Workspace $workspace)
{
return $this->workspaces()->where('workspaces.id', $workspace->id)->exists();
}
/**
* Get the profile photo URL attribute.
*
* @return string
*/
public function getPhotoUrlAttribute()
{
return vsprintf('https://www.gravatar.com/avatar/%s.jpg?s=200&d=%s', [
md5(strtolower($this->email)),
$this->name ? urlencode("https://ui-avatars.com/api/$this->name.jpg") : 'mp',
]);
}
public function getHasFormsAttribute()
{
return $this->workspaces()->whereHas('forms')->exists();
}
public function getIsSubscribedAttribute()
{
return $this->subscribed()
|| in_array($this->email, config('opnform.extra_pro_users_emails'))
|| !is_null($this->activeLicense());
}
public function getHasCustomerIdAttribute()
{
return !is_null($this->stripe_id);
}
public function getAdminAttribute()
{
return in_array($this->email, config('opnform.admin_emails'));
}
public function getModeratorAttribute()
{
return in_array($this->email, config('opnform.moderator_emails')) || $this->admin;
}
public function getTemplateEditorAttribute()
{
return $this->admin || in_array($this->email, config('opnform.template_editor_emails'));
}
/**
* =================================
* Helper Related
* =================================
*/
/**
* Send the password reset notification.
*
* @param string $token
* @return void
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPassword($token));
}
/**
* Send the email verification notification.
*
* @return void
*/
public function sendEmailVerificationNotification()
{
$this->notify(new VerifyEmail());
}
/**
* =================================
* Relationship
* =================================
*/
public function workspaces()
{
return $this->belongsToMany(Workspace::class);
}
public function forms()
{
return $this->hasMany(Form::class, 'creator_id');
}
public function formTemplates()
{
return $this->hasMany(Template::class, 'creator_id');
}
public function licenses()
{
return $this->hasMany(License::class);
}
public function activeLicense(): ?License
{
return $this->licenses()->active()->first();
}
/**
* =================================
* Oauth Related
* =================================
*/
/**
* Get the oauth providers.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function oauthProviders()
{
return $this->hasMany(OAuthProvider::class);
}
/**
* @return int
*/
public function getJWTIdentifier()
{
return $this->getKey();
}
/**
* @return array
*/
public function getJWTCustomClaims()
{
return [
'ip' => \Hash::make(request()->ip()),
'ua' => \Hash::make(request()->userAgent()),
];
}
public function getIsRiskyAttribute()
{
return $this->created_at->isAfter(now()->subDays(3)) || // created in last 3 days
$this->subscriptions()->where(function ($q) {
$q->where('stripe_status', 'trialing')
->orWhere('stripe_status', 'active');
})->first()?->onTrial();
}
public function flushCache()
{
$this->workspaces()->with('forms')->get()->each(function (Workspace $workspace) {
$workspace->flush();
$workspace->forms->each(function (Form $form) {
$form->flush();
});
});
}
public static function boot()
{
parent::boot();
static::deleting(function (User $user) {
// Remove user's workspace if he's the only one with this workspace
foreach ($user->workspaces as $workspace) {
if ($workspace->users()->count() == 1) {
$workspace->delete();
} else {
$workspace->users()->detach($user->id);
}
}
});
}
public function scopeWithActiveSubscription($query)
{
return $query->whereHas('subscriptions', function ($query) {
$query->where(function ($q) {
$q->where('stripe_status', 'trialing')
->orWhere('stripe_status', 'active');
});
});
}
}

View File

@@ -0,0 +1,87 @@
<?php
namespace App\Models;
use App\Jobs\Billing\WorkspaceUsersUpdated;
use App\Mail\UserInvitationEmail;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
class UserInvite extends Model
{
use HasFactory;
public const PENDING_STATUS = 'pending';
public const ACCEPTED_STATUS = 'accepted';
protected $fillable = [
'email',
'role',
'workspace_id',
'valid_until',
'status',
'token',
];
public static function inviteUser(
string $email,
string $role,
Workspace $workspace,
Carbon $validUntil = null
): self {
// Generate a token
do {
$token = Str::random(100);
} while (UserInvite::where('token', $token)->exists());
$invite = self::create([
'email' => $email,
'role' => $role,
'workspace_id' => $workspace->id,
'valid_until' => $validUntil ?? now()->addDays(7),
'token' => $token,
]);
$invite->sendEmail();
return $invite;
}
public function getLink()
{
return front_url('/register?email=' . urlencode($this->email) . '&invite_token=' . urlencode($this->token));
}
public function hasExpired()
{
return Carbon::parse($this->valid_until)->isPast();
}
public function workspace()
{
return $this->belongsTo(Workspace::class);
}
public function markAsAccepted()
{
$this->update(['status' => self::ACCEPTED_STATUS]);
WorkspaceUsersUpdated::dispatch($this->workspace);
return $this;
}
public function sendEmail()
{
Mail::to($this->email)->send(new UserInvitationEmail($this));
}
public function scopeNotExpired($query)
{
return $query->where('valid_until', '>', now());
}
public function scopePending($query)
{
return $query->where('status', self::PENDING_STATUS);
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class UserWorkspace extends Model
{
use HasFactory;
protected $table = 'user_workspace';
protected $fillable = [
'user_id',
'workspace_id',
'role',
];
public function user()
{
return $this->belongsTo(User::class);
}
public function workspace()
{
return $this->belongsTo(Workspace::class);
}
}

View File

@@ -0,0 +1,205 @@
<?php
namespace App\Models;
use App\Models\Forms\Form;
use App\Models\Traits\CachableAttributes;
use App\Models\Traits\CachesAttributes;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
class Workspace extends Model implements CachableAttributes
{
use CachesAttributes;
use HasFactory;
public const MAX_FILE_SIZE_FREE = 5000000; // 5 MB
public const MAX_FILE_SIZE_PRO = 50000000; // 50 MB
public const MAX_DOMAIN_PRO = 1;
protected $fillable = [
'name',
'icon',
'user_id',
'custom_domain',
];
protected $appends = [
'is_pro',
'is_trialing',
'is_enterprise',
];
protected function casts()
{
return [
'custom_domains' => 'array',
];
}
protected $cachableAttributes = [
'is_pro',
'is_enterprise',
'is_risky',
'submissions_count',
'max_file_size',
'custom_domain_count',
];
public function getMaxFileSizeAttribute()
{
if (!pricing_enabled()) {
return self::MAX_FILE_SIZE_PRO;
}
return $this->remember('max_file_size', 15 * 60, function (): int {
// Return max file size depending on subscription
foreach ($this->owners as $owner) {
if ($owner->is_subscribed) {
if ($license = $owner->activeLicense()) {
// In case of special License
return $license->max_file_size;
}
}
return self::MAX_FILE_SIZE_PRO;
}
return self::MAX_FILE_SIZE_FREE;
});
}
public function getCustomDomainCountLimitAttribute()
{
if (!pricing_enabled()) {
return null;
}
return $this->remember('custom_domain_count', 15 * 60, function (): ?int {
foreach ($this->owners as $owner) {
if ($owner->is_subscribed) {
if ($license = $owner->activeLicense()) {
// In case of special License
return $license->custom_domain_limit_count;
}
return self::MAX_DOMAIN_PRO;
}
}
return 0;
});
}
public function getIsProAttribute()
{
if (!pricing_enabled()) {
return true; // If no paid plan so TRUE for ALL
}
return $this->remember('is_pro', 15 * 60, function (): bool {
// Make sure at least one owner is pro
foreach ($this->owners as $owner) {
if ($owner->is_subscribed) {
return true;
}
}
return false;
});
}
public function getIsTrialingAttribute()
{
if (!pricing_enabled()) {
return false; // If no paid plan so FALSE for ALL
}
return $this->remember('is_trialing', 15 * 60, function (): bool {
// Make sure at least one owner is pro
foreach ($this->owners as $owner) {
if ($owner->onTrial()) {
return true;
}
}
return false;
});
}
public function getIsEnterpriseAttribute()
{
if (!pricing_enabled()) {
return true; // If no paid plan so TRUE for ALL
}
return $this->remember('is_enterprise', 15 * 60, function (): bool {
// Make sure at least one owner is pro
foreach ($this->owners as $owner) {
if ($owner->has_enterprise_subscription) {
return true;
}
}
return false;
});
}
public function getIsRiskyAttribute()
{
return $this->remember('is_risky', 15 * 60, function (): bool {
// A workspace is risky if all of his users are risky
foreach ($this->owners as $owner) {
if (!$owner->is_risky) {
return false;
}
}
return true;
});
}
public function getSubmissionsCountAttribute()
{
return $this->remember('submissions_count', 15 * 60, function (): int {
$total = 0;
foreach ($this->forms as $form) {
$total += $form->submissions_count;
}
return $total;
});
}
/**
* Relationships
*/
public function users()
{
return $this->belongsToMany(User::class);
}
public function invites()
{
return $this->hasMany(UserInvite::class);
}
public function owners()
{
return $this->users()->wherePivot('role', 'admin');
}
public function billingOwners(): Collection
{
return $this->owners->filter(fn ($owner) => $owner->is_subscribed);
}
public function forms()
{
return $this->hasMany(Form::class);
}
}