feat(sms): display live MeliPayamak account balance/credit in SMS settings
Deploy Canina / deploy (push) Successful in 44s
Deploy Canina / deploy (push) Successful in 44s
This commit is contained in:
1 parent
16c1ecdff2
commit
f430c4931f
15 files changed
+48263
-48083
No files matched your search
@@ -294,6 +294,72 @@ export class SmsService {
|
||||
return patterns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch remaining SMS balance/credit (in Rials or count) from MeliPayamak
|
||||
*/
|
||||
async getCredit(): Promise<{ success: boolean; credit: number; message?: string }> {
|
||||
const config = await this.getSmsConfig();
|
||||
|
||||
if (!config.username || !config.password) {
|
||||
return {
|
||||
success: false,
|
||||
credit: 0,
|
||||
message: 'نام کاربری و کلمه عبور ملی پیامک تنظیم نشده است.',
|
||||
};
|
||||
}
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const payload = JSON.stringify({
|
||||
username: config.username,
|
||||
password: config.password,
|
||||
});
|
||||
|
||||
const req = https.request(
|
||||
'https://rest.payamak-panel.com/api/SendSMS/GetCredit',
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': Buffer.byteLength(payload),
|
||||
},
|
||||
},
|
||||
(res) => {
|
||||
let data = '';
|
||||
res.on('data', (chunk: Buffer | string) => (data += String(chunk)));
|
||||
res.on('end', () => {
|
||||
try {
|
||||
const json = JSON.parse(data);
|
||||
const val = Number(json.Value ?? json.value ?? 0);
|
||||
resolve({
|
||||
success: true,
|
||||
credit: val,
|
||||
message: `اعتبار با موفقیت دریافت شد: ${val}`,
|
||||
});
|
||||
} catch (err: unknown) {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
resolve({
|
||||
success: false,
|
||||
credit: 0,
|
||||
message: `خطای دریافت اعتبار: ${data || msg}`,
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
req.on('error', (err: Error) => {
|
||||
resolve({
|
||||
success: false,
|
||||
credit: 0,
|
||||
message: `خطای برقراری ارتباط با وبسرویس ملی پیامک: ${err.message}`,
|
||||
});
|
||||
});
|
||||
|
||||
req.write(payload);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch all registered patterns from MeliPayamak (with local cache sync)
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user