x1ongzhu 1 год назад
Родитель
Сommit
d5d9b03efe
1 измененных файлов с 84 добавлено и 93 удалено
  1. 84 93
      src/rcs-number/rcs-number.service.ts

+ 84 - 93
src/rcs-number/rcs-number.service.ts

@@ -58,102 +58,93 @@ export class RcsNumberService {
     }
 
     async create(country?: string, deviceId?: string, taskId?: number) {
-        return await this.lock.acquire(
-            'dispatchTask',
-            async () => {
-                let operatorConfig: OperatorConfig
-                if (country) {
-                    operatorConfig = await this.operatorConfigService.findByCountry(country)
-                    if (!operatorConfig) {
-                        throw new Error(`该国家不可用: ${country}`)
-                    }
-                    if (operatorConfig.matchers.filter((matcher) => matcher.enabled).length === 0) {
-                        throw new Error(`该国家无可用运营商: ${country}`)
-                    }
-                }
-                if (!operatorConfig) {
-                    let availableOperatorConfigs: OperatorConfig[] = null
-                    if (taskId) {
-                        const task = await this.taskService.findById(taskId)
-                        if (task && task.country?.length > 0) {
-                            availableOperatorConfigs = await this.operatorConfigService.findByCountryIn(task.country)
-                        }
-                    }
-                    if (availableOperatorConfigs === null) {
-                        availableOperatorConfigs = await this.operatorConfigService.all()
-                    }
-                    availableOperatorConfigs = availableOperatorConfigs.filter(
-                        (config) => config.matchers.filter((matcher) => matcher.enabled).length > 0
-                    )
-                    if (availableOperatorConfigs.length === 0) {
-                        throw new Error(`无可用国家`)
-                    }
-                    operatorConfig =
-                        availableOperatorConfigs[Math.floor(Math.random() * availableOperatorConfigs.length)]
+        let operatorConfig: OperatorConfig
+        if (country) {
+            operatorConfig = await this.operatorConfigService.findByCountry(country)
+            if (!operatorConfig) {
+                throw new Error(`该国家不可用: ${country}`)
+            }
+            if (operatorConfig.matchers.filter((matcher) => matcher.enabled).length === 0) {
+                throw new Error(`该国家无可用运营商: ${country}`)
+            }
+        }
+        if (!operatorConfig) {
+            let availableOperatorConfigs: OperatorConfig[] = null
+            if (taskId) {
+                const task = await this.taskService.findById(taskId)
+                if (task && task.country?.length > 0) {
+                    availableOperatorConfigs = await this.operatorConfigService.findByCountryIn(task.country)
                 }
+            }
+            if (availableOperatorConfigs === null) {
+                availableOperatorConfigs = await this.operatorConfigService.all()
+            }
+            availableOperatorConfigs = availableOperatorConfigs.filter(
+                (config) => config.matchers.filter((matcher) => matcher.enabled).length > 0
+            )
+            if (availableOperatorConfigs.length === 0) {
+                throw new Error(`无可用国家`)
+            }
+            operatorConfig = availableOperatorConfigs[Math.floor(Math.random() * availableOperatorConfigs.length)]
+        }
 
-                const channels = await this.channelService.all()
-                const availableChannels = channels.filter((channel) => {
-                    return (
-                        channel.countryConfig.find(
-                            (config) =>
-                                config.countryCode.toLowerCase() === operatorConfig.country ||
-                                config.countryCode.toUpperCase() === operatorConfig.country
-                        )?.enabled || false
-                    )
-                })
-                if (!availableChannels.length) {
-                    throw new Error('无可用短信通道')
-                }
-                const channel = availableChannels[Math.floor(Math.random() * availableChannels.length)]
-                let numberService: GetNumberService
+        const channels = await this.channelService.all()
+        const availableChannels = channels.filter((channel) => {
+            return (
+                channel.countryConfig.find(
+                    (config) =>
+                        config.countryCode.toLowerCase() === operatorConfig.country ||
+                        config.countryCode.toUpperCase() === operatorConfig.country
+                )?.enabled || false
+            )
+        })
+        if (!availableChannels.length) {
+            throw new Error('无可用短信通道')
+        }
+        const channel = availableChannels[Math.floor(Math.random() * availableChannels.length)]
+        let numberService: GetNumberService
 
-                switch (channel.source) {
-                    case RcsNumberSource.mwze167:
-                        numberService = this.mwze167
-                        break
-                    case RcsNumberSource.durian:
-                        numberService = this.durian
-                        break
-                    case RcsNumberSource.i18nvc:
-                        numberService = this.i18nvc
-                        break
-                    case RcsNumberSource.firefox:
-                        numberService = this.firefox
-                        break
-                    case RcsNumberSource.d38:
-                        numberService = this.d38
-                        break
-                    default:
-                        throw new Error('不支持的短信通道')
-                }
-                const res = await numberService.getNumber(operatorConfig.country)
-                res.number = checkAndFormatNumber(operatorConfig.country, res.number, res.operatorName)
-                const mapTo = matchOperator(res.operatorCode, operatorConfig)
-                if (!mapTo) {
-                    throw new Error(
-                        `无匹配的运营商, country: ${operatorConfig.country}, operatorCode: ${res.operatorCode}, number: ${res.number}`
-                    )
-                }
-                const number = new RcsNumber()
-                number.from = numberService.source
-                number.number = res.number
-                number.orderId = res.orderId
-                number.deviceId = deviceId
-                number.mcc = mapTo.mcc
-                number.mnc = mapTo.mnc
-                number.areaCode = operatorConfig.areaCode
-                number.country = operatorConfig.country.toLowerCase()
-                number.status = RcsNumberStatus.PENDING
-                number.expiryTime = addMinutes(new Date(), 5)
-                number.taskId = taskId
-                number.rawResponse = JSON.stringify(res.rawResponse)
-                return await this.rcsNumberRepository.save(number)
-            },
-            {
-                timeout: 1
-            }
-        )
+        switch (channel.source) {
+            case RcsNumberSource.mwze167:
+                numberService = this.mwze167
+                break
+            case RcsNumberSource.durian:
+                numberService = this.durian
+                break
+            case RcsNumberSource.i18nvc:
+                numberService = this.i18nvc
+                break
+            case RcsNumberSource.firefox:
+                numberService = this.firefox
+                break
+            case RcsNumberSource.d38:
+                numberService = this.d38
+                break
+            default:
+                throw new Error('不支持的短信通道')
+        }
+        const res = await numberService.getNumber(operatorConfig.country)
+        res.number = checkAndFormatNumber(operatorConfig.country, res.number, res.operatorName)
+        const mapTo = matchOperator(res.operatorCode, operatorConfig)
+        if (!mapTo) {
+            throw new Error(
+                `无匹配的运营商, country: ${operatorConfig.country}, operatorCode: ${res.operatorCode}, number: ${res.number}`
+            )
+        }
+        const number = new RcsNumber()
+        number.from = numberService.source
+        number.number = res.number
+        number.orderId = res.orderId
+        number.deviceId = deviceId
+        number.mcc = mapTo.mcc
+        number.mnc = mapTo.mnc
+        number.areaCode = operatorConfig.areaCode
+        number.country = operatorConfig.country.toLowerCase()
+        number.status = RcsNumberStatus.PENDING
+        number.expiryTime = addMinutes(new Date(), 5)
+        number.taskId = taskId
+        number.rawResponse = JSON.stringify(res.rawResponse)
+        return await this.rcsNumberRepository.save(number)
     }
 
     async delete(id: number): Promise<void> {