@diagramers/cli 1.0.23 → 1.0.24
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/dist/services/template-updater.d.ts +2 -0
- package/dist/services/template-updater.d.ts.map +1 -1
- package/dist/services/template-updater.js +66 -14
- package/dist/services/template-updater.js.map +1 -1
- package/package.json +5 -1
- package/templates/api/certs/auth-app-cert.json +13 -0
- package/templates/api/main.ts +10 -0
- package/templates/api/package.json +70 -0
- package/templates/api/src/assets/css/email-template.css +8 -0
- package/templates/api/src/assets/images/logo_large.png +0 -0
- package/templates/api/src/assets/keys/certificate.pem +22 -0
- package/templates/api/src/assets/keys/private-key.pem +28 -0
- package/templates/api/src/config/config-interface.ts +191 -0
- package/templates/api/src/config/development.ts +145 -0
- package/templates/api/src/config/index.ts +59 -0
- package/templates/api/src/config/production.ts +145 -0
- package/templates/api/src/config/staging.ts +144 -0
- package/templates/api/src/config/uat.ts +144 -0
- package/templates/api/src/controllers/account-controller.ts +162 -0
- package/templates/api/src/entities/audit.ts +12 -0
- package/templates/api/src/entities/base-entity.ts +10 -0
- package/templates/api/src/entities/user.ts +71 -0
- package/templates/api/src/helpers/FrameworkHelper.ts +157 -0
- package/templates/api/src/helpers/auth.ts +971 -0
- package/templates/api/src/helpers/cronHelper.ts +170 -0
- package/templates/api/src/helpers/dbcontext.ts +83 -0
- package/templates/api/src/helpers/encryptionHelper.ts +76 -0
- package/templates/api/src/helpers/enums.ts +258 -0
- package/templates/api/src/helpers/handle-response.ts +49 -0
- package/templates/api/src/helpers/httpHelper.ts +75 -0
- package/templates/api/src/helpers/mailer.ts +152 -0
- package/templates/api/src/helpers/result.ts +47 -0
- package/templates/api/src/helpers/string-helper.ts +27 -0
- package/templates/api/src/routes/account-routes.ts +37 -0
- package/templates/api/src/routes/auth-routes.ts +286 -0
- package/templates/api/src/routes/index.ts +92 -0
- package/templates/api/src/schemas/audit.ts +36 -0
- package/templates/api/src/schemas/otp.ts +52 -0
- package/templates/api/src/schemas/session.ts +57 -0
- package/templates/api/src/schemas/user.ts +125 -0
- package/templates/api/src/server/index.ts +86 -0
- package/templates/api/src/server/socket-server-provider.ts +209 -0
- package/templates/api/src/services/account-service.ts +243 -0
- package/templates/api/src/services/audit-service.ts +56 -0
- package/templates/api/tsconfig.json +16 -0
- package/templates/api/webpack.config.js +66 -0
- package/scripts/publish.sh +0 -58
- package/scripts/setup.sh +0 -38
- package/scripts/version.sh +0 -80
- package/src/commands/api.ts +0 -76
- package/src/commands/extend.ts +0 -35
- package/src/commands/init.ts +0 -32
- package/src/commands/update.ts +0 -25
- package/src/config/template-config.ts +0 -111
- package/src/index.ts +0 -41
- package/src/services/api-generator.ts +0 -378
- package/src/services/project-extender.ts +0 -330
- package/src/services/project-initializer.ts +0 -335
- package/src/services/project-updater.ts +0 -117
- package/src/services/relation-generator.ts +0 -203
- package/src/services/table-generator.ts +0 -114
- package/src/services/template-processor.ts +0 -166
- package/src/services/template-updater.ts +0 -184
- package/tsconfig.json +0 -19
@@ -0,0 +1,52 @@
|
|
1
|
+
import * as mongoose from 'mongoose';
|
2
|
+
import { OTPCode } from '../entities/user';
|
3
|
+
import { AuthProvider } from '../helpers/enums';
|
4
|
+
|
5
|
+
export const otpSchema = new mongoose.Schema({
|
6
|
+
code: {
|
7
|
+
type: String,
|
8
|
+
required: true
|
9
|
+
},
|
10
|
+
user_id: {
|
11
|
+
type: mongoose.Schema.Types.ObjectId,
|
12
|
+
ref: 'user',
|
13
|
+
required: true
|
14
|
+
},
|
15
|
+
auth_provider: {
|
16
|
+
type: String,
|
17
|
+
enum: Object.values(AuthProvider),
|
18
|
+
required: true
|
19
|
+
},
|
20
|
+
purpose: {
|
21
|
+
type: String,
|
22
|
+
enum: ['email_verification', 'phone_verification', 'password_reset', 'login'],
|
23
|
+
required: true
|
24
|
+
},
|
25
|
+
expires_at: {
|
26
|
+
type: Date,
|
27
|
+
required: true
|
28
|
+
},
|
29
|
+
attempts: {
|
30
|
+
type: Number,
|
31
|
+
default: 0
|
32
|
+
},
|
33
|
+
max_attempts: {
|
34
|
+
type: Number,
|
35
|
+
default: 3
|
36
|
+
},
|
37
|
+
is_used: {
|
38
|
+
type: Boolean,
|
39
|
+
default: false
|
40
|
+
},
|
41
|
+
created_at: {
|
42
|
+
type: Date,
|
43
|
+
default: Date.now
|
44
|
+
}
|
45
|
+
}, { timestamps: true, suppressReservedKeysWarning: true });
|
46
|
+
|
47
|
+
// Index for performance
|
48
|
+
otpSchema.index({ user_id: 1, purpose: 1, is_used: 1 });
|
49
|
+
otpSchema.index({ code: 1, user_id: 1 });
|
50
|
+
otpSchema.index({ expires_at: 1 }, { expireAfterSeconds: 0 }); // TTL index
|
51
|
+
|
52
|
+
export const OTPEntity = mongoose.model<OTPCode>('otp', otpSchema);
|
@@ -0,0 +1,57 @@
|
|
1
|
+
import * as mongoose from 'mongoose';
|
2
|
+
import { UserSession } from '../entities/user';
|
3
|
+
import { AuthProvider, AuthMethod } from '../helpers/enums';
|
4
|
+
|
5
|
+
export const sessionSchema = new mongoose.Schema({
|
6
|
+
session_id: {
|
7
|
+
type: String,
|
8
|
+
required: true,
|
9
|
+
unique: true
|
10
|
+
},
|
11
|
+
user_id: {
|
12
|
+
type: mongoose.Schema.Types.ObjectId,
|
13
|
+
ref: 'user',
|
14
|
+
required: true
|
15
|
+
},
|
16
|
+
auth_provider: {
|
17
|
+
type: String,
|
18
|
+
enum: Object.values(AuthProvider),
|
19
|
+
required: true
|
20
|
+
},
|
21
|
+
auth_method: {
|
22
|
+
type: String,
|
23
|
+
enum: Object.values(AuthMethod),
|
24
|
+
required: true
|
25
|
+
},
|
26
|
+
ip_address: {
|
27
|
+
type: String,
|
28
|
+
required: true
|
29
|
+
},
|
30
|
+
user_agent: {
|
31
|
+
type: String,
|
32
|
+
required: true
|
33
|
+
},
|
34
|
+
is_active: {
|
35
|
+
type: Boolean,
|
36
|
+
default: true
|
37
|
+
},
|
38
|
+
expires_at: {
|
39
|
+
type: Date,
|
40
|
+
required: true
|
41
|
+
},
|
42
|
+
created_at: {
|
43
|
+
type: Date,
|
44
|
+
default: Date.now
|
45
|
+
},
|
46
|
+
last_activity: {
|
47
|
+
type: Date,
|
48
|
+
default: Date.now
|
49
|
+
}
|
50
|
+
}, { timestamps: true, suppressReservedKeysWarning: true });
|
51
|
+
|
52
|
+
// Index for performance
|
53
|
+
sessionSchema.index({ user_id: 1, is_active: 1 });
|
54
|
+
sessionSchema.index({ session_id: 1 });
|
55
|
+
sessionSchema.index({ expires_at: 1 }, { expireAfterSeconds: 0 }); // TTL index
|
56
|
+
|
57
|
+
export const SessionEntity = mongoose.model<UserSession>('session', sessionSchema);
|
@@ -0,0 +1,125 @@
|
|
1
|
+
import * as mongoose from 'mongoose';
|
2
|
+
import { IUser } from '../entities/user';
|
3
|
+
import { AuthProvider } from '../helpers/enums';
|
4
|
+
import { v4 as uuidv4 } from 'uuid';
|
5
|
+
|
6
|
+
// Auth Provider Info Schema
|
7
|
+
const authProviderInfoSchema = new mongoose.Schema({
|
8
|
+
provider: {
|
9
|
+
type: String,
|
10
|
+
enum: Object.values(AuthProvider),
|
11
|
+
required: true
|
12
|
+
},
|
13
|
+
provider_user_id: {
|
14
|
+
type: String,
|
15
|
+
required: false
|
16
|
+
},
|
17
|
+
provider_access_token: {
|
18
|
+
type: String,
|
19
|
+
required: false
|
20
|
+
},
|
21
|
+
provider_refresh_token: {
|
22
|
+
type: String,
|
23
|
+
required: false
|
24
|
+
},
|
25
|
+
provider_metadata: {
|
26
|
+
type: mongoose.Schema.Types.Mixed,
|
27
|
+
required: false
|
28
|
+
},
|
29
|
+
is_verified: {
|
30
|
+
type: Boolean,
|
31
|
+
default: false
|
32
|
+
},
|
33
|
+
last_used: {
|
34
|
+
type: Date,
|
35
|
+
default: Date.now
|
36
|
+
},
|
37
|
+
created_at: {
|
38
|
+
type: Date,
|
39
|
+
default: Date.now
|
40
|
+
}
|
41
|
+
}, { _id: false });
|
42
|
+
|
43
|
+
export const userSchema = new mongoose.Schema(
|
44
|
+
{
|
45
|
+
name: {
|
46
|
+
type: mongoose.SchemaTypes.String,
|
47
|
+
required: true,
|
48
|
+
},
|
49
|
+
username: {
|
50
|
+
type: mongoose.SchemaTypes.String,
|
51
|
+
required: false,
|
52
|
+
length: 150,
|
53
|
+
unique: true
|
54
|
+
},
|
55
|
+
email: {
|
56
|
+
type: mongoose.SchemaTypes.String,
|
57
|
+
unique: true,
|
58
|
+
required: true,
|
59
|
+
},
|
60
|
+
emailVerified: {
|
61
|
+
type: mongoose.SchemaTypes.Boolean,
|
62
|
+
default: false,
|
63
|
+
},
|
64
|
+
|
65
|
+
// New authentication fields
|
66
|
+
auth_providers: {
|
67
|
+
type: [authProviderInfoSchema],
|
68
|
+
default: []
|
69
|
+
},
|
70
|
+
primary_auth_provider: {
|
71
|
+
type: String,
|
72
|
+
enum: Object.values(AuthProvider),
|
73
|
+
default: AuthProvider.INTERNAL
|
74
|
+
},
|
75
|
+
|
76
|
+
// Legacy fields (for backward compatibility)
|
77
|
+
hashedPassword: {
|
78
|
+
type: mongoose.SchemaTypes.String,
|
79
|
+
required: false
|
80
|
+
},
|
81
|
+
firebaseUID: {
|
82
|
+
type: mongoose.SchemaTypes.String,
|
83
|
+
required: false
|
84
|
+
},
|
85
|
+
|
86
|
+
// Contact information
|
87
|
+
phone: {
|
88
|
+
type: mongoose.SchemaTypes.String,
|
89
|
+
required: false
|
90
|
+
},
|
91
|
+
phoneVerified: {
|
92
|
+
type: mongoose.SchemaTypes.Boolean,
|
93
|
+
default: false,
|
94
|
+
},
|
95
|
+
|
96
|
+
// Profile information
|
97
|
+
identity: {
|
98
|
+
type: String,
|
99
|
+
default: () => uuidv4()
|
100
|
+
},
|
101
|
+
address: {
|
102
|
+
type: mongoose.Schema.Types.Mixed,
|
103
|
+
required: false
|
104
|
+
},
|
105
|
+
invitationCode: {
|
106
|
+
type: String,
|
107
|
+
required: false
|
108
|
+
},
|
109
|
+
registerationStatus: {
|
110
|
+
type: mongoose.SchemaTypes.Boolean,
|
111
|
+
default: false
|
112
|
+
},
|
113
|
+
userType: {
|
114
|
+
type: String,
|
115
|
+
default: 'NOR'
|
116
|
+
},
|
117
|
+
status: {
|
118
|
+
type: Number,
|
119
|
+
default: 1
|
120
|
+
}
|
121
|
+
},
|
122
|
+
{ timestamps: true, suppressReservedKeysWarning: true },
|
123
|
+
);
|
124
|
+
|
125
|
+
export const UserEntity = mongoose.model<IUser>('user', userSchema);
|
@@ -0,0 +1,86 @@
|
|
1
|
+
import * as express from "express";
|
2
|
+
import * as bodyParser from "body-parser";
|
3
|
+
import { Routes } from "../routes"
|
4
|
+
import { SocketServerProvider } from "./socket-server-provider";
|
5
|
+
const path = require("path");
|
6
|
+
import { IConfig } from "../config/config-interface";
|
7
|
+
import { connectDb } from "../helpers/dbcontext";
|
8
|
+
import { Server as socketServer } from "socket.io";
|
9
|
+
import { Result } from "../helpers/result";
|
10
|
+
import { ResponseCode } from "../helpers/enums";
|
11
|
+
const cors = require('cors');
|
12
|
+
export var SocketServer: socketServer = null;
|
13
|
+
export class Server {
|
14
|
+
app: express.Application;
|
15
|
+
|
16
|
+
constructor() {
|
17
|
+
this.app = express();
|
18
|
+
// this.config();
|
19
|
+
}
|
20
|
+
|
21
|
+
Create(config: IConfig) {
|
22
|
+
process.setMaxListeners(0);
|
23
|
+
|
24
|
+
this.app.set('env', config.variables.NODE_ENV);
|
25
|
+
this.app.set('port', config.props.port);
|
26
|
+
this.app.set('hostname', config.props.host_name);
|
27
|
+
this.app.use(bodyParser.json({ limit:'100mb'}));
|
28
|
+
// Increase body parser limit to accommodate larger payloads
|
29
|
+
this.app.use(express.json({ limit: '100mb' })); // Set JSON body parser limit
|
30
|
+
this.app.use(express.urlencoded({ limit: '100mb', extended: true })); // Set URL-encoded body parser limit
|
31
|
+
this.app.use((req, res, next) => {
|
32
|
+
res.header('Access-Control-Allow-Origin', '*');
|
33
|
+
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Content-Length, Accept, p-request-identifier');
|
34
|
+
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
|
35
|
+
next();
|
36
|
+
});
|
37
|
+
this.app.use(bodyParser.urlencoded({
|
38
|
+
extended: true
|
39
|
+
}));
|
40
|
+
// this.app.use(express.static(__dirname));
|
41
|
+
this.app.options('*', cors());
|
42
|
+
// this.app.use(cors)
|
43
|
+
const assetsPath = path.join(__dirname + '/assets');
|
44
|
+
// this.app.engine('hbs', engines.handlebars);
|
45
|
+
this.app.set('views', path.join(__dirname, '/views'));
|
46
|
+
console.log('***ASSETS PATH *** ', assetsPath);
|
47
|
+
this.app.use(express.static(assetsPath));
|
48
|
+
this.app.set('view engine', 'hbs');
|
49
|
+
var routeConfigs = new Routes();
|
50
|
+
routeConfigs.init(this.app);
|
51
|
+
var socketServerProvider = new SocketServerProvider(this.app, config);
|
52
|
+
SocketServer = socketServerProvider.io;
|
53
|
+
}
|
54
|
+
|
55
|
+
Start(config: IConfig) {
|
56
|
+
// let hostname = config.props.host_name,
|
57
|
+
// port = config.props.port;
|
58
|
+
// this.app.listen(config.props.port, function () {
|
59
|
+
// console.log('Express server listening on - http://' + hostname + ':' + port);
|
60
|
+
// });
|
61
|
+
connectDb().then(async () => {
|
62
|
+
let hostname = this.app.get('hostname'),
|
63
|
+
port = this.app.get('port');
|
64
|
+
// this.app.listen(port, function () {
|
65
|
+
// console.log('Express server listening on - http://' + hostname + ':' + port);
|
66
|
+
// // });
|
67
|
+
// var taskService = new TaskService(new Result(null, ResponseCode.Incompelete))
|
68
|
+
// await taskService.LoadScheduledTasks();
|
69
|
+
}).catch((ex) => {
|
70
|
+
console.error(ex);
|
71
|
+
});
|
72
|
+
console.log('started')
|
73
|
+
}
|
74
|
+
// private config(): void {
|
75
|
+
// //add support for application/json type for data
|
76
|
+
// this.app.use(bodyParser.json());
|
77
|
+
|
78
|
+
// //support application/x-www-form-urlencoded post data
|
79
|
+
// this.app.use(bodyParser.urlencoded({ extended: false }));
|
80
|
+
|
81
|
+
// this.app.get("/", (req, res) => {
|
82
|
+
// res.send("Welcome to HELLO!");
|
83
|
+
// });
|
84
|
+
// }
|
85
|
+
|
86
|
+
}
|
@@ -0,0 +1,209 @@
|
|
1
|
+
import * as express from "express";
|
2
|
+
import { Server, Socket } from 'socket.io';
|
3
|
+
import { IConfig } from "../config/config-interface";
|
4
|
+
import { RequestInit } from 'node-fetch'
|
5
|
+
import { AuditMessageType, ResponseCode, SocketEventCode, SocketIdentifierType } from "../helpers/enums";
|
6
|
+
import { AuditService } from "../services/audit-service";
|
7
|
+
import { Result } from "../helpers/result";
|
8
|
+
import { UUID } from "bson";
|
9
|
+
import { handleSocketResponse } from "../helpers/handle-response";
|
10
|
+
import { EncryptionHelper } from "../helpers/encryptionHelper";
|
11
|
+
import { AuthHelper } from "../helpers/auth";
|
12
|
+
import { readFileSync } from 'fs';
|
13
|
+
import * as https from 'https'
|
14
|
+
import * as path from 'path';
|
15
|
+
import { SocketServer } from "./index";
|
16
|
+
|
17
|
+
const http = require('http');
|
18
|
+
|
19
|
+
export class SocketServerProvider {
|
20
|
+
/**
|
21
|
+
*
|
22
|
+
*/
|
23
|
+
io: Server = null;
|
24
|
+
config: IConfig = null;
|
25
|
+
sockets: any[] = [];
|
26
|
+
expressApp: express.Application;
|
27
|
+
className = 'SocketServerProvider'
|
28
|
+
constructor(app: express.Application, config: IConfig) {
|
29
|
+
var socketServer = http.createServer(app);
|
30
|
+
|
31
|
+
this.expressApp = app
|
32
|
+
socketServer.listen(app.get('port'), () => {
|
33
|
+
console.log(`Express & Socket Server listening to ${app.get('port')} - ${new Date().toString()}`)
|
34
|
+
});
|
35
|
+
const cors = require('cors');
|
36
|
+
const allowList = ['https://app.sendifier.com', 'https://humble-macaw-separately.ngrok-free.app', 'https://www.sendifier.com', 'http://localhost:3000', 'http://127.0.0.1:3000', 'localhost:4000', 'http://127.0.0.1:4000'];
|
37
|
+
const allowUserAgents = ['facebookexternalua', 'PostmanRuntime/7.33.0', 'PostmanRuntime/7.35.0',
|
38
|
+
'PostmanRuntime/7.36.0', 'socket-io-postman-agent', 'curl/7.74.0'];
|
39
|
+
var corsOptionsDelegate = function (origin, callback) {
|
40
|
+
console.log('validating cors', JSON.stringify(origin))
|
41
|
+
|
42
|
+
var corsOptions: any = {}
|
43
|
+
var allowed = false;
|
44
|
+
if (allowList.indexOf(origin) !== -1) {
|
45
|
+
allowed = true;
|
46
|
+
} else {
|
47
|
+
if (allowUserAgents.indexOf(origin) !== -1) {
|
48
|
+
allowed = true;
|
49
|
+
}
|
50
|
+
else {
|
51
|
+
allowed = false;
|
52
|
+
}
|
53
|
+
}
|
54
|
+
corsOptions.origin = allowed;
|
55
|
+
corsOptions.methods = "GET,HEAD,PUT,PATCH,POST,DELETE";
|
56
|
+
corsOptions.headers = ['Authorization', 'Origin', 'X-Requested-With', 'Content-Type', 'Accept', 'Access-Control-Request-Method', 'Access-Control-Request-Headers', 'Cache-Control'];
|
57
|
+
if (allowed || !origin) {
|
58
|
+
callback(null, corsOptions)
|
59
|
+
}
|
60
|
+
else
|
61
|
+
callback('Not allowed source', corsOptions) // callback expects two parameters: error and options
|
62
|
+
}
|
63
|
+
|
64
|
+
this.io = new Server(socketServer, {
|
65
|
+
cors: {
|
66
|
+
origin: corsOptionsDelegate,
|
67
|
+
methods: '*',
|
68
|
+
allowedHeaders: '*',
|
69
|
+
|
70
|
+
'preflightContinue': true
|
71
|
+
},
|
72
|
+
maxHttpBufferSize: 1e8, // Set maximum HTTP buffer size (default is 1e6 bytes)
|
73
|
+
});
|
74
|
+
this.registerEvents();
|
75
|
+
}
|
76
|
+
async registerEvents() {
|
77
|
+
var auditService = new AuditService();
|
78
|
+
this.io.setMaxListeners(0);
|
79
|
+
this.io.sockets.on(SocketEventCode.Connect, async (socket) => {
|
80
|
+
console.log(`user connected with socket id ${socket.id} - ${new Date().toString()}`);
|
81
|
+
const query = socket.request['_query'];
|
82
|
+
if (query) {
|
83
|
+
const tabid = query.tabid;
|
84
|
+
if (tabid != null && tabid != undefined && tabid != 'null' && tabid != 'undefined') {
|
85
|
+
auditService.AddAuditSingle(AuditMessageType.socket, this.className, SocketEventCode.Connect, tabid, 'Socket connected', this.getRequestAdditionalInfo(tabid));
|
86
|
+
if (!this.sockets[tabid]) {
|
87
|
+
this.sockets[tabid] = {};
|
88
|
+
}
|
89
|
+
// else {
|
90
|
+
// this.sockets[tabid] = { socketId: socket.id, tabid: tabid, tkn: query.currentContact };
|
91
|
+
// console.info(`Tab {${tabid}} added with socket {${socket.id}} - ${new Date().toString()}`);
|
92
|
+
// auditService.AddAuditSingle(AuditMessageType.socket, this.className, SocketEventCode.Connect, tabid, `Tab {${tabid}} added with socket {${socket.id}}`, this.getRequestAdditionalInfo(tabid));
|
93
|
+
// }
|
94
|
+
this.sockets[tabid].socketId = socket.id;
|
95
|
+
this.sockets[tabid].tabid = tabid;
|
96
|
+
this.sockets[tabid].tkn = query.currentUser;
|
97
|
+
if (query.currentUser && query.currentUser != 'null') {
|
98
|
+
this.sockets[tabid].user = JSON.parse(new EncryptionHelper().decryptUserLogin(query.currentUser));
|
99
|
+
}
|
100
|
+
auditService.AddAuditSingle(AuditMessageType.socket, this.className, SocketEventCode.Connect, tabid, `Socket {${socket.id}} registered for tab {${tabid}}`, this.getRequestAdditionalInfo(tabid));
|
101
|
+
|
102
|
+
}
|
103
|
+
else {
|
104
|
+
this.io.emit(SocketEventCode.ReloadClient);
|
105
|
+
auditService.AddAuditSingle(AuditMessageType.socket, this.className, SocketEventCode.ReloadClient, tabid, `Connection doesn't have tabid, reloaded`, this.getRequestAdditionalInfo(tabid));
|
106
|
+
}
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
socket.on(SocketEventCode.Error, (error) => {
|
112
|
+
console.error('Server error:', error);
|
113
|
+
});
|
114
|
+
socket.on(SocketEventCode.UserLoggedIn, async (payload, callback) => {
|
115
|
+
try {
|
116
|
+
var data = payload.data;
|
117
|
+
var tabid = payload.tabid;
|
118
|
+
console.log(`User {${data.email}} is logged in from Tab {${tabid}} with socket {${socket.id}} - ${new Date().toString()}`);
|
119
|
+
auditService.AddAuditSingle(AuditMessageType.socket, this.className, SocketEventCode.UserLoggedIn, tabid, `User logged in`, this.getRequestAdditionalInfo(tabid));
|
120
|
+
this.sockets[tabid].tkn = payload.currentUser;
|
121
|
+
var userProfile = new EncryptionHelper().decryptUserLogin(payload.currentUser);
|
122
|
+
this.sockets[tabid].user = userProfile
|
123
|
+
socket.join(`${userProfile._defaultCompanyId}`);
|
124
|
+
socket.join(`${userProfile._defaultCompanyId}_${userProfile._id}`);
|
125
|
+
auditService.AddAuditSingle(AuditMessageType.socket, this.className, SocketEventCode.UserLoggedIn, tabid, `User joined default company room`, this.getRequestAdditionalInfo(tabid));
|
126
|
+
}
|
127
|
+
catch (ex) {
|
128
|
+
callback(new Result(null, ResponseCode.Unauthorized, [ex], ['unauthenticated request'], new UUID().toString()));
|
129
|
+
}
|
130
|
+
});
|
131
|
+
socket.on(SocketEventCode.UserLoggedOut, (payload) => {
|
132
|
+
const tabid = payload.tabid;
|
133
|
+
auditService.AddAuditSingle(AuditMessageType.socket, this.className, SocketEventCode.UserLoggedOut, tabid, `User logged out`, this.getRequestAdditionalInfo(tabid));
|
134
|
+
delete this.sockets[tabid].user;
|
135
|
+
});
|
136
|
+
socket.on(SocketEventCode.Close, (data) => {
|
137
|
+
socket.disconnect(true);
|
138
|
+
var socketToBeDisconnected = this.sockets[data.tabid];
|
139
|
+
auditService.AddAuditSingle(AuditMessageType.socket, this.className, SocketEventCode.UserLoggedOut, data.tabid, `User disonnected`, this.getRequestAdditionalInfo(data.tabid));
|
140
|
+
delete this.sockets[data.tabid]
|
141
|
+
});
|
142
|
+
socket.on(SocketEventCode.RespondWhatsappMessage, async (payload) => {
|
143
|
+
var requestIdentifier = new UUID().toString();
|
144
|
+
var senderSocket = this.sockets[payload.tabid];
|
145
|
+
var result = new Result(null, ResponseCode.Incompelete, [], [], requestIdentifier, this.getRequestAdditionalInfo(payload.tabid), senderSocket.user);
|
146
|
+
result.addMessage(AuditMessageType.socket, this.className, SocketEventCode.RespondWhatsappMessage, 'Send whatsapp message')
|
147
|
+
const data = payload.data;
|
148
|
+
var whatsappProviderService = null;//new WhatsappProviderService(result);
|
149
|
+
await whatsappProviderService.SendWhatsappMessage(data, senderSocket);
|
150
|
+
handleSocketResponse(result);
|
151
|
+
});
|
152
|
+
socket.on(SocketEventCode.VerifyToken, async (payload, callback) => {
|
153
|
+
var tabid = payload.tabid;
|
154
|
+
var userSoket = this.sockets[tabid];
|
155
|
+
if (userSoket && userSoket.user) {
|
156
|
+
var authHelper = new AuthHelper(new Result(null, ResponseCode.Ok, [], [], payload.tabid, {}, userSoket.user));
|
157
|
+
var verifyTokenResult = await authHelper.VerifyToken(userSoket.user.token);
|
158
|
+
if (verifyTokenResult.Status == ResponseCode.Ok) {
|
159
|
+
callback(true)
|
160
|
+
}
|
161
|
+
else {
|
162
|
+
callback(false)
|
163
|
+
}
|
164
|
+
}
|
165
|
+
else {
|
166
|
+
socket.emit(SocketEventCode.ReloadClient);
|
167
|
+
}
|
168
|
+
});
|
169
|
+
socket.on(SocketEventCode.RefreshToken, async (payload, callback) => {
|
170
|
+
var tabid = payload.tabid;
|
171
|
+
var data = payload.data;
|
172
|
+
var encryptionHelper = new EncryptionHelper();
|
173
|
+
var userSoket = this.sockets[tabid];
|
174
|
+
if (userSoket && userSoket.user) {
|
175
|
+
this.sockets[tabid].user.token = data;
|
176
|
+
var response = this.sockets[tabid].user;
|
177
|
+
this.sockets[tabid].tkn = response;
|
178
|
+
callback(response);
|
179
|
+
}
|
180
|
+
else {
|
181
|
+
socket.emit(SocketEventCode.ReloadClient);
|
182
|
+
}
|
183
|
+
});
|
184
|
+
});
|
185
|
+
}
|
186
|
+
|
187
|
+
getRequestAdditionalInfo(tabid) {
|
188
|
+
var requestedSocket = this.sockets[tabid];
|
189
|
+
var result: any = { tabId: tabid };
|
190
|
+
if (requestedSocket) {
|
191
|
+
result.socketId = requestedSocket.socketId;
|
192
|
+
if (requestedSocket.user) {
|
193
|
+
result.userProfile = {
|
194
|
+
id: requestedSocket.user.id,
|
195
|
+
name: requestedSocket.user.name,
|
196
|
+
phone: requestedSocket.user.phone,
|
197
|
+
email: requestedSocket.user.email,
|
198
|
+
defaultCompany: requestedSocket.user._defaultCompanyId,
|
199
|
+
requestIdentifier: requestedSocket.requestIdentifier
|
200
|
+
}
|
201
|
+
}
|
202
|
+
}
|
203
|
+
return result;
|
204
|
+
}
|
205
|
+
}
|
206
|
+
export const emitEvent = async (groupId, eventCode: SocketEventCode, payload) => {
|
207
|
+
SocketServer.to(String(groupId)).emit(eventCode, payload);
|
208
|
+
|
209
|
+
}
|