feat: add admin subscription granting feature
- Implemented the ability for admins to grant subscriptions to doctors and clinics without payment. - Added new API endpoint `/api/v1/admin/subscription/grant` for granting subscriptions. - Updated the subscription model to track the admin who granted the subscription. - Enhanced the subscription report to include details about granted subscriptions. - Introduced a new `is_granted` field to indicate if a subscription was granted by an admin. - Updated the database schema to support the new functionality with a migration. - Added tests to ensure the correct behavior of the subscription granting process.
This commit is contained in:
@@ -13,6 +13,7 @@ import AdminSubscriptionPage from './AdminSubscriptionPage';
|
||||
|
||||
const get = api.get as ReturnType<typeof vi.fn>;
|
||||
const patch = api.patch as ReturnType<typeof vi.fn>;
|
||||
const post = api.post as ReturnType<typeof vi.fn>;
|
||||
|
||||
const PLANS = [
|
||||
{
|
||||
@@ -25,6 +26,23 @@ const PLANS = [
|
||||
},
|
||||
];
|
||||
|
||||
/** همان پلنها، با دوره — تب «اعطای اشتراک» فهرست دورهها را از همینجا میسازد. */
|
||||
const PLANS_WITH_PERIODS = [
|
||||
{
|
||||
...PLANS[0],
|
||||
periods: [
|
||||
{ uuid: 'per-basic-1', label: 'یک ماهه', duration_months: 1, price_rials: 1000000, is_trial: false },
|
||||
{ uuid: 'per-basic-trial', label: 'آزمایشی', duration_months: 1, price_rials: 0, is_trial: true },
|
||||
],
|
||||
},
|
||||
{
|
||||
...PLANS[1],
|
||||
periods: [
|
||||
{ uuid: 'per-pro-1', label: 'یک ماهه', duration_months: 1, price_rials: 3000000, is_trial: false },
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
function mockApi() {
|
||||
get.mockImplementation((url: string) => {
|
||||
if (url.includes('/admin/subscription/plans')) return Promise.resolve({ success: true, data: PLANS });
|
||||
@@ -85,3 +103,142 @@ describe('AdminSubscriptionPage — سقف منابع پلن', () => {
|
||||
expect(await screen.findByLabelText('حداکثر منبع *')).toBeDisabled();
|
||||
});
|
||||
});
|
||||
|
||||
// ── اعطای اشتراک ──────────────────────────────────────────────────────────
|
||||
|
||||
const DOCTORS = [{ uuid: 'doc-1', name: 'دکتر رضایی', mobile: '09120000001' }];
|
||||
|
||||
/** پاسخ `admin/subscription/active` — `null` یعنی مقصد اشتراک فعالی ندارد. */
|
||||
function mockGrantApi(activeSubscription: unknown = null) {
|
||||
get.mockImplementation((url: string) => {
|
||||
if (url.includes('/admin/subscription/plans')) return Promise.resolve({ success: true, data: PLANS_WITH_PERIODS });
|
||||
if (url.includes('/admin/subscription/active')) return Promise.resolve({ success: true, data: { subscription: activeSubscription } });
|
||||
if (url.includes('/admin/doctors')) return Promise.resolve({ success: true, data: DOCTORS, meta: { totalRecords: 1 } });
|
||||
if (url.includes('/admin/clinics')) return Promise.resolve({ success: true, data: [], meta: { totalRecords: 0 } });
|
||||
return Promise.resolve({ success: true, data: [], meta: { totalRecords: 0 } });
|
||||
});
|
||||
post.mockResolvedValue({ success: true, data: {} });
|
||||
}
|
||||
|
||||
/** انتخاب گزینه از SearchableSelect با inputId */
|
||||
async function pickOption(inputId: string, option: string) {
|
||||
const input = document.getElementById(inputId) as HTMLInputElement;
|
||||
fireEvent.focus(input);
|
||||
fireEvent.keyDown(input, { key: 'ArrowDown' });
|
||||
fireEvent.click(await screen.findByText(option));
|
||||
}
|
||||
|
||||
async function openGrantTab() {
|
||||
renderWithProviders(<AdminSubscriptionPage />, { route: '/admin/admin-subscription' });
|
||||
// تبِ «اعطای اشتراک» و دکمهٔ ثبتِ فرم همناماند؛ تب همیشه اولی است.
|
||||
fireEvent.click((await screen.findAllByText('اعطای اشتراک'))[0]);
|
||||
}
|
||||
|
||||
/** دکمهٔ ثبتِ فرم اعطا — با نقش تنها قابل تفکیک نیست، چون تب همنام است. */
|
||||
function submitGrantForm() {
|
||||
fireEvent.click(document.querySelector('button[type="submit"]') as HTMLButtonElement);
|
||||
}
|
||||
|
||||
describe('AdminSubscriptionPage — اعطای اشتراک', () => {
|
||||
beforeEach(() => { get.mockReset(); post.mockReset(); });
|
||||
|
||||
it('برای مقصد بدون اشتراک، دوره را میفرستد و پرداختی در کار نیست', async () => {
|
||||
mockGrantApi(null);
|
||||
await openGrantTab();
|
||||
|
||||
await pickOption('grant-entity', 'دکتر رضایی — 09120000001');
|
||||
expect(await screen.findByText('این مقصد اشتراک فعالی ندارد.')).toBeInTheDocument();
|
||||
|
||||
await pickOption('grant-period', 'حرفهای — یک ماهه (۳۰۰٬۰۰۰ تومان)');
|
||||
submitGrantForm();
|
||||
|
||||
await waitFor(() => expect(post).toHaveBeenCalled());
|
||||
expect(post.mock.calls[0][0]).toBe('/api/v1/admin/subscription/grant');
|
||||
expect(post.mock.calls[0][1]).toEqual({ entity_type: 'doctor', entity_uuid: 'doc-1', period_uuid: 'per-pro-1' });
|
||||
});
|
||||
|
||||
/** دورهٔ تریال، تریالِ نگرفتهٔ کاربر را میسوزاند؛ نباید در فهرست باشد. */
|
||||
it('دورههای تریال در فهرست اعطا نمیآیند', async () => {
|
||||
mockGrantApi(null);
|
||||
await openGrantTab();
|
||||
|
||||
const input = document.getElementById('grant-period') as HTMLInputElement;
|
||||
fireEvent.focus(input);
|
||||
fireEvent.keyDown(input, { key: 'ArrowDown' });
|
||||
|
||||
expect(await screen.findByText('پایه — یک ماهه (۱۰۰٬۰۰۰ تومان)')).toBeInTheDocument();
|
||||
expect(screen.queryByText(/آزمایشی/)).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('اشتراک فعالِ مقصد را قبل از اعطا نشان میدهد', async () => {
|
||||
mockGrantApi({ plan: { name: 'professional', level: 2 }, expires_at: 1800000000, is_trial: false, is_granted: true });
|
||||
await openGrantTab();
|
||||
|
||||
await pickOption('grant-entity', 'دکتر رضایی — 09120000001');
|
||||
|
||||
expect(await screen.findByText('حرفهای')).toBeInTheDocument();
|
||||
expect(screen.getByText('اعطایی')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('پلن پایینتر از پلن فعال، اول تأیید میخواهد', async () => {
|
||||
mockGrantApi({ plan: { name: 'professional', level: 2 }, expires_at: 1800000000, is_trial: false, is_granted: false });
|
||||
await openGrantTab();
|
||||
|
||||
await pickOption('grant-entity', 'دکتر رضایی — 09120000001');
|
||||
await screen.findByText(/اشتراک فعلی/);
|
||||
await pickOption('grant-period', 'پایه — یک ماهه (۱۰۰٬۰۰۰ تومان)');
|
||||
submitGrantForm();
|
||||
|
||||
expect(await screen.findByText('کاهش سطح پلن')).toBeInTheDocument();
|
||||
expect(post).not.toHaveBeenCalled();
|
||||
|
||||
fireEvent.click(screen.getByText('اعطا کن'));
|
||||
|
||||
await waitFor(() => expect(post).toHaveBeenCalled());
|
||||
expect(post.mock.calls[0][1]).toMatchObject({ period_uuid: 'per-basic-1' });
|
||||
});
|
||||
|
||||
it('ارتقا به پلن بالاتر بدون تأیید اضافه ثبت میشود', async () => {
|
||||
mockGrantApi({ plan: { name: 'basic', level: 1 }, expires_at: 1800000000, is_trial: false, is_granted: false });
|
||||
await openGrantTab();
|
||||
|
||||
await pickOption('grant-entity', 'دکتر رضایی — 09120000001');
|
||||
await screen.findByText(/اشتراک فعلی/);
|
||||
await pickOption('grant-period', 'حرفهای — یک ماهه (۳۰۰٬۰۰۰ تومان)');
|
||||
submitGrantForm();
|
||||
|
||||
await waitFor(() => expect(post).toHaveBeenCalled());
|
||||
expect(screen.queryByText('کاهش سطح پلن')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe('AdminSubscriptionPage — گزارش', () => {
|
||||
beforeEach(() => { get.mockReset(); post.mockReset(); });
|
||||
|
||||
it('اشتراک اعطایی را «اعطایی» نشان میدهد، نه «پولی»', async () => {
|
||||
get.mockImplementation((url: string) => {
|
||||
if (url.includes('/admin/subscription/plans')) return Promise.resolve({ success: true, data: PLANS });
|
||||
if (url.includes('/admin/subscription/report')) {
|
||||
return Promise.resolve({
|
||||
success: true,
|
||||
meta: { totalRecords: 1 },
|
||||
data: [{
|
||||
uuid: 's-1', entityType: 'doctor', entityId: 4, entityName: 'دکتر رضایی',
|
||||
isTrial: false, isGranted: true, grantedBy: 'ادمین',
|
||||
startsAt: 1700000000, expiresAt: 1800000000, createdAt: 1700000000,
|
||||
plan_name: 'professional', plan_level: 2,
|
||||
}],
|
||||
});
|
||||
}
|
||||
return Promise.resolve({ success: true, data: [], meta: { totalRecords: 0 } });
|
||||
});
|
||||
|
||||
renderWithProviders(<AdminSubscriptionPage />, { route: '/admin/admin-subscription' });
|
||||
fireEvent.click(await screen.findByText('گزارش فروش'));
|
||||
|
||||
expect(await screen.findByText('اعطایی')).toBeInTheDocument();
|
||||
expect(screen.getByText('دکتر رضایی')).toBeInTheDocument();
|
||||
expect(screen.getByText('ادمین')).toBeInTheDocument();
|
||||
expect(screen.queryByText('پولی')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user