feat: 调整字段必填,调整抽出fetch,修改单测 (#92)
This commit is contained in:
parent
6d74d87ce2
commit
9263ad9f2f
@ -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();
|
||||
|
||||
|
@ -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[];
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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([
|
||||
|
@ -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>(ClientEncryptService);
|
||||
|
||||
messagePushingTaskService = module.get<MessagePushingTaskService>(
|
||||
MessagePushingTaskService,
|
||||
);
|
||||
|
||||
messagePushingLogService = module.get<MessagePushingLogService>(
|
||||
MessagePushingLogService,
|
||||
);
|
||||
|
||||
const pluginManager = module.get<XiaojuSurveyPluginManager>(
|
||||
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: '<p>您的手机号</p>',
|
||||
valueType: 'text',
|
||||
alias: '',
|
||||
value: ['15000000000'],
|
||||
},
|
||||
{
|
||||
questionId: 'data515',
|
||||
title: '<p>您的性别</p>',
|
||||
valueType: 'option',
|
||||
alias: '',
|
||||
value: [
|
||||
{
|
||||
alias: '',
|
||||
id: '115019',
|
||||
text: '<p>男</p>',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
questionId: 'data450',
|
||||
title: '<p>身份证</p>',
|
||||
valueType: 'text',
|
||||
alias: '',
|
||||
value: ['450111000000000000'],
|
||||
},
|
||||
{
|
||||
questionId: 'data405',
|
||||
title: '<p>地址</p>',
|
||||
valueType: 'text',
|
||||
alias: '',
|
||||
value: ['浙江省杭州市西湖区xxx'],
|
||||
},
|
||||
{
|
||||
questionId: 'data770',
|
||||
title: '<p>邮箱</p>',
|
||||
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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -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';
|
||||
|
13
server/src/utils/request.ts
Normal file
13
server/src/utils/request.ts
Normal file
@ -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();
|
||||
};
|
Loading…
Reference in New Issue
Block a user