# All files / js/pages/licenses Show.vue

0% Statements 0/115
0% Branches 0/171
0% Functions 0/39
0% Lines 0/106

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490

     <script setup lang="ts">
    import AppLayout from '@/layouts/AppLayout.vue';
    import { Head, Link, router, useForm } from '@inertiajs/vue3';
    import { computed, ref } from 'vue';
    import { ArrowLeft, Edit, Trash2, Copy, Check, Key, Users, Calendar, Clock, Globe, Rocket, ArrowUpCircle, Tag, ShieldCheck, Lock } from 'lucide-vue-next';
    import { getSafeExternalUrl } from '@/composables/useSafeExternalUrl';
    import {
        Dialog,
        DialogContent,
        DialogDescription,
        DialogFooter,
        DialogHeader,
        DialogTitle,
    } from '@/components/ui/dialog';
    import licenses from '@/routes/licenses';

    interface Customer {
        id: number;
        name: string;
        email: string | null;
    }

    interface License {
        id: number;
        license_key: string;
        url: string | null;
        version: string | null;
        deployed_at: string | null;
        customer_id: number;
        limited_users: number;
        standard_users: number;
        status: 'active' | 'suspended' | 'expired' | 'revoked';
        notes: string | null;
        expires_at: string | null;
        created_at: string;
        updated_at: string;
        customer: Customer | null;
    }

    interface Props {
        license: License;
        canManageLicenseInfrastructure?: boolean;
        canViewFullLicenseKey?: boolean;
    }

    const props = withDefaults(defineProps<Props>(), {
        canManageLicenseInfrastructure: false,
        canViewFullLicenseKey: false,
    });

    const pageTitle = computed(() => {
        if (props.canViewFullLicenseKey) {
            return `License - ${props.license.license_key}`;
        }
        const suffix = props.license.customer?.name ? ` — ${props.license.customer.name}` : '';
        return `License #${props.license.id}${suffix}`;
    });

    const breadcrumbKeyLabel = computed(() => {
        if (props.canViewFullLicenseKey) {
            return props.license.license_key;
        }
        return props.license.customer?.name ?? `License #${props.license.id}`;
    });

    const copied = ref(false);
    const showDeployDialog = ref(false);
    const showUpgradeDialog = ref(false);
    const forcingSSL = ref(false);
    const safeLicenseUrl = computed(() => getSafeExternalUrl(props.license.url));

    const deployForm = useForm({
        zip_file: null as File | null,
    });

    const upgradeForm = useForm({
        zip_file: null as File | null,
    });

    function copyLicenseKey() {
        if (!props.canViewFullLicenseKey) return;
        navigator.clipboard.writeText(props.license.license_key).then(() => {
            copied.value = true;
            setTimeout(() => {
                copied.value = false;
            }, 2000);
        });
    }

    function deleteLicense() {
        if (confirm('Are you sure you want to delete this license? This action cannot be undone.')) {
            router.delete(licenses.destroy(props.license.id).url);
        }
    }

    function onDeployFileChange(event: Event) {
        const target = event.target as HTMLInputElement;
        if (target.files && target.files.length > 0) {
            deployForm.zip_file = target.files[0];
        }
    }

    function onUpgradeFileChange(event: Event) {
        const target = event.target as HTMLInputElement;
        if (target.files && target.files.length > 0) {
            upgradeForm.zip_file = target.files[0];
        }
    }

    function submitDeploy() {
        if (!deployForm.zip_file) return;

        deployForm.post(`/licenses/${props.license.id}/deploy`, {
            forceFormData: true,
            onSuccess: () => {
                showDeployDialog.value = false;
                deployForm.reset();
            },
        });
    }

    function submitUpgrade() {
        if (!upgradeForm.zip_file) return;

        upgradeForm.post(`/licenses/${props.license.id}/upgrade`, {
            forceFormData: true,
            onSuccess: () => {
                showUpgradeDialog.value = false;
                upgradeForm.reset();
            },
        });
    }

    function forceSSL() {
        if (!confirm('This will request an AutoSSL certificate and enable HTTPS redirect. Continue?')) return;

        forcingSSL.value = true;
        router.post(`/licenses/${props.license.id}/force-ssl`, {}, {
            onFinish: () => {
                forcingSSL.value = false;
            },
        });
    }

    function statusBadgeClass(status: string): string {
        switch (status) {
            case 'active': return 'bg-green-100 text-green-800';
            case 'suspended': return 'bg-yellow-100 text-yellow-800';
            case 'expired': return 'bg-gray-100 text-gray-800';
            case 'revoked': return 'bg-red-100 text-red-800';
            default: return 'bg-gray-100 text-gray-800';
        }
    }

    function formatDate(dateString: string | null): string {
        if (!dateString) return '—';
        return new Date(dateString).toLocaleDateString('en-ZA', {
            year: 'numeric',
            month: 'long',
            day: 'numeric',
        });
    }

    function formatDateTime(dateString: string | null): string {
        if (!dateString) return '—';
        return new Date(dateString).toLocaleString('en-ZA', {
            year: 'numeric',
            month: 'long',
            day: 'numeric',
            hour: '2-digit',
            minute: '2-digit',
        });
    }
    </script>

    <template>
        <Head :title="pageTitle" />
        <AppLayout :breadcrumbs="[
            { title: 'Licenses', href: licenses.index().url },
            { title: breadcrumbKeyLabel, href: '#' }
        ]">
            <div class="p-6">
                <div class="mb-6">
                    <Link
                        :href="licenses.index().url"
                        class="mb-4 inline-flex items-center gap-2 text-sm text-gray-600 hover:text-gray-900"
                    >
                        <ArrowLeft class="h-4 w-4" />
                        Back to Licenses
                    </Link>
                    <div class="flex items-start justify-between">
                        <div>
                            <h1 class="text-2xl font-bold text-gray-900">License Details</h1>
                            <div class="mt-2 flex items-center gap-3">
                                <span
                                    :class="statusBadgeClass(props.license.status)"
                                    class="inline-flex rounded-full px-3 py-1 text-sm font-semibold capitalize"
                                >
                                    {{ props.license.status }}
                                </span>
                                <span
                                    v-if="props.license.deployed_at"
                                    class="inline-flex items-center gap-1 rounded-full bg-emerald-100 px-3 py-1 text-sm font-semibold text-emerald-800"
                                >
                                    <ShieldCheck class="h-3.5 w-3.5" />
                                    Deployed
                                </span>
                                <span
                                    v-else
                                    class="inline-flex rounded-full bg-gray-100 px-3 py-1 text-sm font-semibold text-gray-500"
                                >
                                    Not Deployed
                                </span>
                            </div>
                        </div>
                        <div class="flex items-center gap-2">
                            <button
                                v-if="canManageLicenseInfrastructure && !props.license.deployed_at"
                                @click="showDeployDialog = true"
                                class="flex items-center gap-2 rounded-md bg-green-600 px-4 py-2 text-sm font-medium text-white hover:bg-green-700"
                            >
                                <Rocket class="h-4 w-4" />
                                Deploy
                            </button>
                            <button
                                v-if="canManageLicenseInfrastructure && props.license.deployed_at"
                                @click="showUpgradeDialog = true"
                                class="flex items-center gap-2 rounded-md bg-amber-600 px-4 py-2 text-sm font-medium text-white hover:bg-amber-700"
                            >
                                <ArrowUpCircle class="h-4 w-4" />
                                Upgrade
                            </button>
                            <button
                                v-if="canManageLicenseInfrastructure && props.license.deployed_at && props.license.url"
                                @click="forceSSL"
                                :disabled="forcingSSL"
                                class="flex items-center gap-2 rounded-md bg-cyan-600 px-4 py-2 text-sm font-medium text-white hover:bg-cyan-700 disabled:opacity-50 disabled:cursor-not-allowed"
                            >
                                <Lock class="h-4 w-4" />
                                <span v-if="forcingSSL">Forcing SSL...</span>
                                <span v-else>Force SSL</span>
                            </button>
                            <Link
                                :href="licenses.edit(props.license.id).url"
                                class="flex items-center gap-2 rounded-md bg-blue-600 px-4 py-2 text-sm font-medium text-white hover:bg-blue-700"
                            >
                                <Edit class="h-4 w-4" />
                                Edit
                            </Link>
                            <button
                                @click="deleteLicense"
                                class="flex items-center gap-2 rounded-md border border-red-300 bg-white px-4 py-2 text-sm font-medium text-red-600 hover:bg-red-50"
                            >
                                <Trash2 class="h-4 w-4" />
                                Delete
                            </button>
                        </div>
                    </div>
                </div>

                <div class="grid gap-6 lg:grid-cols-2">
                    <!-- License Key Card -->
                    <div class="rounded-lg border border-gray-200 bg-white p-6">
                        <div class="flex items-center gap-2 mb-4">
                            <Key class="h-5 w-5 text-gray-500" />
                            <h2 class="text-lg font-semibold text-gray-900">License Key</h2>
                        </div>
                        <div class="flex items-center gap-3 rounded-lg bg-gray-50 p-4">
                            <code class="flex-1 text-lg font-mono font-semibold text-gray-900 break-all">{{ props.license.license_key }}</code>
                            <button
                                v-if="canViewFullLicenseKey"
                                type="button"
                                @click="copyLicenseKey"
                                class="flex-shrink-0 rounded-md border border-gray-300 bg-white px-3 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50"
                            >
                                <Check v-if="copied" class="h-4 w-4 text-green-500" />
                                <Copy v-else class="h-4 w-4" />
                            </button>
                        </div>
                        <p v-if="!canViewFullLicenseKey" class="mt-3 text-sm text-gray-500">
                            Full license keys are visible only to administrators.
                        </p>
                    </div>

                    <!-- Customer Card -->
                    <div class="rounded-lg border border-gray-200 bg-white p-6">
                        <div class="flex items-center gap-2 mb-4">
                            <Users class="h-5 w-5 text-gray-500" />
                            <h2 class="text-lg font-semibold text-gray-900">Customer</h2>
                        </div>
                        <div v-if="props.license.customer" class="space-y-2">
                            <p class="text-lg font-medium text-gray-900">{{ props.license.customer.name }}</p>
                            <p v-if="props.license.customer.email" class="text-sm text-gray-500">{{ props.license.customer.email }}</p>
                        </div>
                        <p v-else class="text-gray-400">No customer assigned</p>
                    </div>

                    <!-- Authorized URL Card -->
                    <div class="rounded-lg border border-gray-200 bg-white p-6">
                        <div class="flex items-center gap-2 mb-4">
                            <Globe class="h-5 w-5 text-gray-500" />
                            <h2 class="text-lg font-semibold text-gray-900">Authorized URL</h2>
                        </div>
                        <div v-if="safeLicenseUrl" class="rounded-lg bg-gray-50 p-4">
                            <a :href="safeLicenseUrl" target="_blank" rel="noopener noreferrer" class="text-blue-600 hover:text-blue-800 break-all">
                                {{ safeLicenseUrl }}
                            </a>
                            <p class="mt-2 text-xs text-gray-500">Only validation requests from this URL will be accepted.</p>
                        </div>
                        <p v-else class="text-gray-400">No URL restriction — any instance can validate with this key.</p>
                    </div>

                    <!-- Version Card -->
                    <div class="rounded-lg border border-gray-200 bg-white p-6">
                        <div class="flex items-center gap-2 mb-4">
                            <Tag class="h-5 w-5 text-gray-500" />
                            <h2 class="text-lg font-semibold text-gray-900">Version</h2>
                        </div>
                        <div v-if="props.license.version" class="rounded-lg bg-gray-50 p-4">
                            <p class="text-lg font-mono font-semibold text-gray-900">{{ props.license.version }}</p>
                        </div>
                        <p v-else class="text-gray-400">No version information available</p>
                    </div>

                    <!-- User Limits Card -->
                    <div class="rounded-lg border border-gray-200 bg-white p-6">
                        <div class="flex items-center gap-2 mb-4">
                            <Users class="h-5 w-5 text-gray-500" />
                            <h2 class="text-lg font-semibold text-gray-900">User Limits</h2>
                        </div>
                        <div class="grid grid-cols-2 gap-4">
                            <div class="rounded-lg bg-blue-50 p-4 text-center">
                                <p class="text-3xl font-bold text-blue-600">{{ props.license.limited_users }}</p>
                                <p class="mt-1 text-sm text-blue-800">Limited Users</p>
                            </div>
                            <div class="rounded-lg bg-indigo-50 p-4 text-center">
                                <p class="text-3xl font-bold text-indigo-600">{{ props.license.standard_users }}</p>
                                <p class="mt-1 text-sm text-indigo-800">Standard Users</p>
                            </div>
                        </div>
                    </div>

                    <!-- Dates Card -->
                    <div class="rounded-lg border border-gray-200 bg-white p-6">
                        <div class="flex items-center gap-2 mb-4">
                            <Calendar class="h-5 w-5 text-gray-500" />
                            <h2 class="text-lg font-semibold text-gray-900">Dates</h2>
                        </div>
                        <div class="space-y-3">
                            <div class="flex justify-between">
                                <span class="text-sm text-gray-500">Created</span>
                                <span class="text-sm font-medium text-gray-900">{{ formatDateTime(props.license.created_at) }}</span>
                            </div>
                            <div class="flex justify-between">
                                <span class="text-sm text-gray-500">Last Updated</span>
                                <span class="text-sm font-medium text-gray-900">{{ formatDateTime(props.license.updated_at) }}</span>
                            </div>
                            <div class="flex justify-between">
                                <span class="text-sm text-gray-500">Deployed</span>
                                <span class="text-sm font-medium" :class="props.license.deployed_at ? 'text-emerald-700' : 'text-gray-400'">
                                    {{ props.license.deployed_at ? formatDateTime(props.license.deployed_at) : 'Not deployed' }}
                                </span>
                            </div>
                            <div class="flex justify-between">
                                <span class="text-sm text-gray-500">Expires</span>
                                <span class="text-sm font-medium" :class="props.license.expires_at ? 'text-gray-900' : 'text-gray-400'">
                                    {{ props.license.expires_at ? formatDate(props.license.expires_at) : 'Never' }}
                                </span>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Notes -->
                <div v-if="props.license.notes" class="mt-6 rounded-lg border border-gray-200 bg-white p-6">
                    <h2 class="mb-3 text-lg font-semibold text-gray-900">Notes</h2>
                    <p class="whitespace-pre-wrap text-sm text-gray-700">{{ props.license.notes }}</p>
                </div>
            </div>

            <!-- Deploy Dialog -->
            <Dialog v-model:open="showDeployDialog">
                <DialogContent class="sm:max-w-md">
                    <DialogHeader>
                        <DialogTitle>Deploy Instance</DialogTitle>
                        <DialogDescription>
                            Upload a zip file to deploy a new instance to <strong>{{ props.license.url }}</strong>.
                            This will create the subdomain, database, and configure the application.
                        </DialogDescription>
                    </DialogHeader>

                    <form @submit.prevent="submitDeploy" class="space-y-4">
                        <div>
                            <label class="mb-1 block text-sm font-medium text-gray-700">
                                Zip File <span class="text-red-500">*</span>
                            </label>
                            <input
                                type="file"
                                accept=".zip"
                                @change="onDeployFileChange"
                                class="w-full rounded-md border border-gray-300 px-3 py-2 text-sm file:mr-4 file:rounded-md file:border-0 file:bg-blue-50 file:px-4 file:py-2 file:text-sm file:font-semibold file:text-blue-700 hover:file:bg-blue-100"
                            />
                            <p v-if="deployForm.errors.zip_file" class="mt-1 text-sm text-red-600">{{ deployForm.errors.zip_file }}</p>
                            <p class="mt-1 text-xs text-gray-500">Upload the application zip file (max 500MB)</p>
                        </div>

                        <div v-if="!props.license.url" class="rounded-md bg-yellow-50 p-3">
                            <p class="text-sm text-yellow-800">
                                Please set a URL on this license before deploying.
                            </p>
                        </div>

                        <DialogFooter>
                            <button
                                type="button"
                                @click="showDeployDialog = false"
                                class="rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50"
                            >
                                Cancel
                            </button>
                            <button
                                type="submit"
                                :disabled="!deployForm.zip_file || deployForm.processing || !props.license.url"
                                class="flex items-center gap-2 rounded-md bg-green-600 px-4 py-2 text-sm font-medium text-white hover:bg-green-700 disabled:opacity-50 disabled:cursor-not-allowed"
                            >
                                <Rocket class="h-4 w-4" />
                                <span v-if="deployForm.processing">Deploying...</span>
                                <span v-else>Deploy</span>
                            </button>
                        </DialogFooter>
                    </form>
                </DialogContent>
            </Dialog>

            <!-- Upgrade Dialog -->
            <Dialog v-model:open="showUpgradeDialog">
                <DialogContent class="sm:max-w-md">
                    <DialogHeader>
                        <DialogTitle>Upgrade Instance</DialogTitle>
                        <DialogDescription>
                            Upload a zip file to upgrade the instance at <strong>{{ props.license.url }}</strong>.
                            This will upload new files and run migrations.
                        </DialogDescription>
                    </DialogHeader>

                    <form @submit.prevent="submitUpgrade" class="space-y-4">
                        <div>
                            <label class="mb-1 block text-sm font-medium text-gray-700">
                                Zip File <span class="text-red-500">*</span>
                            </label>
                            <input
                                type="file"
                                accept=".zip"
                                @change="onUpgradeFileChange"
                                class="w-full rounded-md border border-gray-300 px-3 py-2 text-sm file:mr-4 file:rounded-md file:border-0 file:bg-amber-50 file:px-4 file:py-2 file:text-sm file:font-semibold file:text-amber-700 hover:file:bg-amber-100"
                            />
                            <p v-if="upgradeForm.errors.zip_file" class="mt-1 text-sm text-red-600">{{ upgradeForm.errors.zip_file }}</p>
                            <p class="mt-1 text-xs text-gray-500">Upload the updated application zip file (max 500MB)</p>
                        </div>

                        <div v-if="!props.license.url" class="rounded-md bg-yellow-50 p-3">
                            <p class="text-sm text-yellow-800">
                                Please set a URL on this license before upgrading.
                            </p>
                        </div>

                        <DialogFooter>
                            <button
                                type="button"
                                @click="showUpgradeDialog = false"
                                class="rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50"
                            >
                                Cancel
                            </button>
                            <button
                                type="submit"
                                :disabled="!upgradeForm.zip_file || upgradeForm.processing || !props.license.url"
                                class="flex items-center gap-2 rounded-md bg-amber-600 px-4 py-2 text-sm font-medium text-white hover:bg-amber-700 disabled:opacity-50 disabled:cursor-not-allowed"
                            >
                                <ArrowUpCircle class="h-4 w-4" />
                                <span v-if="upgradeForm.processing">Upgrading...</span>
                                <span v-else>Upgrade</span>
                            </button>
                        </DialogFooter>
                    </form>
                </DialogContent>
            </Dialog>
        </AppLayout>
    </template>