Skip to content

Commit d3b6acb

Browse files
committed
update
1 parent b89834b commit d3b6acb

File tree

8 files changed

+38
-11
lines changed

8 files changed

+38
-11
lines changed

server/src/channels/base.channel.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class BaseChannel {
2020
this.channelType = channelType
2121
}
2222

23-
public async postMessageToBot({ userId, message = '', data }) {
23+
public async postMessageToBot({ userId, message = '', data, isTest }) {
2424
const uid = this.initConversationId(userId)
2525
try {
2626
const postMsg = await axios({
@@ -41,6 +41,7 @@ export class BaseChannel {
4141
type: 'message',
4242
id: uid,
4343
channelId: this.channelType,
44+
isTest,
4445
serviceUrl: PUBLIC_DOMAIN,
4546
},
4647
})

server/src/channels/line.channel.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export class LineChannel extends BaseChannel {
8888
userId: source.userId,
8989
message: message.text,
9090
data: null,
91+
isTest: false,
9192
})
9293
}
9394
} catch (e) { }

server/src/channels/messenger.channel.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export class MessengerChannel extends BaseChannel {
8686
userId: senderId,
8787
message: messageText || payload,
8888
data: null,
89+
isTest: false,
8990
})
9091
})
9192
})
@@ -96,6 +97,7 @@ export class MessengerChannel extends BaseChannel {
9697
userId,
9798
message: 'ADDRESS',
9899
data: { USER_INFORMATION: Helper.arrayToObj(address) },
100+
isTest: false,
99101
})
100102
}
101103

server/src/controllers/flows.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class FlowController {
7676
)
7777

7878
public getFlowByContactId = catchAsync(async (req: RequestWithUser, res) => {
79-
const data = await this.flowService.getFlowByContactId(req.params.contactId)
79+
const data = await this.flowService.getFlowByContactId(req.params.contactId, req.body.isTest)
8080
res.status(StatusCodes.OK).send(data);
8181
})
8282
}

server/src/dtos/flows.dto.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { IFlowSetting, IFlowVariable } from '@/interfaces/flows.interface'
33
import { Transform } from 'class-transformer'
44
import {
55
IsArray,
6+
IsBoolean,
67
IsNotEmpty,
78
IsObject,
89
IsOptional,
@@ -65,3 +66,11 @@ export class DeleteFlowDTO {
6566
@IsString({ each: true })
6667
ids: string[]
6768
}
69+
70+
export class TestYourBotDTO {
71+
@IsBoolean()
72+
@IsNotEmpty({
73+
message: () => getCurrentLocale().VALIDATE.REQUIRED({ field: 'Is Test' }),
74+
})
75+
isTest: boolean
76+
}

server/src/routes/flow.route.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ENDPOINTS } from '@/constants'
22
import { FlowController } from '@/controllers/flows.controller'
3-
import { FlowDTO } from '@/dtos/flows.dto'
3+
import { FlowDTO, TestYourBotDTO } from '@/dtos/flows.dto'
44
import { PagingDTO } from '@/dtos/paging.dto'
55
import { authMiddleware } from '@/middlewares/auth.middleware'
66
import { validate } from '@/middlewares/validation.middleware'
@@ -50,6 +50,7 @@ export class FlowRoute implements Routes {
5050
//for bot get flow by contactId
5151
this.router.get(
5252
`${ENDPOINTS.FLOW.INDEX}/:contactId`,
53+
validate(TestYourBotDTO, 'body'),
5354
this.controller.getFlowByContactId,
5455
)
5556

server/src/services/flows.service.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { LocaleService } from '@/i18n/ctx'
99
import { FlowExtend } from '@/interfaces/flows.interface'
1010
import { Paging } from '@/interfaces/paging.interface'
1111
import { logger } from '@/utils/logger'
12-
import { and, asc, desc, eq, like, ne, sql } from 'drizzle-orm'
12+
import { and, asc, desc, eq, isNotNull, like, ne, sql } from 'drizzle-orm'
1313
import { StatusCodes } from 'http-status-codes'
1414
import { omit } from 'lodash'
1515
import { Inject, Service } from 'typedi'
@@ -265,13 +265,25 @@ export class FlowService {
265265

266266
return result
267267
}
268-
public async getFlowByContactId(contactId: string) {
268+
public async getFlowByContactId(contactId: string, isTest: boolean) {
269269

270-
const channel = await this.chanelService.findOneByContactId(contactId)
270+
const channel = await this.chanelService.findOneByContactId(contactId);
271+
let flow = null;
271272

272-
const flow = await db.query.flows.findFirst({
273-
where: eq(flows.id, channel?.flowId),
274-
})
273+
if (isTest) {
274+
flow = await db.query.flows.findFirst({
275+
where: eq(flows.id, channel?.flowId),
276+
});
277+
278+
}
279+
else {
280+
flow = await db.query.flows.findFirst({
281+
where: and(
282+
eq(flows.id, channel?.flowId),
283+
isNotNull(flow.publishAt)
284+
),
285+
});
286+
}
275287

276288
if (!flow) {
277289
throw new HttpException(

server/src/services/socket.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ export class SocketService {
2121
}
2222

2323
private async handleIncomingMessage(io: Socket, data: any) {
24-
const { address, message } = data
24+
const { address, message, isTest } = data
25+
console.log('socket data:' + JSON.stringify(data));
2526

2627
if (!address || !message) return
2728

@@ -47,7 +48,7 @@ export class SocketService {
4748
credentials,
4849
)
4950

50-
await webChannel.postMessageToBot({ userId, message, data: '' })
51+
await webChannel.postMessageToBot({ userId, message, data: '', isTest })
5152
}
5253
}
5354

0 commit comments

Comments
 (0)