feat(currency): update currency display to toman in admin panel; implement conversion functions for rial to toman and vice versa

This commit is contained in:
hamed
2026-07-03 10:31:17 +03:30
parent 34d521434f
commit 6dbbeb1c70
11 changed files with 200 additions and 58 deletions
+13 -2
View File
@@ -1,6 +1,8 @@
import { describe, it, expect } from 'vitest';
import {
formatRial,
rialToToman,
tomanToRial,
formatNumber,
toDate,
formatDate,
@@ -19,16 +21,25 @@ import {
const PERSIAN_DIGITS = /[۰-۹]/;
describe('formatRial', () => {
it('عدد را با جداکننده فارسی و پسوند تومان برمی‌گرداند', () => {
const out = formatRial(1000);
it('مقدار ریال را ÷۱۰ به تومان تبدیل و با پسوند تومان برمی‌گرداند', () => {
const out = formatRial(150000); // ۱۵۰٬۰۰۰ ریال = ۱۵٬۰۰۰ تومان
expect(out).toContain('تومان');
expect(out).toMatch(PERSIAN_DIGITS);
expect(out).toContain(new Intl.NumberFormat('fa-IR').format(15000));
});
it('صفر را هم فرمت می‌کند', () => {
expect(formatRial(0)).toContain('تومان');
});
});
describe('rialToToman / tomanToRial', () => {
it('ریال↔تومان با ضریب ۱۰', () => {
expect(rialToToman(150000)).toBe(15000);
expect(tomanToRial(15000)).toBe(150000);
expect(tomanToRial(rialToToman(500))).toBe(500);
});
});
describe('formatNumber', () => {
it('رقم فارسی برمی‌گرداند', () => {
expect(formatNumber(1234)).toMatch(PERSIAN_DIGITS);
+8 -2
View File
@@ -1,7 +1,13 @@
import { z } from 'zod';
export function formatRial(amount: number): string {
return new Intl.NumberFormat('fa-IR').format(amount) + ' تومان';
// واحد ذخیره/API ریال است؛ نمایش تومان (÷۱۰) و ورودی ×۱۰.
export const RIAL_PER_TOMAN = 10;
export const rialToToman = (rial: number): number => Math.round((Number(rial) || 0) / RIAL_PER_TOMAN);
export const tomanToRial = (toman: number): number => Math.round((Number(toman) || 0) * RIAL_PER_TOMAN);
// ورودی مقدار ریال است (سازگاری با همهٔ فراخوانی‌ها) ولی خروجی به تومان نمایش داده می‌شود.
export function formatRial(rial: number): string {
return new Intl.NumberFormat('fa-IR').format(rialToToman(rial)) + ' تومان';
}
export function formatNumber(n: number): string {