|
|
@@ -1,5 +1,5 @@
|
|
|
import { PhoneListService } from './../phone-list/phone-list.service'
|
|
|
-import { forwardRef, Inject, Injectable, Logger, OnModuleInit } from '@nestjs/common'
|
|
|
+import { forwardRef, Inject, Injectable, InternalServerErrorException, Logger, OnModuleInit } from '@nestjs/common'
|
|
|
import { InjectRepository } from '@nestjs/typeorm'
|
|
|
import { Task, TaskStatus } from './entities/task.entity'
|
|
|
import { In, Repository } from 'typeorm'
|
|
|
@@ -40,8 +40,7 @@ export class TaskService implements OnModuleInit {
|
|
|
private readonly sysConfigService: SysConfigService,
|
|
|
private readonly balanceService: BalanceService,
|
|
|
private readonly userService: UsersService
|
|
|
- ) {
|
|
|
- }
|
|
|
+ ) {}
|
|
|
|
|
|
onModuleInit() {
|
|
|
this.lock.acquire('dispatchTask', async () => {
|
|
|
@@ -60,6 +59,13 @@ export class TaskService implements OnModuleInit {
|
|
|
private taskControllers: { [key: number]: AbortController } = {}
|
|
|
|
|
|
async findAllTask(req: PageRequest<Task>): Promise<Pagination<Task>> {
|
|
|
+ console.log(
|
|
|
+ JSON.stringify({
|
|
|
+ where: {
|
|
|
+ name: In(['asdf'])
|
|
|
+ }
|
|
|
+ })
|
|
|
+ )
|
|
|
return await paginate<Task>(this.taskRepository, req.page, req.search)
|
|
|
}
|
|
|
|
|
|
@@ -69,39 +75,62 @@ export class TaskService implements OnModuleInit {
|
|
|
|
|
|
async createTask(task: Task): Promise<Task> {
|
|
|
const phones = await this.phoneListService.findPhoneByListId(task.listId)
|
|
|
- if (phones.length === 0) {
|
|
|
- return null
|
|
|
- }
|
|
|
- let dynamicMessageList = null
|
|
|
- if (task.dynamicMessage && task.dynamicMessage !== '') {
|
|
|
- dynamicMessageList = task.dynamicMessage.split(',')
|
|
|
+ if (!phones || phones.length === 0) {
|
|
|
+ throw new InternalServerErrorException('请先上传料子')
|
|
|
}
|
|
|
+ task.total = phones.length
|
|
|
task = await this.taskRepository.save(task)
|
|
|
|
|
|
- await this.phoneRepository.manager.insert(
|
|
|
- TaskItem,
|
|
|
- this.taskItemRepository.create(
|
|
|
+ await this.taskItemRepository
|
|
|
+ .createQueryBuilder()
|
|
|
+ .insert()
|
|
|
+ .values(
|
|
|
phones.map((phone) => {
|
|
|
const taskItem = new TaskItem()
|
|
|
taskItem.taskId = task.id
|
|
|
taskItem.number = phone.number
|
|
|
- if (dynamicMessageList !== null && task.message.includes('[#random#]')) {
|
|
|
- taskItem.message = task.message.replace(
|
|
|
- '[#random#]',
|
|
|
- dynamicMessageList[Math.floor(Math.random() * dynamicMessageList.length)]
|
|
|
- )
|
|
|
- } else {
|
|
|
- taskItem.message = task.message
|
|
|
- }
|
|
|
+ taskItem.message = task.message
|
|
|
+ task.dynamicMessage?.forEach((dm) => {
|
|
|
+ if (dm.key && dm.values?.length > 0) {
|
|
|
+ taskItem.message = taskItem.message.replaceAll(
|
|
|
+ `${dm.key}`,
|
|
|
+ dm.values[Math.floor(Math.random() * dm.values.length)]
|
|
|
+ )
|
|
|
+ }
|
|
|
+ })
|
|
|
taskItem.status = TaskStatus.IDLE
|
|
|
return taskItem
|
|
|
})
|
|
|
)
|
|
|
- )
|
|
|
+ .updateEntity(false)
|
|
|
+ .execute()
|
|
|
|
|
|
return task
|
|
|
}
|
|
|
|
|
|
+ async updateTask(id: number, user: Users, data: Task) {
|
|
|
+ if (!id) throw new Error('Task id is required')
|
|
|
+ const old = await this.taskRepository.findOneOrFail({
|
|
|
+ where: { id }
|
|
|
+ })
|
|
|
+
|
|
|
+ if (old.userId !== user.id && !user.roles.includes(Role.Admin)) {
|
|
|
+ throw new Error('No permission to update task')
|
|
|
+ }
|
|
|
+
|
|
|
+ return await this.taskRepository.update(
|
|
|
+ { id },
|
|
|
+ {
|
|
|
+ rcsWait: data.rcsWait,
|
|
|
+ rcsInterval: data.rcsInterval,
|
|
|
+ cleanCount: data.cleanCount,
|
|
|
+ requestNumberInterval: data.requestNumberInterval,
|
|
|
+ checkConnection: data.checkConnection,
|
|
|
+ channels: data.channels
|
|
|
+ }
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
async balanceVerification(id: number) {
|
|
|
const task = await this.taskRepository.findOneBy({ id })
|
|
|
// 获取用户信息
|