corebasic 1.0.101 → 1.0.103

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/libs/features.js CHANGED
@@ -39,7 +39,7 @@ let appId = Utils.uid()
39
39
  let SERVICE_ADDRESS = process.env.APP_ENDPOINT || 'http://127.0.0.1:3000'
40
40
 
41
41
 
42
- export const send = async (feature, params, payload, headers) => {
42
+ export const send = async (meta, feature, data, params) => {
43
43
  const throwError = () => {throw {message: `Feature ${feature} not found in internal or external list during inter feature call`}}
44
44
  let {api, service = SERVICE_ADDRESS} = getFeature(feature) ?? SLYP_FEATURES_LIST[feature] ?? throwError()
45
45
  let method = getFeatureMethod(api)
@@ -52,10 +52,10 @@ export const send = async (feature, params, payload, headers) => {
52
52
  throw {feature, when: 'api/params/substitution', message: "Internal Feature Call" }
53
53
  }
54
54
 
55
- payload = { ...payload, feature, txn: Utils.uid() }
55
+ let payload = { ...meta, data, feature, txn: Utils.uid() }
56
56
 
57
57
  // return (await axios[method](`${service}${url}`, { data: payload, headers: {jwt: headers.jwt}, timeout: 1000 })).data
58
- return (await axios[method](`${service}${url}`, { data: payload, headers: {jwt: SERVICE_ACCESS_TOKEN, service: true}, timeout: 1000 })).data
58
+ return (await axios[method](`${service}${url}`, payload, {headers: {jwt: SERVICE_ACCESS_TOKEN, service: true}, timeout: 1000 })).data
59
59
  }
60
60
 
61
61
  let appids = {}
@@ -123,6 +123,8 @@ export const start = async (app, url, file) => {
123
123
  if (!req.params[param])
124
124
  throw { status: 404, message: "Resource not found. One or more url parameter not specified." }
125
125
 
126
+ let meta = {...req.body, data: undefined}
127
+ req.meta = meta
126
128
 
127
129
  if (method === "get") {
128
130
  try {
@@ -201,10 +203,11 @@ export const start = async (app, url, file) => {
201
203
 
202
204
  function prepareMessage(req) {
203
205
  let txn = req.body.txn ?? Utils.uid()
204
- let _id = req.body.data._id ?? txn
205
- let {data, feature, app, user, client, version} = req.body
206
+ let _id = req.body.data?._id ?? txn
207
+ let {data, feature, app, user, outlet, company, client, version} = req.body
208
+ let meta = req.meta
206
209
  let params = req.params
207
- return { data, params, feature, app, user, client, version, txn, id: _id, date: new Date().getTime() }
210
+ return { data, params, meta, feature, app, user, outlet, company, client, version, txn, id: _id, date: new Date().getTime() }
208
211
  }
209
212
  const commandAction = async (req, res, topic) => {
210
213
  let message = prepareMessage(req)
package/libs/messaging.js CHANGED
@@ -74,8 +74,13 @@ export async function start(app) {
74
74
 
75
75
 
76
76
  export async function publish(channel, message) {
77
- if (publisher)
78
- await publisher.publish(channel, typeof message === "object" ? JSON.stringify(message) : message);
77
+ if (publisher && !Utils.isEmpty(channel)) {
78
+ try {
79
+ await publisher.publish(channel, typeof message === "object" ? JSON.stringify(message) : message)
80
+ } catch {
81
+ console.log('Error: Publishing on redis channel', channel)
82
+ }
83
+ }
79
84
  }
80
85
 
81
86
 
@@ -0,0 +1,91 @@
1
+
2
+
3
+ import * as Dip from './dip.js'
4
+
5
+
6
+ let PRIVILEGES_CACHE = {
7
+
8
+ // "<company>": {
9
+ // "Sales Man": {
10
+ // "products.query.list": { type: "Feature" }
11
+ // },
12
+ // "Accountant": {
13
+ // "Sales Man": { type: "Role" },
14
+ // "accounts.query.list": { type: "Feature" },
15
+ // },
16
+ // "Cashier": {
17
+ // "Sales Man": { type: "Role" },
18
+ // "cash.query.list": { type: "Feature" },
19
+ // },
20
+ // }
21
+ }
22
+
23
+
24
+ export const get = async (company) => {
25
+ PRIVILEGES_CACHE[company] = {}
26
+ let privileges = await Dip.query("privileges", { })
27
+ privileges.forEach(privilege => PRIVILEGES_CACHE[company][privilege.name] = privilege)
28
+
29
+ // Format
30
+ for (let role in PRIVILEGES_CACHE[company])
31
+ PRIVILEGES_CACHE[company][role] = PRIVILEGES_CACHE[company][role].items.reduce( (obj, item) => ({...obj, [item.name]: {type: item.type} }), {})
32
+
33
+ return PRIVILEGES_CACHE[company]
34
+ }
35
+
36
+ export const start = async () => {
37
+ let companies = await Dip.query("companies", { })
38
+ for (let company of companies)
39
+ await get(company._id)
40
+ }
41
+
42
+
43
+ function rolesToFeatures(company, roles) {
44
+ let features = []
45
+
46
+ let privileges = PRIVILEGES_CACHE[company]
47
+
48
+ roles.forEach(role => {
49
+ for (let key in privileges[role]) {
50
+ let isFeature = privileges[role][key].type === "Feature"
51
+ let isRole = privileges[role][key].type === "Role"
52
+ if (isFeature)
53
+ features.push(key)
54
+ else if (isRole)
55
+ features.push(...(rolesToFeatures(company, [key])))
56
+ }
57
+ })
58
+ return features
59
+ }
60
+
61
+
62
+
63
+
64
+
65
+
66
+ export const getAllowedFeatures = (company, roles) => {
67
+ return [...new Set(rolesToFeatures(company, roles))]
68
+ }
69
+
70
+ export const check = async (company, user, feature) => {
71
+ let roles = await getRoles(company, user)
72
+ return roles.includes(feature)
73
+ }
74
+
75
+ export const checkRequest = async (req) => {
76
+ return await check(req.body.company, req.body.user, req.body.feature)
77
+ }
78
+
79
+
80
+
81
+
82
+ const getRoles = async (company, user) => {
83
+ let staff = (await Dip.query("staff", { user: user }))[0]
84
+ let roles = (await Dip.query("privileges.staff", { _id: staff._id }))[0].items.map(item => item._id)
85
+ return getAllowedFeatures(company, roles)
86
+ }
87
+
88
+
89
+
90
+
91
+
package/libs/session.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import jwt from 'jsonwebtoken'
2
+ import * as Privilege from './privileges.js'
2
3
  let app
3
4
 
4
5
 
@@ -15,8 +16,9 @@ export const start = (expressApp, allowedUrls) => {
15
16
  urlsAllowed = ["/refreshToken", "/login"].concat(allowedUrls ?? [])
16
17
  app = expressApp
17
18
 
19
+ Privilege.start()
18
20
 
19
- app.use((req, res, next) => {
21
+ app.use(async (req, res, next) => {
20
22
 
21
23
  // return next() // Disable session
22
24
 
@@ -28,7 +30,7 @@ export const start = (expressApp, allowedUrls) => {
28
30
  const service = req.header('SERVICE'); // Case insensitive search
29
31
  if (service && jwt.verify(token, DEPLOY_TOKEN_SECRET))
30
32
  return next()
31
- else if (jwt.verify(token, ACCESS_TOKEN_SECRET))
33
+ else if (jwt.verify(token, ACCESS_TOKEN_SECRET) && (process.env.GRANT_FULL_ACCESS || await Privilege.checkRequest(req)))
32
34
  return next()
33
35
  throw null;
34
36
  } catch (error) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "corebasic",
3
3
  "type": "module",
4
- "version": "1.0.101",
4
+ "version": "1.0.103",
5
5
  "description": "",
6
6
  "main": "index.js",
7
7
  "scripts": {