@go-mailer/jarvis 5.1.3 → 5.2.1

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/index.js CHANGED
@@ -5,11 +5,13 @@ const QueryBuilder = require('./lib/query')
5
5
  const HTTP = require('./lib/middlewares/http')
6
6
  const Authenticator = require('./lib/middlewares/auth')
7
7
  const AutomationConstants = require('./lib/constants/automation')
8
+ const Automata = require('./lib/middlewares/automation')
8
9
  const { RequestLogger, ProcessLogger } = require('./lib/middlewares/logger')
9
10
  const Utility = require('./lib/utilitiy/index')
10
11
 
11
12
  module.exports = {
12
13
  Authenticator,
14
+ Automata,
13
15
  AutomationConstants,
14
16
  EnvVar,
15
17
  FeatureFlag,
@@ -0,0 +1,34 @@
1
+ const axios = require('axios').default
2
+ const Env = require('../env')
3
+ const { fetchTenants } = require('./iam')
4
+ const AUTOMATA_URI = Env.fetch('AUTOMATA_SERVICE_URI', true)
5
+ const GM_API_KEY = Env.fetch('GM_API_KEY', true)
6
+
7
+ const triggerEvent = async ({ event_code, tenant_id }) => {
8
+ const {
9
+ tenants: [tenant]
10
+ } = await fetchTenants(`id=${tenant_id}`)
11
+
12
+ if (!tenant) throw new Error('Cannot trigger event for tenant')
13
+ const {
14
+ emails: [contact_email]
15
+ } = tenant
16
+
17
+ await axios.get(
18
+ `${AUTOMATA_URI}/api/v1/events`,
19
+ {
20
+ data: {
21
+ event_code,
22
+ contact_email,
23
+ context: { ...tenant }
24
+ }
25
+ },
26
+ {
27
+ headers: {
28
+ authorization: `Bearer ${GM_API_KEY}`
29
+ }
30
+ }
31
+ )
32
+ }
33
+
34
+ module.exports = { triggerEvent }
@@ -17,6 +17,20 @@ const checkAuthority = async ({ action, resource, user_id, tenant_id }) => {
17
17
  return payload.is_permitted
18
18
  }
19
19
 
20
+ const fetchTenants = async (query = '') => {
21
+ const { data: response } = await axios.get(`${IAM_URI}/tenants?${query}`, {
22
+ headers: {
23
+ authorization: `Bearer ${DEFAULT_TOKEN}`
24
+ }
25
+ })
26
+
27
+ const { error, payload } = response
28
+ if (error) throw new Error(error)
29
+
30
+ const { data: tenants, meta } = payload
31
+ return { tenants, size: meta?.size || 0 }
32
+ }
33
+
20
34
  const verifyAPIKey = async (key) => {
21
35
  const { error, payload } = (
22
36
  await axios.get(`${IAM_URI}/keys/verify/${key}`, {
@@ -53,4 +67,4 @@ const verifyFeatureFlag = async (flag_name, criteria = {}) => {
53
67
  return payload.is_permitted
54
68
  }
55
69
 
56
- module.exports = { checkAuthority, verifyAPIKey, verifyFeatureFlag }
70
+ module.exports = { checkAuthority, fetchTenants, verifyAPIKey, verifyFeatureFlag }
@@ -112,7 +112,7 @@ const authenticateUser = async (request, response, next) => {
112
112
  const authorizeUser = ({ action, resource }) => {
113
113
  return async (request, response, next) => {
114
114
  try {
115
- const { is_admin,is_service_request, tenant_id, user_id } = request
115
+ const { is_admin, is_service_request, tenant_id, user_id } = request
116
116
  if (is_admin || is_service_request) return next()
117
117
 
118
118
  await checkAuthority({ action, resource, tenant_id, user_id })
@@ -0,0 +1,5 @@
1
+ const { triggerEvent } = require('../clients/automata')
2
+
3
+ module.exports = {
4
+ triggerEvent
5
+ }
package/lib/query.js CHANGED
@@ -39,9 +39,7 @@ const buildQuery = (options) => {
39
39
  const field_value = options[field] ? options[field].toString() : ''
40
40
  let condition
41
41
 
42
- if (field_value.includes(':')) {
43
- condition = buildInQuery(field_value)
44
- } else if (field_value.includes('!')) {
42
+ if (field_value.includes('!')) {
45
43
  condition = buildNorQuery(field_value)
46
44
  } else if (field_value.includes('~')) {
47
45
  condition = buildRangeQuery(field_value)
@@ -1,9 +1,9 @@
1
- const { Schema } = require("redis-om")
1
+ const { Schema } = require('redis-om')
2
2
 
3
- const ActivityLogSchema = new Schema("activitylog", {
4
- id: { type: "number" },
3
+ const ActivityLogSchema = new Schema('activitylog', {
4
+ id: { type: 'number' }
5
5
  })
6
6
 
7
7
  module.exports = {
8
- ActivityLogSchema,
8
+ ActivityLogSchema
9
9
  }
@@ -1,11 +1,11 @@
1
- const { ActivityLogSchema } = require("./iam/Activity");
2
- const { ContactImportSchema } = require("./Contact");
3
- const { PostalSchema } = require("./Postal");
4
- const { TaskSchema } = require("./automation/Task");
5
- const { CampaignSchema } = require("./mailing/Campaign");
6
- const { LinkSchema } = require("./mailing/Link");
7
- const { MailActionSchema } = require("./mailing/Mailaction");
8
- const { AutoTopUpSchema } = require("./sales/Wallet");
1
+ const { ActivityLogSchema } = require('./iam/Activity')
2
+ const { ContactImportSchema } = require('./Contact')
3
+ const { PostalSchema } = require('./Postal')
4
+ const { TaskSchema } = require('./automation/Task')
5
+ const { CampaignSchema } = require('./mailing/Campaign')
6
+ const { LinkSchema } = require('./mailing/Link')
7
+ const { MailActionSchema } = require('./mailing/Mailaction')
8
+ const { AutoTopUpSchema } = require('./sales/Wallet')
9
9
 
10
10
  module.exports = {
11
11
  ActivityLogSchema,
@@ -15,5 +15,5 @@ module.exports = {
15
15
  LinkSchema,
16
16
  MailActionSchema,
17
17
  PostalSchema,
18
- AutomationTaskSchema: TaskSchema,
19
- };
18
+ AutomationTaskSchema: TaskSchema
19
+ }
@@ -1,7 +1,7 @@
1
1
  const { Schema } = require('redis-om')
2
2
 
3
3
  const AutoTopUpSchema = new Schema('auto_topup', {
4
- tenant_id: { type: 'number' },
4
+ tenant_id: { type: 'number' }
5
5
  })
6
6
 
7
7
  module.exports = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-mailer/jarvis",
3
- "version": "5.1.3",
3
+ "version": "5.2.1",
4
4
  "main": "index.js",
5
5
  "repository": "git@github.com:go-mailer-ltd/jarvis-node.git",
6
6
  "author": "Nathan Oguntuberu <nateoguns.work@gmail.com>",