895 lines
30 KiB
TypeScript
895 lines
30 KiB
TypeScript
import { Injectable, OnModuleInit, Logger } from "@nestjs/common";
|
|
import { Prisma } from "@prisma/client";
|
|
import { PrismaService } from "../prisma/prisma.service";
|
|
import { SmsService, SmsLogQuery } from "../common/services/sms.service";
|
|
import { DEFAULT_UI_TEXTS } from "./default-ui-texts";
|
|
|
|
export class ScientificTermData {
|
|
term?: string;
|
|
definition?: string;
|
|
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"],
|
|
|
|
CHARITY_DONATION_ENABLED: [
|
|
"charity_donation_enabled",
|
|
"charityDonationEnabled",
|
|
],
|
|
charity_donation_enabled: [
|
|
"CHARITY_DONATION_ENABLED",
|
|
"charityDonationEnabled",
|
|
],
|
|
charityDonationEnabled: [
|
|
"CHARITY_DONATION_ENABLED",
|
|
"charity_donation_enabled",
|
|
],
|
|
|
|
// Zibal Gateway & Tokens
|
|
ZIBAL_EBANK_TOKEN: [
|
|
"zibal_ebank_token",
|
|
"ZIBAL_API_KEY",
|
|
"zibal_api_key",
|
|
"ZIBAL_TOKEN",
|
|
"zibal_token",
|
|
],
|
|
zibal_ebank_token: [
|
|
"ZIBAL_EBANK_TOKEN",
|
|
"ZIBAL_API_KEY",
|
|
"zibal_api_key",
|
|
"ZIBAL_TOKEN",
|
|
"zibal_token",
|
|
],
|
|
ZIBAL_API_KEY: [
|
|
"ZIBAL_EBANK_TOKEN",
|
|
"zibal_ebank_token",
|
|
"zibal_api_key",
|
|
"ZIBAL_TOKEN",
|
|
"zibal_token",
|
|
],
|
|
zibal_api_key: [
|
|
"ZIBAL_EBANK_TOKEN",
|
|
"zibal_ebank_token",
|
|
"ZIBAL_API_KEY",
|
|
"ZIBAL_TOKEN",
|
|
"zibal_token",
|
|
],
|
|
ZIBAL_MERCHANT: ["zibal_merchant", "merchantId", "merchant"],
|
|
zibal_merchant: ["ZIBAL_MERCHANT", "merchantId", "merchant"],
|
|
ZIBAL_EBANK_ACCOUNT_ID: ["zibal_ebank_account_id", "accountId"],
|
|
zibal_ebank_account_id: ["ZIBAL_EBANK_ACCOUNT_ID", "accountId"],
|
|
|
|
// Modes & Catalog Settings
|
|
MAINTENANCE_MODE: ["maintenance_mode"],
|
|
maintenance_mode: ["MAINTENANCE_MODE"],
|
|
CATALOG_ONLY_MODE: ["catalog_mode", "catalog_only_mode", "CATALOG_MODE"],
|
|
catalog_mode: ["CATALOG_ONLY_MODE", "catalog_only_mode", "CATALOG_MODE"],
|
|
CATALOG_SHOW_PRICES: ["catalog_show_prices", "show_product_prices"],
|
|
catalog_show_prices: ["CATALOG_SHOW_PRICES", "show_product_prices"],
|
|
CATALOG_ALLOW_CART: [
|
|
"catalog_allow_cart",
|
|
"allow_cart_purchase",
|
|
"allow_cart",
|
|
],
|
|
catalog_allow_cart: [
|
|
"CATALOG_ALLOW_CART",
|
|
"allow_cart_purchase",
|
|
"allow_cart",
|
|
],
|
|
CATALOG_ALLOW_CHECKOUT: ["catalog_allow_checkout", "allow_checkout"],
|
|
catalog_allow_checkout: ["CATALOG_ALLOW_CHECKOUT", "allow_checkout"],
|
|
CATALOG_PREORDER_BTN: [
|
|
"catalog_preorder_btn",
|
|
"show_preorder_button",
|
|
"CATALOG_PREORDER_BUTTON",
|
|
],
|
|
catalog_preorder_btn: [
|
|
"CATALOG_PREORDER_BTN",
|
|
"show_preorder_button",
|
|
"CATALOG_PREORDER_BUTTON",
|
|
],
|
|
CATALOG_SHOW_ORDERS: ["catalog_show_orders"],
|
|
catalog_show_orders: ["CATALOG_SHOW_ORDERS"],
|
|
CATALOG_SHOW_WALLET: ["catalog_show_wallet"],
|
|
catalog_show_wallet: ["CATALOG_SHOW_WALLET"],
|
|
CATALOG_SHOW_CHARITY: ["catalog_show_charity"],
|
|
catalog_show_charity: ["CATALOG_SHOW_CHARITY"],
|
|
PRESCRIPTION_UPLOAD_ENABLED: ["prescription_upload_enabled"],
|
|
prescription_upload_enabled: ["PRESCRIPTION_UPLOAD_ENABLED"],
|
|
ORDER_DISABLED_TITLE: ["order_disabled_title"],
|
|
order_disabled_title: ["ORDER_DISABLED_TITLE"],
|
|
ORDER_DISABLED_MESSAGE: ["order_disabled_message"],
|
|
order_disabled_message: ["ORDER_DISABLED_MESSAGE"],
|
|
};
|
|
|
|
@Injectable()
|
|
export class SettingsService implements OnModuleInit {
|
|
private readonly logger = new Logger(SettingsService.name);
|
|
|
|
constructor(
|
|
private prisma: PrismaService,
|
|
private smsService: SmsService,
|
|
) {}
|
|
|
|
async onModuleInit() {
|
|
await this.seedDefaultUiTexts();
|
|
}
|
|
|
|
async seedDefaultUiTexts() {
|
|
try {
|
|
const existing = await this.prisma.uiText.findMany({
|
|
select: { key: true },
|
|
});
|
|
const existingKeys = new Set(existing.map((e) => e.key));
|
|
|
|
const missingEntries = Object.entries(DEFAULT_UI_TEXTS).filter(
|
|
([key]) => !existingKeys.has(key),
|
|
);
|
|
|
|
if (missingEntries.length > 0) {
|
|
await this.prisma.uiText.createMany({
|
|
data: missingEntries.map(([key, value]) => ({ key, value })),
|
|
skipDuplicates: true,
|
|
});
|
|
this.logger.log(
|
|
`[SettingsService] Auto-seeded ${missingEntries.length} default UI texts.`,
|
|
);
|
|
}
|
|
} catch (err: any) {
|
|
this.logger.warn(
|
|
`[SettingsService] Failed to auto-seed UI texts: ${err?.message || err}`,
|
|
);
|
|
}
|
|
}
|
|
|
|
async getUiTexts() {
|
|
// Ensure default texts exist
|
|
await this.seedDefaultUiTexts();
|
|
|
|
const [uiTexts, settings] = await Promise.all([
|
|
this.prisma.uiText.findMany(),
|
|
this.prisma.setting.findMany(),
|
|
]);
|
|
|
|
const mergedMap = new Map<string, string>();
|
|
|
|
// 1. Add all from uiTexts
|
|
uiTexts.forEach((u) => {
|
|
mergedMap.set(u.key, u.value);
|
|
});
|
|
|
|
// 2. Add from settings and unpack object keys
|
|
settings.filter((s) => s.key !== "support_chat").forEach((s) => {
|
|
const valStr =
|
|
typeof s.value === "string" ? s.value : JSON.stringify(s.value);
|
|
mergedMap.set(s.key, valStr);
|
|
|
|
if (
|
|
typeof s.value === "object" &&
|
|
s.value !== null &&
|
|
!Array.isArray(s.value)
|
|
) {
|
|
Object.entries(s.value as Record<string, any>).forEach(([k, v]) => {
|
|
mergedMap.set(k, String(v));
|
|
});
|
|
}
|
|
});
|
|
|
|
// If b2bRegistrationOpen is set, sync b2b_enabled
|
|
if (mergedMap.has("b2bRegistrationOpen")) {
|
|
const val = mergedMap.get("b2bRegistrationOpen")!;
|
|
mergedMap.set("b2b_enabled", val);
|
|
}
|
|
|
|
const SENSITIVE_KEY_PATTERNS = [
|
|
/token/i,
|
|
/secret/i,
|
|
/password/i,
|
|
/api_key/i,
|
|
/apikey/i,
|
|
/private/i,
|
|
/zibal/i,
|
|
/kavenegar/i,
|
|
/ghasedak/i,
|
|
/credential/i,
|
|
/account_id/i,
|
|
/access_key/i,
|
|
];
|
|
|
|
return Array.from(mergedMap.entries())
|
|
.filter(([key]) => !SENSITIVE_KEY_PATTERNS.some((pattern) => pattern.test(key)))
|
|
.map(([key, value]) => ({
|
|
key,
|
|
value,
|
|
}));
|
|
}
|
|
|
|
async updateUiText(key: string, value: string) {
|
|
if (key === "support_chat") throw new Error("Use support-chat settings API");
|
|
const strVal = String(value ?? "");
|
|
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: k, value: strVal },
|
|
});
|
|
}
|
|
|
|
return {
|
|
success: true,
|
|
key,
|
|
value: strVal,
|
|
syncedKeys: Array.from(allKeysToUpdate),
|
|
};
|
|
}
|
|
|
|
async updateBulkUiTexts(texts: Record<string, string>) {
|
|
if (!texts || typeof texts !== "object") return { success: true };
|
|
const entries = Object.entries(texts);
|
|
for (const [key, value] of entries) {
|
|
await this.updateUiText(key, String(value ?? ""));
|
|
}
|
|
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 charityDonationEnabled =
|
|
map.get("CHARITY_DONATION_ENABLED") !== "false" &&
|
|
map.get("charity_donation_enabled") !== "false" &&
|
|
map.get("charityDonationEnabled") !== "false";
|
|
const walletWithdrawalEnabled =
|
|
map.get("walletWithdrawalEnabled") === "true";
|
|
|
|
return {
|
|
standardShippingFee,
|
|
minOrderAmount,
|
|
freeShippingThreshold,
|
|
taxPercentage,
|
|
b2bDiscountPercent,
|
|
charityRoundStep,
|
|
charityDonationEnabled,
|
|
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.charityDonationEnabled !== undefined) {
|
|
const boolStr = String(data.charityDonationEnabled);
|
|
updates["CHARITY_DONATION_ENABLED"] = boolStr;
|
|
updates["charity_donation_enabled"] = boolStr;
|
|
updates["charityDonationEnabled"] = boolStr;
|
|
}
|
|
if (data.walletWithdrawalEnabled !== undefined) {
|
|
updates["walletWithdrawalEnabled"] = String(data.walletWithdrawalEnabled);
|
|
}
|
|
|
|
await this.updateBulkUiTexts(updates);
|
|
return this.getFinancialSettings();
|
|
}
|
|
|
|
async getScientificTerms() {
|
|
return this.prisma.scientificTerm.findMany();
|
|
}
|
|
|
|
async upsertScientificTerm(key: string, data: ScientificTermData) {
|
|
const term = String(data?.term || "");
|
|
const definition = String(data?.definition || "");
|
|
const wikiId = String(data?.wikiId || "general");
|
|
|
|
return this.prisma.scientificTerm.upsert({
|
|
where: { key },
|
|
update: {
|
|
term,
|
|
definition,
|
|
wikiId,
|
|
},
|
|
create: {
|
|
key,
|
|
term,
|
|
definition,
|
|
wikiId,
|
|
},
|
|
});
|
|
}
|
|
|
|
async deleteScientificTerm(key: string) {
|
|
return this.prisma.scientificTerm.delete({
|
|
where: { key },
|
|
});
|
|
}
|
|
|
|
async getCategorySetting(category: string) {
|
|
if (category === "system") {
|
|
const texts = await this.getUiTexts();
|
|
const map = new Map<string, string>();
|
|
texts.forEach((t) => map.set(t.key, t.value));
|
|
|
|
const isMaintenance =
|
|
map.get("MAINTENANCE_MODE") === "true" ||
|
|
map.get("maintenance_mode") === "true" ||
|
|
map.get("maintenanceMode") === "true";
|
|
const allowGuestCheckout = map.get("allowGuestCheckout") !== "false";
|
|
const b2bRegistrationOpen =
|
|
map.get("b2bRegistrationOpen") !== "false" &&
|
|
map.get("b2b_enabled") !== "false";
|
|
const supportPhone =
|
|
map.get("CONTACT_PHONE") ||
|
|
map.get("contact_phone") ||
|
|
map.get("supportPhone") ||
|
|
"۰۲۱-۸۸۸۸۴۴۴۴";
|
|
|
|
return {
|
|
maintenanceMode: isMaintenance,
|
|
allowGuestCheckout,
|
|
b2bRegistrationOpen,
|
|
supportPhone,
|
|
catalogOnlyMode:
|
|
map.get("CATALOG_ONLY_MODE") === "true" ||
|
|
map.get("catalog_mode") === "true",
|
|
catalogShowPrices:
|
|
map.get("CATALOG_SHOW_PRICES") !== "false" &&
|
|
map.get("catalog_show_prices") !== "false",
|
|
catalogAllowCart:
|
|
map.get("CATALOG_ALLOW_CART") === "true" ||
|
|
map.get("catalog_allow_cart") === "true",
|
|
catalogAllowCheckout:
|
|
map.get("CATALOG_ALLOW_CHECKOUT") === "true" ||
|
|
map.get("catalog_allow_checkout") === "true",
|
|
catalogPreorderBtn:
|
|
map.get("CATALOG_PREORDER_BTN") === "true" ||
|
|
map.get("catalog_preorder_btn") === "true",
|
|
catalogShowOrders:
|
|
map.get("CATALOG_SHOW_ORDERS") === "true" ||
|
|
map.get("catalog_show_orders") === "true",
|
|
catalogShowWallet:
|
|
map.get("CATALOG_SHOW_WALLET") === "true" ||
|
|
map.get("catalog_show_wallet") === "true",
|
|
catalogShowCharity:
|
|
map.get("CATALOG_SHOW_CHARITY") === "true" ||
|
|
map.get("catalog_show_charity") === "true",
|
|
prescriptionUploadEnabled:
|
|
map.get("PRESCRIPTION_UPLOAD_ENABLED") !== "false",
|
|
orderDisabledTitle:
|
|
map.get("ORDER_DISABLED_TITLE") ||
|
|
"امکان ثبت سفارش آنلاین موقتاً غیرفعال است",
|
|
orderDisabledMessage:
|
|
map.get("ORDER_DISABLED_MESSAGE") ||
|
|
"سامانه در حال حاضر در حالت کاتالوگ قرار دارد. جهت دریافت مشاوره تخصصی میتوانید با کارشناسان ما تماس بگیرید.",
|
|
};
|
|
}
|
|
|
|
if (category === "seo") {
|
|
const texts = await this.getUiTexts();
|
|
const map = new Map<string, string>();
|
|
texts.forEach((t) => map.set(t.key, t.value));
|
|
|
|
const setting = await this.prisma.setting.findFirst({
|
|
where: { category: "seo" },
|
|
});
|
|
const storedObj =
|
|
setting && typeof setting.value === "object" && setting.value !== null
|
|
? (setting.value as Record<string, any>)
|
|
: {};
|
|
|
|
return {
|
|
brandNameFa:
|
|
storedObj.brandNameFa ||
|
|
storedObj.brand_name_fa ||
|
|
map.get("brand_name_fa") ||
|
|
map.get("BRAND_LOGO_TEXT_FA") ||
|
|
"کنینا ایران",
|
|
brandNameEn:
|
|
storedObj.brandNameEn ||
|
|
storedObj.brand_name_en ||
|
|
map.get("brand_name_en") ||
|
|
map.get("BRAND_LOGO_TEXT_EN") ||
|
|
"Canina Iran",
|
|
titleSeparator:
|
|
storedObj.titleSeparator ||
|
|
storedObj.title_separator ||
|
|
map.get("title_separator") ||
|
|
"|",
|
|
titlePosition:
|
|
storedObj.titlePosition ||
|
|
storedObj.title_position ||
|
|
map.get("title_position") ||
|
|
"suffix",
|
|
defaultMetaTitle:
|
|
storedObj.defaultMetaTitle ||
|
|
map.get("defaultMetaTitle") ||
|
|
map.get("home_meta_title") ||
|
|
"کنینا ایران | نماینده رسمی مکملهای دارویی Canina آلمان",
|
|
defaultMetaDescription:
|
|
storedObj.defaultMetaDescription ||
|
|
map.get("defaultMetaDescription") ||
|
|
map.get("home_meta_desc") ||
|
|
"فروشگاه تخصصی و نماینده انحصاری مکملهای درمانی و تقویتی سگ و گربه کنینا آلمان با فرمولاسیون دارویی و تاییدیه بالینی.",
|
|
keywords:
|
|
storedObj.keywords ||
|
|
map.get("keywords") ||
|
|
map.get("home_keywords") ||
|
|
"مکمل سگ, مکمل گربه, مکمل دارویی حیوانات, Canina Germany",
|
|
canonicalBaseUrl:
|
|
storedObj.canonicalBaseUrl ||
|
|
map.get("canonicalBaseUrl") ||
|
|
"https://canina.ir",
|
|
ogImageUrl:
|
|
storedObj.ogImageUrl ||
|
|
map.get("ogImageUrl") ||
|
|
"/assets/images/regenerated_image_1779109861747.png",
|
|
googleSiteVerification:
|
|
storedObj.googleSiteVerification ||
|
|
storedObj.google_site_verification ||
|
|
map.get("googleSiteVerification") ||
|
|
map.get("google_site_verification") ||
|
|
"",
|
|
googleAnalyticsId:
|
|
storedObj.googleAnalyticsId ||
|
|
storedObj.ga_measurement_id ||
|
|
map.get("googleAnalyticsId") ||
|
|
map.get("ga_measurement_id") ||
|
|
"",
|
|
googleTagManagerId:
|
|
storedObj.googleTagManagerId ||
|
|
storedObj.gtm_id ||
|
|
map.get("googleTagManagerId") ||
|
|
map.get("gtm_id") ||
|
|
"",
|
|
enableGoogleAnalytics:
|
|
storedObj.enableGoogleAnalytics !== undefined
|
|
? Boolean(storedObj.enableGoogleAnalytics)
|
|
: map.get("enableGoogleAnalytics") !== undefined
|
|
? map.get("enableGoogleAnalytics") === "true"
|
|
: true,
|
|
enableGoogleTagManager:
|
|
storedObj.enableGoogleTagManager !== undefined
|
|
? Boolean(storedObj.enableGoogleTagManager)
|
|
: map.get("enableGoogleTagManager") !== undefined
|
|
? map.get("enableGoogleTagManager") === "true"
|
|
: true,
|
|
clarityProjectId:
|
|
storedObj.clarityProjectId ||
|
|
storedObj.clarity_project_id ||
|
|
map.get("clarityProjectId") ||
|
|
map.get("clarity_project_id") ||
|
|
"",
|
|
};
|
|
}
|
|
|
|
const setting = await this.prisma.setting.findFirst({
|
|
where: { category },
|
|
});
|
|
return setting ? setting.value : {};
|
|
}
|
|
|
|
async updateCategorySetting(category: string, value: Prisma.InputJsonValue) {
|
|
const key = `${category}_config`;
|
|
const res = await this.prisma.setting.upsert({
|
|
where: { key },
|
|
update: { category, value },
|
|
create: { key, category, value },
|
|
});
|
|
|
|
// If system config is updated, also sync individual keys into uiText for fast & reliable lookup
|
|
if (category === "system" && typeof value === "object" && value !== null) {
|
|
const obj = value as Record<string, any>;
|
|
const updates: Record<string, string> = {};
|
|
|
|
const toBool = (val: any) =>
|
|
val === true || val === "true" || val === 1 || val === "1";
|
|
|
|
if ("maintenanceMode" in obj || "MAINTENANCE_MODE" in obj) {
|
|
const val = toBool(obj.maintenanceMode ?? obj.MAINTENANCE_MODE);
|
|
const valStr = String(val);
|
|
updates["MAINTENANCE_MODE"] = valStr;
|
|
updates["maintenance_mode"] = valStr;
|
|
updates["maintenanceMode"] = valStr;
|
|
}
|
|
if ("supportPhone" in obj) {
|
|
updates["CONTACT_PHONE"] = String(obj.supportPhone);
|
|
updates["contact_phone"] = String(obj.supportPhone);
|
|
updates["supportPhone"] = String(obj.supportPhone);
|
|
}
|
|
if ("b2bRegistrationOpen" in obj) {
|
|
const valStr = String(toBool(obj.b2bRegistrationOpen));
|
|
updates["b2bRegistrationOpen"] = valStr;
|
|
updates["b2b_enabled"] = valStr;
|
|
}
|
|
if ("allowGuestCheckout" in obj) {
|
|
updates["allowGuestCheckout"] = String(toBool(obj.allowGuestCheckout));
|
|
}
|
|
if ("catalogOnlyMode" in obj || "CATALOG_ONLY_MODE" in obj) {
|
|
const val = String(
|
|
toBool(obj.catalogOnlyMode ?? obj.CATALOG_ONLY_MODE),
|
|
);
|
|
updates["CATALOG_ONLY_MODE"] = val;
|
|
updates["catalog_mode"] = val;
|
|
}
|
|
if ("catalogShowPrices" in obj || "CATALOG_SHOW_PRICES" in obj) {
|
|
const val = String(
|
|
toBool(obj.catalogShowPrices ?? obj.CATALOG_SHOW_PRICES),
|
|
);
|
|
updates["CATALOG_SHOW_PRICES"] = val;
|
|
updates["catalog_show_prices"] = val;
|
|
}
|
|
if ("catalogAllowCart" in obj || "CATALOG_ALLOW_CART" in obj) {
|
|
const val = String(
|
|
toBool(obj.catalogAllowCart ?? obj.CATALOG_ALLOW_CART),
|
|
);
|
|
updates["CATALOG_ALLOW_CART"] = val;
|
|
updates["catalog_allow_cart"] = val;
|
|
}
|
|
if ("catalogAllowCheckout" in obj || "CATALOG_ALLOW_CHECKOUT" in obj) {
|
|
const val = String(
|
|
toBool(obj.catalogAllowCheckout ?? obj.CATALOG_ALLOW_CHECKOUT),
|
|
);
|
|
updates["CATALOG_ALLOW_CHECKOUT"] = val;
|
|
updates["catalog_allow_checkout"] = val;
|
|
}
|
|
if ("catalogPreorderBtn" in obj || "CATALOG_PREORDER_BTN" in obj) {
|
|
const val = String(
|
|
toBool(obj.catalogPreorderBtn ?? obj.CATALOG_PREORDER_BTN),
|
|
);
|
|
updates["CATALOG_PREORDER_BTN"] = val;
|
|
updates["catalog_preorder_btn"] = val;
|
|
}
|
|
if ("catalogShowOrders" in obj || "CATALOG_SHOW_ORDERS" in obj) {
|
|
const val = String(
|
|
toBool(obj.catalogShowOrders ?? obj.CATALOG_SHOW_ORDERS),
|
|
);
|
|
updates["CATALOG_SHOW_ORDERS"] = val;
|
|
updates["catalog_show_orders"] = val;
|
|
}
|
|
if ("catalogShowWallet" in obj || "CATALOG_SHOW_WALLET" in obj) {
|
|
const val = String(
|
|
toBool(obj.catalogShowWallet ?? obj.CATALOG_SHOW_WALLET),
|
|
);
|
|
updates["CATALOG_SHOW_WALLET"] = val;
|
|
updates["catalog_show_wallet"] = val;
|
|
}
|
|
if ("catalogShowCharity" in obj || "CATALOG_SHOW_CHARITY" in obj) {
|
|
const val = String(
|
|
toBool(obj.catalogShowCharity ?? obj.CATALOG_SHOW_CHARITY),
|
|
);
|
|
updates["CATALOG_SHOW_CHARITY"] = val;
|
|
updates["catalog_show_charity"] = val;
|
|
}
|
|
if (
|
|
"prescriptionUploadEnabled" in obj ||
|
|
"PRESCRIPTION_UPLOAD_ENABLED" in obj
|
|
) {
|
|
const val = String(
|
|
toBool(
|
|
obj.prescriptionUploadEnabled ?? obj.PRESCRIPTION_UPLOAD_ENABLED,
|
|
),
|
|
);
|
|
updates["PRESCRIPTION_UPLOAD_ENABLED"] = val;
|
|
}
|
|
if ("orderDisabledTitle" in obj || "ORDER_DISABLED_TITLE" in obj) {
|
|
const val = String(
|
|
obj.orderDisabledTitle || obj.ORDER_DISABLED_TITLE || "",
|
|
);
|
|
updates["ORDER_DISABLED_TITLE"] = val;
|
|
}
|
|
if ("orderDisabledMessage" in obj || "ORDER_DISABLED_MESSAGE" in obj) {
|
|
const val = String(
|
|
obj.orderDisabledMessage || obj.ORDER_DISABLED_MESSAGE || "",
|
|
);
|
|
updates["ORDER_DISABLED_MESSAGE"] = val;
|
|
}
|
|
|
|
await this.updateBulkUiTexts(updates);
|
|
}
|
|
|
|
// If seo config is updated, sync individual keys into uiText for unified frontend consumption
|
|
if (category === "seo" && typeof value === "object" && value !== null) {
|
|
const obj = value as Record<string, any>;
|
|
const updates: Record<string, string> = {};
|
|
|
|
if (obj.brandNameFa || obj.brand_name_fa) {
|
|
const brand = String(obj.brandNameFa || obj.brand_name_fa);
|
|
updates["brand_name_fa"] = brand;
|
|
updates["BRAND_LOGO_TEXT_FA"] = brand;
|
|
}
|
|
if (obj.brandNameEn || obj.brand_name_en) {
|
|
const brandEn = String(obj.brandNameEn || obj.brand_name_en);
|
|
updates["brand_name_en"] = brandEn;
|
|
}
|
|
if (obj.titleSeparator || obj.title_separator) {
|
|
updates["title_separator"] = String(
|
|
obj.titleSeparator || obj.title_separator,
|
|
);
|
|
}
|
|
if (obj.titlePosition || obj.title_position) {
|
|
updates["title_position"] = String(
|
|
obj.titlePosition || obj.title_position,
|
|
);
|
|
}
|
|
if (obj.defaultMetaTitle) {
|
|
updates["defaultMetaTitle"] = String(obj.defaultMetaTitle);
|
|
}
|
|
if (obj.defaultMetaDescription) {
|
|
updates["defaultMetaDescription"] = String(obj.defaultMetaDescription);
|
|
}
|
|
if (obj.keywords) {
|
|
updates["keywords"] = String(obj.keywords);
|
|
}
|
|
if (obj.canonicalBaseUrl) {
|
|
updates["canonicalBaseUrl"] = String(obj.canonicalBaseUrl);
|
|
}
|
|
if (obj.ogImageUrl) {
|
|
updates["ogImageUrl"] = String(obj.ogImageUrl);
|
|
}
|
|
if ("googleSiteVerification" in obj) {
|
|
updates["googleSiteVerification"] = String(
|
|
obj.googleSiteVerification ?? "",
|
|
);
|
|
updates["google_site_verification"] = String(
|
|
obj.googleSiteVerification ?? "",
|
|
);
|
|
}
|
|
if ("googleAnalyticsId" in obj) {
|
|
updates["googleAnalyticsId"] = String(obj.googleAnalyticsId ?? "");
|
|
updates["ga_measurement_id"] = String(obj.googleAnalyticsId ?? "");
|
|
}
|
|
if ("googleTagManagerId" in obj) {
|
|
updates["googleTagManagerId"] = String(obj.googleTagManagerId ?? "");
|
|
updates["gtm_id"] = String(obj.googleTagManagerId ?? "");
|
|
}
|
|
if ("enableGoogleAnalytics" in obj) {
|
|
updates["enableGoogleAnalytics"] = String(
|
|
Boolean(obj.enableGoogleAnalytics),
|
|
);
|
|
}
|
|
if ("enableGoogleTagManager" in obj) {
|
|
updates["enableGoogleTagManager"] = String(
|
|
Boolean(obj.enableGoogleTagManager),
|
|
);
|
|
}
|
|
if ("clarityProjectId" in obj) {
|
|
updates["clarityProjectId"] = String(obj.clarityProjectId ?? "");
|
|
updates["clarity_project_id"] = String(obj.clarityProjectId ?? "");
|
|
}
|
|
|
|
if (Object.keys(updates).length > 0) {
|
|
await this.updateBulkUiTexts(updates);
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
async getSmsSettings() {
|
|
return this.smsService.getSmsConfig();
|
|
}
|
|
|
|
async getSmsCredit() {
|
|
return this.smsService.getCredit();
|
|
}
|
|
|
|
async updateSmsSettings(value: Prisma.InputJsonValue) {
|
|
const key = "sms_config";
|
|
return this.prisma.setting.upsert({
|
|
where: { key },
|
|
update: { category: "sms", value },
|
|
create: { key, category: "sms", value },
|
|
});
|
|
}
|
|
|
|
async testSms(targetPhone: string, patternId?: number, args?: string[]) {
|
|
return this.smsService.testSms(targetPhone, patternId, args);
|
|
}
|
|
|
|
getSmsEvents() {
|
|
return this.smsService.getEventDefinitions();
|
|
}
|
|
|
|
async testSmsRule(rule: any, testPhone?: string) {
|
|
return this.smsService.testRule(rule, testPhone);
|
|
}
|
|
|
|
async getPatterns() {
|
|
return this.smsService.getPatterns();
|
|
}
|
|
|
|
async addPattern(title: string, body: string, blackListId?: number) {
|
|
return this.smsService.addPattern(title, body, blackListId);
|
|
}
|
|
|
|
async editPattern(bodyId: number, body: string) {
|
|
return this.smsService.editPattern(bodyId, body);
|
|
}
|
|
|
|
async getSmsLogs(query: SmsLogQuery) {
|
|
return this.smsService.getSmsLogs(query);
|
|
}
|
|
|
|
async deleteSmsLog(id: string) {
|
|
return this.smsService.deleteSmsLog(id);
|
|
}
|
|
|
|
async clearAllSmsLogs() {
|
|
return this.smsService.clearAllSmsLogs();
|
|
}
|
|
}
|