diff --git a/server/src/modules/message/__test/messagePushingTask.service.spec.ts b/server/src/modules/message/__test/messagePushingTask.service.spec.ts index 5e171849..4cf9a834 100644 --- a/server/src/modules/message/__test/messagePushingTask.service.spec.ts +++ b/server/src/modules/message/__test/messagePushingTask.service.spec.ts @@ -1,14 +1,19 @@ import { Test, TestingModule } from '@nestjs/testing'; -import { MessagePushingTaskService } from '../services/messagePushingTask.service'; + import { MongoRepository } from 'typeorm'; import { getRepositoryToken } from '@nestjs/typeorm'; -import { MessagePushingTask } from 'src/models/messagePushingTask.entity'; +import { ObjectId } from 'mongodb'; + +import { MessagePushingTaskService } from '../services/messagePushingTask.service'; +import { MessagePushingLogService } from '../services/messagePushingLog.service'; + import { CreateMessagePushingTaskDto } from '../dto/createMessagePushingTask.dto'; import { UpdateMessagePushingTaskDto } from '../dto/updateMessagePushingTask.dto'; -import { ObjectId } from 'mongodb'; + import { RECORD_STATUS } from 'src/enums'; import { MESSAGE_PUSHING_TYPE } from 'src/enums/messagePushing'; import { MESSAGE_PUSHING_HOOK } from 'src/enums/messagePushing'; +import { MessagePushingTask } from 'src/models/messagePushingTask.entity'; describe('MessagePushingTaskService', () => { let service: MessagePushingTaskService; @@ -22,6 +27,12 @@ describe('MessagePushingTaskService', () => { provide: getRepositoryToken(MessagePushingTask), useClass: MongoRepository, }, + { + provide: MessagePushingLogService, + useValue: { + createPushingLog: jest.fn(), + }, + }, ], }).compile(); diff --git a/server/src/modules/message/dto/createMessagePushingTask.dto.ts b/server/src/modules/message/dto/createMessagePushingTask.dto.ts index ab53c869..fbd690d5 100644 --- a/server/src/modules/message/dto/createMessagePushingTask.dto.ts +++ b/server/src/modules/message/dto/createMessagePushingTask.dto.ts @@ -5,18 +5,26 @@ import { } from 'src/enums/messagePushing'; export class CreateMessagePushingTaskDto { - @ApiProperty({ description: '任务名称' }) + @ApiProperty({ description: '任务名称', required: true }) name: string; - @ApiProperty({ description: '任务类型' }) - type: MESSAGE_PUSHING_TYPE; + @ApiProperty({ description: '任务类型', required: false, default: 'http' }) + type?: MESSAGE_PUSHING_TYPE; - @ApiProperty({ description: '推送的http链接' }) + @ApiProperty({ description: '推送的http链接', required: true }) pushAddress: string; - @ApiProperty({ description: '触发时机' }) - triggerHook: MESSAGE_PUSHING_HOOK; + @ApiProperty({ + description: '触发时机', + required: false, + default: 'response_inserted', + }) + triggerHook?: MESSAGE_PUSHING_HOOK; - @ApiProperty({ description: '包含问卷id' }) + @ApiProperty({ + description: '绑定的问卷id,初始可以为空后续再绑定', + required: false, + default: [], + }) surveys?: string[]; } diff --git a/server/src/modules/message/services/messagePushingTask.service.ts b/server/src/modules/message/services/messagePushingTask.service.ts index 2bf1e8e6..14aef5bf 100644 --- a/server/src/modules/message/services/messagePushingTask.service.ts +++ b/server/src/modules/message/services/messagePushingTask.service.ts @@ -9,7 +9,7 @@ import { ObjectId } from 'mongodb'; import { RECORD_STATUS } from 'src/enums'; import { MESSAGE_PUSHING_TYPE } from 'src/enums/messagePushing'; import { MessagePushingLogService } from './messagePushingLog.service'; -import fetch from 'node-fetch'; +import { httpPost } from 'src/utils/request'; @Injectable() export class MessagePushingTaskService { @@ -167,19 +167,14 @@ export class MessagePushingTaskService { switch (task.type) { case MESSAGE_PUSHING_TYPE.HTTP: { try { - const res = await fetch(task.pushAddress, { - method: 'POST', - headers: { - Accept: 'application/json, */*', - 'Content-Type': 'application/json', - }, - body: JSON.stringify(sendData), + const res = await httpPost({ + url: task.pushAddress, + body: sendData, }); - const response = await res.json(); await this.messagePushingLogService.createPushingLog({ taskId: task._id.toString(), request: sendData, - response: response, + response: res, status: res.status, }); } catch (error) { diff --git a/server/src/modules/survey/survey.module.ts b/server/src/modules/survey/survey.module.ts index 8236d35d..58528219 100644 --- a/server/src/modules/survey/survey.module.ts +++ b/server/src/modules/survey/survey.module.ts @@ -18,6 +18,7 @@ import { SurveyHistory } from 'src/models/surveyHistory.entity'; import { SurveyMeta } from 'src/models/surveyMeta.entity'; import { SurveyResponse } from 'src/models/surveyResponse.entity'; import { Word } from 'src/models/word.entity'; +import { PluginManagerProvider } from 'src/securityPlugin/pluginManager.provider'; import { DataStatisticService } from './services/dataStatistic.service'; import { SurveyConfService } from './services/surveyConf.service'; @@ -25,8 +26,6 @@ import { SurveyHistoryService } from './services/surveyHistory.service'; import { SurveyMetaService } from './services/surveyMeta.service'; import { ContentSecurityService } from './services/contentSecurity.service'; -import { PluginManagerProvider } from 'src/securityPlugin/pluginManager.provider'; - @Module({ imports: [ TypeOrmModule.forFeature([ diff --git a/server/src/modules/surveyResponse/__test/surveyResponse.controller.spec.ts b/server/src/modules/surveyResponse/__test/surveyResponse.controller.spec.ts index 910c80c2..d0b656f5 100644 --- a/server/src/modules/surveyResponse/__test/surveyResponse.controller.spec.ts +++ b/server/src/modules/surveyResponse/__test/surveyResponse.controller.spec.ts @@ -5,21 +5,19 @@ import { cloneDeep } from 'lodash'; import { mockResponseSchema } from './mockResponseSchema'; import { SurveyResponseController } from '../controllers/surveyResponse.controller'; + import { ResponseSchemaService } from '../services/responseScheme.service'; import { CounterService } from '../services/counter.service'; import { SurveyResponseService } from '../services/surveyResponse.service'; import { ClientEncryptService } from '../services/clientEncrypt.service'; -import { MessagePushingTaskService } from '../../survey/services/messagePushingTask.service'; -import { MessagePushingLogService } from '../services/messagePushingLog.service'; +import { MessagePushingTaskService } from 'src/modules/message/services/messagePushingTask.service'; import { PluginManagerProvider } from 'src/securityPlugin/pluginManager.provider'; import { XiaojuSurveyPluginManager } from 'src/securityPlugin/pluginManager'; import { HttpException } from 'src/exceptions/httpException'; import { SurveyNotFoundException } from 'src/exceptions/surveyNotFoundException'; import { ResponseSecurityPlugin } from 'src/securityPlugin/responseSecurityPlugin'; -import { MessagePushingTask } from 'src/models/messagePushingTask.entity'; -import { MESSAGE_PUSHING_TYPE } from 'src/enums/messagePushing'; import { RECORD_STATUS } from 'src/enums'; import { SurveyResponse } from 'src/models/surveyResponse.entity'; @@ -77,8 +75,6 @@ describe('SurveyResponseController', () => { let responseSchemaService: ResponseSchemaService; let surveyResponseService: SurveyResponseService; let clientEncryptService: ClientEncryptService; - let messagePushingTaskService: MessagePushingTaskService; - let messagePushingLogService: MessagePushingLogService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ @@ -113,19 +109,13 @@ describe('SurveyResponseController', () => { .mockResolvedValue(mockClientEncryptInfo), }, }, + PluginManagerProvider, { provide: MessagePushingTaskService, useValue: { - findAll: jest.fn(), + runResponseDataPush: jest.fn(), }, }, - { - provide: MessagePushingLogService, - useValue: { - createPushingLog: jest.fn(), - }, - }, - PluginManagerProvider, ], }).compile(); @@ -139,14 +129,6 @@ describe('SurveyResponseController', () => { clientEncryptService = module.get(ClientEncryptService); - messagePushingTaskService = module.get( - MessagePushingTaskService, - ); - - messagePushingLogService = module.get( - MessagePushingLogService, - ); - const pluginManager = module.get( XiaojuSurveyPluginManager, ); @@ -214,9 +196,6 @@ describe('SurveyResponseController', () => { jest .spyOn(clientEncryptService, 'deleteEncryptInfo') .mockResolvedValueOnce(undefined); - jest - .spyOn(controller, 'sendSurveyResponseMessage') - .mockReturnValueOnce(undefined); const result = await controller.createResponse(reqBody); @@ -320,101 +299,4 @@ describe('SurveyResponseController', () => { ); }); }); - - describe('sendSurveyResponseMessage', () => { - it('should send survey response message', async () => { - const sendData = { - surveyId: '65f29f3192862d6a9067ad1c', - surveyPath: 'EBzdmnSp', - surveyResponseId: '65fc2dd77f4520858046e129', - data: [ - { - questionId: 'data458', - title: '

您的手机号

', - valueType: 'text', - alias: '', - value: ['15000000000'], - }, - { - questionId: 'data515', - title: '

您的性别

', - valueType: 'option', - alias: '', - value: [ - { - alias: '', - id: '115019', - text: '

', - }, - ], - }, - { - questionId: 'data450', - title: '

身份证

', - valueType: 'text', - alias: '', - value: ['450111000000000000'], - }, - { - questionId: 'data405', - title: '

地址

', - valueType: 'text', - alias: '', - value: ['浙江省杭州市西湖区xxx'], - }, - { - questionId: 'data770', - title: '

邮箱

', - valueType: 'text', - alias: '', - value: ['123456@qq.com'], - }, - ], - }; - - const mockTasks = [ - { - _id: new ObjectId('65fc31dbfd09a5d0619c3b74'), - name: 'Task 1', - type: MESSAGE_PUSHING_TYPE.HTTP, - pushAddress: 'success_url', - }, - { - _id: new ObjectId('65fc31dbfd09a5d0619c3b75'), - name: 'Task 2', - type: MESSAGE_PUSHING_TYPE.HTTP, - pushAddress: 'fail_url', - }, - ] as MessagePushingTask[]; - jest - .spyOn(messagePushingTaskService, 'findAll') - .mockReturnValue(Promise.resolve(mockTasks)); - - const mockFetch = jest.fn().mockImplementation((url) => { - if (url === 'success_url') { - return { - json: () => { - return Promise.resolve({ code: 200, msg: '提交成功' }); - }, - status: 200, - }; - } else { - return { - data: 'failed', - status: 501, - }; - } - }); - jest.mock('node-fetch', () => jest.fn().mockImplementation(mockFetch)); - - await controller.sendSurveyResponseMessage({ - sendData, - surveyId: mockResponseSchema.pageId, - }); - - expect(messagePushingTaskService.findAll).toHaveBeenCalled(); - - expect(messagePushingLogService.createPushingLog).toHaveBeenCalled(); - }); - }); }); diff --git a/server/src/modules/surveyResponse/surveyResponse.module.ts b/server/src/modules/surveyResponse/surveyResponse.module.ts index 50c8f941..0c95105d 100644 --- a/server/src/modules/surveyResponse/surveyResponse.module.ts +++ b/server/src/modules/surveyResponse/surveyResponse.module.ts @@ -6,7 +6,6 @@ import { ResponseSchemaService } from './services/responseScheme.service'; import { SurveyResponseService } from './services/surveyResponse.service'; import { CounterService } from './services/counter.service'; import { ClientEncryptService } from './services/clientEncrypt.service'; -// import { MessagePushingTaskService } from '../messagePushing/services/messagePushingTask.service'; import { ResponseSchema } from 'src/models/responseSchema.entity'; import { Counter } from 'src/models/counter.entity'; diff --git a/server/src/utils/request.ts b/server/src/utils/request.ts new file mode 100644 index 00000000..4d5cd483 --- /dev/null +++ b/server/src/utils/request.ts @@ -0,0 +1,13 @@ +import fetch from 'node-fetch'; + +export const httpPost = async ({ url, body }) => { + const res = await fetch(url, { + method: 'POST', + headers: { + Accept: 'application/json, */*', + 'Content-Type': 'application/json', + }, + body: JSON.stringify(body), + }); + return res.json(); +};