Skip to content

Commit acc057b

Browse files
committed
add factory design pattern for channel
1 parent bf38608 commit acc057b

File tree

8 files changed

+62
-39
lines changed

8 files changed

+62
-39
lines changed

client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"@tiptap/extension-text-align": "^2.2.4",
5555
"@tiptap/extension-text-style": "^2.2.4",
5656
"@tiptap/extension-underline": "^2.2.4",
57+
"@tiptap/pm": "^2.2.4",
5758
"@tiptap/react": "^2.2.4",
5859
"@tiptap/starter-kit": "^2.2.4",
5960
"@xyflow/react": "^12.0.0-next.11",

server/src/channels/base.channel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@ export class BaseChannel {
5959
initConversationId(userId: string) {
6060
return this.contactId + '-' + userId
6161
}
62-
}
62+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { AUTO_REGISTER_WEBHOOK } from "@/config";
2+
import { ChannelInfo } from "@/interfaces/channels.interface";
3+
import { LineChannel } from "./line.channel";
4+
import { MessengerChannel } from "./messenger.channel";
5+
import { WebChannel } from "./web.channel";
6+
7+
export class Creator {
8+
static createChannel(channel: ChannelInfo) {
9+
try {
10+
const { id, contactId, contactName, channelType, credentials } = channel;
11+
12+
switch (channelType) {
13+
case 'MSG':
14+
return new MessengerChannel(
15+
id,
16+
contactId,
17+
contactName,
18+
channelType,
19+
credentials,
20+
)
21+
case 'LIN':
22+
const linChannel = new LineChannel(
23+
id,
24+
contactId,
25+
contactName,
26+
channelType,
27+
credentials,
28+
)
29+
if (AUTO_REGISTER_WEBHOOK == '1') {
30+
linChannel.registerWebhook();
31+
}
32+
return linChannel
33+
case 'WEB':
34+
return new WebChannel(
35+
id,
36+
contactId,
37+
contactName,
38+
channelType,
39+
credentials,
40+
)
41+
default:
42+
throw new Error(`Does not support channel type ${channel.channelType}`);
43+
break;
44+
}
45+
} catch (err) {
46+
throw err;
47+
}
48+
}
49+
}

server/src/config/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ export const {
3333
MAIL_USER,
3434
MAIL_PASS,
3535
API_KEY,
36+
AUTO_REGISTER_WEBHOOK
3637
} = process.env

server/src/controllers/flows.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ export class FlowController {
7777

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

server/src/routes/flow.route.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ export class FlowRoute implements Routes {
5050
//for bot get flow by contactId
5151
this.router.get(
5252
`${ENDPOINTS.FLOW.INDEX}/:contactId`,
53-
authMiddleware,
5453
this.controller.getFlowByContactId,
5554
)
5655

server/src/services/channels.service.ts

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { LineChannel } from '@/channels/line.channel'
2-
import { MessengerChannel } from '@/channels/messenger.channel'
1+
import { Creator } from '@/channels/creator.channel'
32
import { LOCALE_KEY } from '@/constants'
43
import { db } from '@/database/db'
54
import { channelTypes, channels } from '@/database/schema'
@@ -238,39 +237,12 @@ export class ChannelService {
238237
}
239238

240239
initChannel(channel: ChannelInfo) {
241-
const { id, contactId, contactName, channelType, credentials } = channel
242-
243-
logger.info(`[Init channel] ${channelType} - ${contactName} ${contactId}`)
244-
245-
switch (channelType) {
246-
case 'MSG':
247-
return new MessengerChannel(
248-
id,
249-
contactId,
250-
contactName,
251-
channelType,
252-
credentials,
253-
)
254-
case 'LIN':
255-
const linChannel = new LineChannel(
256-
id,
257-
contactId,
258-
contactName,
259-
channelType,
260-
credentials,
261-
)
262-
263-
linChannel.registerWebhook()
264-
265-
return linChannel
266-
default:
267-
logger.info(
268-
`[Init channel] Does not support channel type ${channel.channelType}`,
269-
)
270-
break
240+
try {
241+
logger.info(`[Init channel] ${channel.channelType} - ${channel.contactName} ${channel.contactId}`)
242+
return Creator.createChannel(channel);
243+
} catch (err) {
244+
logger.info(`[Init channel] ${err.message}`);
271245
}
272-
273-
return channel
274246
}
275247

276248
async initChannels() {

server/src/services/flows.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class FlowService {
2020
constructor(
2121
@Inject(LOCALE_KEY) private readonly localeService: LocaleService,
2222
private readonly chanelService: ChannelService,
23-
) {}
23+
) { }
2424

2525
public async create(fields: TNewFlow) {
2626
const flowExisted = await db.query.flows.findFirst({
@@ -264,7 +264,7 @@ export class FlowService {
264264
const flow = db.query.flows.findFirst({
265265
where: and(
266266
eq(flows.deleted, false),
267-
isNotNull(flows.publishAt),
267+
// isNotNull(flows.publishAt),
268268
isNotNull(
269269
db
270270
.select({
@@ -282,6 +282,7 @@ export class FlowService {
282282
this.localeService.i18n().FLOW.NOT_FOUND(),
283283
)
284284
}
285+
console.log(flow);
285286
return flow
286287
}
287288
}

0 commit comments

Comments
 (0)