fix(SMS): change the brand name of the sms

This commit is contained in:
SepehrYahyaee
2026-11-22 16:04:24 +03:30
parent 99aba7e0fd
commit e31e2c7be6
13 changed files with 347 additions and 42 deletions
@@ -0,0 +1,48 @@
import type { VerifyLookUpMessage } from "./sms-gateway.types";
export const DEFAULT_SMS_BRAND_NAME = "یارا۷۲۴";
export function getSmsBrandName(): string {
return process.env.SMS_BRAND_NAME?.trim() || DEFAULT_SMS_BRAND_NAME;
}
export function hasCustomSmsBrand(): boolean {
return getSmsBrandName() !== DEFAULT_SMS_BRAND_NAME;
}
type MessageTokens = Partial<
Pick<VerifyLookUpMessage, "token" | "token2" | "token3" | "token10">
>;
/** Render patterns only: never replace branding or placeholders inside token values. */
export function renderSmsText(
pattern: string,
tokens: MessageTokens = {},
): string {
const brandName = getSmsBrandName();
// Handle old database/config patterns without rewriting links embedded in them.
let brandedPattern = pattern
.split(/(https?:\/\/[^\s]+)/gi)
.map((part) =>
/^https?:\/\//i.test(part)
? part
: part.replace(
/(^|[\s(«،؛:])(?:یارا[ \u200c]*[۷7][۲2][۴4]|yara[ \u200c]*724)(?=$|[\s)»،؛:.!?])/giu,
"$1{brandName}",
),
)
.join("");
if (
!brandedPattern.includes("{brandName}") &&
!brandedPattern.includes(brandName)
) {
brandedPattern += "\n{brandName}";
}
return brandedPattern
.replace(
/\{(brandName|token|token2|token3|token10)\}/g,
(_match, key: "brandName" | keyof MessageTokens) =>
key === "brandName" ? brandName : (tokens[key] ?? ""),
)
.trim();
}