Skip to content
This repository has been archived by the owner on Feb 8, 2025. It is now read-only.

feat: automatic slug when creating or renaming a company #1239

Merged
merged 1 commit into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/Http/ViewHelpers/Adminland/AdminGeneralViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public static function information($company, Employee $loggedEmployee): ?array
return [
'id' => $company->id,
'name' => $name,
'slug' => $company->slug,
'administrators' => $administratorsCollection,
'creation_date' => $creationDate,
'currency' => $company->currency,
Expand Down
1 change: 1 addition & 0 deletions app/Models/Company/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Company extends Model
protected $fillable = [
'name',
'currency',
'slug',
'location',
'has_dummy_data',
'logo_file_id',
Expand Down
8 changes: 8 additions & 0 deletions app/Services/Company/Adminland/Company/CreateCompany.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function execute(array $data): Company
$this->createUniqueInvitationCodeForCompany();
$this->addFirstEmployee();
$this->provisionDefaultAccountData();
$this->generateSlug();
$this->logAccountAudit();

return $this->company;
Expand Down Expand Up @@ -104,6 +105,13 @@ private function provisionDefaultAccountData(): void
ProvisionDefaultAccountData::dispatch($this->employee);
}

private function generateSlug(): void
{
(new UpdateCompanySlug)->execute([
'company_id' => $this->company->id,
]);
}

private function logAccountAudit(): void
{
LogAccountAudit::dispatch([
Expand Down
12 changes: 9 additions & 3 deletions app/Services/Company/Adminland/Company/RenameCompany.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,27 @@ public function execute(array $data): Company

$this->rename();

$this->generateSlug();

$this->log($oldName);

return $this->company;
}

/**
* Rename the company.
*/
private function rename(): void
{
Company::where('id', $this->company->id)->update([
'name' => $this->data['name'],
]);
}

private function generateSlug(): void
{
(new UpdateCompanySlug)->execute([
'company_id' => $this->company->id,
]);
}

/**
* Add an audit log entry for this action.
*
Expand Down
58 changes: 58 additions & 0 deletions app/Services/Company/Adminland/Company/UpdateCompanySlug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace App\Services\Company\Adminland\Company;

use Illuminate\Support\Str;
use App\Services\BaseService;
use App\Models\Company\Company;

class UpdateCompanySlug extends BaseService
{
private Company $company;

/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules(): array
{
return [
'company_id' => 'required|integer|exists:companies,id',
];
}

/**
* Updates the company slug.
*
* @param array $data
*
* @return Company
*/
public function execute(array $data): Company
{
$this->company = Company::find($data['company_id']);

$slug = Str::slug($this->company->name, '-');

$isSlugUnique = false;
while ($isSlugUnique === false) {
$existingCompaniesWithThisSlug = Company::where('slug', '=', $slug)
->where('id', '!=', $this->company->id)
->count();

if ($existingCompaniesWithThisSlug > 0) {
$slug = $slug.'-'.rand(1, 999);
}

$isSlugUnique = Company::where('slug', $slug)
->where('id', '!=', $this->company->id)
->count() == 0;
}

$this->company->slug = $slug;
$this->company->save();

return $this->company->refresh();
}
}
1 change: 1 addition & 0 deletions database/factories/Company/CompanyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function definition()
{
return [
'name' => $this->faker->name,
'slug' => 'company-name',
'currency' => 'USD',
'code_to_join_company' => 'USD',
];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Support\Str;
use App\Models\Company\Company;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddCompanySlugToCompanies extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
// necessary for SQLlite
Schema::enableForeignKeyConstraints();

Schema::table('companies', function (Blueprint $table) {
$table->string('slug')->after('name')->nullable();
});

Company::chunk(200, function ($companies) {
foreach ($companies as $company) {
$company->slug = Str::slug($company->name, '-');
$company->save();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public function it_creates_a_company(): void

$company = (new CreateCompany)->execute($request);

$company->refresh();

$michael = $dwight->getEmployeeObjectForCompany($company);

$this->assertInstanceOf(
Expand All @@ -45,6 +47,11 @@ public function it_creates_a_company(): void

$this->assertNotNull($company->code_to_join_company);

$this->assertEquals(
'dunder-mifflin',
$company->slug
);

Queue::assertPushed(LogAccountAudit::class, function ($job) use ($michael) {
return $job->auditLog['action'] === 'account_created' &&
$job->auditLog['author_id'] === $michael->id &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,32 @@ private function executeService(Employee $michael): void
$request = [
'company_id' => $michael->company_id,
'author_id' => $michael->id,
'name' => 'Pyramid',
'name' => 'Dunder Mifflin 2',
];

$company = (new RenameCompany)->execute($request);

$this->assertDatabaseHas('companies', [
'id' => $company->id,
'name' => 'Pyramid',
'name' => 'Dunder Mifflin 2',
]);

$this->assertInstanceOf(
Company::class,
$company
);

$this->assertEquals(
'dunder-mifflin-2',
$company->refresh()->slug
);

Queue::assertPushed(LogAccountAudit::class, function ($job) use ($michael, $oldName) {
return $job->auditLog['action'] === 'company_renamed' &&
$job->auditLog['author_id'] === $michael->id &&
$job->auditLog['objects'] === json_encode([
'old_name' => $oldName,
'new_name' => 'Pyramid',
'new_name' => 'Dunder Mifflin 2',
]);
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Tests\Unit\Services\Company\Adminland\Company;

use Tests\TestCase;
use App\Models\Company\Company;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Services\Company\Adminland\Company\UpdateCompanySlug;

class UpdateCompanySlugTest extends TestCase
{
use DatabaseTransactions;

/** @test */
public function it_updates_a_slug(): void
{
$company = Company::factory()->create([
'name' => 'Dunder Mifflin',
]);

$request = [
'company_id' => $company->id,
];

$company = (new UpdateCompanySlug)->execute($request);

$this->assertInstanceOf(
Company::class,
$company
);

$this->assertDatabaseHas('companies', [
'id' => $company->id,
'slug' => 'dunder-mifflin',
]);
}

/** @test */
public function it_updates_a_slug_that_is_unique(): void
{
$company = Company::factory()->create([
'name' => 'Dunder Mifflin',
]);
Company::factory()->create([
'slug' => 'dunder-mifflin',
]);

$request = [
'company_id' => $company->id,
];

$company = (new UpdateCompanySlug)->execute($request);

$this->assertInstanceOf(
Company::class,
$company
);

$this->assertEquals(
'dunder-mifflin-',
substr($company->slug, 0, 15)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public function it_gets_information_about_the_company(): void
$response['name']
);

$this->assertEquals(
$michael->company->slug,
$response['slug']
);

$this->assertEquals(
'Jan 01, 2018 12:00 AM',
$response['creation_date']
Expand Down