Jelajahi Sumber

feat(phone-list): 在创建电话列表时添加国家代码验证

xiongzhu 8 bulan lalu
induk
melakukan
567f4dbb6f
1 mengubah file dengan 9 tambahan dan 1 penghapusan
  1. 9 1
      src/phone-list/phone-list.service.ts

+ 9 - 1
src/phone-list/phone-list.service.ts

@@ -1,4 +1,4 @@
-import { Injectable, Req, ServiceUnavailableException } from '@nestjs/common'
+import { Injectable, InternalServerErrorException, Req, ServiceUnavailableException } from '@nestjs/common'
 import { InjectRepository } from '@nestjs/typeorm'
 import { PhoneList } from './entities/phone-list.entity'
 import { DataSource, In, Repository } from 'typeorm'
@@ -11,6 +11,7 @@ import axios from 'axios'
 import { FileService } from '../file/file.service'
 import { encryptData, getFileMd5FromUrl } from '../utils/crypto'
 import * as yauzl from 'yauzl'
+import { getCountryDataList } from 'countries-list'
 
 const token =
     'QYiuiRiEnGWQk/QutmXyo154KeBvRKpg7Eqa/6N6FPgEUlyn5hJV1UmkK2yFRgVMEjQ7ByEAg/2jYFjCleNMaaPUdJfFCr5d9Nndj3b4mx4QM6iVIxxt9B6Phl6ZJ7gVPKtxGE+XTFJepWxaKs9VZSgLI9ORZ/rDD2HZhKQtOm0='
@@ -87,6 +88,13 @@ export class PhoneListService {
 
     async createPhoneList(phoneList: PhoneList, type?: number): Promise<PhoneList> {
         phoneList.type = type ? type : 0
+        if (
+            !getCountryDataList()
+                .map((c) => c.iso2 as string)
+                .includes(phoneList.country)
+        ) {
+            throw new InternalServerErrorException('Country code wrong!')
+        }
         return await this.phoneListRepository.save(phoneList)
     }