@simitgroup/simpleapp-generator 1.1.7 → 1.1.8
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/generate.js +9 -2
- package/dist/generate.js.map +1 -1
- package/package.json +1 -1
- package/src/generate.ts +15 -4
- package/templates/basic/nest/controller.ts.eta +8 -2
- package/templates/basic/nest/test.ts.eta +97 -40
- package/templates/nest/.env._eta +1 -0
- package/templates/nest/src/main.ts.eta +1 -1
- package/templates/nest/src/simpleapp/generate/apischemas/index.ts.eta +3 -3
- package/templates/nest/src/simpleapp/generate/commons/exceptions/SimpleAppExceptionFilter.ts.eta +43 -34
- package/templates/nest/src/simpleapp/generate/commons/interceptors/response.interceptor.ts.eta +110 -59
- package/templates/nest/src/simpleapp/generate/commons/middlewares/tenant.middleware.ts.eta +56 -45
- package/templates/nest/src/simpleapp/generate/commons/user.context.ts.eta +408 -344
- package/templates/nest/src/simpleapp/generate/processors/simpleapp.processor.ts.eta +6 -0
- package/templates/nest/test/app.e2e-spec.ts.eta +20 -14
- package/templates/nest/test/setting.ts.eta +78 -79
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -4,28 +4,37 @@
|
|
|
4
4
|
* last change 2023-10-28
|
|
5
5
|
* Author: Ks Tan
|
|
6
6
|
*/
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
Injectable,
|
|
9
|
+
Scope,
|
|
10
|
+
Inject,
|
|
11
|
+
Logger,
|
|
12
|
+
BadRequestException,
|
|
13
|
+
} from '@nestjs/common';
|
|
8
14
|
import { Model, model, connect, PipelineStage } from 'mongoose';
|
|
9
|
-
import {ModifiedCollection,ModifiedRecords} from '../types'
|
|
10
|
-
import _ from 'lodash'
|
|
15
|
+
import { ModifiedCollection, ModifiedRecords } from '../types';
|
|
16
|
+
import _ from 'lodash';
|
|
11
17
|
import { Module } from '@nestjs/common';
|
|
12
18
|
import * as jwt from 'jsonwebtoken';
|
|
13
19
|
import { Role } from './roles/roles.enum';
|
|
14
|
-
import * as rolegroups from './roles/roles.group'
|
|
20
|
+
import * as rolegroups from './roles/roles.group';
|
|
15
21
|
import { UserService } from './../../services/user.service';
|
|
16
22
|
import { InjectModel } from '@nestjs/mongoose';
|
|
17
23
|
const Base64URL = require('@darkwolf/base64url');
|
|
18
24
|
import { UserMongoSchema } from './../models/user.model';
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
25
|
+
import { User } from './../types/user.type';
|
|
26
|
+
import { Permission } from './../types/perm.type';
|
|
27
|
+
import {
|
|
28
|
+
ProfileUserBranch,
|
|
29
|
+
ProfileUserInvites,
|
|
30
|
+
} from '../../profile/profile.types';
|
|
22
31
|
|
|
23
|
-
import {ClientSession}
|
|
32
|
+
import { ClientSession } from 'mongoose';
|
|
24
33
|
|
|
25
|
-
@Injectable({scope:Scope.REQUEST})
|
|
34
|
+
@Injectable({ scope: Scope.REQUEST })
|
|
26
35
|
export class UserContext {
|
|
27
|
-
protected sessionId
|
|
28
|
-
protected logger = new Logger()
|
|
36
|
+
protected sessionId: string = crypto.randomUUID();
|
|
37
|
+
protected logger = new Logger();
|
|
29
38
|
protected uid: string = '';
|
|
30
39
|
protected _id: string = '';
|
|
31
40
|
protected uname: string = '';
|
|
@@ -34,32 +43,36 @@ export class UserContext {
|
|
|
34
43
|
protected xOrg: string = '';
|
|
35
44
|
protected tenantId: number = 0;
|
|
36
45
|
protected orgId: number = 0;
|
|
37
|
-
protected orgRecordId:string = ''
|
|
38
|
-
protected branchRecordId:string = ''
|
|
46
|
+
protected orgRecordId: string = '';
|
|
47
|
+
protected branchRecordId: string = '';
|
|
39
48
|
protected branchId: number = 0;
|
|
40
49
|
protected ssoACL: any = {};
|
|
41
50
|
protected token: string = '';
|
|
42
51
|
protected refreshtoken: string = '';
|
|
43
52
|
protected group: string = '';
|
|
44
53
|
protected branchCode: string = '';
|
|
45
|
-
protected branchName: string = '';
|
|
54
|
+
protected branchName: string = '';
|
|
46
55
|
protected orgCode: string = '';
|
|
47
|
-
protected orgName: string = '';
|
|
48
|
-
protected branches: any[] = []
|
|
49
|
-
protected lastActivity
|
|
50
|
-
protected invites: any[] = []
|
|
56
|
+
protected orgName: string = '';
|
|
57
|
+
protected branches: any[] = [];
|
|
58
|
+
protected lastActivity: string = new Date().toISOString();
|
|
59
|
+
protected invites: any[] = []; //User + field tenant:Tenant[]
|
|
51
60
|
protected roles: string[] = [];
|
|
52
|
-
|
|
61
|
+
private dbsession: ClientSession
|
|
62
|
+
protected modifiedRecords: ModifiedRecords = {
|
|
63
|
+
createds: {},
|
|
64
|
+
updateds: {},
|
|
65
|
+
deleteds: {},
|
|
66
|
+
};
|
|
53
67
|
|
|
54
68
|
constructor(
|
|
55
|
-
private readonly usermodel:Model<User>,
|
|
56
|
-
private readonly permmodel:Model<Permission>,
|
|
57
|
-
private dbsession:ClientSession,
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
getDBSession = ():ClientSession => this.dbsession
|
|
69
|
+
private readonly usermodel: Model<User>,
|
|
70
|
+
private readonly permmodel: Model<Permission>,
|
|
71
|
+
// private dbsession: ClientSession,
|
|
72
|
+
) {}
|
|
73
|
+
|
|
74
|
+
setDBSession = (dbsession:ClientSession) => {this.dbsession = dbsession}
|
|
75
|
+
getDBSession = (): ClientSession => this.dbsession;
|
|
63
76
|
getId = () => this._id;
|
|
64
77
|
getUid = () => this.uid;
|
|
65
78
|
getUname = () => this.uname;
|
|
@@ -70,36 +83,33 @@ export class UserContext {
|
|
|
70
83
|
getEmail = () => this.email;
|
|
71
84
|
getGroup = () => this.group;
|
|
72
85
|
getRoles = () => this.roles;
|
|
73
|
-
getModifieds = () => this.modifiedRecords
|
|
74
|
-
getBranches = ():ProfileUserBranch[] => {
|
|
86
|
+
getModifieds = () => this.modifiedRecords;
|
|
87
|
+
getBranches = (): ProfileUserBranch[] => {
|
|
75
88
|
this.branches;
|
|
76
|
-
const data:ProfileUserBranch[] = []
|
|
89
|
+
const data: ProfileUserBranch[] = [];
|
|
77
90
|
|
|
78
|
-
if(this.branches){
|
|
79
|
-
for(let i=0;i<this.branches.length; i++){
|
|
80
|
-
const b= this.branches[i]
|
|
91
|
+
if (this.branches) {
|
|
92
|
+
for (let i = 0; i < this.branches.length; i++) {
|
|
93
|
+
const b = this.branches[i];
|
|
81
94
|
data.push({
|
|
82
|
-
_id: b._id,
|
|
95
|
+
_id: b._id,
|
|
83
96
|
branch: b.branch[0],
|
|
84
97
|
group: b.group,
|
|
85
|
-
xOrg: this.generateXorg(b.tenantId,b.orgId,b.branchId)
|
|
86
|
-
|
|
87
|
-
})
|
|
98
|
+
xOrg: this.generateXorg(b.tenantId, b.orgId, b.branchId),
|
|
99
|
+
});
|
|
88
100
|
}
|
|
89
101
|
}
|
|
90
|
-
return data
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
const data:ProfileUserInvites[] = []
|
|
98
|
-
if( this.invites){
|
|
102
|
+
return data;
|
|
103
|
+
};
|
|
104
|
+
getInvites = (): ProfileUserInvites[] => {
|
|
105
|
+
// const usermodel = model<User>('user',UserMongoSchema,'user')
|
|
106
|
+
|
|
107
|
+
const data: ProfileUserInvites[] = [];
|
|
108
|
+
if (this.invites) {
|
|
99
109
|
// console.log("getInvites",res)
|
|
100
|
-
for(let i=0;i< this.invites.length; i++){
|
|
101
|
-
const r =
|
|
102
|
-
|
|
110
|
+
for (let i = 0; i < this.invites.length; i++) {
|
|
111
|
+
const r = this.invites[0];
|
|
112
|
+
|
|
103
113
|
data.push({
|
|
104
114
|
_id: r._id,
|
|
105
115
|
email: r.email,
|
|
@@ -107,147 +117,171 @@ export class UserContext {
|
|
|
107
117
|
fullName: '',
|
|
108
118
|
tenantName: r.tenant[0].tenantName,
|
|
109
119
|
created: r.created,
|
|
110
|
-
})
|
|
120
|
+
});
|
|
111
121
|
}
|
|
112
122
|
}
|
|
113
|
-
return data
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
this.
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
123
|
+
return data;
|
|
124
|
+
};
|
|
125
|
+
setCurrentUserInfo = async (tokenstr: string, xorg) => {
|
|
126
|
+
this.setXorg(xorg);
|
|
127
|
+
await this.setUserToken(tokenstr);
|
|
128
|
+
};
|
|
121
129
|
|
|
122
130
|
/**
|
|
123
131
|
* obtain user profile filter by uid,tenantId,orgId,branchId
|
|
124
132
|
* @returns Promise<User|undefined>
|
|
125
133
|
*/
|
|
126
|
-
obtainProfileFromDb = async ()
|
|
127
|
-
const filter = { $match:{uid:this.uid,tenantId:this.tenantId} }
|
|
128
|
-
|
|
129
|
-
const joinpermission:PipelineStage = {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
134
|
+
obtainProfileFromDb = async (): Promise<User | undefined> => {
|
|
135
|
+
const filter = { $match: { uid: this.uid, tenantId: this.tenantId } };
|
|
136
|
+
|
|
137
|
+
const joinpermission: PipelineStage = {
|
|
138
|
+
$lookup: {
|
|
139
|
+
from: 'permission',
|
|
140
|
+
localField: '_id',
|
|
141
|
+
foreignField: 'userId',
|
|
142
|
+
as: 'permissions',
|
|
143
|
+
pipeline: [
|
|
144
|
+
{
|
|
145
|
+
$match: {
|
|
137
146
|
// userId:this.getId(),
|
|
138
|
-
tenantId:this.tenantId,
|
|
139
|
-
orgId:this.orgId,
|
|
140
|
-
branchId:this.branchId,
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
{
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
147
|
+
tenantId: this.tenantId,
|
|
148
|
+
orgId: this.orgId,
|
|
149
|
+
branchId: this.branchId,
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
$lookup: {
|
|
154
|
+
from: 'branch',
|
|
155
|
+
localField: 'branchId',
|
|
156
|
+
foreignField: 'branchId',
|
|
157
|
+
as: 'currentbranch',
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
$lookup: {
|
|
162
|
+
from: 'organization',
|
|
163
|
+
localField: 'orgId',
|
|
164
|
+
foreignField: 'orgId',
|
|
165
|
+
as: 'currentorg',
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
],
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const pipeline: PipelineStage[] = [filter];
|
|
173
|
+
|
|
174
|
+
if (this.tenantId > 0) {
|
|
175
|
+
pipeline.push(joinpermission);
|
|
151
176
|
}
|
|
152
177
|
// console.log("obtainProfileFromDbobtainProfileFromDb ",pipeline)
|
|
153
178
|
// const users = await usermodel.find(filter)
|
|
154
|
-
const users = await this.usermodel.aggregate(pipeline)
|
|
155
|
-
this.logger.verbose(
|
|
156
|
-
|
|
157
|
-
|
|
179
|
+
const users = await this.usermodel.aggregate(pipeline);
|
|
180
|
+
this.logger.verbose(
|
|
181
|
+
`aggregate user profile from database`,
|
|
182
|
+
'obtainProfileFromDb',
|
|
183
|
+
);
|
|
184
|
+
this.logger.verbose(pipeline, 'obtainProfileFromDb x');
|
|
185
|
+
this.logger.verbose(users, 'obtainProfileFromDb');
|
|
158
186
|
// console.log("obtainProfileFromDbobtainProfileFromDb ",users)
|
|
159
|
-
if(users && users.length>0){
|
|
160
|
-
const userinfo = users[0]
|
|
187
|
+
if (users && users.length > 0) {
|
|
188
|
+
const userinfo = users[0];
|
|
161
189
|
|
|
162
190
|
//console.log(userinfo)
|
|
163
|
-
if(this.tenantId>0){
|
|
164
|
-
const myperm=userinfo.permissions[0]
|
|
165
|
-
|
|
166
|
-
if(myperm && myperm.group){
|
|
167
|
-
userinfo.group = myperm.group
|
|
168
|
-
userinfo.roles = rolegroups[userinfo.group]()
|
|
169
|
-
|
|
170
|
-
userinfo.branchRecordId = myperm.currentbranch[0].branchRecordId
|
|
171
|
-
userinfo.branchCode = myperm.currentbranch[0].branchCode
|
|
172
|
-
userinfo.branchName = myperm.currentbranch[0].branchName
|
|
173
|
-
userinfo.orgRecordId = myperm.currentorg[0]._id
|
|
174
|
-
userinfo.orgCode = myperm.currentorg[0].orgCode
|
|
175
|
-
userinfo.orgName = myperm.currentorg[0].orgName
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
userinfo.group = ''
|
|
191
|
+
if (this.tenantId > 0) {
|
|
192
|
+
const myperm = userinfo.permissions[0];
|
|
193
|
+
|
|
194
|
+
if (myperm && myperm.group) {
|
|
195
|
+
userinfo.group = myperm.group;
|
|
196
|
+
userinfo.roles = rolegroups[userinfo.group]();
|
|
197
|
+
|
|
198
|
+
userinfo.branchRecordId = myperm.currentbranch[0].branchRecordId;
|
|
199
|
+
userinfo.branchCode = myperm.currentbranch[0].branchCode;
|
|
200
|
+
userinfo.branchName = myperm.currentbranch[0].branchName;
|
|
201
|
+
userinfo.orgRecordId = myperm.currentorg[0]._id;
|
|
202
|
+
userinfo.orgCode = myperm.currentorg[0].orgCode;
|
|
203
|
+
userinfo.orgName = myperm.currentorg[0].orgName;
|
|
204
|
+
}
|
|
205
|
+
} else {
|
|
206
|
+
userinfo.group = '';
|
|
180
207
|
}
|
|
181
|
-
|
|
182
|
-
const currentitme = new Date(this.lastActivity).getTime()
|
|
183
|
-
|
|
184
|
-
const dblastactivity = userinfo.lastActivity ?? '2000-01-01T00:00:00Z'
|
|
185
|
-
const lastvisit = new Date(
|
|
208
|
+
|
|
209
|
+
const currentitme = new Date(this.lastActivity).getTime();
|
|
210
|
+
|
|
211
|
+
const dblastactivity = userinfo.lastActivity ?? '2000-01-01T00:00:00Z';
|
|
212
|
+
const lastvisit = new Date(dblastactivity).getTime() ?? 0;
|
|
186
213
|
// if(currentitme - lastvisit)
|
|
187
|
-
|
|
188
214
|
|
|
189
215
|
//update last activtity dont too frequent
|
|
190
|
-
//if(!dblastactivity || currentitme - lastvisit > 5000 ){
|
|
191
|
-
// const newusermodel = await this.usermodel.findById(userinfo._id)
|
|
192
|
-
// newusermodel.lastActivity= this.lastActivity
|
|
216
|
+
//if(!dblastactivity || currentitme - lastvisit > 5000 ){
|
|
217
|
+
// const newusermodel = await this.usermodel.findById(userinfo._id)
|
|
218
|
+
// newusermodel.lastActivity= this.lastActivity
|
|
193
219
|
// const result = await newusermodel.save()
|
|
194
220
|
//}
|
|
195
|
-
|
|
221
|
+
|
|
196
222
|
// const result = await this.usermodel.findOneAndUpdate({_id: userinfo._id},{lastActivity: new Date().toISOString})
|
|
197
|
-
return userinfo
|
|
198
|
-
}else{
|
|
199
|
-
return undefined
|
|
223
|
+
return userinfo;
|
|
224
|
+
} else {
|
|
225
|
+
return undefined;
|
|
200
226
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
setUserToken = async (tokenstr: string) => {
|
|
204
|
-
|
|
227
|
+
};
|
|
228
|
+
setUserToken = async (tokenstr: string) => {
|
|
205
229
|
//define token info
|
|
206
|
-
const tokeninfo = jwt.decode(tokenstr);
|
|
230
|
+
const tokeninfo = jwt.decode(tokenstr);
|
|
207
231
|
this.token = tokenstr;
|
|
208
232
|
this.uid = tokeninfo.sub;
|
|
209
233
|
this.email = tokeninfo.email;
|
|
210
234
|
this.uname = tokeninfo.preferred_username;
|
|
211
|
-
this.fullname = tokeninfo.name;
|
|
212
|
-
this.ssoACL = tokeninfo.resource_access;
|
|
213
|
-
this.logger.verbose(`set token ${this.uid}`)
|
|
235
|
+
this.fullname = tokeninfo.name;
|
|
236
|
+
this.ssoACL = tokeninfo.resource_access;
|
|
237
|
+
this.logger.verbose(`set token ${this.uid}`);
|
|
214
238
|
//read current user from db
|
|
215
239
|
// console.log("await this.obtainProfileFromDb()")
|
|
216
|
-
const userinfo = await this.obtainProfileFromDb()
|
|
217
|
-
this.logger.verbose(userinfo,'obtainProfileFromDb result')
|
|
218
|
-
if(userinfo){
|
|
219
|
-
this.logger.debug(
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
this.
|
|
225
|
-
this.
|
|
226
|
-
this.
|
|
227
|
-
this.
|
|
228
|
-
this.
|
|
229
|
-
this.
|
|
230
|
-
|
|
231
|
-
this.
|
|
240
|
+
const userinfo = await this.obtainProfileFromDb();
|
|
241
|
+
this.logger.verbose(userinfo, 'obtainProfileFromDb result');
|
|
242
|
+
if (userinfo) {
|
|
243
|
+
this.logger.debug(
|
|
244
|
+
`${this.uid} user exists in tenant (${this.tenantId})`,
|
|
245
|
+
'setUserToken',
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
this._id = userinfo._id.toString();
|
|
249
|
+
this.branchCode = userinfo['branchCode'] ?? '';
|
|
250
|
+
this.branchName = userinfo['branchName'] ?? '';
|
|
251
|
+
this.orgCode = userinfo['orgCode'] ?? '';
|
|
252
|
+
this.orgName = userinfo['orgName'] ?? '';
|
|
253
|
+
this.orgRecordId = userinfo['orgRecordId'] ?? '';
|
|
254
|
+
this.branchRecordId = userinfo['branchRecordId'] ?? '';
|
|
255
|
+
this.group = userinfo['group'] ?? '';
|
|
256
|
+
this.roles = userinfo['roles'] ?? [Role.Everyone, Role.User];
|
|
257
|
+
} else {
|
|
258
|
+
this.logger.debug(`Set unknown id of current user`, 'setUserToken');
|
|
232
259
|
// this.group = ''
|
|
233
260
|
// this.tenantId=0
|
|
234
261
|
// this.orgId=0
|
|
235
262
|
// this.orgCode=0
|
|
236
|
-
this.roles = [Role.Everyone,Role.Unknown]
|
|
263
|
+
this.roles = [Role.Everyone, Role.Unknown];
|
|
237
264
|
}
|
|
238
|
-
this.logger.debug(
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
this.
|
|
265
|
+
this.logger.debug(
|
|
266
|
+
`${this.uid} have _id (${this.getId()}), group (${
|
|
267
|
+
this.group
|
|
268
|
+
}) and role (${this.getRoles()})`,
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
if (this.isRealmAdmin() && !this.roles.includes(Role.SuperAdmin)) {
|
|
272
|
+
this.roles.push(Role.SuperAdmin);
|
|
242
273
|
}
|
|
243
|
-
this.logger.verbose(this)
|
|
274
|
+
this.logger.verbose(this);
|
|
244
275
|
};
|
|
245
276
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
277
|
+
generateXorg = (
|
|
278
|
+
tenantId: number,
|
|
279
|
+
orgId: number,
|
|
280
|
+
branchId: number,
|
|
281
|
+
): string => {
|
|
282
|
+
return Base64URL.encodeText(`${tenantId}-${orgId}-${branchId}`);
|
|
283
|
+
};
|
|
284
|
+
getInfo = () => {
|
|
251
285
|
return this;
|
|
252
286
|
};
|
|
253
287
|
getBranchFilter = () => {
|
|
@@ -282,16 +316,16 @@ export class UserContext {
|
|
|
282
316
|
createdBy: u.uid,
|
|
283
317
|
updatedBy: u.uid,
|
|
284
318
|
created: new Date().toISOString(),
|
|
285
|
-
updated: new Date().toISOString()
|
|
319
|
+
updated: new Date().toISOString(),
|
|
286
320
|
};
|
|
287
321
|
};
|
|
288
322
|
getUpdateFilter = () => {
|
|
289
323
|
const u = this;
|
|
290
324
|
return {
|
|
291
325
|
updatedBy: u.uid,
|
|
292
|
-
updated: new Date().toISOString()
|
|
326
|
+
updated: new Date().toISOString(),
|
|
293
327
|
};
|
|
294
|
-
};
|
|
328
|
+
};
|
|
295
329
|
setXorg = (xorg) => {
|
|
296
330
|
try {
|
|
297
331
|
const decodedText: string = Base64URL.decodeText(xorg);
|
|
@@ -301,8 +335,7 @@ export class UserContext {
|
|
|
301
335
|
const u = this;
|
|
302
336
|
u.tenantId = Number(arrXorg[0]);
|
|
303
337
|
u.orgId = Number(arrXorg[1]);
|
|
304
|
-
u.branchId = Number(arrXorg[2]);
|
|
305
|
-
|
|
338
|
+
u.branchId = Number(arrXorg[2]);
|
|
306
339
|
} else {
|
|
307
340
|
throw 'invalid x-org';
|
|
308
341
|
}
|
|
@@ -311,233 +344,264 @@ export class UserContext {
|
|
|
311
344
|
}
|
|
312
345
|
};
|
|
313
346
|
|
|
314
|
-
|
|
315
|
-
async getUserInfo(){
|
|
316
|
-
|
|
347
|
+
async getUserInfo() {
|
|
317
348
|
// obtain basic user info
|
|
318
349
|
const userinfo = {
|
|
319
|
-
_id: this.getId()
|
|
320
|
-
sessionId:this.sessionId,
|
|
350
|
+
_id: this.getId(),
|
|
351
|
+
sessionId: this.sessionId,
|
|
321
352
|
tenantId: this.getTenantId(),
|
|
322
353
|
orgId: this.getOrgId(),
|
|
323
|
-
orgRecordId: this.orgRecordId
|
|
354
|
+
orgRecordId: this.orgRecordId,
|
|
324
355
|
branchRecordId: this.branchRecordId,
|
|
325
356
|
branchId: this.getBranchId(),
|
|
326
357
|
branchCode: this.branchCode,
|
|
327
358
|
branchName: this.branchName,
|
|
328
|
-
orgCode:this.orgCode,
|
|
329
|
-
orgName:this.orgName,
|
|
359
|
+
orgCode: this.orgCode,
|
|
360
|
+
orgName: this.orgName,
|
|
330
361
|
email: this.getEmail(),
|
|
331
362
|
uid: this.getUid(),
|
|
332
363
|
fullName: this.getFullname(),
|
|
333
364
|
group: this.group,
|
|
334
365
|
roles: this.getRoles(),
|
|
335
366
|
branches: this.getBranches(),
|
|
336
|
-
invites: await this.getInvites()
|
|
337
|
-
}
|
|
338
|
-
this.logger.debug(userinfo,
|
|
339
|
-
|
|
340
|
-
if(this.getId()!=''){
|
|
341
|
-
this.logger.debug(userinfo,
|
|
342
|
-
const filter:PipelineStage =
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
367
|
+
invites: await this.getInvites(),
|
|
368
|
+
};
|
|
369
|
+
this.logger.debug(userinfo, 'init getUserInfo()');
|
|
370
|
+
|
|
371
|
+
if (this.getId() != '') {
|
|
372
|
+
this.logger.debug(userinfo, 'obtain permissions and invitations');
|
|
373
|
+
const filter: PipelineStage = {
|
|
374
|
+
$match: {
|
|
375
|
+
uid: this.uid,
|
|
376
|
+
tenantId: this.tenantId,
|
|
377
|
+
},
|
|
378
|
+
} as PipelineStage;
|
|
379
|
+
const permission: PipelineStage = {
|
|
380
|
+
$lookup: {
|
|
381
|
+
from: 'permission',
|
|
382
|
+
localField: '_id',
|
|
383
|
+
foreignField: 'userId',
|
|
384
|
+
as: 'permissions',
|
|
385
|
+
pipeline: [
|
|
386
|
+
// {$match:{userId: this.getId(),},},
|
|
387
|
+
{
|
|
388
|
+
$lookup: {
|
|
389
|
+
from: 'branch',
|
|
390
|
+
localField: 'branchId',
|
|
391
|
+
foreignField: 'branchId',
|
|
392
|
+
as: 'branch',
|
|
393
|
+
},
|
|
394
|
+
},
|
|
395
|
+
],
|
|
396
|
+
},
|
|
397
|
+
};
|
|
398
|
+
const lookupinvitation: PipelineStage = {
|
|
399
|
+
$lookup: {
|
|
400
|
+
from: 'user',
|
|
401
|
+
localField: 'email',
|
|
402
|
+
foreignField: 'email',
|
|
403
|
+
as: 'invites',
|
|
404
|
+
pipeline: [
|
|
405
|
+
{ $match: { uid: '' } },
|
|
406
|
+
{
|
|
407
|
+
$lookup: {
|
|
408
|
+
from: 'tenant',
|
|
409
|
+
localField: 'tenantId',
|
|
410
|
+
foreignField: 'tenantId',
|
|
411
|
+
as: 'tenant',
|
|
412
|
+
},
|
|
413
|
+
},
|
|
414
|
+
],
|
|
415
|
+
},
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
const pipeline: PipelineStage[] = [filter, permission, lookupinvitation];
|
|
419
|
+
this.logger.debug(pipeline, 'getUserInfo');
|
|
420
|
+
// // // const users = await usermodel.find(filter)
|
|
421
|
+
const users = await this.usermodel.aggregate(pipeline);
|
|
422
|
+
this.logger.debug(users, 'users from aggregate');
|
|
423
|
+
|
|
424
|
+
this.invites = users[0].invites;
|
|
425
|
+
this.branches = users[0].permissions;
|
|
426
|
+
this.logger.verbose(users, 'getUserInfo');
|
|
427
|
+
userinfo.branches = this.getBranches();
|
|
428
|
+
userinfo.invites = this.getInvites();
|
|
429
|
+
// this.logger.debug(`getUserInfo result ${userinfo}`)
|
|
430
|
+
} else {
|
|
431
|
+
this.logger.log(
|
|
432
|
+
`${this.getUid()} does not exists in current tenant (${this.tenantId})`,
|
|
433
|
+
);
|
|
387
434
|
}
|
|
388
|
-
|
|
389
435
|
|
|
390
|
-
return userinfo
|
|
436
|
+
return userinfo;
|
|
391
437
|
}
|
|
392
|
-
async decideInvitation
|
|
393
|
-
|
|
438
|
+
async decideInvitation(id: string, decision: string): Promise<boolean> {
|
|
394
439
|
// const usermodel = model<User>('user',UserMongoSchema,'user')
|
|
395
|
-
const res = await this.usermodel.findById(id)
|
|
440
|
+
const res = await this.usermodel.findById(id);
|
|
396
441
|
// console.log("find invitation:",res)
|
|
397
|
-
if(!res.uid){
|
|
398
|
-
res.uid = this.getUid()
|
|
399
|
-
res.fullName = this.fullname
|
|
400
|
-
res.active = true
|
|
401
|
-
|
|
402
|
-
if(decision=='accept'){
|
|
403
|
-
const result = await res.save({session:this.dbsession})
|
|
404
|
-
this.logger.log(result,
|
|
442
|
+
if (!res.uid) {
|
|
443
|
+
res.uid = this.getUid();
|
|
444
|
+
res.fullName = this.fullname;
|
|
445
|
+
res.active = true;
|
|
446
|
+
|
|
447
|
+
if (decision == 'accept') {
|
|
448
|
+
const result = await res.save({ session: this.dbsession });
|
|
449
|
+
this.logger.log(result, 'accept invitation 1');
|
|
405
450
|
//set permission of all user under this.user_id
|
|
406
|
-
const updateresult = await this.permmodel
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
451
|
+
const updateresult = await this.permmodel
|
|
452
|
+
.updateMany({ uid: '', user_id: res._id }, { uid: this.getUid() })
|
|
453
|
+
.session(this.dbsession);
|
|
454
|
+
this.logger.log(updateresult, 'update all permission');
|
|
455
|
+
return true;
|
|
456
|
+
} else {
|
|
457
|
+
const deleteresult = await this.usermodel
|
|
458
|
+
.deleteOne({ _id: id })
|
|
459
|
+
.session(this.dbsession);
|
|
460
|
+
await this.permmodel
|
|
461
|
+
.deleteMany({ uid: '', user_id: res._id })
|
|
462
|
+
.session(this.dbsession);
|
|
463
|
+
return true;
|
|
464
|
+
}
|
|
465
|
+
} else {
|
|
466
|
+
throw new BadRequestException(`${id} not found.`);
|
|
467
|
+
}
|
|
418
468
|
}
|
|
419
469
|
|
|
420
|
-
isRealmAdmin = ()=>{
|
|
421
|
-
const o = this.ssoACL
|
|
422
|
-
const ssoclient = process.env.OAUTH2_CLIENTID
|
|
423
|
-
const adminRole = process.env.OAUTH2_ADMINROLE
|
|
470
|
+
isRealmAdmin = () => {
|
|
471
|
+
const o = this.ssoACL;
|
|
472
|
+
const ssoclient = process.env.OAUTH2_CLIENTID;
|
|
473
|
+
const adminRole = process.env.OAUTH2_ADMINROLE;
|
|
424
474
|
// return false
|
|
425
|
-
if(this.getEmail()===process.env.ADMIN_EMAIL){
|
|
426
|
-
return true
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
475
|
+
if (this.getEmail() === process.env.ADMIN_EMAIL) {
|
|
476
|
+
return true;
|
|
477
|
+
} else if (
|
|
478
|
+
o[ssoclient] &&
|
|
479
|
+
o[ssoclient]['roles'] &&
|
|
480
|
+
o[ssoclient]['roles'] == adminRole
|
|
481
|
+
) {
|
|
482
|
+
return true;
|
|
483
|
+
} else {
|
|
484
|
+
return false;
|
|
432
485
|
}
|
|
433
|
-
|
|
434
|
-
}
|
|
435
|
-
|
|
486
|
+
};
|
|
436
487
|
|
|
437
|
-
searchInsertedRecordId(collection:string,_id:string){
|
|
438
|
-
return this.modifiedRecords.createds[collection].find(
|
|
488
|
+
searchInsertedRecordId(collection: string, _id: string) {
|
|
489
|
+
return this.modifiedRecords.createds[collection].find(
|
|
490
|
+
(item) => item === _id,
|
|
491
|
+
);
|
|
439
492
|
}
|
|
440
|
-
addInsertedRecordId(collection:string,_id:string){
|
|
441
|
-
if(this.modifiedRecords.createds[collection]){
|
|
442
|
-
this.modifiedRecords.createds[collection].push(_id)
|
|
443
|
-
}else{
|
|
444
|
-
this.modifiedRecords.createds[collection] = [_id]
|
|
493
|
+
addInsertedRecordId(collection: string, _id: string) {
|
|
494
|
+
if (this.modifiedRecords.createds[collection]) {
|
|
495
|
+
this.modifiedRecords.createds[collection].push(_id);
|
|
496
|
+
} else {
|
|
497
|
+
this.modifiedRecords.createds[collection] = [_id];
|
|
445
498
|
}
|
|
446
499
|
}
|
|
447
|
-
addUpdatedRecordId(collection:string,_id:string){
|
|
448
|
-
if(this.modifiedRecords.updateds[collection]){
|
|
449
|
-
this.modifiedRecords.updateds[collection].push(_id)
|
|
450
|
-
}else{
|
|
451
|
-
this.modifiedRecords.updateds[collection] = [_id]
|
|
500
|
+
addUpdatedRecordId(collection: string, _id: string) {
|
|
501
|
+
if (this.modifiedRecords.updateds[collection]) {
|
|
502
|
+
this.modifiedRecords.updateds[collection].push(_id);
|
|
503
|
+
} else {
|
|
504
|
+
this.modifiedRecords.updateds[collection] = [_id];
|
|
452
505
|
}
|
|
453
|
-
|
|
454
506
|
}
|
|
455
|
-
addDeletedRecordId(collection:string,_id:string){
|
|
456
|
-
if(this.modifiedRecords.deleteds[collection]){
|
|
457
|
-
this.modifiedRecords.deleteds[collection].push(_id)
|
|
458
|
-
}else{
|
|
459
|
-
this.modifiedRecords.deleteds[collection] = [_id]
|
|
507
|
+
addDeletedRecordId(collection: string, _id: string) {
|
|
508
|
+
if (this.modifiedRecords.deleteds[collection]) {
|
|
509
|
+
this.modifiedRecords.deleteds[collection].push(_id);
|
|
510
|
+
} else {
|
|
511
|
+
this.modifiedRecords.deleteds[collection] = [_id];
|
|
460
512
|
}
|
|
461
|
-
|
|
462
513
|
}
|
|
463
514
|
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
uid:this.uid,
|
|
470
|
-
tenantId
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
515
|
+
async getAllTenants() {
|
|
516
|
+
const results = [];
|
|
517
|
+
if (this.getId() != '') {
|
|
518
|
+
const filteruser: PipelineStage = {
|
|
519
|
+
$match: {
|
|
520
|
+
uid: this.uid,
|
|
521
|
+
tenantId: { $gt: 0 },
|
|
522
|
+
},
|
|
523
|
+
} as PipelineStage;
|
|
524
|
+
|
|
525
|
+
const getTenantData: PipelineStage = {
|
|
526
|
+
$lookup: {
|
|
527
|
+
from: 'tenant',
|
|
528
|
+
localField: 'tenantId',
|
|
529
|
+
foreignField: 'tenantId',
|
|
530
|
+
as: 'tenant',
|
|
531
|
+
},
|
|
532
|
+
};
|
|
533
|
+
|
|
534
|
+
const permission: PipelineStage = {
|
|
535
|
+
$lookup: {
|
|
536
|
+
from: 'permission',
|
|
537
|
+
localField: '_id',
|
|
538
|
+
foreignField: 'userId',
|
|
539
|
+
as: 'permissions',
|
|
540
|
+
pipeline: [
|
|
541
|
+
{
|
|
542
|
+
$lookup: {
|
|
543
|
+
from: 'organization',
|
|
544
|
+
localField: 'orgId',
|
|
545
|
+
foreignField: 'orgId',
|
|
546
|
+
as: 'org',
|
|
547
|
+
},
|
|
548
|
+
},
|
|
549
|
+
{
|
|
550
|
+
$lookup: {
|
|
551
|
+
from: 'branch',
|
|
552
|
+
localField: 'branchId',
|
|
553
|
+
foreignField: 'branchId',
|
|
554
|
+
as: 'branch',
|
|
555
|
+
},
|
|
556
|
+
},
|
|
557
|
+
],
|
|
558
|
+
},
|
|
559
|
+
};
|
|
560
|
+
|
|
561
|
+
const pipelines: PipelineStage[] = [
|
|
562
|
+
filteruser,
|
|
563
|
+
getTenantData,
|
|
564
|
+
permission,
|
|
565
|
+
];
|
|
492
566
|
// const users=[]
|
|
493
|
-
this.logger.warn(pipelines,
|
|
494
|
-
const users = await this.usermodel.aggregate(pipelines)
|
|
495
|
-
|
|
496
|
-
if(users){
|
|
567
|
+
this.logger.warn(pipelines, 'pipelines');
|
|
568
|
+
const users = await this.usermodel.aggregate(pipelines);
|
|
497
569
|
|
|
498
|
-
|
|
499
|
-
const activeusers = users.filter((u)=>{
|
|
570
|
+
if (users) {
|
|
571
|
+
const activeusers = users.filter((u) => {
|
|
500
572
|
// console.log(u.active,'===',true, ' && ',u.tenant[0].active,'===',true)
|
|
501
|
-
return u.active===true && u.tenant[0].active===true
|
|
502
|
-
})
|
|
573
|
+
return u.active === true && u.tenant[0].active === true;
|
|
574
|
+
});
|
|
503
575
|
// return activeusers
|
|
504
|
-
activeusers.forEach((u)=>{
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
results.push({
|
|
576
|
+
activeusers.forEach((u) => {
|
|
577
|
+
const permissions = u.permissions
|
|
578
|
+
.filter((item: any) => {
|
|
579
|
+
return item.org[0].active && item.branch[0].active;
|
|
580
|
+
})
|
|
581
|
+
.map((item: any) => {
|
|
582
|
+
return {
|
|
583
|
+
_id: item._id,
|
|
584
|
+
orgId: item.orgId,
|
|
585
|
+
branchId: item.branchId,
|
|
586
|
+
group: item.group,
|
|
587
|
+
orgCode: item.org[0].orgCode,
|
|
588
|
+
orgName: item.org[0].orgName,
|
|
589
|
+
branchCode: item.branch[0].branchCode,
|
|
590
|
+
branchName: item.branch[0].branchName,
|
|
591
|
+
xOrg: this.generateXorg(u.tenantId, item.orgId, item.branchId),
|
|
592
|
+
};
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
results.push({
|
|
528
596
|
_id: u._id,
|
|
529
|
-
fullname
|
|
597
|
+
fullname: u.fullname,
|
|
530
598
|
tenantId: u.tenantId,
|
|
531
599
|
tenantName: u.tenant[0].tenantName,
|
|
532
|
-
permissions: permissions
|
|
533
|
-
|
|
534
|
-
})
|
|
535
|
-
|
|
600
|
+
permissions: permissions,
|
|
601
|
+
});
|
|
602
|
+
});
|
|
536
603
|
}
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
604
|
}
|
|
541
|
-
return results
|
|
605
|
+
return results;
|
|
542
606
|
}
|
|
543
607
|
}
|