Skip to content

Commit 1cab909

Browse files
done
1 parent 03f405b commit 1cab909

12 files changed

Lines changed: 53 additions & 33 deletions

app/Services/DomainService.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,27 @@ public function resolve ( array $data = [], bool $defaults = true, bool $filled
3333
}
3434
public function clean ( array $items = [] ) {
3535

36-
collect($items)->each(fn($item) => $this->domainRepository->query()->where('dest', $item['dest'])->get()->each(fn($d) =>
37-
$this->remove($d)
38-
));
36+
collect($items)->each(fn($item) =>
37+
$this->domainRepository->query()->where('dest', $item['dest'])->get()->each(fn($d) => $this->remove($d))
38+
);
3939

4040
}
41-
public function remove ( Domain $domain ) {
41+
public function flush () {
4242

43-
$this->vercelService->setProject($domain->dest)->deleteDomain($domain->name);
44-
$this->cloudflareService->setZone($domain->zone?->provider_id)->deleteSubDomain($domain->provider_id ?? $domain->name);
45-
$this->domainRepository->forceDelete($domain->id);
43+
$this->zoneService->flush();
44+
$this->domainRepository->query()->get()->each(fn($d) => $this->remove($d));
4645

4746
}
4847
public function sync ( Domain $domain ) {
4948

50-
$this->domainRepository->query()->where('id', '!=', $domain->id)->where('dest', $domain->dest)->get()->each(fn($d) =>
51-
$this->remove($d)
52-
);
53-
54-
$this->deleteCache();
49+
$this->domainRepository->query()->where('id', '!=', $domain->id)->where('dest', $domain->dest)->get()->each(fn($d) => $this->remove($d));
50+
51+
}
52+
public function remove ( Domain $domain ) {
53+
54+
$this->vercelService->setProject($domain->dest)->deleteDomain($domain->name);
55+
$this->cloudflareService->setZone($domain->zone?->provider_id)->deleteSubDomain($domain->provider_id ?? $domain->name);
56+
$this->delete($domain->id);
5557

5658
}
5759
public function validate ( Zone $zone, string $name ) {

app/Services/StoreService.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ public function subscribe ( Store $store, array $data = [] ) {
1919
if ( !$this->subscriptionService->subscribeOrRenew($store, $data) ) throwError('subscription', 'subscription failed');
2020
if ( !$this->withTenant($store->id, callback: fn() => $this->domainService->apply($data)) ) throwError('domains', 'invalid domains');
2121

22+
}
23+
public function flush ( Store $store ) {
24+
25+
$this->withTenant($store->id, callback: fn() => $this->domainService->flush());
26+
parent::delete($store->id);
27+
2228
}
2329
public function store ( array $data = [], array $scopes = [] ) {
2430

@@ -40,12 +46,18 @@ public function update ( int $id, array $data = [], array $scopes = [] ) {
4046

4147
return $this->storeRepository->dbTransaction(function () use ( $id, $data, $scopes ) {
4248

43-
$store = parent::find($id, $scopes);
49+
$store = $this->find($id, $scopes);
4450
$this->subscribe($store, $data);
4551
return parent::update($id, $data, $scopes);
4652

4753
});
4854

55+
}
56+
public function delete ( int $id, array $scopes = [] ) {
57+
58+
$this->runJob([static::class, 'flush'], [$this->find($id, $scopes)]);
59+
return success();
60+
4961
}
5062
public function renew ( int $id, array $scopes = [] ) {
5163

app/Services/ZoneService.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,28 @@ public function current () {
2727
$zone = store()?->zone;
2828
return !$zone ? $this->parent() : ($zone->isValid() ? $zone : null);
2929

30+
}
31+
public function flush () {
32+
33+
$this->zoneRepository->query()->get()->each(fn($z) => $this->remove($z));
34+
35+
}
36+
public function sync ( Zone $zone ) {
37+
38+
$this->zoneRepository->query()->where('id', '!=', $zone->id)->get()->each(fn($z) => $this->remove($z));
39+
3040
}
3141
public function remove ( Zone $zone ) {
3242

3343
$this->vercelService->deleteDomain($zone->name);
3444

3545
$zone->domains?->each(function($domain) {
3646
$this->vercelService->setProject($domain->dest)->deleteDomain($domain->name);
37-
$this->domainRepository->forceDelete($domain->id);
47+
$this->delete($domain->id);
3848
});
3949

4050
$this->cloudflareService->deleteDomain($zone->provider_id ?? $zone->name);
41-
$this->zoneRepository->forceDelete($zone->id);
42-
43-
}
44-
public function sync ( Zone $zone ) {
45-
46-
$this->zoneRepository->query()->where('id', '!=', $zone->id)->get()->each(fn($z) => $this->remove($z));
47-
$this->deleteCache();
51+
$this->delete($zone->id);
4852

4953
}
5054
public function validate ( string $name ) {

database/migrations/2024_05_14_234216_create_zones_table.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ public function up () {
1111
Schema::create('zones', function (Blueprint $table) {
1212
$table->id();
1313
$table->integer('store_id')->default(0);
14-
$table->string('name')->unique();
14+
$table->string('provider_id')->nullable();
15+
$table->string('name');
1516
$table->string('ns1')->nullable();
1617
$table->string('ns2')->nullable();
17-
$table->string('provider_id')->nullable();
1818
$table->enum('type', ['internal', 'external'])->default('internal');
1919
$table->enum('status', ['pending', 'verified', 'failed'])->default('verified');
2020
$table->boolean('active')->default(true);
2121
$table->timestamps();
2222
$table->softDeletes();
23+
24+
$table->unique(['name', 'deleted_at']);
2325
});
2426

2527
}

database/migrations/2024_05_14_234217_create_domains_table.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ public function up () {
1313
$table->integer('store_id')->default(0);
1414
$table->integer('zone_id')->default(0);
1515
$table->string('provider_id')->nullable();
16-
$table->string('name')->unique();
17-
$table->enum('dest', ['client', 'admin', 'vendor', 'blog', 'app', 'api', 'cdn'])->default('client');
16+
$table->string('name');
17+
$table->enum('dest', ['client', 'admin', 'vendor', 'delivery', 'affiliate', 'blog', 'app', 'api', 'cdn'])->default('client');
1818
$table->enum('status', ['pending', 'verified', 'failed'])->default('verified');
1919
$table->boolean('active')->default(true);
2020
$table->timestamps();
2121
$table->softDeletes();
2222

23-
$table->unique(['zone_id', 'name']);
23+
$table->unique(['zone_id', 'name', 'deleted_at']);
2424
});
2525

2626
}

database/migrations/2024_05_14_234220_create_users_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function up () {
4646
$table->timestamps();
4747
$table->softDeletes();
4848

49-
$table->unique(['store_id', 'email']);
49+
$table->unique(['store_id', 'email', 'deleted_at']);
5050
});
5151

5252
}

database/migrations/2024_05_14_234235_create_level_users_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function up () {
1717
$table->timestamps();
1818
$table->softDeletes();
1919

20-
$table->unique(['store_id', 'level_id', 'user_id']);
20+
$table->unique(['store_id', 'level_id', 'user_id', 'deleted_at']);
2121
});
2222

2323
}

database/migrations/2024_05_14_234235_create_levels_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function up () {
3434
$table->timestamps();
3535
$table->softDeletes();
3636

37-
$table->unique(['store_id', 'rank']);
37+
$table->unique(['store_id', 'rank', 'deleted_at']);
3838
});
3939

4040
}

database/migrations/2024_05_24_105723_create_coupons_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function up () {
4343
$table->timestamps();
4444
$table->softDeletes();
4545

46-
$table->unique(['store_id', 'code']);
46+
$table->unique(['store_id', 'code', 'deleted_at']);
4747
});
4848

4949
}

database/migrations/2024_05_24_105725_create_gift_codes_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function up () {
2525
$table->timestamps();
2626
$table->softDeletes();
2727

28-
$table->unique(['store_id', 'code']);
28+
$table->unique(['store_id', 'code', 'deleted_at']);
2929
});
3030

3131
}

0 commit comments

Comments
 (0)