forked from Yara724/api
49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
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();
|
||
}
|