You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
3.5 KiB
101 lines
3.5 KiB
import axios from 'axios'; |
|
import RedisService from "../infrastructure/database/redisSetup.js"; |
|
import {samehSmsAuth} from './samehAuthentication.js' |
|
import { InternalServerErrorException, NotFoundException } from '@nestjs/common'; |
|
import async from 'async'; |
|
|
|
export default class SmsService { |
|
private smsRedis: any |
|
|
|
constructor() { |
|
this.smsRedis = new RedisService(null, null) |
|
} |
|
|
|
smsListAccess = async (req: any, reply: any) => { |
|
let samehAccessToken: string = await this.smsRedis.get("samehAccessToken"); |
|
|
|
if (!samehAccessToken) { |
|
samehAccessToken = await samehSmsAuth({ username: `${process.env.USERNAME}`, password: `${process.env.PASSWORD}` }); |
|
} |
|
|
|
try { |
|
const fetchResult = await fetch('https://sameh.behdasht.gov.ir/api/v2/sms/updatedList', { |
|
method: 'GET', |
|
headers:{ |
|
Authorization: `Bearer ${samehAccessToken}`, |
|
"Content-Type": "application/json" |
|
}}) |
|
const {data} = await fetchResult.json() |
|
|
|
return { receivedSmsList: data?.updatedList, samehAccessToken }; |
|
} catch (err) { |
|
console.error(err); |
|
throw new InternalServerErrorException('سرویس سامح در دسترس نیست / خطا در دریافت لیست پیامک ها') |
|
} |
|
} |
|
|
|
bulkUpdateSmsStatus = async ({results, samehAccessToken}: {results: any, samehAccessToken: string}) => { |
|
await axios({ |
|
method: 'put', |
|
url: 'https://sameh.behdasht.gov.ir/api/v2/sms/updateSmsStatus', |
|
data: { results }, |
|
headers: { |
|
Authorization: `Bearer ${samehAccessToken}`, |
|
"Content-Type": "application/json" |
|
}, |
|
validateStatus: null, |
|
}) |
|
} |
|
|
|
smsSender = async (req: any, reply: any) => { |
|
const { receivedSmsList, samehAccessToken } = await this.smsListAccess(req, reply); |
|
|
|
if (!receivedSmsList[0]) { |
|
console.log('لیستی جهت ارسال پیامک وجود ندارد') |
|
throw new NotFoundException('لیستی جهت ارسال پیامک وجود ندارد') |
|
} |
|
|
|
console.log("##########", receivedSmsList, receivedSmsList.length) |
|
|
|
try { |
|
const results = await async.mapLimit(receivedSmsList, 10, async (each: any, cb: any) => { |
|
const text = JSON.parse(each?.body); |
|
|
|
axios({ |
|
method: 'get', |
|
url: `https://api.kavenegar.com/v1/${process.env.KAVENEGAR_API_KEY}/verify/lookup.json?receptor=${each?.to}&token=${text.token}&token10=${text.token10}&token20=${text.token20}&template=${each.template}`, |
|
validateStatus: null |
|
}).then(async result => { |
|
const data = result?.data |
|
|
|
console.log(444444, data) |
|
|
|
if (!data.entries) |
|
cb(null, {id: +each.id, status: 3, result: (data?.return?.message).toString()}) |
|
|
|
if ([1, 5].includes(data.entries[0].status) || data.entries[0].statustext === 'ارسال به مخابرات') { |
|
cb(null, { |
|
id: +each.id, |
|
status: 2, |
|
result: (data.entries[0]?.messageid).toString(), |
|
price: +data.entries[0]?.cost |
|
}) |
|
} |
|
}).catch(err => { |
|
console.error(err) |
|
throw new InternalServerErrorException(err) |
|
}) |
|
}) |
|
console.log('Messages status', results) |
|
|
|
await this.bulkUpdateSmsStatus({results, samehAccessToken}) |
|
|
|
console.log('پیامک ها با موفقیت ارسال شدند') |
|
} catch (err) { |
|
console.error(err) |
|
throw new InternalServerErrorException(err) |
|
} |
|
|
|
return {message: 'پیامک ها با موفقیت ارسال شدند'}; |
|
} |
|
};
|
|
|