Initialize the API and add managed app banners.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { SmsService } from './sms.service';
|
||||
|
||||
@Module({
|
||||
providers: [SmsService],
|
||||
exports: [SmsService],
|
||||
})
|
||||
export class SmsModule {}
|
||||
@@ -0,0 +1,109 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
export type SmsSendResult = {
|
||||
success: boolean;
|
||||
raw: unknown;
|
||||
};
|
||||
|
||||
function maskPhone(phone: string): string {
|
||||
return phone.length <= 4
|
||||
? '****'
|
||||
: `${phone.slice(0, 4)}****${phone.slice(-2)}`;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class SmsService {
|
||||
private readonly logger = new Logger(SmsService.name);
|
||||
|
||||
constructor(private readonly config: ConfigService) {}
|
||||
|
||||
isEnabled(): boolean {
|
||||
const raw = (
|
||||
this.config.get<string>('SMS_ENABLED', 'true') ?? 'true'
|
||||
).toLowerCase();
|
||||
return raw === '1' || raw === 'true' || raw === 'yes';
|
||||
}
|
||||
|
||||
async send(to: string, message: string): Promise<SmsSendResult> {
|
||||
const url = this.config.get<string>(
|
||||
'SMS_API_URL',
|
||||
'http://185.231.112.101/api/createsms',
|
||||
);
|
||||
const secretKey = this.config.get<string>('SMS_SECRET_KEY')?.trim();
|
||||
if (!secretKey) {
|
||||
this.logger.error('SMS_SECRET_KEY تنظیم نشده است');
|
||||
return { success: false, raw: 'SMS_SECRET_KEY missing' };
|
||||
}
|
||||
const platform = this.config.get<string>('SMS_PLATFORM', 'rayegansms');
|
||||
const host = this.resolveHost();
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
secret_key: secretKey,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
phone: to,
|
||||
host,
|
||||
text: message.trim(),
|
||||
platform,
|
||||
}),
|
||||
signal: AbortSignal.timeout(15_000),
|
||||
});
|
||||
|
||||
const text = await response.text();
|
||||
let body: unknown = text;
|
||||
try {
|
||||
body = text ? JSON.parse(text) : null;
|
||||
} catch {
|
||||
/* keep raw text */
|
||||
}
|
||||
|
||||
let success = response.ok;
|
||||
if (success && body && typeof body === 'object') {
|
||||
const obj = body as Record<string, unknown>;
|
||||
if ('success' in obj && obj.success === false) success = false;
|
||||
else if ('ok' in obj && obj.ok === false) success = false;
|
||||
else if (
|
||||
typeof obj.status === 'string' &&
|
||||
['error', 'failed', 'fail'].includes(obj.status.toLowerCase())
|
||||
) {
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!success) {
|
||||
this.logger.warn(
|
||||
`SMS failed to ${maskPhone(to)}: ${JSON.stringify(body)}`,
|
||||
);
|
||||
}
|
||||
|
||||
return { success, raw: body };
|
||||
} catch (err) {
|
||||
this.logger.error(
|
||||
`SMS request error to ${maskPhone(to)}`,
|
||||
err instanceof Error ? err.stack : err,
|
||||
);
|
||||
return {
|
||||
success: false,
|
||||
raw: err instanceof Error ? err.message : String(err),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
buildOtpMessage(code: string): string {
|
||||
const appName = this.config.get<string>('SMS_APP_NAME', 'پارچه');
|
||||
return `رمز عبور یکبار مصرف شما جهت ورود به اپلیکیشن ${appName} ${code} می باشد`;
|
||||
}
|
||||
|
||||
private resolveHost(): string {
|
||||
const fromEnv =
|
||||
this.config.get<string>('SMS_HOST')?.trim() ||
|
||||
this.config.get<string>('SMS_APP_NAME')?.trim();
|
||||
if (fromEnv) return fromEnv;
|
||||
return 'fabric';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user