feat(settings): unify settings and ui-texts with canonical aliases, add PriceInput thousand separators, and synchronize financial endpoints
Deploy Canina / deploy (push) Successful in 1m46s

This commit is contained in:
parsa aghaei committed 2026-08-17 09:57:23 +03:30
1 parent 45c9db133c
commit b3c60a1a7d
17 files changed
+79340 -78724

No files matched your search

+26 -1
View File
@@ -10,6 +10,7 @@ import { RedisService } from '../redis/redis.service';
import * as bcrypt from 'bcryptjs';
import { CreateUserDto, UpdateUserDto } from './dto/user.dto';
import { normalizeMobile } from '../common/utils/phone.utils';
import { CANONICAL_ALIASES } from '../settings/settings.service';
export class PaginationQuery {
page?: number | string;
@@ -620,7 +621,17 @@ export class AdminService {
}
async updateSettings(data: Record<string, string>) {
const operations = Object.entries(data).map(([key, value]) => {
if (!data || typeof data !== 'object') return this.getSettings();
const mergedData: Record<string, string> = { ...data };
Object.entries(data).forEach(([key, value]) => {
const aliases = CANONICAL_ALIASES[key] || [];
aliases.forEach((alias) => {
mergedData[alias] = String(value);
});
});
const operations = Object.entries(mergedData).map(([key, value]) => {
return this.prisma.uiText.upsert({
where: { key },
update: { value: String(value) },
@@ -629,6 +640,20 @@ export class AdminService {
});
await this.prisma.$transaction(operations);
// Also sync setting table
for (const [key, value] of Object.entries(mergedData)) {
try {
await this.prisma.setting.upsert({
where: { key },
update: { value: String(value) },
create: { key, value: String(value), category: 'general' },
});
} catch (e) {
// ignore
}
}
return this.getSettings();
}
@@ -77,6 +77,22 @@ export class SettingsController {
return this.settingsService.updateBulkUiTexts(body);
}
@Get('financial')
@ApiOperation({ summary: 'دریافت تنظیمات مالی و ارسال' })
getFinancial() {
return this.settingsService.getFinancialSettings();
}
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('Admin')
@ApiBearerAuth()
@Patch('financial')
@Put('financial')
@ApiOperation({ summary: 'ویرایش تنظیمات مالی و ارسال' })
updateFinancial(@Body() body: any) {
return this.settingsService.updateFinancialSettings(body);
}
@Get('scientific-terms')
@ApiOperation({ summary: 'دریافت تمامی اصطلاحات واژه‌نامه علمی' })
getScientificTerms() {
+162 -11
View File
@@ -10,6 +10,78 @@ export class ScientificTermData {
wikiId?: string;
}
export const CANONICAL_ALIASES: Record<string, string[]> = {
// Brand & Logo
BRAND_LOGO_TEXT_EN: ['site_logo_text_en'],
site_logo_text_en: ['BRAND_LOGO_TEXT_EN'],
BRAND_LOGO_TEXT_FA: ['site_logo_text_fa', 'brand_name_fa'],
site_logo_text_fa: ['BRAND_LOGO_TEXT_FA', 'brand_name_fa'],
brand_name_fa: ['BRAND_LOGO_TEXT_FA', 'site_logo_text_fa'],
BRAND_LOGO_SUBTITLE: ['site_logo_subtitle', 'brand_subtitle'],
site_logo_subtitle: ['BRAND_LOGO_SUBTITLE', 'brand_subtitle'],
brand_subtitle: ['BRAND_LOGO_SUBTITLE', 'site_logo_subtitle'],
BRAND_LOGO_URL: ['site_logo', 'brand_logo_url'],
site_logo: ['BRAND_LOGO_URL', 'brand_logo_url'],
brand_logo_url: ['BRAND_LOGO_URL', 'site_logo'],
// Contact Info
CONTACT_PHONE: ['contact_phone', 'maintenance_contact_phone', 'footer_phone', 'supportPhone'],
contact_phone: ['CONTACT_PHONE', 'maintenance_contact_phone', 'footer_phone', 'supportPhone'],
maintenance_contact_phone: ['CONTACT_PHONE', 'contact_phone', 'footer_phone', 'supportPhone'],
footer_phone: ['CONTACT_PHONE', 'contact_phone', 'maintenance_contact_phone', 'supportPhone'],
supportPhone: ['CONTACT_PHONE', 'contact_phone', 'maintenance_contact_phone', 'footer_phone'],
CONTACT_PHONE_LINK: ['contact_phone_link', 'maintenance_contact_phone_link'],
contact_phone_link: ['CONTACT_PHONE_LINK', 'maintenance_contact_phone_link'],
maintenance_contact_phone_link: ['CONTACT_PHONE_LINK', 'contact_phone_link'],
CONTACT_EMAIL: ['contact_email', 'footer_email'],
contact_email: ['CONTACT_EMAIL', 'footer_email'],
footer_email: ['CONTACT_EMAIL', 'contact_email'],
CONTACT_ADDRESS: ['contact_address', 'footer_address'],
contact_address: ['CONTACT_ADDRESS', 'footer_address'],
footer_address: ['CONTACT_ADDRESS', 'contact_address'],
SOCIAL_WHATSAPP: ['contact_whatsapp'],
contact_whatsapp: ['SOCIAL_WHATSAPP'],
SOCIAL_TELEGRAM: ['contact_telegram'],
contact_telegram: ['SOCIAL_TELEGRAM'],
SOCIAL_INSTAGRAM: ['contact_instagram'],
contact_instagram: ['SOCIAL_INSTAGRAM'],
// Financial & Shipping
SHIPPING_FEE: ['shipping_fee', 'standardShippingFee'],
shipping_fee: ['SHIPPING_FEE', 'standardShippingFee'],
standardShippingFee: ['SHIPPING_FEE', 'shipping_fee'],
MIN_ORDER_AMOUNT: ['min_order_amount', 'minOrderAmount'],
min_order_amount: ['MIN_ORDER_AMOUNT', 'minOrderAmount'],
minOrderAmount: ['MIN_ORDER_AMOUNT', 'min_order_amount'],
FREE_SHIPPING_THRESHOLD: ['free_shipping_threshold', 'freeShippingThreshold'],
free_shipping_threshold: ['FREE_SHIPPING_THRESHOLD', 'freeShippingThreshold'],
freeShippingThreshold: ['FREE_SHIPPING_THRESHOLD', 'free_shipping_threshold'],
B2B_DISCOUNT_PERCENT: ['b2b_discount_percent', 'b2bDiscountPercent'],
b2b_discount_percent: ['B2B_DISCOUNT_PERCENT', 'b2bDiscountPercent'],
b2bDiscountPercent: ['B2B_DISCOUNT_PERCENT', 'b2b_discount_percent'],
TAX_PERCENTAGE: ['tax_percentage', 'taxPercentage'],
tax_percentage: ['TAX_PERCENTAGE', 'taxPercentage'],
taxPercentage: ['TAX_PERCENTAGE', 'tax_percentage'],
CHARITY_ROUND_STEP: ['charity_round_step', 'charityRoundStep'],
charity_round_step: ['CHARITY_ROUND_STEP', 'charityRoundStep'],
charityRoundStep: ['CHARITY_ROUND_STEP', 'charity_round_step'],
// Modes
MAINTENANCE_MODE: ['maintenance_mode'],
maintenance_mode: ['MAINTENANCE_MODE'],
CATALOG_ONLY_MODE: ['catalog_mode'],
catalog_mode: ['CATALOG_ONLY_MODE'],
};
@Injectable()
export class SettingsService implements OnModuleInit {
private readonly logger = new Logger(SettingsService.name);
@@ -91,21 +163,29 @@ export class SettingsService implements OnModuleInit {
async updateUiText(key: string, value: string) {
const strVal = String(value ?? '');
try {
await this.prisma.setting.upsert({
where: { key },
const allKeysToUpdate = new Set<string>([key]);
const aliases = CANONICAL_ALIASES[key] || [];
aliases.forEach((a) => allKeysToUpdate.add(a));
for (const k of allKeysToUpdate) {
try {
await this.prisma.setting.upsert({
where: { key: k },
update: { value: strVal },
create: { key: k, value: strVal, category: 'general' },
});
} catch (e) {
this.logger.warn(`Failed to sync setting key ${k}: ${e}`);
}
await this.prisma.uiText.upsert({
where: { key: k },
update: { value: strVal },
create: { key, value: strVal, category: 'general' },
create: { key: k, value: strVal },
});
} catch (e) {
this.logger.warn(`Failed to sync setting key ${key}: ${e}`);
}
return this.prisma.uiText.upsert({
where: { key },
update: { value: strVal },
create: { key, value: strVal },
});
return { success: true, key, value: strVal, syncedKeys: Array.from(allKeysToUpdate) };
}
async updateBulkUiTexts(texts: Record<string, string>) {
@@ -117,6 +197,77 @@ export class SettingsService implements OnModuleInit {
return { success: true, count: entries.length };
}
async getFinancialSettings() {
const texts = await this.getUiTexts();
const map = new Map<string, string>();
texts.forEach((t) => map.set(t.key, t.value));
const standardShippingFee = Number(map.get('SHIPPING_FEE') || map.get('shipping_fee') || map.get('standardShippingFee') || '0');
const minOrderAmount = Number(map.get('MIN_ORDER_AMOUNT') || map.get('min_order_amount') || map.get('minOrderAmount') || '0');
const freeShippingThreshold = Number(map.get('FREE_SHIPPING_THRESHOLD') || map.get('free_shipping_threshold') || map.get('freeShippingThreshold') || '2000000');
const taxPercentage = Number(map.get('TAX_PERCENTAGE') || map.get('tax_percentage') || map.get('taxPercentage') || '10');
const b2bDiscountPercent = Number(map.get('B2B_DISCOUNT_PERCENT') || map.get('b2b_discount_percent') || map.get('b2bDiscountPercent') || '0');
const charityRoundStep = Number(map.get('CHARITY_ROUND_STEP') || map.get('charity_round_step') || map.get('charityRoundStep') || '10000');
const walletWithdrawalEnabled = map.get('walletWithdrawalEnabled') === 'true';
return {
standardShippingFee,
minOrderAmount,
freeShippingThreshold,
taxPercentage,
b2bDiscountPercent,
charityRoundStep,
charityDonationOptions: [10000, 20000, 50000],
walletWithdrawalEnabled,
};
}
async updateFinancialSettings(data: any) {
const updates: Record<string, string> = {};
if (data.standardShippingFee !== undefined) {
updates['SHIPPING_FEE'] = String(data.standardShippingFee);
updates['shipping_fee'] = String(data.standardShippingFee);
updates['standardShippingFee'] = String(data.standardShippingFee);
}
if (data.shippingFee !== undefined) {
updates['SHIPPING_FEE'] = String(data.shippingFee);
updates['shipping_fee'] = String(data.shippingFee);
updates['standardShippingFee'] = String(data.shippingFee);
}
if (data.minOrderAmount !== undefined) {
updates['MIN_ORDER_AMOUNT'] = String(data.minOrderAmount);
updates['min_order_amount'] = String(data.minOrderAmount);
updates['minOrderAmount'] = String(data.minOrderAmount);
}
if (data.freeShippingThreshold !== undefined) {
updates['FREE_SHIPPING_THRESHOLD'] = String(data.freeShippingThreshold);
updates['free_shipping_threshold'] = String(data.freeShippingThreshold);
updates['freeShippingThreshold'] = String(data.freeShippingThreshold);
}
if (data.taxPercentage !== undefined) {
updates['TAX_PERCENTAGE'] = String(data.taxPercentage);
updates['tax_percentage'] = String(data.taxPercentage);
updates['taxPercentage'] = String(data.taxPercentage);
}
if (data.b2bDiscountPercent !== undefined) {
updates['B2B_DISCOUNT_PERCENT'] = String(data.b2bDiscountPercent);
updates['b2b_discount_percent'] = String(data.b2bDiscountPercent);
updates['b2bDiscountPercent'] = String(data.b2bDiscountPercent);
}
if (data.charityRoundStep !== undefined) {
updates['CHARITY_ROUND_STEP'] = String(data.charityRoundStep);
updates['charity_round_step'] = String(data.charityRoundStep);
updates['charityRoundStep'] = String(data.charityRoundStep);
}
if (data.walletWithdrawalEnabled !== undefined) {
updates['walletWithdrawalEnabled'] = String(data.walletWithdrawalEnabled);
}
await this.updateBulkUiTexts(updates);
return this.getFinancialSettings();
}
async getScientificTerms() {
return this.prisma.scientificTerm.findMany();
}