@simitgroup/simpleapp-generator 1.0.32 → 1.0.35

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.
Files changed (281) hide show
  1. package/.vscode/settings.json +3 -0
  2. package/README.md +185 -16
  3. package/buildinschemas copy/autoincreament.autoinc.jsonschema.json +39 -0
  4. package/buildinschemas copy/branch.branch.jsonschema.json +41 -0
  5. package/buildinschemas copy/docnoformat.docno.jsonschema.json +41 -0
  6. package/buildinschemas copy/organization.org.jsonschema.json +50 -0
  7. package/buildinschemas copy/permission.perm.jsonschema.json +23 -0
  8. package/buildinschemas copy/permission.perm.jsonschema.try.json +25 -0
  9. package/buildinschemas copy/tenant.tenant.jsonschema.json +21 -0
  10. package/buildinschemas copy/tenant.tenant.jsonschema.try.json +27 -0
  11. package/buildinschemas copy/user.user.jsonschema.json +31 -0
  12. package/dist/buildinschemas/autoincreament.d.ts +3 -0
  13. package/dist/buildinschemas/autoincreament.d.ts.map +1 -0
  14. package/dist/buildinschemas/autoincreament.js +36 -0
  15. package/dist/buildinschemas/autoincreament.js.map +1 -0
  16. package/dist/buildinschemas/branch.d.ts +3 -0
  17. package/dist/buildinschemas/branch.d.ts.map +1 -0
  18. package/dist/buildinschemas/branch.js +41 -0
  19. package/dist/buildinschemas/branch.js.map +1 -0
  20. package/dist/buildinschemas/docnoformat.d.ts +3 -0
  21. package/dist/buildinschemas/docnoformat.d.ts.map +1 -0
  22. package/dist/buildinschemas/docnoformat.js +60 -0
  23. package/dist/buildinschemas/docnoformat.js.map +1 -0
  24. package/dist/buildinschemas/index.d.ts +8 -0
  25. package/dist/buildinschemas/index.d.ts.map +1 -0
  26. package/dist/buildinschemas/index.js +18 -0
  27. package/dist/buildinschemas/index.js.map +1 -0
  28. package/dist/buildinschemas/organization.d.ts +3 -0
  29. package/dist/buildinschemas/organization.d.ts.map +1 -0
  30. package/dist/buildinschemas/organization.js +34 -0
  31. package/dist/buildinschemas/organization.js.map +1 -0
  32. package/dist/buildinschemas/permission.d.ts +3 -0
  33. package/dist/buildinschemas/permission.d.ts.map +1 -0
  34. package/dist/buildinschemas/permission.js +34 -0
  35. package/dist/buildinschemas/permission.js.map +1 -0
  36. package/dist/buildinschemas/tenant.d.ts +3 -0
  37. package/dist/buildinschemas/tenant.d.ts.map +1 -0
  38. package/dist/buildinschemas/tenant.js +41 -0
  39. package/dist/buildinschemas/tenant.js.map +1 -0
  40. package/dist/buildinschemas/user.d.ts +3 -0
  41. package/dist/buildinschemas/user.d.ts.map +1 -0
  42. package/dist/buildinschemas/user.js +31 -0
  43. package/dist/buildinschemas/user.js.map +1 -0
  44. package/dist/constant.d.ts +4 -0
  45. package/dist/constant.d.ts.map +1 -0
  46. package/dist/constant.js +2 -1
  47. package/dist/constant.js.map +1 -1
  48. package/dist/framework.d.ts +10 -0
  49. package/dist/framework.d.ts.map +1 -0
  50. package/dist/framework.js +120 -68
  51. package/dist/framework.js.map +1 -1
  52. package/dist/generate-allow-changebackend.js +305 -0
  53. package/dist/generate-allow-changebackend.js.map +1 -0
  54. package/dist/generate.d.ts +2 -0
  55. package/dist/generate.d.ts.map +1 -0
  56. package/dist/generate.js +310 -230
  57. package/dist/generate.js.map +1 -1
  58. package/dist/index.d.ts +3 -0
  59. package/dist/index.d.ts.map +1 -0
  60. package/dist/index.js +84 -20
  61. package/dist/index.js.map +1 -1
  62. package/dist/libs.d.ts +2 -0
  63. package/dist/libs.d.ts.map +1 -0
  64. package/dist/processors/groupsbuilder.js +2 -0
  65. package/dist/processors/groupsbuilder.js.map +1 -0
  66. package/dist/processors/jsonschemabuilder.d.ts +4 -0
  67. package/dist/processors/jsonschemabuilder.d.ts.map +1 -0
  68. package/dist/processors/jsonschemabuilder.js +146 -172
  69. package/dist/processors/jsonschemabuilder.js.map +1 -1
  70. package/dist/schematype/baseschema.js +25 -0
  71. package/dist/schematype/baseschema.js.map +1 -0
  72. package/dist/schematype/default.js +2 -0
  73. package/dist/schematype/default.js.map +1 -0
  74. package/dist/schematype/index.js +12 -0
  75. package/dist/schematype/index.js.map +1 -0
  76. package/dist/schematype/primarymasterdata.js +38 -0
  77. package/dist/schematype/primarymasterdata.js.map +1 -0
  78. package/dist/schematype/simple.js +24 -0
  79. package/dist/schematype/simple.js.map +1 -0
  80. package/dist/schematype/simplemasterdata.js +31 -0
  81. package/dist/schematype/simplemasterdata.js.map +1 -0
  82. package/dist/schematype/transaction.js +74 -0
  83. package/dist/schematype/transaction.js.map +1 -0
  84. package/dist/storage.d.ts +3 -0
  85. package/dist/storage.d.ts.map +1 -0
  86. package/dist/storage.js +2 -2
  87. package/dist/storage.js.map +1 -1
  88. package/dist/type.d.ts +164 -0
  89. package/dist/type.d.ts.map +1 -0
  90. package/dist/type.js +16 -1
  91. package/dist/type.js.map +1 -1
  92. package/dist/validation.d.ts +1 -0
  93. package/dist/validation.d.ts.map +1 -0
  94. package/documentation/designconcept.bpmn +349 -0
  95. package/documentation/documentation.png +0 -0
  96. package/documentation/infra.drawio +141 -0
  97. package/documentation/infra.png +0 -0
  98. package/documentation/management.drawio +57 -0
  99. package/documentation/stack.drawio +106 -0
  100. package/package.json +3 -1
  101. package/src/buildinschemas/autoincreament.ts +34 -0
  102. package/src/buildinschemas/branch.ts +39 -0
  103. package/src/buildinschemas/docnoformat.ts +58 -0
  104. package/src/buildinschemas/index.ts +7 -0
  105. package/src/buildinschemas/organization.ts +31 -0
  106. package/src/buildinschemas/permission.ts +31 -0
  107. package/src/buildinschemas/tenant.ts +38 -0
  108. package/src/buildinschemas/user.ts +28 -0
  109. package/src/constant.ts +2 -1
  110. package/src/framework.ts +126 -67
  111. package/src/generate.ts +327 -266
  112. package/src/generate.ts.backup +339 -0
  113. package/src/index.ts +93 -18
  114. package/src/processors/jsonschemabuilder.ts +199 -226
  115. package/src/processors/jsonschemabuilder.ts-old +383 -0
  116. package/src/storage.ts +1 -1
  117. package/src/type.ts +94 -27
  118. package/templates/basic/nest/controller.ts.eta +255 -0
  119. package/templates/basic/nest/default.ts.eta +42 -0
  120. package/templates/basic/{model.eta → nest/model.ts.eta} +18 -5
  121. package/templates/basic/nest/processor.ts.eta +129 -0
  122. package/templates/basic/nest/service.ts.eta +64 -0
  123. package/templates/basic/{type.eta → nest/type.ts.eta} +3 -3
  124. package/templates/basic/nuxt/default.ts.eta +42 -0
  125. package/templates/basic/{pageindex.vue.eta → nuxt/pages.crud.vue.eta} +85 -22
  126. package/templates/basic/nuxt/simpleapp.doc.ts.eta +11 -0
  127. package/templates/basic/nuxt/simpleapp.generate.client.ts.eta +212 -0
  128. package/templates/nest/.env.eta +31 -0
  129. package/templates/nest/.gitignore.eta +40 -0
  130. package/templates/nest/src/app.controller.ts.eta +19 -0
  131. package/templates/nest/src/app.module.ts.eta +77 -0
  132. package/templates/nest/src/app.service.ts.eta +9 -0
  133. package/templates/nest/src/main.ts.eta +58 -0
  134. package/templates/nest/src/simpleapp/generate/apischemas/index.ts.eta +16 -0
  135. package/templates/nest/src/simpleapp/generate/commons/decorators/appuser.decorator.ts.eta +8 -0
  136. package/templates/nest/src/simpleapp/generate/commons/dicts/foreignkeys.ts.eta +1 -0
  137. package/templates/nest/src/simpleapp/generate/commons/docnogenerator.service.ts.eta +101 -0
  138. package/templates/nest/src/simpleapp/generate/commons/exceptions/SimpleAppExceptionFilter.ts.eta +39 -0
  139. package/templates/nest/src/simpleapp/generate/commons/interceptors/response.interceptor.ts.eta +38 -0
  140. package/templates/nest/src/simpleapp/generate/commons/middlewares/tenant.middleware.ts.eta +88 -0
  141. package/templates/nest/{Workflow.eta → src/simpleapp/generate/commons/providers/workflow.provider.ts.etax} +20 -14
  142. package/templates/nest/src/simpleapp/generate/commons/roles/roles.decorator.ts.eta +6 -0
  143. package/templates/nest/src/simpleapp/generate/commons/roles/roles.enum.ts.eta +28 -0
  144. package/templates/nest/src/simpleapp/generate/commons/roles/roles.group.ts.eta +10 -0
  145. package/templates/nest/src/simpleapp/generate/commons/roles/roles.guard.ts.eta +34 -0
  146. package/templates/nest/src/simpleapp/generate/commons/user.context.ts.eta +391 -0
  147. package/templates/nest/src/simpleapp/generate/controllers/simpleapp.controller.ts.eta +71 -0
  148. package/templates/nest/src/simpleapp/generate/models/perm.model.ts.eta +53 -0
  149. package/templates/nest/src/simpleapp/generate/models/tenant.model.ts.eta +45 -0
  150. package/templates/nest/src/simpleapp/generate/models/user.model.ts.eta +57 -0
  151. package/templates/nest/src/simpleapp/generate/processors/simpleapp.processor.ts.eta +624 -0
  152. package/templates/nest/src/simpleapp/generate/types/index.ts.eta +19 -0
  153. package/templates/nest/src/simpleapp/profile/profile.apischema.ts.eta +74 -0
  154. package/templates/nest/src/simpleapp/profile/profile.controller.ts.eta +110 -0
  155. package/templates/nest/src/simpleapp/profile/profile.service.ts.eta +195 -0
  156. package/templates/nest/src/simpleapp/profile/profile.types.ts.eta +18 -0
  157. package/templates/nest/src/simpleapp/services/autoinc.service.ts.eta +89 -0
  158. package/templates/nest/src/simpleapp/services/branch.service.ts.eta +66 -0
  159. package/templates/nest/src/simpleapp/services/docno.service.ts.eta +93 -0
  160. package/templates/nest/src/simpleapp/services/org.service.ts.eta +67 -0
  161. package/templates/nest/src/simpleapp/services/perm.service.ts.eta +102 -0
  162. package/templates/nest/src/simpleapp/services/tenant.service.ts.eta +69 -0
  163. package/templates/nest/src/simpleapp/services/user.service.ts.eta +66 -0
  164. package/templates/nest/src/simpleapp/simpleapp.module.ts.eta +43 -0
  165. package/templates/nuxt/.env.eta +21 -0
  166. package/templates/nuxt/.gitignore.eta +28 -0
  167. package/templates/nuxt/app.vue.eta +5 -2
  168. package/templates/nuxt/assets/css/tailwind.css.eta +35 -0
  169. package/templates/nuxt/components/CrudNestedDoc.vue.eta +164 -0
  170. package/templates/nuxt/components/CrudSimple.vue.eta +179 -0
  171. package/templates/nuxt/{components.debugdocdata.vue.eta → components/DebugDocumentData.vue.eta} +4 -1
  172. package/templates/nuxt/{components.eventmonitor.vue.eta → components/EventMonitor.vue.eta} +27 -27
  173. package/templates/nuxt/components/Invitation.vue.eta +50 -0
  174. package/templates/nuxt/components/Menus.vue.eta +58 -0
  175. package/templates/nuxt/components/PermissionInfo.vue.eta +92 -0
  176. package/templates/nuxt/components/SimpleAppAutocomplete.vue.eta +131 -0
  177. package/templates/nuxt/components/SimpleAppAutocompletemulti.vue.eta +73 -0
  178. package/templates/nuxt/components/SimpleAppCalendar.vue.eta +55 -0
  179. package/templates/nuxt/components/SimpleAppCheckbox.vue.eta +29 -0
  180. package/templates/nuxt/components/SimpleAppChip.vue.eta +28 -0
  181. package/templates/nuxt/components/SimpleAppColor.vue.eta +41 -0
  182. package/templates/nuxt/components/SimpleAppDatatable.vue.eta +20 -0
  183. package/templates/nuxt/components/SimpleAppDocumentNo.vue.eta +90 -0
  184. package/templates/nuxt/components/SimpleAppDynamicInput.vue.eta +29 -0
  185. package/templates/nuxt/components/SimpleAppEditor.vue.eta +31 -0
  186. package/templates/nuxt/components/SimpleAppForm.vue.eta +131 -0
  187. package/templates/nuxt/components/SimpleAppInputTable.vue.eta +104 -0
  188. package/templates/nuxt/components/SimpleAppList.vue.eta +38 -0
  189. package/templates/nuxt/components/SimpleAppListmulti.vue.eta +41 -0
  190. package/templates/nuxt/components/SimpleAppNumber.vue.eta +32 -0
  191. package/templates/nuxt/components/SimpleAppPassword.vue.eta +41 -0
  192. package/templates/nuxt/components/SimpleAppRadio.vue.eta +42 -0
  193. package/templates/nuxt/components/SimpleAppRating.vue.eta +41 -0
  194. package/templates/nuxt/components/SimpleAppSelect.vue.eta +38 -0
  195. package/templates/nuxt/components/SimpleAppSelectmulti.vue.eta +39 -0
  196. package/templates/nuxt/components/SimpleAppSlider.vue.eta +42 -0
  197. package/templates/nuxt/components/SimpleAppSwitch.vue.eta +30 -0
  198. package/templates/nuxt/components/SimpleAppText.vue.eta +50 -0
  199. package/templates/nuxt/components/SimpleAppTextarea.vue.eta +30 -0
  200. package/templates/nuxt/components/SimpleAppValue.vue.eta +86 -0
  201. package/templates/nuxt/components/SimpleFieldContainer.vue.eta +102 -0
  202. package/templates/nuxt/components/XorgPicker.vue.eta +66 -0
  203. package/templates/nuxt/components/helper.ts.eta +90 -0
  204. package/templates/nuxt/components/type.ts.eta +32 -0
  205. package/templates/nuxt/composables/docformat.generate.ts.eta +5 -0
  206. package/templates/nuxt/{composables.getautocomplete.ts.eta → composables/getAutocomplete.generate.ts.eta} +4 -5
  207. package/templates/nuxt/{composables.getmenus.ts.eta → composables/getMenus.generate.ts.eta} +22 -7
  208. package/templates/nuxt/composables/getOpenApi.generate.ts.eta +6 -0
  209. package/templates/nuxt/composables/getTenant.generate.ts.eta +4 -0
  210. package/templates/nuxt/composables/getUserStore.generate.ts.eta +22 -0
  211. package/templates/nuxt/composables/logout.generate.ts.eta +22 -0
  212. package/templates/nuxt/composables/roles.generate.ts.eta +48 -0
  213. package/templates/nuxt/composables/stringHelper.generate.ts.eta +5 -0
  214. package/templates/nuxt/{layouts.default.vue.eta → layouts/default.vue.eta} +2 -0
  215. package/templates/nuxt/middleware/10.acl.global.ts.eta +38 -0
  216. package/templates/nuxt/nuxt.config.ts.eta +12 -2
  217. package/templates/nuxt/pages/[xorg]/branch/index.vue.eta +102 -0
  218. package/templates/nuxt/pages/[xorg]/docnoformat/[id].vue.eta +17 -0
  219. package/templates/nuxt/pages/[xorg]/docnoformat/index.vue.eta +269 -0
  220. package/templates/nuxt/pages/[xorg]/index.vue.eta +36 -0
  221. package/templates/nuxt/pages/[xorg]/organization/index.vue.eta +148 -0
  222. package/templates/nuxt/pages/[xorg]/permission/index.vue.eta +280 -0
  223. package/templates/nuxt/pages/[xorg]/tenant/index.vue.eta +93 -0
  224. package/templates/nuxt/pages/[xorg]/user/index.vue.eta +468 -0
  225. package/templates/nuxt/pages/index.vue.eta +191 -0
  226. package/templates/nuxt/pages/login.vue.eta +21 -0
  227. package/templates/nuxt/plugins/10.simpleapp-event.ts.eta +53 -0
  228. package/templates/nuxt/plugins/20.simpleapp-userstore.ts.eta +155 -0
  229. package/templates/nuxt/plugins/50.simpleapp-client.ts.eta +23 -0
  230. package/templates/nuxt/{server.api.ts.eta → server/api/[xorg]/[...].ts.eta} +5 -2
  231. package/templates/nuxt/server/api/auth/[...].ts.eta +68 -0
  232. package/templates/nuxt/{server.api.auth.logout.ts.eta → server/api/auth/logout.ts.eta} +1 -3
  233. package/templates/nuxt/server/api/profile/[...].ts.eta +150 -0
  234. package/templates/nuxt/server/api/profile/index.ts.eta +103 -0
  235. package/templates/nuxt/simpleapp/generate/clients/SimpleAppClient.ts.eta +187 -0
  236. package/templates/nuxt/simpleapp/generate/commons/documents.ts.eta +6 -0
  237. package/templates/nuxt/simpleapp/generate/commons/events.ts.eta +5 -0
  238. package/templates/nuxt/simpleapp/generate/commons/groups.ts.eta +11 -0
  239. package/templates/nuxt/simpleapp/generate/commons/roles.ts.eta +21 -0
  240. package/templates/nuxt/types/index.ts.eta +65 -0
  241. package/templates/project/README.md +11 -0
  242. package/templates/project/build.sh.eta +17 -0
  243. package/templates/project/config.json.eta +2 -0
  244. package/templates/project/generate.ts.eta +10 -0
  245. package/templates/project/schemas/category.ts.eta +26 -0
  246. package/templates/project/schemas/index.ts.eta +5 -0
  247. package/templates/project/schemas/product.ts.eta +59 -0
  248. package/templates/project/shares/hello.ts.eta +1 -0
  249. package/templates/project/shares/index.ts.eta +2 -0
  250. package/tsconfig.json +9 -2
  251. package/tsconfig.tsbuildinfo +1 -0
  252. package/definations/category.cat.jsonschema.json +0 -55
  253. package/definations/level.lvl.jsonschema.json +0 -48
  254. package/definations/product.prd.jsonschema.json +0 -46
  255. package/templates/basic/controller.eta +0 -152
  256. package/templates/basic/module.eta +0 -22
  257. package/templates/basic/service.eta +0 -62
  258. package/templates/basic/simpleappclient.eta +0 -120
  259. package/templates/nest/SimpleAppController.eta +0 -69
  260. package/templates/nest/SimpleAppService.eta +0 -366
  261. package/templates/nest/TenantMiddleware.eta +0 -34
  262. package/templates/nest/UserProvider.eta +0 -127
  263. package/templates/nest/app.controller.eta +0 -12
  264. package/templates/nest/app.module.eta +0 -64
  265. package/templates/nest/app.service.eta +0 -8
  266. package/templates/nest/inputvalidation-exception.eta +0 -6
  267. package/templates/nest/nest.env.eta +0 -28
  268. package/templates/nest/nest.main.eta +0 -31
  269. package/templates/nuxt/components.crudsimple.vue.eta +0 -124
  270. package/templates/nuxt/components.menus.vue.eta +0 -35
  271. package/templates/nuxt/env.eta +0 -17
  272. package/templates/nuxt/pages.[xorg].index.vue.eta +0 -20
  273. package/templates/nuxt/pages.index.vue.eta +0 -72
  274. package/templates/nuxt/pages.login.vue.eta +0 -20
  275. package/templates/nuxt/plugins.simpleapp.ts.eta +0 -88
  276. package/templates/nuxt/server.api.auth[...].ts.eta +0 -233
  277. package/templates/nuxt/tailwind.css.eta +0 -49
  278. /package/templates/basic/{apischema.eta → nest/apischema.ts.eta} +0 -0
  279. /package/templates/basic/{jsonschema.eta → nest/jsonschema.ts.eta} +0 -0
  280. /package/templates/basic/{pageindexwithid.vue.eta → nuxt/pages.[id].vue.eta} +0 -0
  281. /package/templates/nest/{oauth2-redirect.eta → public_html/oauth2-redirect.html.eta} +0 -0
@@ -0,0 +1,391 @@
1
+ /**
2
+ * todo
3
+ * 1.base on xorg get current role and group
4
+ * 2. get profile want to obtain branchlist and invitation list
5
+ * 3. xorglist to user shall display appropriate tenant/org/branch name
6
+ */
7
+
8
+ import { Injectable, Scope,Inject,Logger, BadRequestException } from '@nestjs/common';
9
+ import { Model, model, connect, PipelineStage } from 'mongoose';
10
+ import _ from 'lodash'
11
+ import { Module } from '@nestjs/common';
12
+ import * as jwt from 'jsonwebtoken';
13
+ import { Role } from './roles/roles.enum';
14
+ import * as rolegroups from './roles/roles.group'
15
+ import { UserService } from './../../services/user.service';
16
+ import { InjectModel } from '@nestjs/mongoose';
17
+ const Base64URL = require('@darkwolf/base64url');
18
+ import { UserMongoSchema } from './../models/user.model';
19
+ import { User, } from './../types/user.type';
20
+ import { Permission, } from './../types/perm.type';
21
+ import {ProfileUserBranch , ProfileUserInvites} from '../../profile/profile.types'
22
+
23
+ import {ClientSession} from 'mongoose'
24
+ @Injectable()
25
+ export class UserContext {
26
+ protected logger = new Logger()
27
+ protected uid: string = '';
28
+ protected _id: string = '';
29
+ protected uname: string = '';
30
+ protected email: string = '';
31
+ protected fullname: string = '';
32
+ protected xOrg: string = '';
33
+ protected tenantId: number = 0;
34
+ protected orgId: number = 0;
35
+ protected branchId: number = 0;
36
+ protected ssoACL: any = {};
37
+ protected token: string = '';
38
+ protected refreshtoken: string = '';
39
+ protected group: string = '';
40
+ protected branchCode: string = '';
41
+ protected branchName: string = '';
42
+ protected branches: any[] = []
43
+ protected invites: any[] = [] //User + field tenant:Tenant[]
44
+ protected roles: string[] = [];
45
+
46
+
47
+ constructor(
48
+ private readonly usermodel:Model<User>,
49
+ private readonly permmodel:Model<Permission>,
50
+ private dbsession:ClientSession
51
+ ) {
52
+
53
+ }
54
+ getDBSession = ():ClientSession => this.dbsession
55
+
56
+ getId = () => this._id;
57
+ getUid = () => this.uid;
58
+ getUname = () => this.uname;
59
+ getFullname = () => this.fullname;
60
+ getTenantId = () => this.tenantId;
61
+ getOrgId = () => this.orgId;
62
+ getBranchId = () => this.branchId;
63
+ getEmail = () => this.email;
64
+ getGroup = () => this.group;
65
+ getRoles = () => this.roles;
66
+ getBranches = ():ProfileUserBranch[] => {
67
+ this.branches;
68
+ const data:ProfileUserBranch[] = []
69
+
70
+ if(this.branches){
71
+ for(let i=0;i<this.branches.length; i++){
72
+ const b= this.branches[i]
73
+ data.push({
74
+ _id: b._id,
75
+ branch: b.branch[0],
76
+ group: b.group,
77
+ xOrg: this.generateXorg(b.tenantId,b.orgId,b.branchId)
78
+
79
+ })
80
+ }
81
+ }
82
+ return data
83
+
84
+ }
85
+ getInvites = ():ProfileUserInvites[] =>{
86
+
87
+ // const usermodel = model<User>('user',UserMongoSchema,'user')
88
+
89
+ const data:ProfileUserInvites[] = []
90
+ if( this.invites){
91
+ // console.log("getInvites",res)
92
+ for(let i=0;i< this.invites.length; i++){
93
+ const r = this.invites[0]
94
+
95
+ data.push({
96
+ _id: r._id,
97
+ email: r.email,
98
+ tenantId: r.tenantId,
99
+ fullName: '',
100
+ tenantName: r.tenant[0].tenantName,
101
+ created: r.created,
102
+ })
103
+ }
104
+ }
105
+ return data
106
+
107
+ };
108
+ setCurrentUserInfo = async (tokenstr: string,xorg)=>{
109
+ this.setXorg(xorg);
110
+ await this.setUserToken(tokenstr)
111
+
112
+ }
113
+
114
+ /**
115
+ * obtain user profile filter by uid,tenantId,orgId,branchId
116
+ * @returns Promise<User|undefined>
117
+ */
118
+ obtainProfileFromDb = async () : Promise<User|undefined>=>{
119
+ const filter = { $match:{uid:this.uid,tenantId:this.tenantId} }
120
+
121
+
122
+
123
+
124
+ const joinpermission:PipelineStage = {$lookup: {
125
+ from : 'permission',
126
+ localField : 'uid',
127
+ foreignField : 'uid',
128
+ as : 'permissions',
129
+ pipeline:[
130
+ { $match:{
131
+ uid:this.uid,
132
+ tenantId:this.tenantId,
133
+ orgId:this.orgId,
134
+ branchId:this.branchId,
135
+ }},
136
+ {$lookup:{from: 'branch',localField: 'branchId',foreignField:'branchId',as: 'currentbranch'}}
137
+ ]
138
+ }}
139
+
140
+ const pipeline:PipelineStage[]=[filter,]
141
+
142
+ if(this.tenantId>0){
143
+ pipeline.push(joinpermission)
144
+ }
145
+ // console.log("obtainProfileFromDbobtainProfileFromDb ",pipeline)
146
+ // const users = await usermodel.find(filter)
147
+ const users = await this.usermodel.aggregate(pipeline)
148
+ this.logger.verbose(`aggregate user profile from database`,'obtainProfileFromDb' )
149
+ this.logger.verbose(pipeline,'obtainProfileFromDb x' )
150
+ this.logger.verbose(users,'obtainProfileFromDb' )
151
+ // console.log("obtainProfileFromDbobtainProfileFromDb ",users)
152
+ if(users && users.length>0){
153
+ const userinfo = users[0]
154
+ // console.log("userinfouserinfouserinfouserinfouserinfouserinfo ",userinfo)
155
+
156
+ if(this.tenantId>0){
157
+ const myperm=userinfo.permissions[0]
158
+ if(myperm && myperm.group){
159
+ userinfo.group = myperm.group
160
+ userinfo.roles = rolegroups[userinfo.group]
161
+ userinfo.branchCode = myperm.currentbranch[0].branchCode
162
+ userinfo.branchName = myperm.currentbranch[0].branchName
163
+ }
164
+ }else{
165
+ userinfo.group = ''
166
+ }
167
+
168
+ return userinfo
169
+ }else{
170
+ return undefined
171
+ }
172
+
173
+ }
174
+ setUserToken = async (tokenstr: string) => {
175
+
176
+ //define token info
177
+ const tokeninfo = jwt.decode(tokenstr);
178
+ this.token = tokenstr;
179
+ this.uid = tokeninfo.sub;
180
+ this.email = tokeninfo.email;
181
+ this.uname = tokeninfo.preferred_username;
182
+ this.fullname = tokeninfo.name;
183
+ this.ssoACL = tokeninfo.resource_access;
184
+ this.logger.verbose(`set token ${this.uid}`)
185
+ //read current user from db
186
+ // console.log("await this.obtainProfileFromDb()")
187
+ const userinfo = await this.obtainProfileFromDb()
188
+ this.logger.verbose(userinfo,'obtainProfileFromDb result')
189
+ if(userinfo){
190
+ this.logger.debug(`${this.uid} user exists in tenant (${this.tenantId})`,'setUserToken' )
191
+
192
+ this._id = userinfo._id.toString()
193
+ this.branchCode = userinfo['branchCode'] ?? ''
194
+ this.branchName = userinfo['branchName'] ?? ''
195
+ this.group = userinfo['group'] ?? ''
196
+ this.roles = userinfo['roles'] ?? [Role.Everyone,Role.User]
197
+ }else{
198
+ this.logger.debug(`Set unknown id of current user`,'setUserToken' )
199
+ // this.group = ''
200
+ // this.tenantId=0
201
+ // this.orgId=0
202
+ // this.branchId=0
203
+ this.roles = [Role.Everyone,Role.Unknown]
204
+ }
205
+ this.logger.debug(`${this.uid} have _id (${this.getId()}), group (${this.group}) and role (${this.getRoles()})` )
206
+
207
+ if(this.isRealmAdmin() && !this.roles.includes(Role.SuperAdmin)){
208
+ this.roles.push(Role.SuperAdmin)
209
+ }
210
+ this.logger.verbose(this)
211
+ };
212
+
213
+
214
+ generateXorg = (tenantId:number,orgId:number,branchId:number):string=>{
215
+ return Base64URL.encodeText(`${tenantId}-${orgId}-${branchId}`)
216
+ }
217
+ getInfo = () => {
218
+ return this;
219
+ };
220
+ getBranchFilter = () => {
221
+ return {
222
+ tenantId: this.tenantId,
223
+ orgId: this.orgId,
224
+ branchId: this.branchId,
225
+ };
226
+ };
227
+ getTenantFilter = () => {
228
+ return { tenantId: this.tenantId };
229
+ };
230
+ getOrgFilter = () => {
231
+ return {
232
+ tenantId: this.tenantId,
233
+ orgId: this.orgId,
234
+ };
235
+ };
236
+ getWorkflowTaskFilter() {
237
+ return {
238
+ 'data.tenantId': this.tenantId,
239
+ 'assignments.assignee': this.getUid(),
240
+ // 'assignments.assignee': User.getInstance().getUid(),
241
+ };
242
+ }
243
+ getCreateFilter = () => {
244
+ const u = this;
245
+ return {
246
+ tenantId: u.tenantId,
247
+ orgId: u.orgId,
248
+ branchId: u.branchId,
249
+ createdby: u.uid,
250
+ updatedby: u.uid,
251
+ created: new Date().toISOString(),
252
+ updated: new Date().toISOString()
253
+ };
254
+ };
255
+ getUpdateFilter = () => {
256
+ const u = this;
257
+ return {
258
+ updatedby: u.uid,
259
+ updated: new Date().toISOString()
260
+ };
261
+ };
262
+ setXorg = (xorg) => {
263
+ try {
264
+ const decodedText: string = Base64URL.decodeText(xorg);
265
+ const arrXorg = decodedText.split('-');
266
+
267
+ if (arrXorg.length == 3) {
268
+ const u = this;
269
+ u.tenantId = Number(arrXorg[0]);
270
+ u.orgId = Number(arrXorg[1]);
271
+ u.branchId = Number(arrXorg[2]);
272
+
273
+ } else {
274
+ throw 'invalid x-org';
275
+ }
276
+ } catch (err) {
277
+ throw err;
278
+ }
279
+ };
280
+
281
+
282
+ async getUserInfo(){
283
+
284
+ // obtain basic user info
285
+ const userinfo = {
286
+ _id: this.getId() ,
287
+ tenantId: this.getTenantId(),
288
+ orgId: this.getOrgId(),
289
+ branchId: this.getBranchId(),
290
+ branchCode: this.branchCode,
291
+ branchName: this.branchName,
292
+ email: this.getEmail(),
293
+ uid: this.getUid(),
294
+ fullName: this.getFullname(),
295
+ group: this.group,
296
+ roles: this.getRoles(),
297
+ branches: this.getBranches(),
298
+ invites: await this.getInvites()
299
+ }
300
+ this.logger.debug(userinfo,"init getUserInfo()")
301
+
302
+ if(this.getId()!=''){
303
+ this.logger.debug(userinfo,"obtain permissions and invitations")
304
+ const filter:PipelineStage ={ $match:{uid:this.uid }} as PipelineStage
305
+ const permission:PipelineStage = {$lookup: {
306
+ from : 'permission',
307
+ localField : 'uid',
308
+ foreignField : 'uid',
309
+ as : 'permissions',
310
+ pipeline:[
311
+ {$match:{uid: this.uid,},},
312
+ {$lookup:{from : 'branch',localField : 'branchId',foreignField : 'branchId',as : 'branch',}},
313
+ ]
314
+ }}
315
+ const lookupinvitation:PipelineStage = {$lookup: {
316
+ from : 'user',
317
+ localField : 'email',
318
+ foreignField : 'email',
319
+ as : 'invites',
320
+ pipeline:[
321
+ {$match:{uid: ''}},
322
+ {$lookup:{from : 'tenant',localField : 'tenantId',foreignField : 'tenantId',as : 'tenant',}},
323
+ ]
324
+ }}
325
+
326
+ const pipeline:PipelineStage[]=[
327
+ filter,
328
+ permission,
329
+ lookupinvitation
330
+ ]
331
+ this.logger.debug(pipeline,"getUserInfo")
332
+ // // // const users = await usermodel.find(filter)
333
+ const users = await this.usermodel.aggregate(pipeline)
334
+
335
+
336
+ this.invites = users[0].invites
337
+ this.branches = users[0].permissions
338
+ this.logger.verbose(users,"getUserInfo")
339
+ userinfo.branches=this.getBranches()
340
+ userinfo.invites=this.getInvites()
341
+ // this.logger.debug(`getUserInfo result ${userinfo}`)
342
+
343
+ }else{
344
+ this.logger.log(`${this.getUid()} does not exists in current tenant (${this.tenantId})`)
345
+ }
346
+
347
+
348
+ return userinfo
349
+ }
350
+ async decideInvitation (id:string, decision:string):Promise<boolean> {
351
+
352
+ // const usermodel = model<User>('user',UserMongoSchema,'user')
353
+ const res = await this.usermodel.findById(id)
354
+ // console.log("find invitation:",res)
355
+ if(!res.uid){
356
+ res.uid = this.getUid()
357
+ res.fullname = this.fullname
358
+ res.active = true
359
+
360
+ if(decision=='accept'){
361
+ const result = await res.save({session:this.dbsession})
362
+ this.logger.log(result,"accept invitation 1")
363
+ //set permission of all user under this.user_id
364
+ const updateresult = await this.permmodel.updateMany({ uid:'',user_id:res._id},{uid:this.getUid()}).session(this.dbsession)
365
+ this.logger.log(updateresult,"update all permission")
366
+ return true
367
+ }else{
368
+ const deleteresult = await this.usermodel.deleteOne({_id: id}).session(this.dbsession)
369
+ await this.permmodel.deleteMany({ uid:'',user_id:res._id}).session(this.dbsession)
370
+ return true
371
+ }
372
+ }else{
373
+ throw new BadRequestException(`${id} not found.`)
374
+ }
375
+
376
+ }
377
+
378
+ isRealmAdmin = ()=>{
379
+ const o = this.ssoACL
380
+ const ssoclient = process.env.OAUTH2_CLIENTID
381
+ const adminRole = process.env.OAUTH2_ADMINROLE
382
+ if( o[ssoclient] && o[ssoclient]['roles'] && o[ssoclient]['roles'] == adminRole ){
383
+ //console.log("isadmin")
384
+ return true
385
+ }else{
386
+ //console.log("not admin")
387
+ return false
388
+ }
389
+
390
+ }
391
+ }
@@ -0,0 +1,71 @@
1
+ import {
2
+ Controller,
3
+ Get,
4
+ Put,
5
+ Post,
6
+ Delete,
7
+ Body,
8
+ Param,
9
+ Type,
10
+ } from '@nestjs/common';
11
+ // import { ApiTags, ApiBody, ApiResponse, ApiOperation } from '@nestjs/swagger';
12
+ import {UserContext} from '../commons/user.context'
13
+ import {SearchBody} from '../types';
14
+ const doctype = 'person'.toUpperCase();
15
+ type ServiceType = {
16
+ list: Function;
17
+ search: Function;
18
+ create: Function;
19
+ //update: Function;
20
+ //delete: Function;
21
+ findById: Function;
22
+ findIdThenDelete: Function;
23
+ findIdThenUpdate: Function;
24
+ setData: Function;
25
+ getAutoComplete: Function;
26
+ };
27
+
28
+ // @ApiTags(doctype)
29
+ // @Controller(doctype.toLowerCase())
30
+ export class SimpleAppAbstractController<TService extends ServiceType, TApiSchema, T> {
31
+ protected service: TService;
32
+ protected tryno: number;
33
+ // protected apiSchemaClass=
34
+
35
+ constructor(service: TService) {
36
+ this.service = service;
37
+ }
38
+
39
+ async _list(appuser:UserContext) {
40
+ return this.service.list(appuser,);
41
+ }
42
+ async _search(appuser:UserContext,searchObject:SearchBody) {
43
+
44
+ return this.service.search(appuser,searchObject['filter'], searchObject['fields'],searchObject['sorts']);
45
+ }
46
+ async _autocomplete(appuser:UserContext,keyword: string) {
47
+ return this.service.getAutoComplete(appuser,keyword);
48
+ }
49
+ async _findOne(appuser:UserContext,id: string) {
50
+ const result = (await this.service.findById(appuser,id)) as TApiSchema;
51
+
52
+ return result as Type<TApiSchema>;
53
+ }
54
+
55
+ async _create(appuser:UserContext,data: TApiSchema) {
56
+ //const newdata: persontype.Person = { ...data };
57
+ const newdata: T = {} as T; //= { ...data };
58
+ Object.assign(newdata, data); //
59
+ return this.service.create(appuser,newdata) as TApiSchema;
60
+ }
61
+
62
+ async _update(appuser:UserContext,id: string, data: TApiSchema) {
63
+ const newdata: T = {} as T; //= { ...data };
64
+ Object.assign(newdata, data); //
65
+ return this.service.findIdThenUpdate(appuser,id, newdata) as TApiSchema;
66
+ }
67
+
68
+ async _delete(appuser:UserContext,id: string) {
69
+ return this.service.findIdThenDelete(appuser,id);
70
+ }
71
+ }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * This file was automatically generated by simpleapp generator.
3
+ * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
4
+ * and regenerate this file.
5
+ * last change 2023-09-09
6
+ * Author: Ks Tan
7
+ */
8
+
9
+ import { Schema } from 'mongoose';
10
+ import { Permission, } from '../types/perm.type';
11
+ const schemasetting = {
12
+
13
+
14
+
15
+ tenantId: {type: Number, required: false}, //field
16
+
17
+
18
+ orgId: {type: Number, required: false}, //field
19
+
20
+
21
+ branchId: {type: Number, required: false}, //field
22
+
23
+
24
+ group: {type: String, required: false}, //field
25
+
26
+
27
+ uid: {type: String, required: false}, //field
28
+
29
+
30
+ user_id: {type: String, required: false}, //field
31
+
32
+
33
+ _id: {type:'string', required:true},
34
+
35
+
36
+ doctype: {type: String, required: false}, //field
37
+
38
+
39
+ created: {type: String, required: false}, //field
40
+
41
+
42
+ updated: {type: String, required: false}, //field
43
+
44
+
45
+ createdby: {type: String, required: false}, //field
46
+
47
+
48
+ updatedby: {type: String, required: false}, //field
49
+ };
50
+
51
+ export const PermissionMongoSchema = new Schema(schemasetting,{collection: 'permission'})
52
+ .index({user_id:1,branchId:1},{unique:true})
53
+ // .index({productName:1,orgId:1});
@@ -0,0 +1,45 @@
1
+ /**
2
+ * This file was automatically generated by simpleapp generator.
3
+ * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
4
+ * and regenerate this file.
5
+ * last change 2023-09-09
6
+ * Author: Ks Tan
7
+ */
8
+
9
+ import { Schema } from 'mongoose';
10
+ import { TenantOwner, Tenant } from '../types/tenant.type';
11
+ const schemasetting = {
12
+ tenantId: { type: Number, required: true }, //field
13
+
14
+ tenantName: { type: String, required: true }, //field
15
+
16
+ uuid: { type: String, required: false }, //field
17
+
18
+ active: { type: Boolean, required: false,default:true }, //field
19
+
20
+ description: { type: String, required: false }, //field
21
+
22
+ owner: { type: <TenantOwner>{}, required: false }, //object
23
+
24
+ _id: {type:'string', required:true},
25
+
26
+ doctype: { type: String, required: false }, //field
27
+
28
+ created: { type: String, required: false }, //field
29
+
30
+ updated: { type: String, required: false }, //field
31
+
32
+ createdby: { type: String, required: false }, //field
33
+
34
+ updatedby: { type: String, required: false }, //field
35
+
36
+ orgId: { type: Number, required: false }, //field
37
+
38
+ branchId: { type: Number, required: false }, //field
39
+ };
40
+
41
+ export const TenantMongoSchema = new Schema(schemasetting, {
42
+ collection: 'tenant',
43
+ })
44
+ .index({ tenantId: 1 }, { unique: true })
45
+ .index({ uuid:1 },{unique:true});
@@ -0,0 +1,57 @@
1
+ /**
2
+ * This file was automatically generated by simpleapp generator.
3
+ * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
4
+ * and regenerate this file.
5
+ * last change 2023-09-09
6
+ * Author: Ks Tan
7
+ */
8
+
9
+ import { Schema } from 'mongoose';
10
+ import { User, } from '../types/user.type';
11
+ const schemasetting = {
12
+
13
+
14
+
15
+ uid: {type: String, required: false}, //field
16
+
17
+
18
+ fullname: {type: String, required: true}, //field
19
+
20
+
21
+ email: {type: String, required: true}, //field
22
+
23
+
24
+ active: {type: Boolean, required: false,default:true}, //field
25
+
26
+
27
+ _id: {type:'string', required:true},
28
+
29
+
30
+ doctype: {type: String, required: false}, //field
31
+
32
+
33
+ created: {type: String, required: false}, //field
34
+
35
+
36
+ updated: {type: String, required: false}, //field
37
+
38
+
39
+ createdby: {type: String, required: false}, //field
40
+
41
+
42
+ updatedby: {type: String, required: false}, //field
43
+
44
+
45
+ tenantId: {type: Number, required: false}, //field
46
+
47
+
48
+ orgId: {type: Number, required: false}, //field
49
+
50
+
51
+ branchId: {type: Number, required: false}, //field
52
+ };
53
+
54
+ export const UserMongoSchema = new Schema(schemasetting,{collection: 'user'})
55
+ .index({email:1,tenantId:1},{unique:true})
56
+ .index({fullname:1,tenantId:1});
57
+