Skip to content

Commit 450b6fa

Browse files
authored
Merge pull request #40 from Dialogue-Bot/DIAL-34-Add-training-model
DIAL-34-Add-training-model
2 parents d2a62ee + fe94053 commit 450b6fa

30 files changed

+4200
-5475
lines changed

server/model.nlp

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

server/package-lock.json

Lines changed: 1341 additions & 5008 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"moment": "^2.30.1",
5757
"morgan": "^1.10.0",
5858
"multer": "1.4.5-lts.1",
59+
"node-nlp": "^4.27.0",
5960
"nodemailer": "^6.9.8",
6061
"pg": "^8.11.3",
6162
"react": "^18.2.0",
@@ -100,7 +101,7 @@
100101
"node-gyp": "^9.1.0",
101102
"nodemon": "^2.0.19",
102103
"pm2": "^5.2.0",
103-
"prettier": "^2.8.8",
104+
"prettier": "^2.7.1",
104105
"supertest": "^6.2.4",
105106
"ts-jest": "^28.0.7",
106107
"ts-node": "^10.9.1",

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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ export const ENDPOINTS = {
5656
ADD_CHANNELS: '/flow/add-channels',
5757
SELECT_FLOWS_FOR_CHANNEL: '/flow/select',
5858
},
59+
INTENT: {
60+
INDEX: '/intent',
61+
PREDICT: '/intent/predict',
62+
},
5963
}
6064

6165
export const LOCALE_KEY = 'lang'

server/src/controllers/flows.controller.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,14 @@ export class FlowController {
6666
res.status(StatusCodes.OK).json({ data })
6767
})
6868

69-
public getFlowsForSelect = catchAsync(async (req: RequestWithUser, res) => {
70-
console.log('req.query.channelId', req.query.channelId)
71-
const data = await this.flowService.getFlowsForSelect(
72-
req.user?.id as string,
73-
)
74-
res.status(StatusCodes.OK).json({ data })
75-
})
69+
public selectFlowsForChannel = catchAsync(
70+
async (req: RequestWithUser, res) => {
71+
const data = await this.flowService.getFlowsForSelect(
72+
req.user?.id as string,
73+
)
74+
res.status(StatusCodes.OK).json({ data })
75+
},
76+
)
7677

7778
public getFlowByContactId = catchAsync(async (req: RequestWithUser, res) => {
7879
const data = await this.flowService.getFlowByContactId(req.params.contactId)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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'
10+
11+
export class IntentController {
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,
67+
})
68+
})
69+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
CREATE TABLE IF NOT EXISTS "intents" (
2+
"id" varchar(36) PRIMARY KEY NOT NULL,
3+
"name" text NOT NULL,
4+
"reference_id" text NOT NULL,
5+
"intents" json DEFAULT '[]'::json NOT NULL,
6+
"entities" json DEFAULT '[]'::json NOT NULL,
7+
"user_id" varchar(36) NOT NULL,
8+
"created_at" timestamp DEFAULT now(),
9+
"updated_at" timestamp DEFAULT now(),
10+
CONSTRAINT "intents_reference_id_unique" UNIQUE("reference_id")
11+
);
12+
--> statement-breakpoint
13+
ALTER TABLE "flows" ALTER COLUMN "variables" SET DEFAULT '[]'::json;--> statement-breakpoint
14+
ALTER TABLE "flows" DROP COLUMN IF EXISTS "diagrams";--> statement-breakpoint
15+
DO $$ BEGIN
16+
ALTER TABLE "intents" ADD CONSTRAINT "intents_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE no action ON UPDATE no action;
17+
EXCEPTION
18+
WHEN duplicate_object THEN null;
19+
END $$;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
ALTER TABLE "intents" DROP CONSTRAINT "intents_reference_id_unique";--> statement-breakpoint
2+
ALTER TABLE "intents" DROP CONSTRAINT "intents_user_id_users_id_fk";
3+
--> statement-breakpoint
4+
ALTER TABLE "intents" ALTER COLUMN "intents" DROP NOT NULL;--> statement-breakpoint
5+
ALTER TABLE "intents" ADD COLUMN "deleted" boolean DEFAULT false;--> statement-breakpoint
6+
DO $$ BEGIN
7+
ALTER TABLE "intents" ADD CONSTRAINT "intents_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE cascade ON UPDATE no action;
8+
EXCEPTION
9+
WHEN duplicate_object THEN null;
10+
END $$;
11+
--> statement-breakpoint
12+
ALTER TABLE "intents" DROP COLUMN IF EXISTS "reference_id";
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
CREATE TABLE IF NOT EXISTS "intents" (
2+
"id" varchar(36) PRIMARY KEY NOT NULL,
3+
"name" text NOT NULL,
4+
"reference_id" text NOT NULL,
5+
"intents" json DEFAULT '[]'::json,
6+
"entities" json DEFAULT '[]'::json NOT NULL,
7+
"user_id" varchar(36) NOT NULL,
8+
"deleted" boolean DEFAULT false,
9+
"created_at" timestamp DEFAULT now(),
10+
"updated_at" timestamp DEFAULT now()
11+
);
12+
--> statement-breakpoint
13+
ALTER TABLE "flows" DROP CONSTRAINT "flows_user_id_users_id_fk";
14+
--> statement-breakpoint
15+
DO $$ BEGIN
16+
ALTER TABLE "flows" ADD CONSTRAINT "flows_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE no action ON UPDATE no action;
17+
EXCEPTION
18+
WHEN duplicate_object THEN null;
19+
END $$;
20+
--> statement-breakpoint
21+
DO $$ BEGIN
22+
ALTER TABLE "intents" ADD CONSTRAINT "intents_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE cascade ON UPDATE no action;
23+
EXCEPTION
24+
WHEN duplicate_object THEN null;
25+
END $$;

0 commit comments

Comments
 (0)