Files
CRM_Montzar/apps/web/src/views/adm/AdmBillingPanel.vue
T

331 lines
10 KiB
Vue
Raw Normal View History

2026-09-21 16:38:24 +03:30
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import axios from 'axios';
import type { BillingAccountDto, BillingPlanCode, OwnerSeatUsageDto, ProductPlanCode, StorageUsageDto } from '@mento/shared';
import {
BILLING_PLAN_CODES,
BILLING_PLAN_LABELS,
BILLING_USER_CAPS,
PRODUCT_PLAN_CODES,
PRODUCT_PLAN_LABELS,
isPermanentBillingPlan,
} from '@mento/shared';
2026-09-21 16:38:24 +03:30
import { toFaDigits } from '@/lib/jalali';
import { platformApi } from '@/lib/platform-api';
2026-09-21 16:38:24 +03:30
const props = defineProps<{
2026-09-21 16:38:24 +03:30
billing?: BillingAccountDto | null;
busyId: string;
userId: string;
}>();
const emit = defineEmits<{
apply: [planCode: BillingPlanCode];
product: [planCode: ProductPlanCode];
2026-09-21 16:38:24 +03:30
}>();
const usage = ref<StorageUsageDto | null>(null);
const seats = ref<OwnerSeatUsageDto | null>(null);
const customUsers = ref('');
const seatBusy = ref(false);
const seatError = ref('');
const quotaGb = ref('');
const storageBusy = ref(false);
const storageError = ref('');
function formatBytes(bytes: number): string {
const n = Math.max(0, bytes);
const units: Array<[number, string]> = [
[1024 ** 3, 'گیگابایت'],
[1024 ** 2, 'مگابایت'],
[1024, 'کیلوبایت'],
];
for (const [size, label] of units) {
if (n >= size) {
const value = n / size;
const text = value >= 10 ? value.toFixed(0) : value.toFixed(1);
return `${toFaDigits(text)} ${label}`;
}
}
return `${toFaDigits(Math.round(n))} بایت`;
}
function fillQuota(next: StorageUsageDto) {
usage.value = next;
quotaGb.value =
next.quotaBytes == null ? '' : String(Math.round((next.quotaBytes / 1024 ** 3) * 10) / 10);
}
function errorText(err: unknown, fallback: string) {
if (axios.isAxiosError(err)) {
const raw = (err.response?.data as { message?: string | string[] } | undefined)?.message;
if (Array.isArray(raw)) return raw.join(' ');
if (raw) return raw;
}
return fallback;
}
async function loadStorage(refresh = false) {
storageError.value = '';
try {
const { data } = await platformApi.get<StorageUsageDto>(
`/platform/users/${props.userId}/storage`,
{ params: refresh ? { refresh: '1' } : undefined },
);
fillQuota(data);
} catch (err) {
storageError.value = errorText(err, 'حجم فایل خوانده نشد');
}
}
async function saveQuota(quota: number | null) {
storageBusy.value = true;
storageError.value = '';
try {
const { data } = await platformApi.patch<StorageUsageDto>(
`/platform/users/${props.userId}/billing/storage`,
{ quotaGb: quota },
);
fillQuota(data);
} catch (err) {
storageError.value = errorText(err, 'سقف حجم ذخیره نشد');
} finally {
storageBusy.value = false;
}
}
function saveTypedQuota() {
const raw = quotaGb.value.trim().replace(/,/g, '');
if (!raw) {
storageError.value = 'سقف را به گیگابایت وارد کنید، یا «بدون سقف» را بزنید';
return;
}
const value = Number(raw);
if (!Number.isFinite(value) || value < 0) {
storageError.value = 'سقف حجم نامعتبر است';
return;
}
void saveQuota(value);
}
async function loadSeats() {
seatError.value = '';
try {
const { data } = await platformApi.get<OwnerSeatUsageDto>(`/platform/users/${props.userId}/seats`);
seats.value = data;
customUsers.value =
data.maxUsers != null && !(BILLING_USER_CAPS as readonly number[]).includes(data.maxUsers)
? String(data.maxUsers)
: '';
} catch (err) {
seatError.value = errorText(err, 'تعداد کاربر خوانده نشد');
}
}
async function saveUserCap(maxUsers: number) {
seatBusy.value = true;
seatError.value = '';
try {
const { data } = await platformApi.patch<OwnerSeatUsageDto>(
`/platform/users/${props.userId}/billing/users`,
{ maxUsers },
);
seats.value = data;
customUsers.value =
data.maxUsers != null && !(BILLING_USER_CAPS as readonly number[]).includes(data.maxUsers)
? String(data.maxUsers)
: '';
} catch (err) {
seatError.value = errorText(err, 'سقف کاربر ذخیره نشد');
} finally {
seatBusy.value = false;
}
}
function saveCustomUsers() {
const value = Number(customUsers.value.trim());
if (!Number.isInteger(value) || value < 21) {
seatError.value = 'برای بالای ۲۰ کاربر، عدد توافقی را از ۲۱ به بالا وارد کنید';
return;
}
void saveUserCap(value);
}
onMounted(() => {
if (props.billing) {
void loadStorage();
void loadSeats();
}
});
2026-09-21 16:38:24 +03:30
</script>
<template>
<div class="billing">
<strong>اشتراک مالک</strong>
<p v-if="billing" class="muted">
{{ BILLING_PLAN_LABELS[billing.planCode] }}
· {{ billing.status }}
· {{ isPermanentBillingPlan(billing.planCode) ? 'بدون انقضا' : `${toFaDigits(billing.daysRemaining)} روز` }}
2026-09-21 16:38:24 +03:30
· سقف محیط {{ toFaDigits(billing.maxOrganizations) }}
</p>
<p v-else class="muted">هنوز پلن ندارد — یکی از پلن‌ها را اعمال کنید.</p>
<div class="plan-row">
<button
v-for="code in BILLING_PLAN_CODES.filter((c) => c !== 'custom')"
:key="code"
class="btn btn-ghost"
type="button"
:disabled="busyId === `plan-${userId}-${code}`"
@click="emit('apply', code)"
>
{{ BILLING_PLAN_LABELS[code] }}
</button>
</div>
<strong>سطح امکانات</strong>
<p class="muted">
انتخاب سطح، دسترسی محیط‌های این مالک را طبق تعرفه تیک می‌زند. هسته و تیم و سازمان سقف کاربر و محیط را هم تنظیم می‌کنند. ثبت‌نام دمو از هسته شروع می‌شود.
</p>
<div class="plan-row">
<button
v-for="code in PRODUCT_PLAN_CODES"
:key="code"
class="btn btn-ghost"
type="button"
:disabled="busyId === `product-${userId}-${code}`"
@click="emit('product', code)"
>
{{ PRODUCT_PLAN_LABELS[code] }}
</button>
</div>
<div v-if="billing" class="storage">
<strong>تعداد کاربر</strong>
<p v-if="seats" class="muted">
فعال {{ toFaDigits(seats.usedUsers) }}
<template v-if="seats.maxUsers == null"> · سقف تعیین نشده</template>
<template v-else> از {{ toFaDigits(seats.maxUsers) }}</template>
</p>
<p v-else-if="!seatError" class="muted">در حال خواندن کاربران…</p>
<p v-if="seats?.maxUsers != null && seats.usedUsers > seats.maxUsers" class="error">
تعداد فعلی از سقف بیشتر است. تا کم شدن کاربران، افزودن کاربر جدید بسته است.
</p>
<div class="plan-row">
<button
v-for="cap in BILLING_USER_CAPS"
:key="cap"
class="btn"
:class="{ 'btn-ghost': seats?.maxUsers !== cap }"
type="button"
:disabled="seatBusy"
@click="saveUserCap(cap)"
>
تا {{ toFaDigits(cap) }} کاربر
</button>
</div>
<label class="quota">
بالای ۲۰، توافقی
<input v-model="customUsers" type="number" min="21" step="1" inputmode="numeric" />
</label>
<div class="plan-row">
<button class="btn" type="button" :disabled="seatBusy" @click="saveCustomUsers">
ذخیره سقف توافقی
</button>
</div>
<p v-if="seatError" class="error">{{ seatError }}</p>
</div>
<div v-if="billing" class="storage">
<strong>حجم فایل</strong>
<p v-if="usage" class="muted">
مصرف {{ formatBytes(usage.usedBytes) }}
<template v-if="usage.quotaBytes == null"> · بدون سقف</template>
<template v-else>
از {{ formatBytes(usage.quotaBytes) }}
· {{ toFaDigits(usage.percent ?? 0) }}٪
</template>
</p>
<p v-else-if="!storageError" class="muted">در حال خواندن مصرف…</p>
<ul v-if="usage?.byOrganization?.length" class="orgs">
<li v-for="org in usage.byOrganization" :key="org.id">
{{ org.name }} · {{ formatBytes(org.usedBytes) }}
</li>
</ul>
<p class="muted">
عکس و صوت فعالیت، پیوست یادداشت، لوگو، کاور فرم، تصویر پروفایل، پیوست مالی و فایل گفتگو حساب می‌شود.
متن مشتری، فعالیت و فاکتور حساب نمی‌شود. فایل‌های قدیمی گفتگو که قبلاً روی سرور خارجی رفته‌اند داخل این عدد نیستند.
</p>
<label class="quota">
سقف حجم (گیگابایت)
<input v-model="quotaGb" type="number" min="0" step="0.5" inputmode="decimal" />
</label>
<div class="plan-row">
<button class="btn" type="button" :disabled="storageBusy" @click="saveTypedQuota">
ذخیره سقف
</button>
<button class="btn btn-ghost" type="button" :disabled="storageBusy" @click="saveQuota(null)">
بدون سقف
</button>
<button class="btn btn-ghost" type="button" :disabled="storageBusy" @click="loadStorage(true)">
محاسبه دوباره
</button>
</div>
<p v-if="storageError" class="error">{{ storageError }}</p>
</div>
2026-09-21 16:38:24 +03:30
</div>
</template>
<style scoped>
.billing {
margin-bottom: 0.85rem;
padding: 0.75rem;
border-radius: 12px;
background: #f8fafc;
border: 1px solid var(--line);
display: grid;
gap: 0.45rem;
}
.muted {
margin: 0;
color: var(--muted);
font-size: 0.88rem;
}
.plan-row {
display: flex;
flex-wrap: wrap;
gap: 0.4rem;
}
.storage {
display: grid;
gap: 0.45rem;
padding-top: 0.35rem;
border-top: 1px solid var(--line);
}
.orgs {
margin: 0;
padding: 0 1.1rem 0 0;
color: var(--muted);
font-size: 0.84rem;
}
.quota {
display: grid;
gap: 0.3rem;
font-size: 0.84rem;
}
.quota input {
width: 8rem;
padding: 0.4rem 0.55rem;
border: 1px solid var(--line);
border-radius: 8px;
}
.error {
margin: 0;
color: #b91c1c;
font-size: 0.84rem;
}
2026-09-21 16:38:24 +03:30
</style>