@peacom/core 0.0.85 → 0.0.87
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +18 -0
- package/data/Chuyen-de-1.pdf +63386 -304
- package/data/report_bulk_campaign_Kplus_1665394311017.xlsx +0 -0
- package/jest.config.ts +16 -0
- package/package.json +9 -10
- package/project.json +41 -0
- package/src/index.ts +5 -0
- package/src/lib/api/api.constant.ts +82 -0
- package/src/lib/api/core.api.ts +86 -0
- package/src/lib/api/index.ts +4 -0
- package/src/lib/api/micro-service.api.ts +35 -0
- package/src/lib/api/micro-service.spec.ts +29 -0
- package/src/lib/api/queue/queue.api.ts +39 -0
- package/src/lib/core.spec.ts +7 -0
- package/src/lib/core.ts +3 -0
- package/src/lib/error/FormError.ts +64 -0
- package/src/lib/error/HttpError.ts +9 -0
- package/src/lib/model/Application.ts +63 -0
- package/src/lib/model/BackgroundTask.ts +5 -0
- package/src/lib/model/Billing.ts +11 -0
- package/src/lib/model/Currency.ts +5 -0
- package/src/lib/model/FileInfo.ts +9 -0
- package/src/lib/model/conversation/constant.ts +34 -0
- package/src/lib/model/conversation/index.ts +1 -0
- package/src/lib/model/index.ts +11 -0
- package/src/lib/model/message/Message.ts +159 -0
- package/src/lib/model/message/TemplateMessage.ts +4 -0
- package/src/lib/model/message/index.ts +4 -0
- package/src/lib/model/message/sinch/SinchWhatsappTemplate.ts +27 -0
- package/src/lib/model/message/zalo/ZaloZNSTemplate.ts +26 -0
- package/src/lib/model/partner/PartnerEvent.ts +24 -0
- package/src/lib/model/partner/index.ts +6 -0
- package/src/lib/model/queue/QueueMessage.ts +31 -0
- package/src/lib/model/queue/index.ts +2 -0
- package/src/lib/model/queue/queue.constant.ts +6 -0
- package/src/lib/model/template/Template.ts +86 -0
- package/src/lib/model/template/index.ts +1 -0
- package/src/lib/model/viber-business/constant.ts +4 -0
- package/src/lib/model/viber-business/index.ts +1 -0
- package/src/lib/service/index.ts +1 -0
- package/src/lib/service/storage/aws/constants.ts +22 -0
- package/src/lib/service/storage/aws/index.ts +2 -0
- package/src/lib/service/storage/aws/s3.service.ts +128 -0
- package/src/lib/service/storage/aws/s3.spec.ts +12 -0
- package/src/lib/service/storage/index.ts +1 -0
- package/src/lib/util/date/constant.ts +3 -0
- package/src/lib/util/date/date.spec.ts +22 -0
- package/src/lib/util/date/date.util.ts +64 -0
- package/src/lib/util/date/index.ts +2 -0
- package/src/lib/util/file/file.spec.ts +40 -0
- package/src/lib/util/file/file.util.ts +60 -0
- package/src/lib/util/general.util.ts +3 -0
- package/src/lib/util/index.ts +6 -0
- package/src/lib/util/message/message.util.ts +118 -0
- package/src/lib/util/phone.spec.ts +48 -0
- package/src/lib/util/phone.util.ts +51 -0
- package/src/lib/util/string.spec.ts +15 -0
- package/src/lib/util/string.util.ts +118 -0
- package/tsconfig.json +22 -0
- package/tsconfig.lib.json +10 -0
- package/tsconfig.spec.json +14 -0
- package/src/index.d.ts +0 -4
- package/src/index.js +0 -8
- package/src/index.js.map +0 -1
- package/src/lib/api/api.constant.d.ts +0 -58
- package/src/lib/api/api.constant.js +0 -20
- package/src/lib/api/api.constant.js.map +0 -1
- package/src/lib/api/core.api.d.ts +0 -9
- package/src/lib/api/core.api.js +0 -78
- package/src/lib/api/core.api.js.map +0 -1
- package/src/lib/api/index.d.ts +0 -4
- package/src/lib/api/index.js +0 -8
- package/src/lib/api/index.js.map +0 -1
- package/src/lib/api/micro-service.api.d.ts +0 -2
- package/src/lib/api/micro-service.api.js +0 -38
- package/src/lib/api/micro-service.api.js.map +0 -1
- package/src/lib/api/queue/queue.api.d.ts +0 -7
- package/src/lib/api/queue/queue.api.js +0 -37
- package/src/lib/api/queue/queue.api.js.map +0 -1
- package/src/lib/core.d.ts +0 -1
- package/src/lib/core.js +0 -8
- package/src/lib/core.js.map +0 -1
- package/src/lib/error/FormError.d.ts +0 -53
- package/src/lib/error/FormError.js +0 -58
- package/src/lib/error/FormError.js.map +0 -1
- package/src/lib/error/HttpError.d.ts +0 -4
- package/src/lib/error/HttpError.js +0 -12
- package/src/lib/error/HttpError.js.map +0 -1
- package/src/lib/model/Application.d.ts +0 -52
- package/src/lib/model/Application.js +0 -59
- package/src/lib/model/Application.js.map +0 -1
- package/src/lib/model/Billing.d.ts +0 -10
- package/src/lib/model/Billing.js +0 -16
- package/src/lib/model/Billing.js.map +0 -1
- package/src/lib/model/Currency.d.ts +0 -4
- package/src/lib/model/Currency.js +0 -9
- package/src/lib/model/Currency.js.map +0 -1
- package/src/lib/model/FileInfo.d.ts +0 -9
- package/src/lib/model/FileInfo.js +0 -3
- package/src/lib/model/FileInfo.js.map +0 -1
- package/src/lib/model/conversation/constant.d.ts +0 -25
- package/src/lib/model/conversation/constant.js +0 -25
- package/src/lib/model/conversation/constant.js.map +0 -1
- package/src/lib/model/conversation/index.d.ts +0 -1
- package/src/lib/model/conversation/index.js +0 -5
- package/src/lib/model/conversation/index.js.map +0 -1
- package/src/lib/model/index.d.ts +0 -10
- package/src/lib/model/index.js +0 -14
- package/src/lib/model/index.js.map +0 -1
- package/src/lib/model/message/Message.d.ts +0 -129
- package/src/lib/model/message/Message.js +0 -88
- package/src/lib/model/message/Message.js.map +0 -1
- package/src/lib/model/message/TemplateMessage.d.ts +0 -4
- package/src/lib/model/message/TemplateMessage.js +0 -9
- package/src/lib/model/message/TemplateMessage.js.map +0 -1
- package/src/lib/model/message/index.d.ts +0 -4
- package/src/lib/model/message/index.js +0 -8
- package/src/lib/model/message/index.js.map +0 -1
- package/src/lib/model/message/sinch/SinchWhatsappTemplate.d.ts +0 -22
- package/src/lib/model/message/sinch/SinchWhatsappTemplate.js +0 -14
- package/src/lib/model/message/sinch/SinchWhatsappTemplate.js.map +0 -1
- package/src/lib/model/message/zalo/ZaloZNSTemplate.d.ts +0 -23
- package/src/lib/model/message/zalo/ZaloZNSTemplate.js +0 -3
- package/src/lib/model/message/zalo/ZaloZNSTemplate.js.map +0 -1
- package/src/lib/model/partner/PartnerEvent.d.ts +0 -22
- package/src/lib/model/partner/PartnerEvent.js +0 -3
- package/src/lib/model/partner/PartnerEvent.js.map +0 -1
- package/src/lib/model/partner/index.d.ts +0 -5
- package/src/lib/model/partner/index.js +0 -11
- package/src/lib/model/partner/index.js.map +0 -1
- package/src/lib/model/queue/QueueMessage.d.ts +0 -28
- package/src/lib/model/queue/QueueMessage.js +0 -9
- package/src/lib/model/queue/QueueMessage.js.map +0 -1
- package/src/lib/model/queue/index.d.ts +0 -2
- package/src/lib/model/queue/index.js +0 -6
- package/src/lib/model/queue/index.js.map +0 -1
- package/src/lib/model/queue/queue.constant.d.ts +0 -6
- package/src/lib/model/queue/queue.constant.js +0 -11
- package/src/lib/model/queue/queue.constant.js.map +0 -1
- package/src/lib/model/template/Template.d.ts +0 -43
- package/src/lib/model/template/Template.js +0 -89
- package/src/lib/model/template/Template.js.map +0 -1
- package/src/lib/model/template/index.d.ts +0 -1
- package/src/lib/model/template/index.js +0 -5
- package/src/lib/model/template/index.js.map +0 -1
- package/src/lib/model/viber-business/constant.d.ts +0 -4
- package/src/lib/model/viber-business/constant.js +0 -9
- package/src/lib/model/viber-business/constant.js.map +0 -1
- package/src/lib/model/viber-business/index.d.ts +0 -1
- package/src/lib/model/viber-business/index.js +0 -5
- package/src/lib/model/viber-business/index.js.map +0 -1
- package/src/lib/util/date/constant.d.ts +0 -3
- package/src/lib/util/date/constant.js +0 -7
- package/src/lib/util/date/constant.js.map +0 -1
- package/src/lib/util/date/date.util.d.ts +0 -19
- package/src/lib/util/date/date.util.js +0 -59
- package/src/lib/util/date/date.util.js.map +0 -1
- package/src/lib/util/date/index.d.ts +0 -2
- package/src/lib/util/date/index.js +0 -6
- package/src/lib/util/date/index.js.map +0 -1
- package/src/lib/util/file/file.util.d.ts +0 -5
- package/src/lib/util/file/file.util.js +0 -35
- package/src/lib/util/file/file.util.js.map +0 -1
- package/src/lib/util/general.util.d.ts +0 -1
- package/src/lib/util/general.util.js +0 -8
- package/src/lib/util/general.util.js.map +0 -1
- package/src/lib/util/index.d.ts +0 -6
- package/src/lib/util/index.js +0 -10
- package/src/lib/util/index.js.map +0 -1
- package/src/lib/util/message/message.util.d.ts +0 -90
- package/src/lib/util/message/message.util.js +0 -72
- package/src/lib/util/message/message.util.js.map +0 -1
- package/src/lib/util/phone.util.d.ts +0 -24
- package/src/lib/util/phone.util.js +0 -51
- package/src/lib/util/phone.util.js.map +0 -1
- package/src/lib/util/string.util.d.ts +0 -14
- package/src/lib/util/string.util.js +0 -118
- package/src/lib/util/string.util.js.map +0 -1
Binary file
|
package/jest.config.ts
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
/* eslint-disable */
|
2
|
+
export default {
|
3
|
+
displayName: 'core',
|
4
|
+
preset: '../../jest.preset.js',
|
5
|
+
globals: {},
|
6
|
+
transform: {
|
7
|
+
'^.+\\.[tj]s$': [
|
8
|
+
'ts-jest',
|
9
|
+
{
|
10
|
+
tsconfig: '<rootDir>/tsconfig.spec.json',
|
11
|
+
},
|
12
|
+
],
|
13
|
+
},
|
14
|
+
moduleFileExtensions: ['ts', 'js', 'html'],
|
15
|
+
coverageDirectory: '../../coverage/packages/core',
|
16
|
+
};
|
package/package.json
CHANGED
@@ -1,24 +1,23 @@
|
|
1
1
|
{
|
2
2
|
"name": "@peacom/core",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.87",
|
4
4
|
"type": "commonjs",
|
5
5
|
"dependencies": {
|
6
6
|
"moment": "^2.29.4",
|
7
7
|
"moment-timezone": "^0.5.43",
|
8
8
|
"mime-types": "^2.1.35",
|
9
9
|
"google-libphonenumber": "^3.2.32",
|
10
|
-
"bullmq": "^3.15.
|
11
|
-
"uuid": "^9.0.0"
|
10
|
+
"bullmq": "^3.15.5",
|
11
|
+
"uuid": "^9.0.0",
|
12
|
+
"@aws-sdk/client-s3": "^3.350.0",
|
13
|
+
"@aws-sdk/s3-request-presigner": "^3.350.0"
|
12
14
|
},
|
13
15
|
"devDependencies": {
|
14
16
|
"@types/mime-types": "^2.1.1",
|
15
17
|
"@types/google-libphonenumber": "^7.4.23",
|
16
18
|
"@types/uuid": "^9.0.1",
|
17
|
-
"
|
18
|
-
|
19
|
-
|
20
|
-
"types": "./src/index.d.ts",
|
21
|
-
"peerDependencies": {
|
22
|
-
"tslib": "2.4.1"
|
19
|
+
"@types/node": "^20.3.1",
|
20
|
+
"isomorphic-fetch": "^3.0.0",
|
21
|
+
"node-fetch": "^3.3.1"
|
23
22
|
}
|
24
|
-
}
|
23
|
+
}
|
package/project.json
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
{
|
2
|
+
"name": "core",
|
3
|
+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
|
4
|
+
"sourceRoot": "packages/core/src",
|
5
|
+
"projectType": "library",
|
6
|
+
"targets": {
|
7
|
+
"build": {
|
8
|
+
"executor": "@nx/js:tsc",
|
9
|
+
"outputs": ["{options.outputPath}"],
|
10
|
+
"options": {
|
11
|
+
"outputPath": "dist/packages/core",
|
12
|
+
"main": "packages/core/src/index.ts",
|
13
|
+
"tsConfig": "packages/core/tsconfig.lib.json",
|
14
|
+
"assets": ["packages/core/*.md"]
|
15
|
+
}
|
16
|
+
},
|
17
|
+
"publish": {
|
18
|
+
"executor": "nx:run-commands",
|
19
|
+
"options": {
|
20
|
+
"command": "node tools/scripts/publish.mjs core {args.ver} {args.tag}"
|
21
|
+
},
|
22
|
+
"dependsOn": ["build"]
|
23
|
+
},
|
24
|
+
"lint": {
|
25
|
+
"executor": "@nx/linter:eslint",
|
26
|
+
"outputs": ["{options.outputFile}"],
|
27
|
+
"options": {
|
28
|
+
"lintFilePatterns": ["packages/core/**/*.ts"]
|
29
|
+
}
|
30
|
+
},
|
31
|
+
"test": {
|
32
|
+
"executor": "@nx/jest:jest",
|
33
|
+
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
|
34
|
+
"options": {
|
35
|
+
"jestConfig": "packages/core/jest.config.ts",
|
36
|
+
"passWithNoTests": true
|
37
|
+
}
|
38
|
+
}
|
39
|
+
},
|
40
|
+
"tags": []
|
41
|
+
}
|
package/src/index.ts
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
import {Application, ApplicationInfo, Message, RawMessage, SEND_MESSAGE_VIA} from '../model/';
|
2
|
+
import {Conversation} from "../model";
|
3
|
+
|
4
|
+
interface Contact {
|
5
|
+
id: string
|
6
|
+
name?: string
|
7
|
+
phone?: string
|
8
|
+
isSubscribed: boolean
|
9
|
+
extraData: Record<string, unknown>
|
10
|
+
avatarUrl?: string
|
11
|
+
language: string
|
12
|
+
country: string
|
13
|
+
}
|
14
|
+
|
15
|
+
export interface ApiCoreHandleParams {
|
16
|
+
to: ApplicationInfo
|
17
|
+
rawMessage: RawMessage
|
18
|
+
replyToApplicationMessageId?: string
|
19
|
+
sentTime: number;
|
20
|
+
extraData?: Record<string, unknown>
|
21
|
+
applicationMessageId: string;
|
22
|
+
from: Contact
|
23
|
+
conversation?: Conversation
|
24
|
+
}
|
25
|
+
|
26
|
+
export interface ApiCoreLiveAgentParams {
|
27
|
+
to: ApplicationInfo
|
28
|
+
sentTime: number;
|
29
|
+
extraData?: Record<string, unknown>
|
30
|
+
applicationMessageId?: string;
|
31
|
+
from: Contact
|
32
|
+
conversation?: Conversation
|
33
|
+
}
|
34
|
+
|
35
|
+
export interface ApiCoreLiveAgentResponse {
|
36
|
+
conversation?: Conversation
|
37
|
+
}
|
38
|
+
|
39
|
+
export interface ApiCoreHandleResponse {
|
40
|
+
receiverId: string
|
41
|
+
messages: Array<Message>
|
42
|
+
contact: unknown
|
43
|
+
applicationInfo: ApplicationInfo
|
44
|
+
conversation?: Conversation
|
45
|
+
}
|
46
|
+
|
47
|
+
export interface ApiChannelMessageParams {
|
48
|
+
applicationInfo?: ApplicationInfo
|
49
|
+
message: Message
|
50
|
+
receiverId: string
|
51
|
+
sendVia?: SEND_MESSAGE_VIA
|
52
|
+
conversation?: Conversation
|
53
|
+
queueOption?: Record<string, unknown>,
|
54
|
+
|
55
|
+
[key: string]: unknown
|
56
|
+
}
|
57
|
+
|
58
|
+
export interface ApiChannelMessageResponse {
|
59
|
+
applicationMessageId: string
|
60
|
+
extraData?: Record<string, unknown>,
|
61
|
+
|
62
|
+
[key: string]: unknown
|
63
|
+
}
|
64
|
+
|
65
|
+
export type LOG_FUNCTION = ((t: unknown) => void) | null
|
66
|
+
|
67
|
+
export const MICROSERVICE_URL: Record<Application, string> = {
|
68
|
+
[Application.APPLE]: process.env['APPLE_INTEGRATION'] || 'http://127.0.0.1:4005/eip/apple',
|
69
|
+
[Application.ZALO]: process.env['ZALO_INTEGRATION'] || 'http://127.0.0.1:4003/eip/zalo/bot',
|
70
|
+
[Application.ZALO_ZNS]: process.env['ZALO_ZNS_INTEGRATION'] || 'http://127.0.0.1:4003/eip/zalo/zns',
|
71
|
+
[Application.RCS]: process.env['RCS_INTEGRATION'] || 'http://127.0.0.1:4006',
|
72
|
+
[Application.VIBER]: process.env['VIBER_BOT_INTEGRATION'] || 'http://127.0.0.1:4001/eip/viber/bot',
|
73
|
+
[Application.VIBER_BUSINESS]: process.env['VIBER_BUSINESS_INTEGRATION'] || 'http://127.0.0.1:4001/eip/viber/business',
|
74
|
+
[Application.TELEGRAM]: process.env['TELEGRAM_INTEGRATION'] || 'http://127.0.0.1:4009',
|
75
|
+
[Application.FACEBOOK]: process.env['FACEBOOK_INTEGRATION'] || 'http://127.0.0.1:4010',
|
76
|
+
[Application.SMS]: process.env['SMS_INTEGRATION'] || 'http://127.0.0.1:4011',
|
77
|
+
[Application.VIBER_BOT]: process.env['VIBER_BOT_INTEGRATION'] || 'http://127.0.0.1:4001/eip/viber/bot',
|
78
|
+
[Application.WHATSAPP]: process.env['WHATSAPP_INTEGRATION'] || 'http://127.0.0.1:4008/eip/whatsapp',
|
79
|
+
[Application.GOOGLE_BUSINESS]: process.env['GOOGLE_BUSINESS_INTEGRATION'] || 'http://127.0.0.1:4006/eip/google/business',
|
80
|
+
}
|
81
|
+
|
82
|
+
export const INTEGRATED_CHANNEL = [Application.APPLE, Application.ZALO, Application.ZALO_ZNS, Application.GOOGLE_BUSINESS]
|
@@ -0,0 +1,86 @@
|
|
1
|
+
import {
|
2
|
+
ApiCoreHandleParams,
|
3
|
+
ApiCoreHandleResponse,
|
4
|
+
ApiCoreLiveAgentParams,
|
5
|
+
ApiCoreLiveAgentResponse,
|
6
|
+
LOG_FUNCTION
|
7
|
+
} from "./api.constant";
|
8
|
+
import {FormError} from "../error/FormError";
|
9
|
+
|
10
|
+
export enum CORE_API_PATH {
|
11
|
+
MESSAGE = "/message",
|
12
|
+
LIVE_AGENT = "/conversation/live-agent",
|
13
|
+
UNSUBSCRIBE = "/unsubscribe"
|
14
|
+
}
|
15
|
+
|
16
|
+
export const handleCoreMessage = async (CORE_URL: string, params: ApiCoreHandleParams, log: LOG_FUNCTION = null): Promise<ApiCoreHandleResponse> => {
|
17
|
+
const url = `${CORE_URL}${CORE_API_PATH.MESSAGE}`
|
18
|
+
if (log) {
|
19
|
+
log(`CORE REQUEST: ${url} - ${JSON.stringify(params)}`)
|
20
|
+
}
|
21
|
+
|
22
|
+
const rs = await fetch(url, {
|
23
|
+
method: "POST",
|
24
|
+
body: JSON.stringify(params),
|
25
|
+
headers: {
|
26
|
+
"content-type": "application/json"
|
27
|
+
}
|
28
|
+
})
|
29
|
+
const bodyStr = await rs.text();
|
30
|
+
if (log) {
|
31
|
+
log(`CORE RESPONSE: ${url} - ${bodyStr}`)
|
32
|
+
}
|
33
|
+
|
34
|
+
if (!rs.ok) {
|
35
|
+
throw new FormError(JSON.parse(bodyStr))
|
36
|
+
}
|
37
|
+
return JSON.parse(bodyStr)
|
38
|
+
}
|
39
|
+
|
40
|
+
export const handleCoreLiveAgent = async (CORE_URL: string, params: ApiCoreLiveAgentParams, log: LOG_FUNCTION = null): Promise<ApiCoreLiveAgentResponse> => {
|
41
|
+
const url = `${CORE_URL}${CORE_API_PATH.LIVE_AGENT}`
|
42
|
+
if (log) {
|
43
|
+
log(`CORE LIVE AGENT REQUEST: ${url} - ${JSON.stringify(params)}`)
|
44
|
+
}
|
45
|
+
|
46
|
+
const rs = await fetch(url, {
|
47
|
+
method: "POST",
|
48
|
+
body: JSON.stringify(params),
|
49
|
+
headers: {
|
50
|
+
"content-type": "application/json"
|
51
|
+
}
|
52
|
+
})
|
53
|
+
const bodyStr = await rs.text();
|
54
|
+
if (log) {
|
55
|
+
log(`CORE LIVE AGENT RESPONSE: ${url} - ${bodyStr}`)
|
56
|
+
}
|
57
|
+
|
58
|
+
if (!rs.ok) {
|
59
|
+
throw new FormError(JSON.parse(bodyStr))
|
60
|
+
}
|
61
|
+
return JSON.parse(bodyStr)
|
62
|
+
}
|
63
|
+
|
64
|
+
export const handleUnsubscribe = async (CORE_URL: string, params: ApiCoreLiveAgentParams, log: LOG_FUNCTION = null): Promise<ApiCoreLiveAgentResponse> => {
|
65
|
+
const url = `${CORE_URL}${CORE_API_PATH.UNSUBSCRIBE}`
|
66
|
+
if (log) {
|
67
|
+
log(`CORE UNSUBSCRIBE REQUEST: ${url} - ${JSON.stringify(params)}`)
|
68
|
+
}
|
69
|
+
|
70
|
+
const rs = await fetch(url, {
|
71
|
+
method: "POST",
|
72
|
+
body: JSON.stringify(params),
|
73
|
+
headers: {
|
74
|
+
"content-type": "application/json"
|
75
|
+
}
|
76
|
+
})
|
77
|
+
const bodyStr = await rs.text();
|
78
|
+
if (log) {
|
79
|
+
log(`CORE UNSUBSCRIBE RESPONSE: ${url} - ${bodyStr}`)
|
80
|
+
}
|
81
|
+
|
82
|
+
if (!rs.ok) {
|
83
|
+
throw new FormError(JSON.parse(bodyStr))
|
84
|
+
}
|
85
|
+
return JSON.parse(bodyStr)
|
86
|
+
}
|
@@ -0,0 +1,35 @@
|
|
1
|
+
import {ApiChannelMessageParams, ApiChannelMessageResponse, LOG_FUNCTION} from "./api.constant";
|
2
|
+
|
3
|
+
export const sendChannelMessage = async (MICROSERVICE_URL: string, params: ApiChannelMessageParams, log: LOG_FUNCTION = null): Promise<ApiChannelMessageResponse> => {
|
4
|
+
const url = `${MICROSERVICE_URL}/message`
|
5
|
+
if (log) {
|
6
|
+
log(`SERVICE REQUEST: ${url} - ${JSON.stringify(params)}`)
|
7
|
+
}
|
8
|
+
|
9
|
+
const rs = await fetch(url, {
|
10
|
+
method: "POST",
|
11
|
+
body: JSON.stringify(params),
|
12
|
+
headers: {
|
13
|
+
"content-type": "application/json"
|
14
|
+
}
|
15
|
+
})
|
16
|
+
const bodyStr = await rs.text();
|
17
|
+
if (log) {
|
18
|
+
log(`SERVICE RESPONSE: ${url} - ${bodyStr}`)
|
19
|
+
}
|
20
|
+
|
21
|
+
if (!rs.ok) {
|
22
|
+
let errorMessage = ''
|
23
|
+
try {
|
24
|
+
const errors = JSON.parse(bodyStr)
|
25
|
+
if (errors.length) {
|
26
|
+
errorMessage = errors[0]?.message
|
27
|
+
}
|
28
|
+
} catch (e) {
|
29
|
+
errorMessage = bodyStr
|
30
|
+
}
|
31
|
+
|
32
|
+
throw new Error(errorMessage)
|
33
|
+
}
|
34
|
+
return JSON.parse(bodyStr)
|
35
|
+
}
|
@@ -0,0 +1,29 @@
|
|
1
|
+
import {sendChannelMessage} from "./micro-service.api";
|
2
|
+
import {Application, MESSAGE_TYPE, PARTNER} from "../model";
|
3
|
+
|
4
|
+
require('isomorphic-fetch');
|
5
|
+
|
6
|
+
describe('micro-service.spec', () => {
|
7
|
+
it('sendChannelMessage', async () => {
|
8
|
+
try {
|
9
|
+
console.log(await sendChannelMessage('http://localhost:4001/eip/viber/business', {
|
10
|
+
message: {
|
11
|
+
rawMessage: {
|
12
|
+
type: MESSAGE_TYPE.TEXT,
|
13
|
+
message: 'testing'
|
14
|
+
},
|
15
|
+
id: 1,
|
16
|
+
publicId: '1234'
|
17
|
+
}, receiverId: "84938130683",
|
18
|
+
applicationInfo: {
|
19
|
+
applicationId: Application.VIBER_BUSINESS,
|
20
|
+
setting: {},
|
21
|
+
partnerId: PARTNER.VIBER,
|
22
|
+
id: '001'
|
23
|
+
}
|
24
|
+
}))
|
25
|
+
} catch (e) {
|
26
|
+
console.error(e)
|
27
|
+
}
|
28
|
+
});
|
29
|
+
});
|
@@ -0,0 +1,39 @@
|
|
1
|
+
import {QueueMessageStatus, QueueOutgoingMessage} from "../../model";
|
2
|
+
import {Queue} from "bullmq";
|
3
|
+
import {v4} from 'uuid'
|
4
|
+
import {QueuePartnerEventParam} from "../../model/partner/PartnerEvent";
|
5
|
+
import {PARTNER_MESSAGE_TYPE} from "../../model/partner";
|
6
|
+
|
7
|
+
const toBullQueueMessage = (message: QueueMessageStatus) => {
|
8
|
+
const jobId = `${message.applicationInfo.applicationId}_${message.applicationMessageId || ''}_${message.status}_${message.sentTime}`
|
9
|
+
return {
|
10
|
+
name: jobId, data: message, opts: {
|
11
|
+
...(message.jobOpt || {}), jobId
|
12
|
+
}
|
13
|
+
}
|
14
|
+
}
|
15
|
+
|
16
|
+
export const queueAddMessageStatus = (queue: Queue, message: QueueMessageStatus) => {
|
17
|
+
const queueMessage = toBullQueueMessage(message)
|
18
|
+
return queue.add(queueMessage.name, queueMessage.data, queueMessage.opts)
|
19
|
+
}
|
20
|
+
|
21
|
+
export const queueAddBulkMessageStatus = (queue: Queue, messages: Array<QueueMessageStatus>) => {
|
22
|
+
return queue.addBulk(messages.map(t => toBullQueueMessage(t)))
|
23
|
+
}
|
24
|
+
|
25
|
+
export const queueAddOutgoingMessage = (queue: Queue, message: QueueOutgoingMessage) => {
|
26
|
+
return queue.add(message.name || v4(), message, message.jobOpt)
|
27
|
+
}
|
28
|
+
|
29
|
+
export const queueAddPartnerEvent = (queue: Queue, message: QueuePartnerEventParam) => {
|
30
|
+
let jobId = ''
|
31
|
+
switch (message.event.type) {
|
32
|
+
case PARTNER_MESSAGE_TYPE.EVENT:
|
33
|
+
jobId = `${message.event.messageId}_${message.event.status}`
|
34
|
+
break;
|
35
|
+
case PARTNER_MESSAGE_TYPE.MESSAGE:
|
36
|
+
jobId = message.event.messageId
|
37
|
+
}
|
38
|
+
return queue.add(jobId, message, {jobId, ...(message.jobOpt || {})})
|
39
|
+
}
|
package/src/lib/core.ts
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
import {HttpError} from "./HttpError";
|
2
|
+
|
3
|
+
export enum HTTP_ERROR {
|
4
|
+
ACCESS_DENIED = 403,
|
5
|
+
NOT_FOUND = 404,
|
6
|
+
TIME_OUT = 402,
|
7
|
+
BAD_REQUEST = 400,
|
8
|
+
NOT_AUTHENTICATE = 401,
|
9
|
+
INTERNAL_SERVER_ERROR = 500
|
10
|
+
}
|
11
|
+
|
12
|
+
interface FieldError {
|
13
|
+
name: string
|
14
|
+
code: string
|
15
|
+
message: string
|
16
|
+
}
|
17
|
+
|
18
|
+
export const FIELD_ERROR = {
|
19
|
+
INVALID: 'INVALID',
|
20
|
+
EMAIL_NOT_ACTIVE: 'EMAIL_NOT_ACTIVE',
|
21
|
+
USER_NOT_ACTIVE: 'USER_NOT_ACTIVE',
|
22
|
+
WRONG_PASSWORD: 'WRONG_PASSWORD',
|
23
|
+
USER_NOT_FOUND: 'USER_NOT_FOUND',
|
24
|
+
EMAIL_INVALID: 'EMAIL_INVALID',
|
25
|
+
EMAIL_NOT_ALLOW_REGISTER: 'EMAIL_NOT_ALLOW_REGISTER',
|
26
|
+
INVALID_TOKEN: 'INVALID_TOKEN',
|
27
|
+
EMAIL_NOT_FOUND: 'EMAIL_NOT_FOUND',
|
28
|
+
EMAIL_EXISTED: 'EMAIL_EXISTED',
|
29
|
+
TOKEN_EXPIRED: 'TOKEN_EXPIRED',
|
30
|
+
PASSWORD_NOT_MATCH: 'PASSWORD_NOT_MATCH',
|
31
|
+
SIGNED_URL_FAILED: 'SIGNED_URL_FAILED',
|
32
|
+
UPLOAD_FAILED: 'UPLOAD_FAILED',
|
33
|
+
USER_NAME_IS_EXITED: 'USER_NAME_IS_EXITED',
|
34
|
+
CURRENT_PASSWORD_NOT_MATCH: 'CURRENT_PASSWORD_NOT_MATCH',
|
35
|
+
CURRENT_EMAIL_NOT_MATCH: 'CURRENT_EMAIL_NOT_MATCH',
|
36
|
+
CONFIRM_EMAIL_NOT_MATCH: 'CONFIRM_EMAIL_NOT_MATCH',
|
37
|
+
NEW_EMAIL_SAME_OLD_EMAIL: 'NEW_EMAIL_SAME_OLD_EMAIL',
|
38
|
+
USERNAME_IS_A_REQUIRED_FIELD: 'USERNAME_IS_A_REQUIRED_FIELD',
|
39
|
+
USER_NOT_ACTIVE_KYC: 'USER_NOT_ACTIVE_KYC',
|
40
|
+
EXISTED: 'EXISTED',
|
41
|
+
COMPANY_IS_NOT_ACTIVE: 'COMPANY_IS_NOT_ACTIVE',
|
42
|
+
BAD_REQUEST: 'BAD_REQUEST',
|
43
|
+
NOT_AUTHENTICATE: 'NOT_AUTHENTICATE',
|
44
|
+
FORBIDDEN_ERROR: 'FORBIDDEN_ERROR',
|
45
|
+
RESOURCE_NOT_FOUND: 'RESOURCE_NOT_FOUND',
|
46
|
+
INTERNAL_SERVER_ERROR: 'INTERNAL_SERVER_ERROR',
|
47
|
+
SERVICE_UNAVAILABLE: 'SERVICE_UNAVAILABLE',
|
48
|
+
LOGIN_REQUIRED_OTP: 'LOGIN_REQUIRED_OTP',
|
49
|
+
API_KEY_NOT_FOUND: 'API_KEY_NOT_FOUND'
|
50
|
+
};
|
51
|
+
|
52
|
+
|
53
|
+
export class FormError extends HttpError {
|
54
|
+
errors: Array<FieldError>
|
55
|
+
|
56
|
+
constructor(_errors: FieldError | Array<FieldError>) {
|
57
|
+
super(HTTP_ERROR.BAD_REQUEST, 'Bad request');
|
58
|
+
this.errors = Array.isArray(_errors) ? [..._errors] : [_errors];
|
59
|
+
}
|
60
|
+
}
|
61
|
+
|
62
|
+
export function badRequest(name: string, code: string, message: string) {
|
63
|
+
return new FormError({name, code, message});
|
64
|
+
}
|
@@ -0,0 +1,63 @@
|
|
1
|
+
export enum Application {
|
2
|
+
VIBER = 1,
|
3
|
+
RCS = 2,
|
4
|
+
SMS = 3,
|
5
|
+
APPLE = 4,
|
6
|
+
TELEGRAM = 5,
|
7
|
+
FACEBOOK = 6,
|
8
|
+
ZALO = 7,
|
9
|
+
ZALO_ZNS = 8,
|
10
|
+
VIBER_BUSINESS = 9, // Not use now
|
11
|
+
VIBER_BOT = 10, // Not use now
|
12
|
+
WHATSAPP = 11,
|
13
|
+
GOOGLE_BUSINESS = 12
|
14
|
+
}
|
15
|
+
|
16
|
+
export const Application_Text = {
|
17
|
+
[Application.VIBER]: "VIBER",
|
18
|
+
[Application.RCS]: "RCS",
|
19
|
+
[Application.SMS]: "SMS",
|
20
|
+
[Application.APPLE]: "APPLE",
|
21
|
+
[Application.TELEGRAM]: "TELEGRAM",
|
22
|
+
[Application.FACEBOOK]: "FACEBOOK",
|
23
|
+
[Application.ZALO]: "ZALO",
|
24
|
+
[Application.ZALO_ZNS]: "ZALO ZNS",
|
25
|
+
[Application.VIBER_BUSINESS]: "VIBER BUSINESS",
|
26
|
+
[Application.VIBER_BOT]: "VIBER BOT",
|
27
|
+
[Application.WHATSAPP]: "WHATSAPP",
|
28
|
+
[Application.GOOGLE_BUSINESS]: "GOOGLE BUSINESS",
|
29
|
+
}
|
30
|
+
|
31
|
+
export enum PARTNER {
|
32
|
+
PEACOM = 1,
|
33
|
+
GOOGLE = 2,
|
34
|
+
GMS = 3,
|
35
|
+
VIBER = 4,
|
36
|
+
APPLE = 5,
|
37
|
+
SINCH = 6,
|
38
|
+
ZALO = 7,
|
39
|
+
FACEBOOK = 8,
|
40
|
+
TELEGRAM = 9,
|
41
|
+
DATALYTIS = 10,
|
42
|
+
CM = 11,
|
43
|
+
BRAND_SMS,
|
44
|
+
VIET_GUY
|
45
|
+
}
|
46
|
+
|
47
|
+
export const isBotApplication = (appId: string | number) => {
|
48
|
+
return [Application.VIBER_BOT, Application.APPLE, Application.TELEGRAM, Application.FACEBOOK, Application.ZALO, Application.GOOGLE_BUSINESS].includes(Number(appId))
|
49
|
+
}
|
50
|
+
|
51
|
+
export const isOAApplication = (appId: string | number) => {
|
52
|
+
return [Application.RCS, Application.SMS, Application.ZALO_ZNS,
|
53
|
+
Application.WHATSAPP,
|
54
|
+
Application.VIBER_BUSINESS, Application.VIBER].includes(Number(appId))
|
55
|
+
}
|
56
|
+
|
57
|
+
export interface ApplicationInfo {
|
58
|
+
setting?: Record<string, unknown>,
|
59
|
+
applicationId: Application,
|
60
|
+
id: string // Official Account username - Mapping with Partner OfficialAccount ID
|
61
|
+
publicId?: string // Public ID of Application Channel
|
62
|
+
partnerId: PARTNER
|
63
|
+
}
|
@@ -0,0 +1,34 @@
|
|
1
|
+
// AUTO (SUBSCRIBED) ==> REQUEST ==> JOINED ==> AUTO
|
2
|
+
// AUTO ==> UNSUBSCRIBED
|
3
|
+
// REQUEST ==> UNSUBSCRIBED
|
4
|
+
// JOINED ==> UNSUBSCRIBED
|
5
|
+
export enum LIVE_AGENT_STATUS {
|
6
|
+
REQUEST = 1,
|
7
|
+
JOINED = 2,
|
8
|
+
LEAVED = 3
|
9
|
+
}
|
10
|
+
|
11
|
+
export enum CONVERSATION_STATUS {
|
12
|
+
SUBSCRIBED = 1,
|
13
|
+
UNSUBSCRIBED
|
14
|
+
}
|
15
|
+
|
16
|
+
export const LIVE_AGENT_REQUEST_MESSAGE = 'LIVE_AGENT_REQUEST'
|
17
|
+
|
18
|
+
export enum RepresentativeType {
|
19
|
+
BOT = "BOT",
|
20
|
+
HUMAN = "HUMAN"
|
21
|
+
}
|
22
|
+
|
23
|
+
export interface Representative {
|
24
|
+
avatar?: string,
|
25
|
+
name: string,
|
26
|
+
type: RepresentativeType
|
27
|
+
}
|
28
|
+
|
29
|
+
export interface Conversation {
|
30
|
+
id?: number // Local Conversation ID
|
31
|
+
partnerId?: string | null // Partner conversation ID
|
32
|
+
status?: LIVE_AGENT_STATUS
|
33
|
+
representative?: Representative
|
34
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './constant'
|
@@ -0,0 +1,11 @@
|
|
1
|
+
export * from './Application'
|
2
|
+
export * from './Billing'
|
3
|
+
export * from './queue'
|
4
|
+
export * from './viber-business'
|
5
|
+
export * from './conversation'
|
6
|
+
export * from './Currency'
|
7
|
+
export * from './template'
|
8
|
+
export * from './message'
|
9
|
+
export * from './FileInfo'
|
10
|
+
export * from './partner'
|
11
|
+
export * from './BackgroundTask'
|