Skip to content

Commit 8445406

Browse files
committed
updaye
1 parent 99e7083 commit 8445406

File tree

6 files changed

+69
-51
lines changed

6 files changed

+69
-51
lines changed

server/src/controllers/flows.controller.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ export class FlowController {
2424
public updateFlow = catchAsync(async (req: RequestWithUser, res) => {
2525
req.body.userId = req.user?.id;
2626
const data = await this.flowService.updateFlowById(
27-
req.params.id,
28-
req.body
27+
{
28+
fields: req.body,
29+
id: req.params.id,
30+
userId: req.user.id
31+
}
2932
);
3033
res.status(StatusCodes.OK).json({
3134
message: this.localeService.i18n().FLOW.UPDATE_SUCCESS(),
@@ -65,21 +68,6 @@ export class FlowController {
6568
res.status(StatusCodes.OK).json({ data });
6669
});
6770

68-
public addMultipleChannels = catchAsync(
69-
async (req: RequestWithUser, res) => {
70-
await this.flowService.addMultipleChannels(
71-
req.body.channelIds,
72-
req.body.flowId,
73-
req.user?.id as string
74-
);
75-
res.status(StatusCodes.OK).json({
76-
message: this.localeService
77-
.i18n()
78-
.FLOW.ADD_MULTIPLE_CHANNELS_FLOW__SUCCESS(),
79-
});
80-
}
81-
);
82-
8371
public selectFlowsForChannel = catchAsync(
8472
async (req: RequestWithUser, res) => {
8573
const data = await this.flowService.selectFlowsForChannel(

server/src/database/schema.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { IFlowSetting, IFlowVariable } from '@/interfaces/flows.interface';
12
import { createId } from '@paralleldrive/cuid2';
23
import { relations } from 'drizzle-orm';
34
import {
@@ -127,15 +128,14 @@ export const flows = pgTable('flows', {
127128
deleted: boolean('deleted').default(false),
128129
updatedAt: timestamp('updated_at'),
129130
createdAt: timestamp('created_at').defaultNow(),
130-
edges: json('edges').default([]).$type<any[]>(),
131-
nodes: json('nodes').default([]).$type<any[]>(),
132-
settings: json('settings').default([]).$type<any[]>(),
133-
variables: json('variables').default([]).$type<
134-
Array<{
135-
name: string;
136-
value: string;
137-
}>
131+
edges: json('edges').default([]).$type<
132+
Array<Record<any, any>>
138133
>(),
134+
nodes: json('nodes').default([]).$type<
135+
Array<Record<any, any>>
136+
>(),
137+
settings: json('settings').default([]).$type<Array<IFlowSetting>>(),
138+
variables: json('variables').default([]).$type<Array<IFlowVariable>>(),
139139
flows: json('flows').default([]).$type<any[]>(),
140140
publishAt: timestamp('publish_at'),
141141
});

server/src/dtos/flows.dto.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { getCurrentLocale } from '@/i18n/get-current';
2+
import { IFlowSetting, IFlowVariable } from '@/interfaces/flows.interface';
23
import { Transform } from 'class-transformer';
34
import {
45
IsArray,
@@ -16,11 +17,6 @@ export class FlowDTO {
1617
@Transform(({ value }) => value.trim())
1718
name: string;
1819

19-
@IsArray()
20-
@IsOptional()
21-
@IsObject({ each: true })
22-
diagrams: Array<Record<any, any>>;
23-
2420
@IsArray()
2521
@IsOptional()
2622
@IsObject({ each: true })
@@ -34,17 +30,22 @@ export class FlowDTO {
3430
@IsArray()
3531
@IsOptional()
3632
@IsObject({ each: true })
37-
settings: Array<Record<any, any>>;
33+
settings: Array<IFlowSetting>;
3834

3935
@IsArray()
4036
@IsOptional()
4137
@IsObject({ each: true })
42-
variables: Array<Record<any, any>>;
38+
variables: Array<IFlowVariable>;
4339

4440
@IsArray()
4541
@IsOptional()
4642
@IsObject({ each: true })
4743
flows: Array<Record<any, any>>;
44+
45+
@IsArray()
46+
@IsOptional()
47+
@IsString({ each: true })
48+
channelIds: string[];
4849
}
4950

5051
export class SelectMultipleChannelDTO {

server/src/interfaces/flows.interface.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,16 @@ export interface FlowExtend {
22
id: string;
33
name: string;
44
publishAt: Date;
5+
}
6+
7+
export interface IFlowSetting {
8+
type: string;
9+
label: string;
10+
value: any
11+
default: any
12+
}
13+
14+
export interface IFlowVariable {
15+
name: string;
16+
value: any
517
}

server/src/routes/flow.route.ts

Lines changed: 8 additions & 14 deletions
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, SelectMultipleChannelDTO } from '@/dtos/flows.dto';
3+
import { FlowDTO } 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';
@@ -41,6 +41,13 @@ export class FlowRoute implements Routes {
4141
this.controller.deleteFlow
4242
);
4343

44+
//for bot get flow by contactId
45+
this.router.get(
46+
`${ENDPOINTS.FLOW.INDEX}/:contactId`,
47+
authMiddleware,
48+
this.controller.getFlowByContactId
49+
);
50+
4451
this.router.get(
4552
`${ENDPOINTS.FLOW.GET_ONE}/:id`,
4653
authMiddleware,
@@ -54,23 +61,10 @@ export class FlowRoute implements Routes {
5461
this.controller.getAllFlows
5562
);
5663

57-
this.router.post(
58-
ENDPOINTS.FLOW.ADD_CHANNELS,
59-
validate(SelectMultipleChannelDTO, 'body'),
60-
authMiddleware,
61-
this.controller.addMultipleChannels
62-
);
63-
6464
this.router.get(
6565
ENDPOINTS.FLOW.SELECT_FLOWS_FOR_CHANNEL,
6666
authMiddleware,
6767
this.controller.selectFlowsForChannel
6868
);
69-
70-
this.router.get(
71-
`${ENDPOINTS.FLOW.INDEX}/:contactId`,
72-
authMiddleware,
73-
this.controller.getFlowByContactId
74-
);
7569
}
7670
}

server/src/services/flows.service.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { LOCALE_KEY } from '@/constants';
22
import { db } from '@/database/db';
33
import { channels, flows } from '@/database/schema';
4-
import { TNewFlow, TUpdateFlow } from '@/database/types';
4+
import { TNewFlow } from '@/database/types';
5+
import { FlowDTO } from '@/dtos/flows.dto';
56
import { PagingDTO } from '@/dtos/paging.dto';
67
import { HttpException } from '@/exceptions/http-exception';
78
import { LocaleService } from '@/i18n/ctx';
89
import { FlowExtend } from '@/interfaces/flows.interface';
910
import { Paging } from '@/interfaces/paging.interface';
1011
import { and, asc, desc, eq, isNotNull, like, ne, notExists, sql } from 'drizzle-orm';
1112
import { StatusCodes } from 'http-status-codes';
13+
import { omit } from 'lodash';
1214
import { Inject, Service } from 'typedi';
1315
import { ChannelService } from './channels.service';
1416

@@ -47,7 +49,12 @@ export class FlowService {
4749
return newFlow;
4850
}
4951

50-
public async updateFlowById(id: string, fields: TUpdateFlow) {
52+
public async updateFlowById({ fields, id, userId }: {
53+
userId: string;
54+
id: string;
55+
fields: FlowDTO
56+
}) {
57+
5158
const flowExisted = await db.query.flows.findFirst({
5259
where: eq(flows.id, id),
5360
});
@@ -63,7 +70,7 @@ export class FlowService {
6370
where: and(
6471
ne(flows.id, id),
6572
eq(flows.name, fields.name),
66-
eq(flows.userId, fields.userId),
73+
eq(flows.userId, userId),
6774
eq(flows.deleted, false)
6875
),
6976
});
@@ -75,11 +82,27 @@ export class FlowService {
7582
);
7683
}
7784

78-
fields.updatedAt = new Date();
85+
// handle update flowId for channel
86+
const addFlowIdForChannels = await this.chanelService.updateFlowId(
87+
fields.channelIds,
88+
flowExisted.id
89+
);
90+
91+
if (!addFlowIdForChannels) {
92+
throw new HttpException(
93+
StatusCodes.BAD_REQUEST,
94+
this.localeService.i18n().FLOW.ADD_MULTIPLE_CHANNELS_FLOW__FAILED()
95+
);
96+
}
97+
7998

8099
const [updateFlow] = await db
81100
.update(flows)
82-
.set(fields)
101+
.set({
102+
...omit(fields, ['channelIds']),
103+
updatedAt: new Date()
104+
105+
})
83106
.where(eq(flows.id, id))
84107
.returning();
85108

0 commit comments

Comments
 (0)