Skip to content

Commit 596cf6a

Browse files
committed
add api intent
1 parent 9501752 commit 596cf6a

File tree

18 files changed

+1474
-187
lines changed

18 files changed

+1474
-187
lines changed

server/model.nlp

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

server/src/config/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ export const {
3232
BOT_ENDPOINT,
3333
MAIL_USER,
3434
MAIL_PASS,
35+
API_KEY,
3536
} = process.env

server/src/constants/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ export const ENDPOINTS = {
5858
},
5959
INTENT: {
6060
INDEX: '/intent',
61-
}
61+
PREDICT: '/intent/predict',
62+
},
6263
}
6364

6465
export const LOCALE_KEY = 'lang'
Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,69 @@
1-
import { LOCALE_KEY } from "@/constants"
2-
import { LocaleService } from "@/i18n/ctx"
3-
import { RequestWithUser } from "@/interfaces/auth.interface"
4-
import { IntentService } from "@/services/intent.service"
5-
import { catchAsync } from "@/utils/catch-async"
6-
import { StatusCodes } from "http-status-codes"
7-
import Container from "typedi"
1+
import { LOCALE_KEY } from '@/constants'
2+
import { PagingDTO } from '@/dtos/paging.dto'
3+
import { LocaleService } from '@/i18n/ctx'
4+
import { RequestWithUser } from '@/interfaces/auth.interface'
5+
import { IntentService } from '@/services/intent.service'
6+
import { catchAsync } from '@/utils/catch-async'
7+
import { plainToClass } from 'class-transformer'
8+
import { StatusCodes } from 'http-status-codes'
9+
import Container from 'typedi'
810

911
export class IntentController {
10-
public intentService = Container.get(IntentService)
11-
public localeService = Container.get<LocaleService>(LOCALE_KEY)
12-
13-
public createIntent = catchAsync(async (req: RequestWithUser, res) => {
14-
req.body.userId = req.user?.id
15-
const data = await this.intentService.create(req.body)
16-
res.status(StatusCodes.OK).json({
17-
message: this.localeService.i18n().INTENT.CREATE_SUCCESS(),
18-
data,
19-
})
12+
public intentService = Container.get(IntentService)
13+
public localeService = Container.get<LocaleService>(LOCALE_KEY)
14+
15+
public createIntent = catchAsync(async (req: RequestWithUser, res) => {
16+
const data = await this.intentService.create({
17+
fields: req.body,
18+
userId: req.user?.id as string,
19+
})
20+
res.status(StatusCodes.OK).json({
21+
message: this.localeService.i18n().INTENT.CREATE_SUCCESS(),
22+
data,
23+
})
24+
})
25+
26+
public predictIntent = catchAsync(async (req: RequestWithUser, res) => {
27+
req.body.userId = req.user?.id
28+
const data = await this.intentService.PredictTrainIntent(req.body)
29+
res.status(StatusCodes.OK).json({
30+
message: this.localeService.i18n().INTENT.PREDICT_SUCCESS(),
31+
data,
32+
})
33+
})
34+
35+
public getById = catchAsync(async (req, res) => {
36+
const data = await this.intentService.getById(req.params.id)
37+
res.status(StatusCodes.OK).json({ data })
38+
})
39+
40+
public updateIntent = catchAsync(async (req: RequestWithUser, res) => {
41+
const data = await this.intentService.updateById({
42+
id: req.params.id,
43+
fields: req.body,
44+
userId: req.user?.id as string,
45+
})
46+
47+
res.status(StatusCodes.OK).json({
48+
message: this.localeService.i18n().INTENT.UPDATE_SUCCESS(),
49+
data,
50+
})
51+
})
52+
53+
public getAllIntents = catchAsync(async (req: RequestWithUser, res) => {
54+
const paging = plainToClass(PagingDTO, req.query)
55+
const data = await this.intentService.getAllIntents(
56+
paging,
57+
req.user?.id as string,
58+
)
59+
res.status(StatusCodes.OK).json({ data })
60+
})
61+
62+
public deleteIntent = catchAsync(async (req, res) => {
63+
const data = await this.intentService.deleteById(req.params.id)
64+
res.status(StatusCodes.OK).json({
65+
message: this.localeService.i18n().INTENT.DELETE_SUCCESS(),
66+
data,
2067
})
21-
}
68+
})
69+
}

server/src/database/migrations/meta/0007_snapshot.json

Lines changed: 18 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,12 @@
4141
"channel_types_name_unique": {
4242
"name": "channel_types_name_unique",
4343
"nullsNotDistinct": false,
44-
"columns": [
45-
"name"
46-
]
44+
"columns": ["name"]
4745
},
4846
"channel_types_description_unique": {
4947
"name": "channel_types_description_unique",
5048
"nullsNotDistinct": false,
51-
"columns": [
52-
"description"
53-
]
49+
"columns": ["description"]
5450
}
5551
}
5652
},
@@ -133,38 +129,26 @@
133129
"name": "channels_channel_type_id_channel_types_id_fk",
134130
"tableFrom": "channels",
135131
"tableTo": "channel_types",
136-
"columnsFrom": [
137-
"channel_type_id"
138-
],
139-
"columnsTo": [
140-
"id"
141-
],
132+
"columnsFrom": ["channel_type_id"],
133+
"columnsTo": ["id"],
142134
"onDelete": "no action",
143135
"onUpdate": "no action"
144136
},
145137
"channels_user_id_users_id_fk": {
146138
"name": "channels_user_id_users_id_fk",
147139
"tableFrom": "channels",
148140
"tableTo": "users",
149-
"columnsFrom": [
150-
"user_id"
151-
],
152-
"columnsTo": [
153-
"id"
154-
],
141+
"columnsFrom": ["user_id"],
142+
"columnsTo": ["id"],
155143
"onDelete": "cascade",
156144
"onUpdate": "no action"
157145
},
158146
"channels_flow_id_flows_id_fk": {
159147
"name": "channels_flow_id_flows_id_fk",
160148
"tableFrom": "channels",
161149
"tableTo": "flows",
162-
"columnsFrom": [
163-
"flow_id"
164-
],
165-
"columnsTo": [
166-
"id"
167-
],
150+
"columnsFrom": ["flow_id"],
151+
"columnsTo": ["id"],
168152
"onDelete": "set null",
169153
"onUpdate": "no action"
170154
}
@@ -174,9 +158,7 @@
174158
"channels_contact_id_unique": {
175159
"name": "channels_contact_id_unique",
176160
"nullsNotDistinct": false,
177-
"columns": [
178-
"contact_id"
179-
]
161+
"columns": ["contact_id"]
180162
}
181163
}
182164
},
@@ -270,12 +252,8 @@
270252
"name": "flows_user_id_users_id_fk",
271253
"tableFrom": "flows",
272254
"tableTo": "users",
273-
"columnsFrom": [
274-
"user_id"
275-
],
276-
"columnsTo": [
277-
"id"
278-
],
255+
"columnsFrom": ["user_id"],
256+
"columnsTo": ["id"],
279257
"onDelete": "no action",
280258
"onUpdate": "no action"
281259
}
@@ -353,12 +331,8 @@
353331
"name": "intents_user_id_users_id_fk",
354332
"tableFrom": "intents",
355333
"tableTo": "users",
356-
"columnsFrom": [
357-
"user_id"
358-
],
359-
"columnsTo": [
360-
"id"
361-
],
334+
"columnsFrom": ["user_id"],
335+
"columnsTo": ["id"],
362336
"onDelete": "cascade",
363337
"onUpdate": "no action"
364338
}
@@ -396,12 +370,8 @@
396370
"name": "settings_user_id_users_id_fk",
397371
"tableFrom": "settings",
398372
"tableTo": "users",
399-
"columnsFrom": [
400-
"user_id"
401-
],
402-
"columnsTo": [
403-
"id"
404-
],
373+
"columnsFrom": ["user_id"],
374+
"columnsTo": ["id"],
405375
"onDelete": "cascade",
406376
"onUpdate": "no action"
407377
}
@@ -460,9 +430,7 @@
460430
"indexes": {
461431
"email_idx": {
462432
"name": "email_idx",
463-
"columns": [
464-
"email"
465-
],
433+
"columns": ["email"],
466434
"isUnique": true
467435
}
468436
},
@@ -472,9 +440,7 @@
472440
"users_email_unique": {
473441
"name": "users_email_unique",
474442
"nullsNotDistinct": false,
475-
"columns": [
476-
"email"
477-
]
443+
"columns": ["email"]
478444
}
479445
}
480446
}
@@ -494,4 +460,4 @@
494460
"schemas": {},
495461
"tables": {}
496462
}
497-
}
463+
}

server/src/database/migrations/meta/_journal.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@
5959
"breakpoints": true
6060
}
6161
]
62-
}
62+
}

server/src/dtos/flows.dto.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { getCurrentLocale } from '@/i18n/get-current';
2-
import { IFlowSetting, IFlowVariable } from '@/interfaces/flows.interface';
3-
import { Transform } from 'class-transformer';
1+
import { getCurrentLocale } from '@/i18n/get-current'
2+
import { IFlowSetting, IFlowVariable } from '@/interfaces/flows.interface'
3+
import { Transform } from 'class-transformer'
44
import {
55
IsArray,
66
IsNotEmpty,
77
IsObject,
88
IsOptional,
99
IsString,
10-
} from 'class-validator';
10+
} from 'class-validator'
1111

1212
export class FlowDTO {
1313
@IsString()

server/src/dtos/intent.dto.ts

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,68 @@
1-
import { getCurrentLocale } from '@/i18n/get-current';
2-
import { IIntents } from '@/interfaces/intents.interface';
3-
import { Transform } from 'class-transformer';
1+
import { getCurrentLocale } from '@/i18n/get-current'
2+
import { IIntents, ITrainDescription } from '@/interfaces/intents.interface'
3+
import { Transform } from 'class-transformer'
44
import {
5-
IsArray,
6-
IsNotEmpty,
7-
IsObject,
8-
IsOptional,
9-
IsString,
10-
} from 'class-validator';
5+
IsArray,
6+
IsNotEmpty,
7+
IsObject,
8+
IsOptional,
9+
IsString,
10+
ValidateIf,
11+
} from 'class-validator'
1112

1213
export class IntentDTO {
13-
@IsString()
14-
@IsNotEmpty({
15-
message: () => getCurrentLocale().VALIDATE.REQUIRED({ field: 'Name' }),
16-
})
17-
@Transform(({ value }) => value.trim())
18-
name: string
19-
20-
@IsString()
21-
@IsNotEmpty({
22-
message: () => getCurrentLocale().VALIDATE.REQUIRED({ field: 'Reference Id' }),
23-
})
24-
@Transform(({ value }) => value.trim())
25-
referenceId: string
26-
27-
@IsArray()
28-
@IsOptional()
29-
@IsObject({ each: true })
30-
intents: Array<IIntents>
31-
32-
@IsArray()
33-
@IsOptional()
34-
@IsObject({ each: true })
35-
entities: Array<Record<any, any>>
36-
}
14+
@IsString()
15+
@IsNotEmpty({
16+
message: () => getCurrentLocale().VALIDATE.REQUIRED({ field: 'Name' }),
17+
})
18+
@Transform(({ value }) => value.trim())
19+
name: string
20+
21+
@IsString()
22+
@IsNotEmpty({
23+
message: () =>
24+
getCurrentLocale().VALIDATE.REQUIRED({ field: 'Reference Id' }),
25+
})
26+
@Transform(({ value }) => value.trim())
27+
referenceId: string
28+
29+
@IsString()
30+
@IsNotEmpty({
31+
message: () =>
32+
getCurrentLocale().VALIDATE.REQUIRED({ field: 'Train Type' }),
33+
})
34+
@Transform(({ value }) => value ?? 'manual')
35+
trainType: 'manual' | 'automation'
36+
37+
@IsArray()
38+
@ValidateIf((object, value) => object.trainType === 'automation')
39+
@IsObject({ each: true })
40+
trainDescription: Array<ITrainDescription>
41+
42+
@IsArray()
43+
@IsOptional()
44+
@IsObject({ each: true })
45+
intents: Array<IIntents>
46+
47+
@IsArray()
48+
@IsOptional()
49+
@IsObject({ each: true })
50+
entities: Array<Record<any, any>>
51+
}
52+
53+
export class PredictIntentDTO {
54+
@IsString()
55+
@IsNotEmpty({
56+
message: () =>
57+
getCurrentLocale().VALIDATE.REQUIRED({ field: 'Reference Id' }),
58+
})
59+
@Transform(({ value }) => value.trim())
60+
referenceId: string
61+
62+
@IsString()
63+
@IsNotEmpty({
64+
message: () => getCurrentLocale().VALIDATE.REQUIRED({ field: 'Text' }),
65+
})
66+
@Transform(({ value }) => value.trim())
67+
text: string
68+
}

server/src/i18n/en/intent.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export const INTENT = {
66
UPDATE_SUCCESS: 'Update train success!',
77
UPDATE_FAILED: 'Update train failed!',
88
DELETE_SUCCESS: 'Delete train success!',
9+
PREDICT_SUCCESS: 'Predict Intent success!',
910
}

0 commit comments

Comments
 (0)