File size: 2,679 Bytes
b2dcf0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6773345
 
 
 
 
 
 
b2dcf0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php

namespace App\Domain\Config;

use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use stdClass;

class ConfigRepository
{
    public function listNamespace(string $namespace): array
    {
        return DB::table('configuration_settings as current_settings')
            ->where('current_settings.namespace', $namespace)
            ->whereNull('current_settings.deleted_at')
            ->whereRaw('current_settings.version_number = (
                SELECT MAX(version_number)
                FROM configuration_settings
                WHERE namespace = current_settings.namespace
                  AND setting_key = current_settings.setting_key
                  AND deleted_at IS NULL
            )')
            ->orderBy('current_settings.setting_key')
            ->get()
            ->map(fn ($row) => (array) $row)
            ->all();
    }

    public function latest(string $namespace, string $key): ?stdClass
    {
        return DB::table('configuration_settings')
            ->where('namespace', $namespace)
            ->where('setting_key', $key)
            ->whereNull('deleted_at')
            ->orderByDesc('version_number')
            ->first();
    }

    public function versions(string $namespace, string $key): array
    {
        return DB::table('configuration_settings')
            ->where('namespace', $namespace)
            ->where('setting_key', $key)
            ->whereNull('deleted_at')
            ->orderByDesc('version_number')
            ->get()
            ->map(fn ($row) => (array) $row)
            ->all();
    }

    public function createVersion(string $id, string $namespace, string $key, mixed $value, string $status = 'DRAFT'): string
    {
        $latest = $this->latest($namespace, $key);
        $now = Carbon::now();

        DB::table('configuration_settings')->insert([
            'id' => $id,
            'namespace' => $namespace,
            'setting_key' => $key,
            'setting_value' => json_encode($value, JSON_THROW_ON_ERROR),
            'status' => $status,
            'version_number' => $latest === null ? 1 : ((int) $latest->version_number) + 1,
            'created_at' => $now,
            'updated_at' => $now,
        ]);

        return $id;
    }

    public function publish(string $id, ?string $approvedByUserId): void
    {
        DB::table('configuration_settings')->where('id', $id)->whereNull('deleted_at')->update([
            'status' => 'PUBLISHED',
            'approved_by_user_id' => $approvedByUserId,
            'approved_at' => Carbon::now(),
            'published_at' => Carbon::now(),
            'updated_at' => Carbon::now(),
        ]);
    }
}