@ductape/sdk 0.0.4-v4 → 0.0.4-v40

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 (193) hide show
  1. package/dist/api/services/appApi.service.d.ts +48 -2
  2. package/dist/api/services/appApi.service.js +101 -2
  3. package/dist/api/services/appApi.service.js.map +1 -1
  4. package/dist/api/services/pricingApi.service.d.ts +10 -0
  5. package/dist/api/services/pricingApi.service.js +34 -0
  6. package/dist/api/services/pricingApi.service.js.map +1 -0
  7. package/dist/api/services/processorApi.service.d.ts +12 -2
  8. package/dist/api/services/processorApi.service.js +12 -2
  9. package/dist/api/services/processorApi.service.js.map +1 -1
  10. package/dist/api/services/productsApi.service.d.ts +39 -1
  11. package/dist/api/services/productsApi.service.js +76 -0
  12. package/dist/api/services/productsApi.service.js.map +1 -1
  13. package/dist/api/services/userApi.service.js +1 -0
  14. package/dist/api/services/userApi.service.js.map +1 -1
  15. package/dist/api/services/workspaceApi.service.js +1 -0
  16. package/dist/api/services/workspaceApi.service.js.map +1 -1
  17. package/dist/api/urls.d.ts +6 -1
  18. package/dist/api/urls.js +12 -2
  19. package/dist/api/urls.js.map +1 -1
  20. package/dist/api/utils/cache.utils.d.ts +1 -1
  21. package/dist/api/utils/cache.utils.js +2 -2
  22. package/dist/api/utils/cache.utils.js.map +1 -1
  23. package/dist/api/utils/strings.utils.d.ts +2 -0
  24. package/dist/api/utils/strings.utils.js +14 -0
  25. package/dist/api/utils/strings.utils.js.map +1 -1
  26. package/dist/apps/services/app.service.d.ts +41 -33
  27. package/dist/apps/services/app.service.js +473 -185
  28. package/dist/apps/services/app.service.js.map +1 -1
  29. package/dist/apps/validators/joi-validators/create.appAction.validator.d.ts +1 -2
  30. package/dist/apps/validators/joi-validators/create.appAction.validator.js +21 -2
  31. package/dist/apps/validators/joi-validators/create.appAction.validator.js.map +1 -1
  32. package/dist/apps/validators/joi-validators/update.appAction.validator.js +11 -1
  33. package/dist/apps/validators/joi-validators/update.appAction.validator.js.map +1 -1
  34. package/dist/apps/validators/joi-validators/update.appActionResponse.validator.d.ts +1 -1
  35. package/dist/apps/validators/joi-validators/update.appActionResponse.validator.js +34 -1
  36. package/dist/apps/validators/joi-validators/update.appActionResponse.validator.js.map +1 -1
  37. package/dist/clients/apps.client.js +1 -1
  38. package/dist/clients/apps.client.js.map +1 -1
  39. package/dist/clients/pricing.client.d.ts +3 -0
  40. package/dist/clients/pricing.client.js +33 -0
  41. package/dist/clients/pricing.client.js.map +1 -0
  42. package/dist/imports/imports.service.d.ts +3 -3
  43. package/dist/imports/imports.service.js +7 -7
  44. package/dist/imports/imports.service.js.map +1 -1
  45. package/dist/imports/imports.types.d.ts +8 -0
  46. package/dist/imports/repos/postmanV21.repo.d.ts +1 -1
  47. package/dist/imports/repos/postmanV21.repo.js +29 -2
  48. package/dist/imports/repos/postmanV21.repo.js.map +1 -1
  49. package/dist/index.d.ts +1009 -91
  50. package/dist/index.js +969 -126
  51. package/dist/index.js.map +1 -1
  52. package/dist/inputs/inputs.service.js +2 -2
  53. package/dist/inputs/inputs.service.js.map +1 -1
  54. package/dist/inputs/utils/inputs.utils.create.js +1 -1
  55. package/dist/inputs/utils/inputs.utils.create.js.map +1 -1
  56. package/dist/logs/logs.service.js +1 -0
  57. package/dist/logs/logs.service.js.map +1 -1
  58. package/dist/logs/logs.types.d.ts +5 -0
  59. package/dist/logs/logs.types.js.map +1 -1
  60. package/dist/parsers/index.d.ts +3 -0
  61. package/dist/parsers/index.js +27 -0
  62. package/dist/parsers/index.js.map +1 -0
  63. package/dist/parsers/pipelines/postman.pipelines.d.ts +15 -0
  64. package/dist/parsers/pipelines/postman.pipelines.js +103 -0
  65. package/dist/parsers/pipelines/postman.pipelines.js.map +1 -0
  66. package/dist/parsers/types/postman.types.d.ts +200 -0
  67. package/dist/parsers/types/postman.types.js +3 -0
  68. package/dist/parsers/types/postman.types.js.map +1 -0
  69. package/dist/parsers/utils/postman.utils.d.ts +12 -0
  70. package/dist/parsers/utils/postman.utils.js +116 -0
  71. package/dist/parsers/utils/postman.utils.js.map +1 -0
  72. package/dist/parsers/validators/postman-auth.validators.d.ts +10 -0
  73. package/dist/parsers/validators/postman-auth.validators.js +127 -0
  74. package/dist/parsers/validators/postman-auth.validators.js.map +1 -0
  75. package/dist/parsers/validators/postman-request.validators.d.ts +13 -0
  76. package/dist/parsers/validators/postman-request.validators.js +139 -0
  77. package/dist/parsers/validators/postman-request.validators.js.map +1 -0
  78. package/dist/parsers/validators/postman-response.validators.d.ts +13 -0
  79. package/dist/parsers/validators/postman-response.validators.js +150 -0
  80. package/dist/parsers/validators/postman-response.validators.js.map +1 -0
  81. package/dist/parsers/validators/postman-variable.validators.d.ts +14 -0
  82. package/dist/parsers/validators/postman-variable.validators.js +163 -0
  83. package/dist/parsers/validators/postman-variable.validators.js.map +1 -0
  84. package/dist/pricing/pricing.repo.d.ts +0 -0
  85. package/dist/pricing/pricing.repo.js +1 -0
  86. package/dist/pricing/pricing.repo.js.map +1 -0
  87. package/dist/pricing/pricing.service.d.ts +24 -0
  88. package/dist/pricing/pricing.service.js +51 -0
  89. package/dist/pricing/pricing.service.js.map +1 -0
  90. package/dist/pricing/pricing.types.d.ts +76 -0
  91. package/dist/pricing/pricing.types.js +21 -0
  92. package/dist/pricing/pricing.types.js.map +1 -0
  93. package/dist/pricing/utils/string.utils.d.ts +1 -0
  94. package/dist/pricing/utils/string.utils.js +9 -0
  95. package/dist/pricing/utils/string.utils.js.map +1 -0
  96. package/dist/processor/repos/sms.repo.d.ts +4 -4
  97. package/dist/processor/repos/sms.repo.js +23 -10
  98. package/dist/processor/repos/sms.repo.js.map +1 -1
  99. package/dist/processor/services/messagebrokers/kafka.service.js +0 -2
  100. package/dist/processor/services/messagebrokers/kafka.service.js.map +1 -1
  101. package/dist/processor/services/messagebrokers/rabbitmq.service.d.ts +9 -1
  102. package/dist/processor/services/messagebrokers/rabbitmq.service.js +40 -11
  103. package/dist/processor/services/messagebrokers/rabbitmq.service.js.map +1 -1
  104. package/dist/processor/services/processor.service.d.ts +40 -9
  105. package/dist/processor/services/processor.service.js +638 -276
  106. package/dist/processor/services/processor.service.js.map +1 -1
  107. package/dist/processor/services/request.service.d.ts +36 -0
  108. package/dist/processor/services/request.service.js +304 -0
  109. package/dist/processor/services/request.service.js.map +1 -0
  110. package/dist/processor/types/request.types.d.ts +14 -0
  111. package/dist/processor/types/request.types.js +3 -0
  112. package/dist/processor/types/request.types.js.map +1 -0
  113. package/dist/processor/utils/processor.utils.d.ts +3 -0
  114. package/dist/processor/utils/processor.utils.js +89 -6
  115. package/dist/processor/utils/processor.utils.js.map +1 -1
  116. package/dist/processor/utils/request.utils.d.ts +20 -0
  117. package/dist/processor/utils/request.utils.js +113 -0
  118. package/dist/processor/utils/request.utils.js.map +1 -0
  119. package/dist/processor/utils/storage.util.js +54 -40
  120. package/dist/processor/utils/storage.util.js.map +1 -1
  121. package/dist/products/services/products.service.d.ts +98 -77
  122. package/dist/products/services/products.service.js +569 -325
  123. package/dist/products/services/products.service.js.map +1 -1
  124. package/dist/products/services/utils/crypt.utils.d.ts +1 -0
  125. package/dist/products/services/utils/crypt.utils.js +17 -0
  126. package/dist/products/services/utils/crypt.utils.js.map +1 -0
  127. package/dist/products/services/utils/functions.utils.d.ts +13 -0
  128. package/dist/products/services/utils/functions.utils.js +294 -0
  129. package/dist/products/services/utils/functions.utils.js.map +1 -0
  130. package/dist/products/services/utils/objects.utils.d.ts +13 -0
  131. package/dist/products/services/utils/objects.utils.js +89 -0
  132. package/dist/products/services/utils/objects.utils.js.map +1 -0
  133. package/dist/products/services/utils/string.utils.d.ts +12 -0
  134. package/dist/products/services/utils/string.utils.js +168 -0
  135. package/dist/products/services/utils/string.utils.js.map +1 -0
  136. package/dist/products/utils/string.utils.d.ts +1 -1
  137. package/dist/products/utils/string.utils.js +14 -2
  138. package/dist/products/utils/string.utils.js.map +1 -1
  139. package/dist/products/validators/index.d.ts +2 -1
  140. package/dist/products/validators/index.js +3 -1
  141. package/dist/products/validators/index.js.map +1 -1
  142. package/dist/products/validators/joi-validators/create.productEnv.validator.js +1 -0
  143. package/dist/products/validators/joi-validators/create.productEnv.validator.js.map +1 -1
  144. package/dist/products/validators/joi-validators/create.productHealthcheck.validator.d.ts +4 -0
  145. package/dist/products/validators/joi-validators/create.productHealthcheck.validator.js +58 -0
  146. package/dist/products/validators/joi-validators/create.productHealthcheck.validator.js.map +1 -0
  147. package/dist/products/validators/joi-validators/create.productMessageBrokers.validator.js +9 -4
  148. package/dist/products/validators/joi-validators/create.productMessageBrokers.validator.js.map +1 -1
  149. package/dist/products/validators/joi-validators/create.productNotification.validator.js +5 -2
  150. package/dist/products/validators/joi-validators/create.productNotification.validator.js.map +1 -1
  151. package/dist/products/validators/joi-validators/create.userAuth.validator.js +1 -0
  152. package/dist/products/validators/joi-validators/create.userAuth.validator.js.map +1 -1
  153. package/dist/products/validators/joi-validators/update.dataValue.validator.js +1 -0
  154. package/dist/products/validators/joi-validators/update.dataValue.validator.js.map +1 -1
  155. package/dist/products/validators/joi-validators/update.productEnv.validator.js +3 -0
  156. package/dist/products/validators/joi-validators/update.productEnv.validator.js.map +1 -1
  157. package/dist/products/validators/joi-validators/update.userAuth.validator.js +1 -0
  158. package/dist/products/validators/joi-validators/update.userAuth.validator.js.map +1 -1
  159. package/dist/test/test.health.d.ts +1 -0
  160. package/dist/test/test.health.js +49 -0
  161. package/dist/test/test.health.js.map +1 -0
  162. package/dist/test/test.import.js +51 -4
  163. package/dist/test/test.import.js.map +1 -1
  164. package/dist/test/test.imports.js +22 -7
  165. package/dist/test/test.imports.js.map +1 -1
  166. package/dist/test/test.notifiers.d.ts +1 -0
  167. package/dist/test/test.notifiers.js +85 -0
  168. package/dist/test/test.notifiers.js.map +1 -0
  169. package/dist/test/test.processor.js +32 -115
  170. package/dist/test/test.processor.js.map +1 -1
  171. package/dist/test/test.products.d.ts +1 -0
  172. package/dist/test/test.products.js +49 -0
  173. package/dist/test/test.products.js.map +1 -0
  174. package/dist/types/appBuilder.types.d.ts +4 -11
  175. package/dist/types/enums.d.ts +3 -1
  176. package/dist/types/enums.js +2 -0
  177. package/dist/types/enums.js.map +1 -1
  178. package/dist/types/index.types.d.ts +4 -0
  179. package/dist/types/pricing.types.d.ts +4 -0
  180. package/dist/types/pricing.types.js +3 -0
  181. package/dist/types/pricing.types.js.map +1 -0
  182. package/dist/types/processor.types.d.ts +66 -9
  183. package/dist/types/processor.types.js.map +1 -1
  184. package/dist/types/productsBuilder.types.d.ts +52 -8
  185. package/dist/types/productsBuilder.types.js +9 -1
  186. package/dist/types/productsBuilder.types.js.map +1 -1
  187. package/dist/types/request-tracker.interface.d.ts +0 -0
  188. package/dist/types/request-tracker.interface.js +1 -0
  189. package/dist/types/request-tracker.interface.js.map +1 -0
  190. package/dist/utils/constants.d.ts +1 -0
  191. package/dist/utils/constants.js +5 -0
  192. package/dist/utils/constants.js.map +1 -0
  193. package/package.json +15 -3
@@ -22,13 +22,13 @@ const inputs_utils_create_1 = require("../../inputs/utils/inputs.utils.create");
22
22
  const enums_1 = require("../../types/enums");
23
23
  const productsBuilder_types_1 = require("../../types/productsBuilder.types");
24
24
  const validators_1 = require("../validators");
25
- const objects_utils_1 = require("../utils/objects.utils");
25
+ const objects_utils_1 = require("./utils/objects.utils");
26
26
  const processor_utils_1 = require("../../processor/utils/processor.utils");
27
27
  const create_productDatabaseAction_validator_1 = require("../validators/joi-validators/create.productDatabaseAction.validator");
28
28
  const update_productDatabaseAction_validator_1 = require("../validators/joi-validators/update.productDatabaseAction.validator");
29
29
  const create_productStorage_validator_1 = require("../validators/joi-validators/create.productStorage.validator");
30
30
  const inputs_types_1 = require("../../types/inputs.types");
31
- const string_utils_1 = require("../utils/string.utils");
31
+ const string_utils_1 = require("./utils/string.utils");
32
32
  const workspaceApi_service_1 = require("../../api/services/workspaceApi.service");
33
33
  const string_utils_2 = require("../../apps/utils/string.utils");
34
34
  const create_productDatabaseMigration_validator_1 = __importDefault(require("../validators/joi-validators/create.productDatabaseMigration.validator"));
@@ -38,11 +38,11 @@ const create_productMessageBrokerTopic_validator_1 = __importDefault(require("..
38
38
  const update_productMessageBrokerTopic_validator_1 = __importDefault(require("../validators/joi-validators/update.productMessageBrokerTopic.validator"));
39
39
  const update_productNotificationMessage_validator_1 = __importDefault(require("../validators/joi-validators/update.productNotificationMessage.validator"));
40
40
  const create_productNotification_validator_1 = require("../validators/joi-validators/create.productNotification.validator");
41
- const functions_utils_1 = require("../utils/functions.utils");
41
+ const functions_utils_1 = require("./utils/functions.utils");
42
42
  const objects_utils_2 = require("../../apps/utils/objects.utils");
43
43
  const webhooksApi_service_1 = require("../../api/services/webhooksApi.service");
44
44
  class ProductsBuilderService {
45
- constructor({ workspace_id, public_key, user_id, token, env_type, redis_client }) {
45
+ constructor({ workspace_id, public_key, user_id, token, env_type, redis_client, queues }) {
46
46
  this.workspace_id = workspace_id;
47
47
  this.public_key = public_key;
48
48
  this.user_id = user_id;
@@ -54,6 +54,150 @@ class ProductsBuilderService {
54
54
  this.appApi = new appApi_service_1.AppApiService(env_type, redis_client);
55
55
  this.inputsService = new inputs_service_1.default();
56
56
  this.thirdPartyApps = [];
57
+ if (redis_client) {
58
+ this.redisClient = redis_client;
59
+ // Start healthcheck workers automatically
60
+ //this.startHealthcheckWorkers();
61
+ }
62
+ if (queues) {
63
+ this.queues = queues;
64
+ }
65
+ }
66
+ fetchPrivateKey() {
67
+ return this.product.private_key;
68
+ }
69
+ fetchWorkspaceId() {
70
+ return this.product.workspace_id;
71
+ }
72
+ fetchProductId() {
73
+ return this.product._id;
74
+ }
75
+ async createHealthcheck(data) {
76
+ try {
77
+ await validators_1.CreateProductHealthcheckSchema.validateAsync(data);
78
+ if (!data.tag) {
79
+ throw new Error('tag field is required');
80
+ }
81
+ const exists = await this.fetchHealthcheck(data.app, data.tag);
82
+ if (!exists) {
83
+ const { app: access_tag, event: action } = data;
84
+ const app = await this.fetchThirdPartyAppByAccessTag(access_tag);
85
+ const version = app.versions.find((data) => data.tag === app.version);
86
+ if (!version) {
87
+ throw new Error(`Version ${app.version} not found`);
88
+ }
89
+ const event = version.actions.find((act) => act.tag === action);
90
+ data.envs = await Promise.all(data.envs.map(async (env) => {
91
+ const exists = await this.fetchEnv(env.slug);
92
+ if (!exists) {
93
+ throw new Error(`Env ${env.slug} does not exist`);
94
+ }
95
+ await this.validateActionDataInput({ input: env.input }, event, env.input, 0, 0);
96
+ env.input = (0, processor_utils_1.encrypt)(JSON.stringify(env.input), this.product.private_key);
97
+ console.log("INPUT", env);
98
+ return env;
99
+ }));
100
+ const envs = await this.fetchEnvs();
101
+ console.log("ENVS ===>>>>", envs);
102
+ console.log("DBENVS ====>>>>", data.envs);
103
+ envs.map((env) => {
104
+ const exists = data.envs.findIndex((dbEnv) => dbEnv.slug === env.slug);
105
+ if (exists === -1) {
106
+ throw new Error(`Product env ${env.slug} is not defined, please provide details`);
107
+ }
108
+ });
109
+ console.log("UPDATING VALUE", data);
110
+ await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({}, data), { action: enums_1.RequestAction.CREATE, component: enums_1.ProductComponents.HEALTHCHECK }), this.getUserAccess());
111
+ data.envs.map(async (env) => {
112
+ const payload = {
113
+ app: data.app,
114
+ event: data.event,
115
+ input: env.input,
116
+ env: env.slug,
117
+ product: this.product.tag,
118
+ retries: data.retries,
119
+ };
120
+ const jobId = `healthcheck-${data.tag}`;
121
+ const job = await this.queues.health.getJob(jobId);
122
+ if (job) {
123
+ await job.remove();
124
+ }
125
+ await this.queues.health.add(jobId, payload, { jobId, repeat: { every: data.interval } });
126
+ });
127
+ }
128
+ }
129
+ catch (e) {
130
+ console.log(e);
131
+ throw e;
132
+ }
133
+ }
134
+ async updateHealthcheck(tag, data) {
135
+ try {
136
+ // 1. Fetch the existing healthcheck
137
+ const healthcheck = await this.fetchHealthcheck(data.app, tag);
138
+ if (!healthcheck) {
139
+ throw new Error(`Healthcheck with tag: ${tag} not found`);
140
+ }
141
+ // 2. Validate the incoming data
142
+ await validators_1.CreateProductHealthcheckSchema.validateAsync(data); // No Update schema, use Create
143
+ // 3. Check for tag conflicts
144
+ if (data.tag && data.tag !== tag && await this.fetchHealthcheck(data.app, data.tag)) {
145
+ throw new Error(`Healthcheck with tag ${data.tag} already exists`);
146
+ }
147
+ // 4. Validate and process envs
148
+ if (data.envs) {
149
+ data.envs = await Promise.all(data.envs.map(async (env) => {
150
+ const exists = await this.fetchEnv(env.slug);
151
+ if (!exists) {
152
+ throw new Error(`Env ${env.slug} does not exist`);
153
+ }
154
+ // Validate input if present
155
+ if (env.input) {
156
+ const app = await this.fetchThirdPartyAppByAccessTag(data.app);
157
+ const version = app.versions.find((v) => v.tag === app.version);
158
+ if (!version) {
159
+ throw new Error(`Version ${app.version} not found`);
160
+ }
161
+ const event = version.actions.find((act) => act.tag === data.event);
162
+ await this.validateActionDataInput({ input: env.input }, event, env.input, 0, 0);
163
+ env.input = (0, processor_utils_1.encrypt)(JSON.stringify(env.input), this.product.private_key);
164
+ }
165
+ return env;
166
+ }));
167
+ }
168
+ // 5. Ensure all product envs are covered
169
+ const envs = await this.fetchEnvs();
170
+ envs.map((env) => {
171
+ var _a;
172
+ const exists = (_a = data.envs) === null || _a === void 0 ? void 0 : _a.findIndex((dbEnv) => dbEnv.slug === env.slug);
173
+ if (exists === -1) {
174
+ throw new Error(`Product env ${env.slug} is not defined, please provide details`);
175
+ }
176
+ });
177
+ // 6. Update the healthcheck
178
+ await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign(Object.assign({}, healthcheck), data), { tag, component: enums_1.ProductComponents.HEALTHCHECK, action: enums_1.RequestAction.UPDATE }), this.getUserAccess());
179
+ }
180
+ catch (e) {
181
+ throw e;
182
+ }
183
+ }
184
+ async fetchHealthcheck(access_tag, tag, throwError = false) {
185
+ const healthchecks = await this.productApi.fetchProductComponents(this.product_id, 'healthcheck', this.getUserAccess());
186
+ const health = healthchecks.find((data) => data.tag === tag && data.app === access_tag);
187
+ if (!health && throwError)
188
+ throw new Error(`Healthcheck ${tag} not found`);
189
+ return health || null;
190
+ }
191
+ async fetchHealthchecks(access_tag, throwError = false) {
192
+ const healthchecks = await this.productApi.fetchProductComponents(this.product_id, 'healthcheck', this.getUserAccess());
193
+ const health = healthchecks.filter((data) => data.app === access_tag);
194
+ if (!health && throwError)
195
+ throw new Error(`Access tag ${access_tag} not found`);
196
+ return health;
197
+ }
198
+ async fetchProductHealthchecks() {
199
+ const healthchecks = await this.productApi.fetchProductComponents(this.product_id, 'healthcheck', this.getUserAccess());
200
+ return healthchecks;
57
201
  }
58
202
  async updateDataValidation(selector, update) {
59
203
  if (!selector.startsWith('$Data{') && !selector.startsWith('$Filter')) {
@@ -68,26 +212,27 @@ class ProductsBuilderService {
68
212
  if (stages.length < 3) {
69
213
  throw new Error(`Invalid selector ${selector}`);
70
214
  }
71
- const tag = stages[0];
72
- const type = stages[1];
215
+ const tag = String(stages[0]);
216
+ const type = String(stages[1]);
73
217
  let size = 2;
74
218
  let data;
75
219
  switch (type) {
76
220
  case productsBuilder_types_1.FeatureEventTypes.DB_ACTION:
77
- const action = this.fetchDatabaseAction(tag);
221
+ const action = await this.fetchDatabaseAction(tag);
78
222
  if (!action)
79
223
  throw new Error(`DB Action ${tag} not found`);
80
224
  data = useData ? action.data : action.filterData;
81
225
  break;
82
226
  case productsBuilder_types_1.FeatureEventTypes.NOTIFICATION:
83
227
  size = 3;
84
- if (!stages[3] || (stages[3] !== 'push' && stages[3] !== 'callback' && stages[3] !== 'email')) {
85
- throw new Error(`Invalid value ${stages[3]} in ${selector}, expected to be "push", "callback" or "email" in notification`);
228
+ const stage3Str = String(stages[3]);
229
+ if (!stages[3] || (stage3Str !== 'push' && stage3Str !== 'callback' && stage3Str !== 'email')) {
230
+ throw new Error(`Invalid value ${stage3Str} in ${selector}, expected to be "push", "callback" or "email" in notification`);
86
231
  }
87
- const notification = this.fetchNotificationMessage(tag);
232
+ const notification = await this.fetchNotificationMessage(tag);
88
233
  if (!notification)
89
234
  throw new Error(`Notification ${tag} not found`);
90
- if (stages[3] === 'push') {
235
+ if (stage3Str === 'push') {
91
236
  data = notification.push_notification_data;
92
237
  }
93
238
  if (stages[3] === 'callback') {
@@ -129,11 +274,11 @@ class ProductsBuilderService {
129
274
  await validators_1.CreateProductBuilderSchema.validateAsync(data);
130
275
  const exists = await this.checkIfProductExists(data.name);
131
276
  if (exists && (exists === null || exists === void 0 ? void 0 : exists._id)) {
132
- await this.initializeProduct(exists === null || exists === void 0 ? void 0 : exists._id);
277
+ // await this.initializeProduct(exists?._id);
133
278
  }
134
279
  else {
135
280
  const product = await this.createNewProduct(data);
136
- await this.initializeProduct(product._id);
281
+ // await this.initializeProduct(product._id);
137
282
  }
138
283
  }
139
284
  catch (e) {
@@ -147,7 +292,7 @@ class ProductsBuilderService {
147
292
  }
148
293
  async initializeProduct(product_id) {
149
294
  try {
150
- this.product = await this.productApi.fetchProduct(product_id, this.getUserAccess());
295
+ this.product = await this.productApi.initProduct(product_id, this.getUserAccess());
151
296
  this.product_id = product_id;
152
297
  }
153
298
  catch (e) {
@@ -156,7 +301,10 @@ class ProductsBuilderService {
156
301
  }
157
302
  async initializeProductByTag(tag) {
158
303
  try {
159
- this.product = await this.productApi.fetchProductByTag(tag, this.getUserAccess());
304
+ this.product = await this.productApi.initProduct(tag, this.getUserAccess());
305
+ if (!this.product) {
306
+ throw new Error(`Product with tag "${tag}" not found or failed to fetch`);
307
+ }
160
308
  this.product_id = this.product._id;
161
309
  }
162
310
  catch (e) {
@@ -169,7 +317,6 @@ class ProductsBuilderService {
169
317
  throw new Error('Product not initialized');
170
318
  await validators_1.CreateProductBuilderSchema.validateAsync(data);
171
319
  await this.productApi.updateProduct(this.product_id, Object.assign({}, data), this.getUserAccess());
172
- await this.initializeProduct(this.product_id);
173
320
  }
174
321
  catch (e) {
175
322
  throw e;
@@ -186,24 +333,49 @@ class ProductsBuilderService {
186
333
  return false;
187
334
  }
188
335
  }
189
- fetchProduct() {
336
+ async fetchProduct() {
190
337
  if (!this.product) {
191
338
  throw new Error('Product not yet initiated');
192
339
  }
193
- return this.product;
340
+ return await this.productApi.fetchProductByTag(this.product.tag, this.getUserAccess());
194
341
  }
195
- async createSession(data) {
342
+ async createSession(data, throwErrorIfExists = false) {
196
343
  try {
197
- // TODO: figure out a way to check if this has run before, halt if it has
198
- if (!this.fetchSession(data.tag)) {
199
- await validators_1.CreateProductSessionSchema.validateAsync(data);
200
- data.schema_data = (await this.inputsService.parseJson({
344
+ await validators_1.CreateProductSessionSchema.validateAsync(data);
345
+ if (!data.tag) {
346
+ throw new Error('tag field is required');
347
+ }
348
+ const exists = await this.fetchSession(data.tag);
349
+ if (!exists) {
350
+ if (!data.selector.startsWith('$Session{')) {
351
+ throw new Error('Selector should be in the format $Session{...}{key}');
352
+ }
353
+ const stages = (0, string_utils_1.extractStages)(data.selector);
354
+ let current = data.schema;
355
+ for (const stage of stages) {
356
+ if (current && typeof current === 'object' && stage in current) {
357
+ current = current[stage];
358
+ }
359
+ else {
360
+ throw new Error(`${data.selector} not found in event sample`);
361
+ }
362
+ }
363
+ if (current === null ||
364
+ typeof current === "undefined" ||
365
+ typeof current === "object") {
366
+ throw new Error("Selector value is not allowed to be an object|array|null|undefined");
367
+ }
368
+ data.schema_data = await this.inputsService.parseJson({
201
369
  data: data.schema,
202
- expected: inputs_types_1.ExpectedValues.PARSESAMPLE,
203
- category: enums_1.Categories.DATA,
204
- }));
205
- await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({}, data), { component: enums_1.ProductComponents.SESSION, action: enums_1.RequestAction.CREATE }), this.getUserAccess());
206
- await this.initializeProduct(this.product_id);
370
+ expected: inputs_types_1.ExpectedValues.PARSESAMPLE
371
+ });
372
+ data.selectorValue = current;
373
+ const payload = Object.assign(Object.assign({}, data), { component: enums_1.ProductComponents.SESSION, action: enums_1.RequestAction.CREATE });
374
+ await this.productApi.updateProduct(this.product_id, payload, this.getUserAccess());
375
+ }
376
+ else {
377
+ if (throwErrorIfExists)
378
+ throw new Error(`Session ${data.tag} already exists`);
207
379
  }
208
380
  }
209
381
  catch (e) {
@@ -212,13 +384,13 @@ class ProductsBuilderService {
212
384
  }
213
385
  async updateSession(tag, data) {
214
386
  try {
215
- const auth = this.fetchSession(tag);
216
- if (!auth) {
217
- throw new Error(`Session ${tag} not found`);
387
+ const session = await this.fetchSession(tag);
388
+ if (!session) {
389
+ throw new Error(`Session with tag: ${tag} not found`);
218
390
  }
219
391
  //const { _id } = auth;
220
392
  await validators_1.UpdateProductSessionSchema.validateAsync(data); // Change to update;
221
- if (data.tag && this.fetchEnv(data.tag) && data.tag !== tag) {
393
+ if (data.tag && this.fetchSession(data.tag) && data.tag !== tag) {
222
394
  throw new Error(`tag ${data.tag} is in use`); // TODO: also check on the backend
223
395
  }
224
396
  if (!data.tag) {
@@ -230,20 +402,40 @@ class ProductsBuilderService {
230
402
  expected: inputs_types_1.ExpectedValues.PARSESAMPLE,
231
403
  category: enums_1.Categories.DATA,
232
404
  }));
405
+ if (!data.selector) {
406
+ throw new Error('Selector is expected when updating schema');
407
+ }
233
408
  }
234
- await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({}, Object.assign(Object.assign({}, auth), data)), { component: enums_1.ProductComponents.SESSION, action: enums_1.RequestAction.UPDATE }), this.getUserAccess());
235
- await this.initializeProduct(this.product_id);
409
+ if (data.selector) {
410
+ const stages = (0, string_utils_1.extractStages)(data.selector);
411
+ let current = data.schema;
412
+ for (const stage of stages) {
413
+ if (current && typeof current === 'object' && stage in current) {
414
+ current = current[stage];
415
+ }
416
+ else {
417
+ throw new Error(`${data.selector} not found in event sample`);
418
+ }
419
+ }
420
+ if (current === null ||
421
+ typeof current === "undefined" ||
422
+ typeof current === "object") {
423
+ throw new Error("Selector value is not allowed to be an object|array|null|undefined");
424
+ }
425
+ }
426
+ await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({}, Object.assign(Object.assign({}, session), data)), { component: enums_1.ProductComponents.SESSION, action: enums_1.RequestAction.UPDATE }), this.getUserAccess());
236
427
  }
237
428
  catch (e) {
238
429
  throw e;
239
430
  }
240
431
  }
241
- fetchSession(tag) {
242
- var _a;
243
- return (_a = this.product.sessions) === null || _a === void 0 ? void 0 : _a.find((auth) => auth.tag === tag);
432
+ async fetchSessions() {
433
+ const components = await this.productApi.fetchProductComponents(this.product_id, 'session', this.getUserAccess());
434
+ return components;
244
435
  }
245
- fetchSessions() {
246
- return this.product.sessions;
436
+ async fetchSession(tag) {
437
+ const component = await this.productApi.fetchProductComponentByTag(this.product_id, 'session', tag, this.getUserAccess());
438
+ return component;
247
439
  }
248
440
  async createMessageBrokerTopic(data, throwErrorIfExists = false) {
249
441
  try {
@@ -257,16 +449,17 @@ class ProductsBuilderService {
257
449
  if (data.sample)
258
450
  data.sample = JSON.stringify(data.sample);
259
451
  await create_productMessageBrokerTopic_validator_1.default.validateAsync(data);
260
- const exists = this.fetchMessageBrokerTopic(data.tag);
261
- const broker = this.fetchMessageBroker(messageBrokerTag);
262
- (0, functions_utils_1.validateAWSSQSQueueUrl)(broker.envs, data.queueUrls);
452
+ const exists = await this.fetchMessageBrokerTopic(data.tag);
453
+ const broker = await this.fetchMessageBroker(messageBrokerTag);
454
+ if (broker) {
455
+ (0, functions_utils_1.validateAWSSQSQueueUrl)(broker.envs, data.queueUrls);
456
+ }
263
457
  data.tag = tag;
264
458
  if (!exists) {
265
459
  if (data.sample) {
266
460
  data.data = (await (0, string_utils_1.extractPlaceholders)(data.sample));
267
461
  }
268
462
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({}, data), { messageBrokerTag, component: enums_1.ProductComponents.MESSAGEBROKER_TOPIC, action: enums_1.RequestAction.CREATE }), this.getUserAccess());
269
- await this.initializeProduct(this.product_id);
270
463
  }
271
464
  else {
272
465
  if (throwErrorIfExists)
@@ -289,12 +482,12 @@ class ProductsBuilderService {
289
482
  if (!messageBrokerTag || !tag) {
290
483
  throw new Error(`tag is expected to be defined as "messageBrokr_tag:topic_tag"`);
291
484
  }
292
- const exists = this.fetchMessageBrokerTopic(data.tag);
485
+ const exists = await this.fetchMessageBrokerTopic(data.tag);
293
486
  if (!exists) {
294
487
  throw new Error(`Topic ${data.tag} not found`);
295
488
  }
296
- const broker = this.fetchMessageBroker(messageBrokerTag);
297
- if (data.queueUrls) {
489
+ const broker = await this.fetchMessageBroker(messageBrokerTag);
490
+ if (data.queueUrls && broker) {
298
491
  (0, functions_utils_1.validateAWSSQSQueueUrl)(broker.envs, data.queueUrls);
299
492
  }
300
493
  data.tag = tag;
@@ -303,7 +496,6 @@ class ProductsBuilderService {
303
496
  data.data = (await (0, string_utils_1.extractPlaceholders)(data.sample));
304
497
  }
305
498
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({}, data), { messageBrokerTag, component: enums_1.ProductComponents.MESSAGEBROKER_TOPIC, action: enums_1.RequestAction.UPDATE }), this.getUserAccess());
306
- await this.initializeProduct(this.product_id);
307
499
  }
308
500
  else {
309
501
  throw new Error(`Message Broker Topic ${data.tag} not found`);
@@ -313,12 +505,12 @@ class ProductsBuilderService {
313
505
  throw e;
314
506
  }
315
507
  }
316
- fetchMessageBrokerTopic(tag, throwErrorIfExists = false) {
508
+ async fetchMessageBrokerTopic(tag, throwErrorIfExists = false) {
317
509
  const [messageBrokerTag, topicTag] = tag.split(':');
318
510
  if (!messageBrokerTag || !topicTag) {
319
511
  throw new Error(`tag is expected to be defined as "messageBroker_tag:topic_tag"`);
320
512
  }
321
- const messageBroker = this.product.messageBrokers.find((data) => data.tag === messageBrokerTag);
513
+ const messageBroker = await this.fetchMessageBroker(messageBrokerTag);
322
514
  if (!messageBroker)
323
515
  throw new Error(`MessageBroker ${messageBrokerTag} not found`);
324
516
  const topic = messageBroker.topics.find((data) => data.tag === topicTag);
@@ -326,8 +518,8 @@ class ProductsBuilderService {
326
518
  throw new Error(`MessageBroker topic ${tag} not found`);
327
519
  return topic;
328
520
  }
329
- fetchMessageBrokerTopics(messageBrokerTag) {
330
- const messageBroker = this.product.messageBrokers.find((data) => data.tag === messageBrokerTag);
521
+ async fetchMessageBrokerTopics(messageBrokerTag) {
522
+ const messageBroker = await this.fetchMessageBroker(messageBrokerTag);
331
523
  if (!messageBroker)
332
524
  throw new Error(`Message Broker ${messageBrokerTag} not found`);
333
525
  return messageBroker.topics;
@@ -364,7 +556,7 @@ class ProductsBuilderService {
364
556
  this.checkActionQuotaFallbackInput(action.body, option.input.body, input, 'body');
365
557
  }
366
558
  else {
367
- const feature = this.fetchFeature(option.event);
559
+ const feature = await this.fetchFeature(option.event);
368
560
  if (!feature) {
369
561
  throw new Error(`Feature ${option.event} not found`);
370
562
  }
@@ -454,7 +646,7 @@ class ProductsBuilderService {
454
646
  }
455
647
  else {
456
648
  // fetch feature
457
- const feature = this.fetchFeature(option.event);
649
+ const feature = await this.fetchFeature(option.event);
458
650
  if (!feature) {
459
651
  throw new Error(`Feature ${option.event} not found`);
460
652
  }
@@ -522,7 +714,7 @@ class ProductsBuilderService {
522
714
  if (!data.tag) {
523
715
  throw new Error('tag field is required');
524
716
  }
525
- if (!this.fetchQuota(data.tag)) {
717
+ if (!(await this.fetchQuota(data.tag))) {
526
718
  await validators_1.CreateProductQuotaSchema.validateAsync(data);
527
719
  await this.validateQuotaFallbackInputAndOutput(data, 'quota');
528
720
  data.total_quota = 0;
@@ -565,7 +757,7 @@ class ProductsBuilderService {
565
757
  }
566
758
  async updateQuota(tag, data) {
567
759
  try {
568
- const quota = this.fetchQuota(tag);
760
+ const quota = await this.fetchQuota(tag);
569
761
  if (quota) {
570
762
  await validators_1.UpdateProductQuotaSchema.validateAsync(data);
571
763
  if (data.options) {
@@ -613,18 +805,20 @@ class ProductsBuilderService {
613
805
  throw e;
614
806
  }
615
807
  }
616
- fetchQuota(tag) {
617
- return this.fetchQuotas().find((data) => data.tag === tag);
808
+ async fetchQuotas() {
809
+ const components = await this.productApi.fetchProductComponents(this.product_id, 'quota', this.getUserAccess());
810
+ return components;
618
811
  }
619
- fetchQuotas() {
620
- return this.product.quota;
812
+ async fetchQuota(tag) {
813
+ const component = await this.productApi.fetchProductComponentByTag(this.product_id, 'quota', tag, this.getUserAccess());
814
+ return component;
621
815
  }
622
816
  async createFallback(data) {
623
817
  try {
624
818
  if (!data.tag) {
625
819
  throw new Error('tag field is required');
626
820
  }
627
- if (!this.fetchFallback(data.tag)) {
821
+ if (!(await this.fetchFallback(data.tag))) {
628
822
  await validators_1.CreateProductFallbackSchema.validateAsync(data);
629
823
  await this.validateQuotaFallbackInputAndOutput(data, 'fallback');
630
824
  await Promise.all(data.options.map(async (d) => {
@@ -664,7 +858,7 @@ class ProductsBuilderService {
664
858
  }
665
859
  async updateFallback(tag, data) {
666
860
  try {
667
- const fallback = this.fetchFallback(tag);
861
+ const fallback = await this.fetchFallback(tag);
668
862
  if (fallback) {
669
863
  await validators_1.UpdateProductFallbackSchema.validateAsync(data);
670
864
  if (data.options) {
@@ -709,19 +903,20 @@ class ProductsBuilderService {
709
903
  throw e;
710
904
  }
711
905
  }
712
- fetchFallback(tag) {
713
- return this.fetchFallbacks().find((data) => data.tag === tag);
906
+ async fetchFallbacks() {
907
+ const components = await this.productApi.fetchProductComponents(this.product_id, 'fallback', this.getUserAccess());
908
+ return components;
714
909
  }
715
- fetchFallbacks() {
716
- return this.product.fallback;
910
+ async fetchFallback(tag) {
911
+ const component = await this.productApi.fetchProductComponentByTag(this.product_id, 'fallback', tag, this.getUserAccess());
912
+ return component;
717
913
  }
718
914
  async createEnv(data, throwErrorIfExists = false) {
719
915
  try {
720
916
  // TODO: figure out a way to check if this has run before, halt if it has
721
- if (!this.fetchEnv(data.slug)) {
917
+ if (!(await this.fetchEnv(data.slug))) {
722
918
  await validators_1.CreateProductEnvSchema.validateAsync(data);
723
919
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({}, data), { component: enums_1.ProductComponents.ENV, action: enums_1.RequestAction.CREATE }), this.getUserAccess());
724
- await this.initializeProduct(this.product_id);
725
920
  }
726
921
  else {
727
922
  if (throwErrorIfExists)
@@ -734,54 +929,53 @@ class ProductsBuilderService {
734
929
  }
735
930
  async updateEnv(slug, data) {
736
931
  try {
737
- const env = this.fetchEnv(slug, true);
932
+ const env = await this.fetchEnv(slug);
738
933
  if (!env) {
739
934
  throw new Error(`Env ${slug} not found`);
740
935
  }
741
936
  const { _id } = env;
742
937
  await validators_1.UpdateProductEnvSchema.validateAsync(data); // Change to update;
743
- if (data.slug && this.fetchEnv(data.slug)) {
938
+ if (data.slug && (await this.fetchEnv(data.slug))) {
744
939
  throw new Error(`slug ${slug} is in use`); // TODO: also check on the backend
745
940
  }
746
941
  if (!data.slug) {
747
942
  data.slug = slug;
748
943
  }
749
944
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({}, Object.assign(Object.assign({}, env), data)), { component: enums_1.ProductComponents.ENV, action: enums_1.RequestAction.UPDATE }), this.getUserAccess());
750
- await this.initializeProduct(this.product_id);
751
945
  }
752
946
  catch (e) {
753
947
  throw e;
754
948
  }
755
949
  }
756
- fetchEnvs() {
757
- return this.product.envs;
950
+ async fetchEnvs() {
951
+ const components = await this.productApi.fetchProductComponents(this.product_id, 'env', this.getUserAccess());
952
+ return components;
758
953
  }
759
- fetchEnv(slug, throwErrorIfExists = false) {
760
- const env = this.product.envs.find((data) => data.slug === slug);
761
- if (!env && throwErrorIfExists)
762
- throw new Error(`Env ${slug} not found`);
763
- return env;
954
+ async fetchEnv(slug) {
955
+ const component = await this.productApi.fetchProductComponentByTag(this.product_id, 'env', slug, this.getUserAccess());
956
+ return component;
764
957
  }
765
958
  async createMessageBroker(data, throwErrorIfExists = false) {
766
- if (!this.fetchMessageBroker(data.tag, false)) {
959
+ if (!(await this.fetchMessageBroker(data.tag))) {
767
960
  await validators_1.CreateMessageBrokerSchema.validateAsync(data);
768
- data.envs = data.envs.map((env) => {
769
- const exists = this.fetchEnv(env.slug);
961
+ const processedEnvs = [];
962
+ for (const env of data.envs) {
963
+ const exists = await this.fetchEnv(env.slug);
770
964
  if (!exists) {
771
965
  throw new Error(`Env ${env.slug} does not exist`);
772
966
  }
773
- env.config = (0, processor_utils_1.encrypt)(JSON.stringify(env.config), this.fetchProduct().private_key);
774
- return env;
775
- });
776
- const envs = this.fetchEnvs();
777
- envs.map((env) => {
967
+ env.config = (0, processor_utils_1.encrypt)(JSON.stringify(env.config), this.product.private_key);
968
+ processedEnvs.push(env);
969
+ }
970
+ data.envs = processedEnvs;
971
+ const envs = await this.fetchEnvs();
972
+ for (const env of envs) {
778
973
  const exists = data.envs.findIndex((dbEnv) => dbEnv.slug === env.slug);
779
974
  if (exists === -1) {
780
975
  throw new Error(`Product env ${env.slug} is not defined, please provide connection details`);
781
976
  }
782
- });
977
+ }
783
978
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({}, data), { component: enums_1.ProductComponents.MESSAGEBROKER, action: enums_1.RequestAction.CREATE }), this.getUserAccess());
784
- await this.initializeProduct(this.product_id);
785
979
  }
786
980
  else {
787
981
  if (throwErrorIfExists)
@@ -790,30 +984,32 @@ class ProductsBuilderService {
790
984
  }
791
985
  async updateMessageBroker(tag, data) {
792
986
  try {
793
- const messageBroker = this.fetchMessageBroker(tag);
987
+ const messageBroker = await this.fetchMessageBroker(tag);
794
988
  if (!messageBroker) {
795
989
  throw new Error(`Broker ${tag} not found`);
796
990
  }
797
991
  const { _id, envs } = messageBroker;
798
992
  await validators_1.UpdateMessageBrokerSchema.validateAsync(data); // Change to update;
799
- if (data.tag && this.fetchMessageBroker(data.tag)) {
993
+ if (data.tag && (await this.fetchMessageBroker(data.tag))) {
800
994
  throw new Error(`tag ${tag} is in use`); // TODO: also check on the backend
801
995
  }
802
996
  console.log('2', data.envs);
803
- data.envs = data.envs.map((env) => {
804
- const exists = this.fetchEnv(env.slug);
997
+ const processedEnvs = [];
998
+ for (const env of data.envs) {
999
+ const exists = await this.fetchEnv(env.slug);
805
1000
  if (!exists) {
806
1001
  throw new Error(`Env ${env.slug} does not exist`);
807
1002
  }
808
1003
  if (env.config)
809
- env.config = (0, processor_utils_1.encrypt)(JSON.stringify(env.config), this.fetchProduct().private_key);
810
- return env;
811
- });
1004
+ env.config = (0, processor_utils_1.encrypt)(JSON.stringify(env.config), this.product.private_key);
1005
+ processedEnvs.push(env);
1006
+ }
1007
+ data.envs = processedEnvs;
812
1008
  const overwrite = [];
813
1009
  const newEnvs = [];
814
- data.envs.map((dataEnv) => {
1010
+ for (const dataEnv of data.envs) {
815
1011
  const exists = envs.findIndex((env) => env.slug === dataEnv.slug);
816
- if (!this.fetchEnv(dataEnv.slug)) {
1012
+ if (!(await this.fetchEnv(dataEnv.slug))) {
817
1013
  throw new Error(`Product Environment ${dataEnv.slug} doesn't exist`);
818
1014
  }
819
1015
  if (exists === -1) {
@@ -824,48 +1020,51 @@ class ProductsBuilderService {
824
1020
  }
825
1021
  else {
826
1022
  if (envs[exists].config) {
827
- envs[exists].config = (0, processor_utils_1.encrypt)(JSON.stringify(envs[exists].config), this.fetchProduct().private_key);
1023
+ envs[exists].config = (0, processor_utils_1.encrypt)(JSON.stringify(envs[exists].config), this.product.private_key);
828
1024
  }
829
1025
  overwrite.push(Object.assign(Object.assign({}, envs[exists]), dataEnv));
830
1026
  }
831
- });
1027
+ }
832
1028
  const unchanged = [];
833
- envs.map((env) => {
1029
+ for (const env of envs) {
834
1030
  const newEnv = newEnvs.findIndex((dataEnv) => env.slug === dataEnv.slug) > -1;
835
1031
  const overwriteEnv = overwrite.findIndex((dataEnv) => env.slug === dataEnv.slug) > -1;
836
1032
  if (!newEnv && !overwriteEnv) {
837
1033
  if (env.config) {
838
- env.config = (0, processor_utils_1.encrypt)(JSON.stringify(env.config), this.fetchProduct().private_key);
1034
+ env.config = (0, processor_utils_1.encrypt)(JSON.stringify(env.config), this.product.private_key);
839
1035
  }
840
1036
  unchanged.push(env);
841
1037
  }
842
- });
1038
+ }
843
1039
  data.envs = [...unchanged, ...overwrite, ...newEnvs];
844
1040
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign(Object.assign({ _id }, messageBroker), data), { component: enums_1.ProductComponents.MESSAGEBROKER, action: enums_1.RequestAction.UPDATE }), this.getUserAccess());
845
- await this.initializeProduct(this.product_id);
846
1041
  }
847
1042
  catch (e) {
848
1043
  throw e;
849
1044
  }
850
1045
  }
851
- fetchMessageBroker(tag, throwErrorIfExists = false) {
852
- const messageBroker = this.product.messageBrokers.find((data) => data.tag === tag);
853
- if (!messageBroker && throwErrorIfExists)
854
- throw new Error(`Message Broker ${tag} not found`);
1046
+ async fetchMessageBrokers() {
1047
+ const components = await this.productApi.fetchProductComponents(this.product_id, 'message_broker', this.getUserAccess());
1048
+ components.map((broker) => {
1049
+ return broker.envs.map((env) => {
1050
+ if (typeof env.config === 'string') {
1051
+ env.config = JSON.parse((0, processor_utils_1.decrypt)(String(env.config), this.product.private_key));
1052
+ }
1053
+ });
1054
+ });
1055
+ return components;
1056
+ }
1057
+ async fetchMessageBroker(tag) {
1058
+ const messageBroker = await this.productApi.fetchProductComponentByTag(this.product_id, 'message_broker', tag, this.getUserAccess());
855
1059
  if (messageBroker) {
856
- messageBroker.envs.map((broker) => {
857
- if (typeof broker.config === 'string') {
858
- broker.config = JSON.parse((0, processor_utils_1.decrypt)(String(broker.config), this.fetchProduct().private_key));
1060
+ messageBroker.envs.map((env) => {
1061
+ if (typeof env.config === 'string') {
1062
+ env.config = JSON.parse((0, processor_utils_1.decrypt)(String(env.config), this.product.private_key));
859
1063
  }
860
1064
  });
861
1065
  }
862
1066
  return messageBroker;
863
1067
  }
864
- fetchMessageBrokers() {
865
- return this.product.messageBrokers.map((broker) => {
866
- return this.fetchMessageBroker(broker.tag);
867
- });
868
- }
869
1068
  async fetchStorageFiles(filter) {
870
1069
  try {
871
1070
  const result = await this.productApi.fetchProductStorageFiles(Object.assign(Object.assign({}, filter), { product: this.product.tag }), this.getUserAccess());
@@ -883,11 +1082,28 @@ class ProductsBuilderService {
883
1082
  throw (e);
884
1083
  }
885
1084
  }
1085
+ async fetchSessionUsers(filter) {
1086
+ try {
1087
+ const result = await this.productApi.fetchProductSessionUsers(Object.assign(Object.assign({}, filter), { product: this.product.tag }), this.getUserAccess());
1088
+ if (!result)
1089
+ return [];
1090
+ return result.map((res) => {
1091
+ delete res._id;
1092
+ delete res.workspace_id;
1093
+ delete res.__v;
1094
+ return res;
1095
+ });
1096
+ }
1097
+ catch (e) {
1098
+ throw (e);
1099
+ }
1100
+ }
886
1101
  async createStorage(data, throwErrorIfExists = false) {
887
- if (!this.fetchStorage(data.tag, false)) {
1102
+ if (!(await this.fetchStorage(data.tag))) {
888
1103
  await create_productStorage_validator_1.CreateProductStorageSchema.validateAsync(data);
889
- data.envs = data.envs.map((env) => {
890
- const exists = this.fetchEnv(env.slug);
1104
+ const processedEnvs = [];
1105
+ for (const env of data.envs) {
1106
+ const exists = await this.fetchEnv(env.slug);
891
1107
  if (!exists) {
892
1108
  throw new Error(`Env ${env.slug} does not exist`);
893
1109
  }
@@ -895,18 +1111,18 @@ class ProductsBuilderService {
895
1111
  // @ts-ignore
896
1112
  //env.config.config.private_key = gcpPKCSConvert(env.config.config.private_key)
897
1113
  }
898
- env.config = (0, processor_utils_1.encrypt)(JSON.stringify(env.config), this.fetchProduct().private_key);
899
- return env;
900
- });
901
- const envs = this.fetchEnvs();
902
- envs.map((env) => {
1114
+ env.config = (0, processor_utils_1.encrypt)(JSON.stringify(env.config), this.product.private_key);
1115
+ processedEnvs.push(env);
1116
+ }
1117
+ data.envs = processedEnvs;
1118
+ const envs = await this.fetchEnvs();
1119
+ for (const env of envs) {
903
1120
  const exists = data.envs.findIndex((dbEnv) => dbEnv.slug === env.slug);
904
1121
  if (exists === -1) {
905
1122
  throw new Error(`Product env ${env.slug} is not defined, please provide connection details`);
906
1123
  }
907
- });
1124
+ }
908
1125
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({}, data), { component: enums_1.ProductComponents.STORAGE, action: enums_1.RequestAction.CREATE }), this.getUserAccess());
909
- await this.initializeProduct(this.product_id);
910
1126
  }
911
1127
  else {
912
1128
  if (throwErrorIfExists)
@@ -915,7 +1131,7 @@ class ProductsBuilderService {
915
1131
  }
916
1132
  async updateStorage(tag, data) {
917
1133
  try {
918
- const storage = this.fetchStorage(tag);
1134
+ const storage = await this.fetchStorage(tag);
919
1135
  if (!storage) {
920
1136
  throw new Error(`Storage ${tag} not found`);
921
1137
  }
@@ -924,8 +1140,8 @@ class ProductsBuilderService {
924
1140
  if (data.tag && this.fetchStorage(data.tag)) {
925
1141
  throw new Error(`tag ${tag} is in use`); // TODO: also check on the backend
926
1142
  }
927
- data.envs = data.envs.map((env) => {
928
- const exists = this.fetchEnv(env.slug);
1143
+ data.envs = await Promise.all(data.envs.map(async (env) => {
1144
+ const exists = await this.fetchEnv(env.slug);
929
1145
  if (!exists) {
930
1146
  throw new Error(`Env ${env.slug} does not exist`);
931
1147
  }
@@ -934,9 +1150,9 @@ class ProductsBuilderService {
934
1150
  //env.config.config.private_key = gcpPKCSConvert(env.config.config.private_key)
935
1151
  }
936
1152
  if (env.config)
937
- env.config = (0, processor_utils_1.encrypt)(JSON.stringify(env.config), this.fetchProduct().private_key);
1153
+ env.config = (0, processor_utils_1.encrypt)(JSON.stringify(env.config), this.product.private_key);
938
1154
  return env;
939
- });
1155
+ }));
940
1156
  const overwrite = [];
941
1157
  const newEnvs = [];
942
1158
  data.envs.map((dataEnv) => {
@@ -952,7 +1168,7 @@ class ProductsBuilderService {
952
1168
  }
953
1169
  else {
954
1170
  if (envs[exists].config) {
955
- envs[exists].config = (0, processor_utils_1.encrypt)(JSON.stringify(envs[exists].config), this.fetchProduct().private_key);
1171
+ envs[exists].config = (0, processor_utils_1.encrypt)(JSON.stringify(envs[exists].config), this.product.private_key);
956
1172
  }
957
1173
  overwrite.push(Object.assign(Object.assign({}, envs[exists]), dataEnv));
958
1174
  }
@@ -963,37 +1179,40 @@ class ProductsBuilderService {
963
1179
  const overwriteEnv = overwrite.findIndex((dataEnv) => env.slug === dataEnv.slug) > -1;
964
1180
  if (!newEnv && !overwriteEnv) {
965
1181
  if (env.config) {
966
- env.config = (0, processor_utils_1.encrypt)(JSON.stringify(env.config), this.fetchProduct().private_key);
1182
+ env.config = (0, processor_utils_1.encrypt)(JSON.stringify(env.config), this.product.private_key);
967
1183
  }
968
1184
  unchanged.push(env);
969
1185
  }
970
1186
  });
971
1187
  data.envs = [...unchanged, ...overwrite, ...newEnvs];
972
1188
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign(Object.assign({ _id }, storage), data), { component: enums_1.ProductComponents.STORAGE, action: enums_1.RequestAction.UPDATE }), this.getUserAccess());
973
- await this.initializeProduct(this.product_id);
974
1189
  }
975
1190
  catch (e) {
976
1191
  throw e;
977
1192
  }
978
1193
  }
979
- fetchStorage(tag, throwErrorIfExists = false) {
980
- const storage = this.product.storage.find((data) => data.tag === tag);
981
- if (!storage && throwErrorIfExists)
982
- throw new Error(`Storage ${tag} not found`);
1194
+ async fetchStorages() {
1195
+ const components = await this.productApi.fetchProductComponents(this.product_id, 'storage', this.getUserAccess());
1196
+ components.map((storage) => {
1197
+ storage.envs.map((env) => {
1198
+ if (typeof env.config === 'string') {
1199
+ env.config = JSON.parse((0, processor_utils_1.decrypt)(String(env.config), this.product.private_key));
1200
+ }
1201
+ });
1202
+ });
1203
+ return components;
1204
+ }
1205
+ async fetchStorage(tag) {
1206
+ const storage = await this.productApi.fetchProductComponentByTag(this.product_id, 'storage', tag, this.getUserAccess());
983
1207
  if (storage) {
984
1208
  storage.envs.map((store) => {
985
1209
  if (typeof store.config === 'string') {
986
- store.config = JSON.parse((0, processor_utils_1.decrypt)(String(store.config), this.fetchProduct().private_key));
1210
+ store.config = JSON.parse((0, processor_utils_1.decrypt)(String(store.config), this.product.private_key));
987
1211
  }
988
1212
  });
989
1213
  }
990
1214
  return storage;
991
1215
  }
992
- fetchStorages() {
993
- return this.product.storage.map((storage) => {
994
- return this.fetchStorage(storage.tag);
995
- });
996
- }
997
1216
  async validateAppData(appData, app) {
998
1217
  // TODO:
999
1218
  const { envs } = app;
@@ -1015,6 +1234,9 @@ class ProductsBuilderService {
1015
1234
  }
1016
1235
  }
1017
1236
  validateVariablesValues(variables, appVariables) {
1237
+ if (!variables) {
1238
+ variables = [];
1239
+ }
1018
1240
  if (appVariables && appVariables.length) {
1019
1241
  appVariables.map((data) => {
1020
1242
  const find = variables.find((value) => value.key === data.key);
@@ -1031,7 +1253,7 @@ class ProductsBuilderService {
1031
1253
  const { envs: appEnvs } = version;
1032
1254
  return envs.map((env) => {
1033
1255
  const { auth } = env;
1034
- this.fetchEnv(env.product_env_slug, true);
1256
+ this.fetchEnv(env.product_env_slug);
1035
1257
  const appEnv = appEnvs.find((appEnv) => appEnv.slug === env.app_env_slug);
1036
1258
  if (!appEnv) {
1037
1259
  throw new Error(`app_slug ${env.app_env_slug} not found`);
@@ -1121,10 +1343,10 @@ class ProductsBuilderService {
1121
1343
  });
1122
1344
  (0, inputs_utils_create_1.validateInputSchema)(queryValues, querySchema);
1123
1345
  }
1124
- const updateData = (0, processor_utils_1.encrypt)(JSON.stringify(data), this.fetchProduct().private_key);
1346
+ const updateData = (0, processor_utils_1.encrypt)(JSON.stringify(data), this.product.private_key);
1125
1347
  let updateValues = null;
1126
1348
  if (exists.setup_type === enums_1.AuthTypes.TOKEN) {
1127
- updateValues = (0, processor_utils_1.encrypt)(JSON.stringify(data), this.fetchProduct().private_key);
1349
+ updateValues = (0, processor_utils_1.encrypt)(JSON.stringify(data), this.product.private_key);
1128
1350
  }
1129
1351
  return { data: updateData, values: updateValues };
1130
1352
  }
@@ -1157,8 +1379,10 @@ class ProductsBuilderService {
1157
1379
  throw new Error(`App ${app.access_tag} not found`);
1158
1380
  }
1159
1381
  const cleanedAppData = await this.validateAppData(appData, app);
1160
- await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({}, cleanedAppData), { component: enums_1.ProductComponents.APP, action: enums_1.RequestAction.CREATE }), this.getUserAccess());
1161
- await this.initializeProduct(this.product_id);
1382
+ if (!cleanedAppData.pricing_tag) {
1383
+ cleanedAppData.pricing_tag = `${appData.tag}:${productsBuilder_types_1.PricingTag.FREE}`;
1384
+ }
1385
+ await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({}, cleanedAppData), { app_tag: appData.tag, component: enums_1.ProductComponents.APP, action: enums_1.RequestAction.CREATE }), this.getUserAccess());
1162
1386
  }
1163
1387
  else {
1164
1388
  if (throwErrorIfExists)
@@ -1176,7 +1400,7 @@ class ProductsBuilderService {
1176
1400
  throw new Error(`App with access tag ${access_tag} not found`);
1177
1401
  }
1178
1402
  const version = app.versions.find((version) => version.tag === app.version);
1179
- const status = await this.webhooksApi.fetchWebhooks({ access_tag, app_tag: app.tag, version: app.version }, this.getUserAccess());
1403
+ //const status = await this.webhooksApi.fetchWebhooks({access_tag, app_tag: app.tag, version: app.version}, this.getUserAccess())
1180
1404
  console.log(status);
1181
1405
  if (!version) {
1182
1406
  throw new Error(`Required app version not found`);
@@ -1189,7 +1413,7 @@ class ProductsBuilderService {
1189
1413
  }
1190
1414
  async updateApp(access_tag, data) {
1191
1415
  try {
1192
- const app = this.fetchApp(access_tag);
1416
+ const app = await this.fetchApp(access_tag);
1193
1417
  if (!app) {
1194
1418
  throw new Error(`App ${access_tag} not found`);
1195
1419
  }
@@ -1205,17 +1429,18 @@ class ProductsBuilderService {
1205
1429
  throw new Error(`access_tag ${access_tag} is in use`); // TODO: also check on the backend
1206
1430
  }*/
1207
1431
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({ _id }, data), { access_tag, component: enums_1.ProductComponents.APP, action: enums_1.RequestAction.UPDATE }), this.getUserAccess());
1208
- await this.initializeProduct(this.product_id);
1209
1432
  }
1210
1433
  catch (e) {
1211
1434
  throw e;
1212
1435
  }
1213
1436
  }
1214
- fetchApps() {
1215
- return this.product.apps;
1437
+ async fetchApps() {
1438
+ const app = await this.productApi.fetchProductComponents(this.product_id, 'app', this.getUserAccess());
1439
+ return app;
1216
1440
  }
1217
- fetchApp(tag, throwErrorIfExists = false) {
1218
- const app = this.product.apps.find((data) => data.access_tag === tag);
1441
+ async fetchApp(tag, throwErrorIfExists = false) {
1442
+ const apps = await this.fetchApps();
1443
+ const app = apps.find((data) => data.app_tag === tag || data.access_tag === tag);
1219
1444
  if (!app && throwErrorIfExists)
1220
1445
  throw new Error(`App ${tag} not found`);
1221
1446
  return app;
@@ -1223,10 +1448,10 @@ class ProductsBuilderService {
1223
1448
  async createFunction(data, throwErrorIfExists = false) {
1224
1449
  try {
1225
1450
  // TODO: figure out a way to check if this has run before, halt if it has
1226
- if (!this.fetchFunction(data.tag)) {
1451
+ if (!(await this.fetchFunction(data.tag))) {
1227
1452
  await validators_1.CreateProductFunctionSchema.validateAsync(data);
1228
1453
  data.envs.map((env) => {
1229
- env.auth = (0, processor_utils_1.encrypt)(JSON.stringify(env.auth), this.fetchProduct().private_key);
1454
+ env.auth = (0, processor_utils_1.encrypt)(JSON.stringify(env.auth), this.product.private_key);
1230
1455
  });
1231
1456
  data.sample_data = (await this.inputsService.parseJson({
1232
1457
  data: data.sample,
@@ -1239,7 +1464,6 @@ class ProductsBuilderService {
1239
1464
  category: enums_1.Categories.DATA,
1240
1465
  }));
1241
1466
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({}, data), { component: enums_1.ProductComponents.FUNCTION, action: enums_1.RequestAction.CREATE }), this.getUserAccess());
1242
- await this.initializeProduct(this.product_id);
1243
1467
  }
1244
1468
  else {
1245
1469
  if (throwErrorIfExists)
@@ -1252,37 +1476,35 @@ class ProductsBuilderService {
1252
1476
  }
1253
1477
  async updateFunction(tag, data) {
1254
1478
  try {
1255
- const func = this.fetchFunction(tag);
1479
+ const func = await this.fetchFunction(tag);
1256
1480
  if (!func) {
1257
1481
  throw new Error(`Function ${tag} not found`);
1258
1482
  }
1259
1483
  const { _id } = func;
1260
1484
  await validators_1.UpdateProductFunctionSchema.validateAsync(data); // Change to update;
1261
- if (data.tag && this.fetchFunction(data.tag)) {
1485
+ if (data.tag && await this.fetchFunction(data.tag)) {
1262
1486
  throw new Error(`tag ${tag} is in use`); // TODO: also check on the backend
1263
1487
  }
1264
1488
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign(Object.assign({ _id }, func), data), { component: enums_1.ProductComponents.FUNCTION, action: enums_1.RequestAction.UPDATE }), this.getUserAccess());
1265
- await this.initializeProduct(this.product_id);
1266
1489
  }
1267
1490
  catch (e) {
1268
1491
  throw e;
1269
1492
  }
1270
1493
  }
1271
- fetchFunctions() {
1272
- return this.product.functions;
1494
+ async fetchFunctions() {
1495
+ const components = await this.productApi.fetchProductComponents(this.product_id, 'function', this.getUserAccess());
1496
+ return components;
1273
1497
  }
1274
- fetchFunction(tag) {
1275
- const func = this.product.functions.find((data) => data.tag === tag);
1276
- // if (!func) throw new Error(`Function ${tag} not found`);
1277
- return func;
1498
+ async fetchFunction(tag) {
1499
+ const component = await this.productApi.fetchProductComponentByTag(this.product_id, 'function', tag, this.getUserAccess());
1500
+ return component;
1278
1501
  }
1279
1502
  async createCache(data, throwErrorIfExists = false) {
1280
1503
  try {
1281
1504
  // TODO: figure out a way to check if this has run before, halt if it has
1282
- if (!this.fetchCache(data.tag)) {
1505
+ if (!(await this.fetchCache(data.tag))) {
1283
1506
  await validators_1.CreateProductCacheSchema.validateAsync(data);
1284
1507
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({}, data), { component: enums_1.ProductComponents.CACHE, action: enums_1.RequestAction.CREATE }), this.getUserAccess());
1285
- await this.initializeProduct(this.product_id);
1286
1508
  }
1287
1509
  else {
1288
1510
  if (throwErrorIfExists)
@@ -1295,62 +1517,59 @@ class ProductsBuilderService {
1295
1517
  }
1296
1518
  async updateCache(tag, data) {
1297
1519
  try {
1298
- const cache = this.fetchCache(tag);
1520
+ const cache = await this.fetchCache(tag);
1299
1521
  if (!cache) {
1300
1522
  throw new Error(`Cache ${tag} not found`);
1301
1523
  }
1302
1524
  const { _id } = cache;
1303
1525
  await validators_1.UpdateProductCacheSchema.validateAsync(data); // Change to update;
1304
- if (data.tag && this.fetchCache(data.tag)) {
1526
+ if (data.tag && await this.fetchCache(data.tag)) {
1305
1527
  throw new Error(`tag ${tag} is in use`); // TODO: also check on the backend
1306
1528
  }
1307
1529
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({ _id }, data), { component: enums_1.ProductComponents.CACHE, action: enums_1.RequestAction.UPDATE }), this.getUserAccess());
1308
- await this.initializeProduct(this.product_id);
1309
1530
  }
1310
1531
  catch (e) {
1311
1532
  throw e;
1312
1533
  }
1313
1534
  }
1314
- fetchCache(tag, throwErrorIfExists = false) {
1315
- const cache = this.product.caches.find((data) => data.tag === tag);
1316
- if (!cache && throwErrorIfExists)
1317
- throw new Error(`Cache ${tag} not found`);
1318
- return cache;
1535
+ async fetchCaches() {
1536
+ const components = await this.productApi.fetchProductComponents(this.product_id, 'cache', this.getUserAccess());
1537
+ return components;
1319
1538
  }
1320
- fetchCaches() {
1321
- return this.product.caches;
1539
+ async fetchCache(tag) {
1540
+ const component = await this.productApi.fetchProductComponentByTag(this.product_id, 'cache', tag, this.getUserAccess());
1541
+ return component;
1322
1542
  }
1323
1543
  async createNotification(data, throwErrorIfExists = false) {
1324
1544
  try {
1325
1545
  // TODO: figure out a way to check if this has run before, halt if it has
1326
- if (!this.fetchNotification(data.tag)) {
1546
+ if (!(await this.fetchNotification(data.tag))) {
1327
1547
  await validators_1.CreateProductNotificationSchema.validateAsync(data);
1328
- const envs = this.fetchEnvs();
1329
- envs.map((env) => {
1548
+ const envs = await this.fetchEnvs();
1549
+ for (const env of envs) {
1330
1550
  const exists = data.envs.findIndex((nEnv) => nEnv.slug === env.slug);
1331
1551
  if (exists === -1) {
1332
1552
  throw new Error(`Notification for environment ${env.slug} is not defined, please provide the environment details`);
1333
1553
  }
1334
- });
1554
+ }
1335
1555
  for (let i = 0; i < data.envs.length; i++) {
1336
1556
  const updates = {
1337
1557
  push_notifications: data.envs[i].push_notifications
1338
- ? (0, processor_utils_1.encrypt)(JSON.stringify(data.envs[i].push_notifications), this.fetchProduct().private_key)
1558
+ ? (0, processor_utils_1.encrypt)(JSON.stringify(data.envs[i].push_notifications), this.product.private_key)
1339
1559
  : undefined,
1340
1560
  emails: data.envs[i].emails
1341
- ? (0, processor_utils_1.encrypt)(JSON.stringify(data.envs[i].emails), this.fetchProduct().private_key)
1561
+ ? (0, processor_utils_1.encrypt)(JSON.stringify(data.envs[i].emails), this.product.private_key)
1342
1562
  : undefined,
1343
1563
  callbacks: data.envs[i].callbacks
1344
- ? (0, processor_utils_1.encrypt)(JSON.stringify(data.envs[i].callbacks), this.fetchProduct().private_key)
1564
+ ? (0, processor_utils_1.encrypt)(JSON.stringify(data.envs[i].callbacks), this.product.private_key)
1345
1565
  : undefined,
1346
1566
  sms: data.envs[i].sms
1347
- ? (0, processor_utils_1.encrypt)(JSON.stringify(data.envs[i].sms), this.fetchProduct().private_key)
1567
+ ? (0, processor_utils_1.encrypt)(JSON.stringify(data.envs[i].sms), this.product.private_key)
1348
1568
  : undefined,
1349
1569
  };
1350
1570
  data.envs[i] = Object.assign(Object.assign({}, data.envs[i]), updates);
1351
1571
  }
1352
1572
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({}, data), { component: enums_1.ProductComponents.NOTIFICATION, action: enums_1.RequestAction.CREATE }), this.getUserAccess());
1353
- await this.initializeProduct(this.product_id);
1354
1573
  }
1355
1574
  else {
1356
1575
  if (throwErrorIfExists)
@@ -1371,7 +1590,7 @@ class ProductsBuilderService {
1371
1590
  if (!notificationTag || !tag) {
1372
1591
  throw new Error(`tag is expected to be defined as "notification_tag:message_tag"`);
1373
1592
  }
1374
- const exists = this.fetchNotificationMessage(data.tag);
1593
+ const exists = await this.fetchNotificationMessage(data.tag);
1375
1594
  data.tag = tag;
1376
1595
  if (!exists) {
1377
1596
  if (!data.email && !data.push_notification && !data.callback) {
@@ -1408,7 +1627,6 @@ class ProductsBuilderService {
1408
1627
  data.sms_data = (0, string_utils_1.extractPlaceholders)(data.sms, '');
1409
1628
  }
1410
1629
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({}, data), { notificationTag, component: enums_1.ProductComponents.NOTIFICATION_MESSAGE, action: enums_1.RequestAction.CREATE }), this.getUserAccess());
1411
- await this.initializeProduct(this.product_id);
1412
1630
  }
1413
1631
  else {
1414
1632
  if (throwErrorIfExists)
@@ -1419,12 +1637,12 @@ class ProductsBuilderService {
1419
1637
  throw e;
1420
1638
  }
1421
1639
  }
1422
- fetchNotificationMessage(tag, throwErrorIfExists = false) {
1640
+ async fetchNotificationMessage(tag, throwErrorIfExists = false) {
1423
1641
  const [notificationTag, messageTag] = tag.split(':');
1424
1642
  if (!notificationTag || !messageTag) {
1425
1643
  throw new Error(`tag is expected to be defined as "notification_tag:message_tag"`);
1426
1644
  }
1427
- const notification = this.fetchNotification(notificationTag);
1645
+ const notification = await this.fetchNotification(notificationTag);
1428
1646
  if (!notification)
1429
1647
  throw new Error(`Notification ${notificationTag} not found`);
1430
1648
  const message = notification.messages.find((data) => data.tag === messageTag);
@@ -1432,8 +1650,8 @@ class ProductsBuilderService {
1432
1650
  throw new Error(`Notification message ${tag} not found`);
1433
1651
  return message;
1434
1652
  }
1435
- fetchNotificationMessages(notificationTag, throwErrorIfExists = false) {
1436
- const notification = this.product.notifications.find((data) => data.tag === notificationTag);
1653
+ async fetchNotificationMessages(notificationTag, throwErrorIfExists = false) {
1654
+ const notification = await this.fetchNotification(notificationTag); //this.product.notifications.find((data: IProductNotification) => data.tag === notificationTag);
1437
1655
  if (!notification)
1438
1656
  throw new Error(`Notification ${notificationTag} not found`);
1439
1657
  return notification.messages;
@@ -1486,7 +1704,6 @@ class ProductsBuilderService {
1486
1704
  const payload = Object.assign(Object.assign(Object.assign({}, message), data), { notificationTag, component: enums_1.ProductComponents.NOTIFICATION_MESSAGE, action: enums_1.RequestAction.UPDATE });
1487
1705
  // Update product and reinitialize
1488
1706
  await this.productApi.updateProduct(this.product_id, payload, this.getUserAccess());
1489
- await this.initializeProduct(this.product_id);
1490
1707
  }
1491
1708
  catch (error) {
1492
1709
  throw error;
@@ -1494,21 +1711,25 @@ class ProductsBuilderService {
1494
1711
  }
1495
1712
  async updateNotification(tag, data) {
1496
1713
  try {
1497
- const { _id, envs } = this.fetchNotification(tag);
1498
- if (data.tag && tag !== data.tag && this.fetchNotification(data.tag)) {
1714
+ const notificationResult = await this.fetchNotification(tag);
1715
+ if (!notificationResult) {
1716
+ throw new Error(`Notification with tag ${tag} not found`);
1717
+ }
1718
+ const { _id, envs } = notificationResult;
1719
+ if (data.tag && tag !== data.tag && (await this.fetchNotification(data.tag))) {
1499
1720
  throw new Error(`Notification of tag ${data.tag} already exists`);
1500
1721
  }
1501
1722
  await validators_1.UpdateProductNotificationSchema.validateAsync(data); // Change to update;
1502
- let notification = this.fetchNotification(tag);
1723
+ let notification = await this.fetchNotification(tag);
1503
1724
  if (!notification) {
1504
1725
  throw new Error(`Notification with tag ${tag} not found`);
1505
1726
  }
1506
1727
  if (data.envs) {
1507
1728
  const overwrite = [];
1508
1729
  const newEnvs = [];
1509
- data.envs.map(async (dataEnv) => {
1730
+ for (const dataEnv of data.envs) {
1510
1731
  const exists = envs.findIndex((env) => env.slug === dataEnv.slug);
1511
- if (!this.fetchEnv(dataEnv.slug)) {
1732
+ if (!(await this.fetchEnv(dataEnv.slug))) {
1512
1733
  throw new Error(`Product Environment ${dataEnv.slug} doesn't exist`);
1513
1734
  }
1514
1735
  if (exists === -1) {
@@ -1518,70 +1739,74 @@ class ProductsBuilderService {
1518
1739
  else {
1519
1740
  overwrite.push(Object.assign(Object.assign({}, envs[exists]), dataEnv));
1520
1741
  }
1521
- });
1742
+ }
1522
1743
  const unchanged = [];
1523
- envs.map((env) => {
1744
+ for (const env of envs) {
1524
1745
  const newEnv = newEnvs.findIndex((dataEnv) => env.slug === dataEnv.slug) > -1;
1525
1746
  const overwriteEnv = overwrite.findIndex((dataEnv) => env.slug === dataEnv.slug) > -1;
1526
1747
  if (!newEnv && !overwriteEnv) {
1527
1748
  unchanged.push(env);
1528
1749
  }
1529
- });
1750
+ }
1530
1751
  data.envs = [...unchanged, ...overwrite, ...newEnvs];
1531
1752
  }
1532
1753
  const update = Object.assign(Object.assign({}, notification), data);
1533
- update.envs.map((env) => {
1754
+ for (const env of update.envs) {
1534
1755
  if (env.emails)
1535
- env.emails = (0, processor_utils_1.encrypt)(JSON.stringify(env.emails), this.fetchProduct().private_key);
1756
+ env.emails = (0, processor_utils_1.encrypt)(JSON.stringify(env.emails), this.product.private_key);
1536
1757
  if (env.push_notifications)
1537
- env.push_notifications = (0, processor_utils_1.encrypt)(JSON.stringify(env.push_notifications), this.fetchProduct().private_key);
1758
+ env.push_notifications = (0, processor_utils_1.encrypt)(JSON.stringify(env.push_notifications), this.product.private_key);
1538
1759
  if (env.callbacks)
1539
- env.callbacks = (0, processor_utils_1.encrypt)(JSON.stringify(env.callbacks), this.fetchProduct().private_key);
1760
+ env.callbacks = (0, processor_utils_1.encrypt)(JSON.stringify(env.callbacks), this.product.private_key);
1540
1761
  if (env.sms)
1541
- env.sms = (0, processor_utils_1.encrypt)(JSON.stringify(env.sms), this.fetchProduct().private_key);
1542
- return env;
1543
- });
1762
+ env.sms = (0, processor_utils_1.encrypt)(JSON.stringify(env.sms), this.product.private_key);
1763
+ }
1544
1764
  const payload = Object.assign(Object.assign({ _id }, update), { component: enums_1.ProductComponents.NOTIFICATION, action: enums_1.RequestAction.UPDATE });
1545
1765
  await this.productApi.updateProduct(this.product_id, payload, this.getUserAccess());
1546
- await this.initializeProduct(this.product_id);
1547
1766
  }
1548
1767
  catch (e) {
1549
1768
  throw e;
1550
1769
  }
1551
1770
  }
1552
- fetchNotification(tag, notification_slug = '', throwErrorIfExists = false) {
1553
- const notification = this.product.notifications.find((data) => data.tag === tag);
1554
- if (notification && throwErrorIfExists)
1555
- throw new Error(`Notification ${tag} is in use`);
1556
- /*if (notification_slug) {
1557
- const { notifications, emails, callbacks } = notification.envs.find(
1558
- (data: INotificationEnv) => (data.slug = notification_slug),
1559
- );
1560
- return { notifications, emails, callbacks };
1561
- }*/
1562
- if (notification) {
1563
- notification.envs.map((data) => {
1771
+ async fetchNotifications() {
1772
+ let components = await this.productApi.fetchProductComponents(this.product_id, 'notification', this.getUserAccess());
1773
+ const notifications = components.map((notification) => {
1774
+ return notification.envs.map((data) => {
1564
1775
  if (data.callbacks && typeof data.callbacks == 'string') {
1565
- data.callbacks = JSON.parse((0, processor_utils_1.decrypt)(String(data.callbacks), this.fetchProduct().private_key));
1776
+ data.callbacks = JSON.parse((0, processor_utils_1.decrypt)(String(data.callbacks), this.product.private_key));
1566
1777
  }
1567
1778
  if (data.push_notifications && typeof data.push_notifications == 'string') {
1568
- data.push_notifications = JSON.parse((0, processor_utils_1.decrypt)(String(data.push_notifications), this.fetchProduct().private_key));
1779
+ data.push_notifications = JSON.parse((0, processor_utils_1.decrypt)(String(data.push_notifications), this.product.private_key));
1569
1780
  }
1570
1781
  if (data.emails && typeof data.emails == 'string') {
1571
- data.emails = JSON.parse((0, processor_utils_1.decrypt)(String(data.emails), this.fetchProduct().private_key));
1782
+ data.emails = JSON.parse((0, processor_utils_1.decrypt)(String(data.emails), this.product.private_key));
1572
1783
  }
1573
1784
  if (data.sms && typeof data.sms == 'string') {
1574
- data.sms = JSON.parse((0, processor_utils_1.decrypt)(String(data.sms), this.fetchProduct().private_key));
1785
+ data.sms = JSON.parse((0, processor_utils_1.decrypt)(String(data.sms), this.product.private_key));
1575
1786
  }
1576
1787
  return data;
1577
1788
  });
1578
- }
1579
- return notification;
1789
+ });
1790
+ return notifications;
1580
1791
  }
1581
- fetchNotifications() {
1582
- return this.product.notifications.map((notification) => {
1583
- return this.fetchNotification(notification.tag);
1792
+ async fetchNotification(tag) {
1793
+ const component = await this.productApi.fetchProductComponentByTag(this.product_id, 'notification', tag, this.getUserAccess());
1794
+ component.envs.map((data) => {
1795
+ if (data.callbacks && typeof data.callbacks == 'string') {
1796
+ data.callbacks = JSON.parse((0, processor_utils_1.decrypt)(String(data.callbacks), this.product.private_key));
1797
+ }
1798
+ if (data.push_notifications && typeof data.push_notifications == 'string') {
1799
+ data.push_notifications = JSON.parse((0, processor_utils_1.decrypt)(String(data.push_notifications), this.product.private_key));
1800
+ }
1801
+ if (data.emails && typeof data.emails == 'string') {
1802
+ data.emails = JSON.parse((0, processor_utils_1.decrypt)(String(data.emails), this.product.private_key));
1803
+ }
1804
+ if (data.sms && typeof data.sms == 'string') {
1805
+ data.sms = JSON.parse((0, processor_utils_1.decrypt)(String(data.sms), this.product.private_key));
1806
+ }
1807
+ return data;
1584
1808
  });
1809
+ return component;
1585
1810
  }
1586
1811
  validateFeatureSequence(array) {
1587
1812
  // Validate uniqueness of sequence_tag
@@ -1681,24 +1906,27 @@ class ProductsBuilderService {
1681
1906
  await this.validateActionDataInput(data, action, event.input, event_index, sequence_index);
1682
1907
  }
1683
1908
  async checkAndValidateFunctionDataInput(data, sequence_index, event, event_index) {
1684
- const func = this.fetchFunction(event.event);
1909
+ const func = await this.fetchFunction(event.event);
1685
1910
  await this.validateActionDataInput(data, func, event.input, event_index, sequence_index);
1686
1911
  }
1687
1912
  async checkAndValidateDBActionDataInput(data, sequence_index, event, event_index) {
1688
1913
  console.log('EVENTY', event.event);
1689
- const { filterData, data: dbData, filterTemplate, template } = this.fetchDatabaseAction(event.event);
1914
+ const { filterData, data: dbData, filterTemplate, template } = await this.fetchDatabaseAction(event.event);
1690
1915
  await this.validateDBActionDataInput(data, { filterData, data: dbData, template, filter: filterTemplate }, event.input, event_index, sequence_index);
1691
1916
  }
1692
1917
  async checkAndValidateFeatureDataInput(data, sequence_index, event, event_index) {
1693
- const { input } = this.fetchFeature(event.event);
1918
+ const feature = await this.fetchFeature(event.event);
1919
+ if (!feature)
1920
+ throw new Error(`Feature ${event.event} not found`);
1921
+ const { input } = feature;
1694
1922
  await this.validateFeatureDataInput(data, input, event.input, event_index, sequence_index);
1695
1923
  }
1696
1924
  async checkAndValidateNotificationDataInput(data, sequence_index, event, event_index) {
1697
- const { callback_data: callback, push_notification_data: notification, email_data: email, sms_data: sms, } = this.fetchNotificationMessage(event.event);
1925
+ const { callback_data: callback, push_notification_data: notification, email_data: email, sms_data: sms, } = await this.fetchNotificationMessage(event.event);
1698
1926
  await this.validateNotificationDataInput(data, { callback, notification, email, sms }, event.input, event_index, sequence_index);
1699
1927
  }
1700
1928
  async checkAndValidatePublishDataInput(data, sequence_index, event, event_index) {
1701
- const topic = this.fetchMessageBrokerTopic(event.event);
1929
+ const topic = await this.fetchMessageBrokerTopic(event.event);
1702
1930
  if (!topic) {
1703
1931
  throw new Error(`Topic ${event.event} not registered`);
1704
1932
  }
@@ -1706,14 +1934,14 @@ class ProductsBuilderService {
1706
1934
  //await this.validatePublishTopicDataInput(data, { data: topicData }, event.input, event_index, sequence_index)
1707
1935
  }
1708
1936
  async checkAndValidateStorageDataInput(data, sequence_index, event, event_index) {
1709
- const storage = this.fetchStorage(event.event);
1937
+ const storage = await this.fetchStorage(event.event);
1710
1938
  if (!storage) {
1711
1939
  throw new Error(`Storage ${event.event} does not exist`);
1712
1940
  }
1713
1941
  //await this.validateStorageDataInput(data, {}, event.input, event_index, sequence_index);
1714
1942
  }
1715
1943
  async checkAndValidateJobDataInput(data, sequence_index, event, event_index) {
1716
- const job = this.fetchJob(event.event);
1944
+ const job = await this.fetchJob(event.event);
1717
1945
  if (job.type === productsBuilder_types_1.JobEventTypes.ACTION) {
1718
1946
  await this.checkAndValidateActionDataInput(data, sequence_index, event, event_index);
1719
1947
  }
@@ -1753,7 +1981,7 @@ class ProductsBuilderService {
1753
1981
  await this.checkAndValidatePublishDataInput(data, sequence_index, event, event_index);
1754
1982
  }
1755
1983
  /*if (event.type === FeatureEventTypes.SUBSCRIBE) {
1756
- const topic = this.fetchMessageBrokerTopic(event.event);
1984
+ const topic = await this.fetchMessageBrokerTopic(event.event);
1757
1985
 
1758
1986
  if(!topic) {
1759
1987
  throw new Error(`Topic ${event.event} not registered`);
@@ -1956,7 +2184,7 @@ class ProductsBuilderService {
1956
2184
  });
1957
2185
  }
1958
2186
  async validateActionInputData(data, skipSample = false, option = false, optionIndex = 0) {
1959
- var _a;
2187
+ var _a, _b;
1960
2188
  const { obj } = data, base_data = __rest(data, ["obj"]);
1961
2189
  if (!data.sample && Object.keys(obj).length === 0) {
1962
2190
  return;
@@ -1964,8 +2192,8 @@ class ProductsBuilderService {
1964
2192
  if (!data.sample) {
1965
2193
  throw new Error('Something unexpected happened in validating action input');
1966
2194
  }
1967
- const sequence = data.feature.sequence[data.sequence_index];
1968
- if (obj === undefined || obj === null) {
2195
+ const sequence = data.feature.sequence ? (_a = data.feature) === null || _a === void 0 ? void 0 : _a.sequence[data.sequence_index] : null;
2196
+ if ((obj === undefined || obj === null) && sequence) {
1969
2197
  if (!option) {
1970
2198
  throw new Error(`sequence ${sequence.tag} event ${sequence.events[data.event_index].event} ${data.type} should not be ${obj}`);
1971
2199
  }
@@ -1973,7 +2201,7 @@ class ProductsBuilderService {
1973
2201
  throw new Error(`option event index ${optionIndex}, ${data.type} should not be ${obj}`);
1974
2202
  }
1975
2203
  }
1976
- if (Object.values(obj).length > 0 && ((_a = data.sample.data) === null || _a === void 0 ? void 0 : _a.length) === 0) {
2204
+ if (sequence && (Object.values(obj).length > 0 && ((_b = data.sample.data) === null || _b === void 0 ? void 0 : _b.length) === 0)) {
1977
2205
  if (!option) {
1978
2206
  throw new Error(`sequence ${sequence.tag} event ${sequence.events[data.event_index].event} ${data.type} should be an empty object`);
1979
2207
  }
@@ -1984,7 +2212,7 @@ class ProductsBuilderService {
1984
2212
  if (Object.values(obj).length === 0 && data.sample.data.length === 0 /*data.sample?.data?.length === 0*/) {
1985
2213
  return;
1986
2214
  }
1987
- if (Object.values(obj).length === 0 && data.sample.data.length > 0 && !skipSample) {
2215
+ if (sequence && Object.values(obj).length === 0 && data.sample.data.length > 0 && !skipSample) {
1988
2216
  //console.log("validity err: ",obj, data.sample)
1989
2217
  if (!option) {
1990
2218
  throw new Error(`sequence ${sequence.tag} event ${sequence.events[data.event_index].event} ${data.type} should not be an empty object`);
@@ -2160,7 +2388,9 @@ class ProductsBuilderService {
2160
2388
  let current_input = input;
2161
2389
  for (let i = 0; i < stages.length; i++) {
2162
2390
  let stage = stages[i];
2163
- const matches = stage.match(/^\[(\d+)\]$/);
2391
+ // Convert to string for .match() call
2392
+ const stageStr = String(stage);
2393
+ const matches = stageStr.match(/^\[(\d+)\]$/);
2164
2394
  if (matches && matches.length === 2) {
2165
2395
  const number = parseInt(matches[1], 10);
2166
2396
  if (!isNaN(number)) {
@@ -2216,14 +2446,16 @@ class ProductsBuilderService {
2216
2446
  }
2217
2447
  else {
2218
2448
  //const envs =
2219
- const app = this.fetchApp(access_tag);
2449
+ const app = await this.fetchApp(access_tag);
2220
2450
  const { envs } = app;
2221
2451
  await Promise.all(envs.map((env) => {
2222
2452
  if (stages[0] !== env.auth.auth_tag) {
2223
2453
  throw new Error(`Auth ${stages[0]} does not exist on env ${env.product_env_slug} on app ${access_tag}`);
2224
2454
  }
2225
- const decrypted = JSON.parse((0, processor_utils_1.decrypt)(env.auth.values, this.fetchProduct().private_key));
2226
- const check = (0, objects_utils_1.findFaultyKeys)(stages.slice(1), decrypted);
2455
+ const decrypted = JSON.parse((0, processor_utils_1.decrypt)(env.auth.values, this.product.private_key));
2456
+ // Convert stages to string[] for findFaultyKeys
2457
+ const stringStages = stages.slice(1).map(stage => String(stage));
2458
+ const check = (0, objects_utils_1.findFaultyKeys)(stringStages, decrypted);
2227
2459
  if (check.faultyKeys) {
2228
2460
  throw new Error(`Auth Key(s) ${check.faultyKeys.join(', ')} is/are invalid, they do not exist in authorization for ${env.product_env_slug} on app ${access_tag}`);
2229
2461
  }
@@ -2239,7 +2471,9 @@ class ProductsBuilderService {
2239
2471
  let i = 1;
2240
2472
  while (i < stages.length) {
2241
2473
  let stage = stages[i];
2242
- const matches = stage.match(/^\[(\d+)\]$/);
2474
+ // Convert to string for .match() call
2475
+ const stageStr = String(stage);
2476
+ const matches = stageStr.match(/^\[(\d+)\]$/);
2243
2477
  if (matches && matches.length === 2) {
2244
2478
  const number = parseInt(matches[1], 10);
2245
2479
  if (!isNaN(number)) {
@@ -2313,11 +2547,15 @@ class ProductsBuilderService {
2313
2547
  // let current_data;
2314
2548
  if (i === 0) {
2315
2549
  // find sequence by tag, see if it exists and its before current sequence
2316
- this.validateSequenceInputParents(stage, meta.sequence_index, meta.feature.sequence);
2317
- sequence = this.fetchPriorSequence(meta, stage);
2550
+ // Convert stage to string for function calls
2551
+ const stageStr = String(stage);
2552
+ this.validateSequenceInputParents(stageStr, meta.sequence_index, meta.feature.sequence);
2553
+ sequence = this.fetchPriorSequence(meta, stageStr);
2318
2554
  }
2319
2555
  if (i === 1 && sequence) {
2320
- event = this.fetchSequenceEvent(sequence, stage);
2556
+ // Convert stage to string for function calls
2557
+ const stageStr = String(stage);
2558
+ event = this.fetchSequenceEvent(sequence, stageStr);
2321
2559
  if (!event) {
2322
2560
  throw new Error(`event ${stage} not found in sequence ${sequence.tag}`);
2323
2561
  }
@@ -2346,7 +2584,9 @@ class ProductsBuilderService {
2346
2584
  if (i > 1 && response) {
2347
2585
  let parent_index = 0;
2348
2586
  let increment = false;
2349
- const matches = stage.match(/^\[(\d+)\]$/);
2587
+ // Convert to string for .match() call
2588
+ const stageStr = String(stage);
2589
+ const matches = stageStr.match(/^\[(\d+)\]$/);
2350
2590
  if (matches && matches.length === 2) {
2351
2591
  const number = parseInt(matches[1], 10);
2352
2592
  if (!isNaN(number)) {
@@ -2404,7 +2644,7 @@ class ProductsBuilderService {
2404
2644
  if (stages.length > 2) {
2405
2645
  throw new Error(`sequence ${sequence.tag} event ${sequence.events[meta.event_index].event} ${meta.type}, has invalid varibale definition ${value}, only two keys is required`);
2406
2646
  }
2407
- const app = this.fetchApp(stages[0]);
2647
+ const app = await this.fetchApp(String(stages[0]));
2408
2648
  if (!app) {
2409
2649
  throw new Error(`App ${stages[0]} not found in sequence ${sequence.tag} event ${sequence.events[meta.event_index].event} ${meta.type}, has invalid varibale definition ${value}. `);
2410
2650
  }
@@ -2421,8 +2661,8 @@ class ProductsBuilderService {
2421
2661
  if (stages.length !== 2) {
2422
2662
  throw new Error(`When using constants you need to specify the constant in the format $Constant{app_tag}{key} instead of ${value}`);
2423
2663
  }
2424
- const app_tag = stages[0];
2425
- const key = stages[1];
2664
+ const app_tag = String(stages[0]);
2665
+ const key = String(stages[1]);
2426
2666
  const _c = await this.fetchThirdPartyAppByAccessTag(app_tag), { version } = _c, app = __rest(_c, ["version"]);
2427
2667
  if (!app) {
2428
2668
  throw new Error(`App ${app_tag} not found in constant ${value}`);
@@ -2512,9 +2752,10 @@ class ProductsBuilderService {
2512
2752
  return found;
2513
2753
  }
2514
2754
  validateActionKeyPlacement(data) {
2755
+ var _a;
2515
2756
  const actionData = data.sample.data;
2516
2757
  const { indexes } = data;
2517
- const sequence = data.feature.sequence[data.sequence_index];
2758
+ const sequence = data.feature.sequence ? (_a = data.feature) === null || _a === void 0 ? void 0 : _a.sequence[data.sequence_index] : null;
2518
2759
  const datapoint = actionData.find((item) => {
2519
2760
  return (item.parent_key === indexes.parent_key && item.key === data.key && item.level === indexes.level //&&
2520
2761
  //indexes.index === item.index
@@ -2522,7 +2763,12 @@ class ProductsBuilderService {
2522
2763
  });
2523
2764
  if (!datapoint) {
2524
2765
  console.log('VALIDATE', data.key, data.value, actionData, indexes);
2525
- throw new Error(`Key ${data.key} not found for ${data.type} for sequence ${sequence.tag} event ${sequence.events[data.event_index].event}`);
2766
+ if (sequence) {
2767
+ throw new Error(`Key ${data.key} not found for ${data.type} for sequence ${sequence.tag} event ${sequence.events[data.event_index].event}`);
2768
+ }
2769
+ else {
2770
+ throw new Error(`Key ${data.key} not found`);
2771
+ }
2526
2772
  }
2527
2773
  return datapoint;
2528
2774
  }
@@ -2553,12 +2799,11 @@ class ProductsBuilderService {
2553
2799
  }
2554
2800
  async createFeature(data, throwErrorIfExists = false) {
2555
2801
  try {
2556
- if (!this.fetchFeature(data.tag, false)) {
2802
+ if (!(await this.fetchFeature(data.tag))) {
2557
2803
  await validators_1.CreateProductFeatureSchema.validateAsync(data);
2558
2804
  try {
2559
2805
  await this.validateFeatureData(data);
2560
2806
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({}, data), { component: enums_1.ProductComponents.FEATURE, action: enums_1.RequestAction.CREATE }), this.getUserAccess());
2561
- await this.initializeProduct(this.product_id);
2562
2807
  }
2563
2808
  catch (e) {
2564
2809
  throw e;
@@ -2575,7 +2820,7 @@ class ProductsBuilderService {
2575
2820
  }
2576
2821
  async updateFeature(tag, data) {
2577
2822
  try {
2578
- const feature = this.fetchFeature(tag);
2823
+ const feature = await this.fetchFeature(tag);
2579
2824
  if (!feature) {
2580
2825
  throw new Error(`Feature ${tag} not found`);
2581
2826
  }
@@ -2587,43 +2832,42 @@ class ProductsBuilderService {
2587
2832
  }
2588
2833
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({ _id,
2589
2834
  tag }, data), { component: enums_1.ProductComponents.FEATURE, action: enums_1.RequestAction.UPDATE }), this.getUserAccess());
2590
- await this.initializeProduct(this.product_id);
2591
2835
  }
2592
2836
  catch (e) {
2593
2837
  throw e;
2594
2838
  }
2595
2839
  }
2596
- fetchFeature(tag, throwErrorIfExists = true) {
2597
- const feature = this.product.features.find((data) => data.tag === tag);
2598
- if (!feature && throwErrorIfExists)
2599
- throw new Error(`Feature ${tag} not found`);
2600
- return feature;
2840
+ async fetchFeatures() {
2841
+ const components = await this.productApi.fetchProductComponents(this.product_id, 'feature', this.getUserAccess());
2842
+ return components;
2601
2843
  }
2602
- fetchFeatures() {
2603
- return this.product.features;
2844
+ async fetchFeature(tag) {
2845
+ const component = await this.productApi.fetchProductComponentByTag(this.product_id, 'feature', tag, this.getUserAccess());
2846
+ return component;
2604
2847
  }
2605
2848
  async createDatabase(data, throwErrorIfExists = false) {
2606
2849
  try {
2607
2850
  // TODO: figure out a way to check if this has run before, halt if it has
2608
- if (!this.fetchDatabase(data.tag, false)) {
2851
+ if (!(await this.fetchDatabase(data.tag))) {
2609
2852
  await validators_1.CreateProductDatabaseSchema.validateAsync(data);
2610
- data.envs = data.envs.map((env) => {
2611
- const exists = this.fetchEnv(env.slug);
2853
+ const processedEnvs = [];
2854
+ for (const env of data.envs) {
2855
+ const exists = await this.fetchEnv(env.slug);
2612
2856
  if (!exists) {
2613
2857
  throw new Error(`Env ${env.slug} does not exist`);
2614
2858
  }
2615
- env.connection_url = (0, processor_utils_1.encrypt)(env.connection_url, this.fetchProduct().private_key);
2616
- return env;
2617
- });
2618
- const envs = this.fetchEnvs();
2619
- envs.map((env) => {
2859
+ env.connection_url = (0, processor_utils_1.encrypt)(env.connection_url, this.product.private_key);
2860
+ processedEnvs.push(env);
2861
+ }
2862
+ data.envs = processedEnvs;
2863
+ const envs = await this.fetchEnvs();
2864
+ for (const env of envs) {
2620
2865
  const exists = data.envs.findIndex((dbEnv) => dbEnv.slug === env.slug);
2621
2866
  if (exists === -1) {
2622
2867
  throw new Error(`Product env ${env.slug} is not defined, please provide connection details`);
2623
2868
  }
2624
- });
2869
+ }
2625
2870
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({}, data), { component: enums_1.ProductComponents.DATABASE, action: enums_1.RequestAction.CREATE }), this.getUserAccess());
2626
- await this.initializeProduct(this.product_id);
2627
2871
  }
2628
2872
  else {
2629
2873
  if (throwErrorIfExists)
@@ -2636,7 +2880,7 @@ class ProductsBuilderService {
2636
2880
  }
2637
2881
  async updateDatabase(tag, data) {
2638
2882
  try {
2639
- const db = this.fetchDatabase(tag);
2883
+ const db = await this.fetchDatabase(tag);
2640
2884
  if (!db) {
2641
2885
  throw new Error(`Database ${tag} not found`);
2642
2886
  }
@@ -2645,15 +2889,16 @@ class ProductsBuilderService {
2645
2889
  if (data.tag && this.fetchDatabase(data.tag)) {
2646
2890
  throw new Error(`tag ${tag} is in use`); // TODO: also check on the backend
2647
2891
  }
2648
- data.envs = data.envs.map((env) => {
2649
- const exists = this.fetchEnv(env.slug);
2892
+ data.envs = await Promise.all(data.envs.map(async (env) => {
2893
+ const exists = await this.fetchEnv(env.slug);
2650
2894
  if (!exists) {
2651
2895
  throw new Error(`Env ${env.slug} does not exist`);
2652
2896
  }
2653
- if (env.connection_url)
2654
- env.connection_url = (0, processor_utils_1.encrypt)(env.connection_url, this.fetchProduct().private_key);
2897
+ if (env.connection_url) {
2898
+ env.connection_url = (0, processor_utils_1.encrypt)(env.connection_url, this.product.private_key);
2899
+ }
2655
2900
  return env;
2656
- });
2901
+ }));
2657
2902
  const overwrite = [];
2658
2903
  const newEnvs = [];
2659
2904
  data.envs.map((dataEnv) => {
@@ -2682,24 +2927,27 @@ class ProductsBuilderService {
2682
2927
  data.envs = [...unchanged, ...overwrite, ...newEnvs];
2683
2928
  //console.log("UPDATED!!!", JSON.stringify(data.envs))
2684
2929
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({ _id }, data), { tag, component: enums_1.ProductComponents.DATABASE, action: enums_1.RequestAction.UPDATE }), this.getUserAccess());
2685
- await this.initializeProduct(this.product_id);
2686
2930
  }
2687
2931
  catch (e) {
2688
2932
  throw e;
2689
2933
  }
2690
2934
  }
2691
- fetchDatabase(tag, throwErrorIfExists = false) {
2692
- const database = this.product.databases.find((data) => data.tag === tag);
2935
+ async fetchDatabases() {
2936
+ const components = await this.productApi.fetchProductComponents(this.product_id, 'database', this.getUserAccess());
2937
+ components.map((database) => {
2938
+ database.envs.map((env) => {
2939
+ env.connection_url = (0, processor_utils_1.decrypt)(env.connection_url, this.product.private_key);
2940
+ });
2941
+ });
2942
+ return components;
2943
+ }
2944
+ async fetchDatabase(tag) {
2945
+ const database = await this.productApi.fetchProductComponentByTag(this.product_id, 'database', tag, this.getUserAccess());
2693
2946
  database.envs.map((env) => {
2694
- env.connection_url = (0, processor_utils_1.decrypt)(env.connection_url, this.fetchProduct().private_key);
2947
+ env.connection_url = (0, processor_utils_1.decrypt)(env.connection_url, this.product.private_key);
2695
2948
  });
2696
- if (!database && throwErrorIfExists)
2697
- throw new Error(`Database ${tag} not found`);
2698
2949
  return database;
2699
2950
  }
2700
- fetchDatabases() {
2701
- return this.product.databases;
2702
- }
2703
2951
  async createDatabaseAction(data, throwErrorIfExists = false) {
2704
2952
  try {
2705
2953
  if (!data.tag) {
@@ -2709,10 +2957,10 @@ class ProductsBuilderService {
2709
2957
  if (!databaseTag || !tag) {
2710
2958
  throw new Error(`tag is expected to be defined as "database_tag:action_tag"`);
2711
2959
  }
2712
- const exists = this.fetchDatabaseAction(data.tag);
2960
+ const exists = await this.fetchDatabaseAction(data.tag);
2713
2961
  data.tag = tag;
2714
2962
  if (!exists) {
2715
- const database = this.fetchDatabase(databaseTag);
2963
+ const database = await this.fetchDatabase(databaseTag);
2716
2964
  let values, template;
2717
2965
  if (database.type === productsBuilder_types_1.DatabaseTypes.MONGODB) {
2718
2966
  await create_productDatabaseAction_validator_1.NOSQLDatabaseActionsSchema.validateAsync(data);
@@ -2736,7 +2984,6 @@ class ProductsBuilderService {
2736
2984
  }
2737
2985
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({}, data), { data: values, template,
2738
2986
  databaseTag, component: enums_1.ProductComponents.DATABASE_ACTION, action: enums_1.RequestAction.CREATE }), this.getUserAccess());
2739
- await this.initializeProduct(this.product_id);
2740
2987
  }
2741
2988
  else {
2742
2989
  if (throwErrorIfExists)
@@ -2747,7 +2994,7 @@ class ProductsBuilderService {
2747
2994
  throw e;
2748
2995
  }
2749
2996
  }
2750
- fetchDatabaseAction(tag, throwErrorIfExists = false) {
2997
+ async fetchDatabaseAction(tag, throwErrorIfExists = false) {
2751
2998
  const [databaseTag, actionTag] = tag.split(':');
2752
2999
  if (!databaseTag || !actionTag) {
2753
3000
  throw new Error(`tag is expected to be defined as "database_tag:action_tag"`);
@@ -2774,7 +3021,7 @@ class ProductsBuilderService {
2774
3021
  throw new Error(`tag is expected to be defined as "database_tag:action_tag"`);
2775
3022
  }
2776
3023
  // Fetch required data
2777
- const database = await this.fetchDatabase(databaseTag, true);
3024
+ const database = await this.fetchDatabase(databaseTag);
2778
3025
  const action = await this.fetchDatabaseAction(data.tag, true);
2779
3026
  // Construct payload
2780
3027
  const payload = Object.assign(Object.assign(Object.assign({}, action), data), { databaseTag, component: enums_1.ProductComponents.DATABASE_ACTION, action: enums_1.RequestAction.UPDATE });
@@ -2802,14 +3049,13 @@ class ProductsBuilderService {
2802
3049
  }
2803
3050
  // Update product and reinitialize
2804
3051
  await this.productApi.updateProduct(this.product_id, payload, this.getUserAccess());
2805
- await this.initializeProduct(this.product_id);
2806
3052
  }
2807
3053
  catch (error) {
2808
3054
  throw error;
2809
3055
  }
2810
3056
  }
2811
- fetchDatabaseActions(databaseTag) {
2812
- const database = this.product.databases.find((data) => data.tag === databaseTag);
3057
+ async fetchDatabaseActions(databaseTag) {
3058
+ const database = await this.fetchDatabase(databaseTag);
2813
3059
  if (!database)
2814
3060
  throw new Error(`Database ${databaseTag} not found`);
2815
3061
  const actions = database.actions;
@@ -2832,12 +3078,11 @@ class ProductsBuilderService {
2832
3078
  data.tag = tag;
2833
3079
  await create_productDatabaseMigration_validator_1.default.validateAsync(Object.assign(Object.assign({}, data), { databaseTag })); // Change to update;
2834
3080
  if (!exists) {
2835
- const database = this.fetchDatabase(databaseTag, true);
3081
+ const database = await this.fetchDatabase(databaseTag);
2836
3082
  if (database.type === productsBuilder_types_1.DatabaseTypes.MONGODB) {
2837
3083
  throw new Error(`${database.type} does not support migrations`);
2838
3084
  }
2839
3085
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({}, data), { databaseTag, component: enums_1.ProductComponents.DATABASE_MIGRATION }), this.getUserAccess());
2840
- await this.initializeProduct(this.product_id);
2841
3086
  }
2842
3087
  else {
2843
3088
  if (throwErrorIfExists)
@@ -2859,14 +3104,13 @@ class ProductsBuilderService {
2859
3104
  }
2860
3105
  data.tag = tag;
2861
3106
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign(Object.assign({}, action), Object.assign(Object.assign({}, data), { databaseTag })), { component: enums_1.ProductComponents.DATABASE_MIGRATION }), this.getUserAccess());
2862
- await this.initializeProduct(this.product_id);
2863
3107
  }
2864
- fetchDatabaseMigration(tag, throwError = false) {
3108
+ async fetchDatabaseMigration(tag, throwError = false) {
2865
3109
  const [databaseTag, migrationTag] = tag.split(':');
2866
3110
  if (!databaseTag || !migrationTag) {
2867
3111
  throw new Error(`tag is expected to be defined as "database_tag:action_tag"`);
2868
3112
  }
2869
- const database = this.product.databases.find((data) => data.tag === databaseTag);
3113
+ const database = await this.fetchDatabase(databaseTag); //this.product.databases.find((data: IProductDatabase) => data.tag === databaseTag);
2870
3114
  if (!database)
2871
3115
  throw new Error(`Database ${databaseTag} not found`);
2872
3116
  const migration = database.migrations.find((data) => data.tag === migrationTag);
@@ -2874,8 +3118,8 @@ class ProductsBuilderService {
2874
3118
  throw new Error(`Database migration ${tag} not found`);
2875
3119
  return migration;
2876
3120
  }
2877
- fetchDatabaseMigrations(databaseTag) {
2878
- const database = this.product.databases.find((data) => data.tag === databaseTag);
3121
+ async fetchDatabaseMigrations(databaseTag) {
3122
+ const database = await this.fetchDatabase(databaseTag); //this.product.databases.find((data: IProductDatabase) => data.tag === databaseTag);
2879
3123
  if (!database)
2880
3124
  throw new Error(`Database ${databaseTag} not found`);
2881
3125
  const migrations = database.migrations;
@@ -2895,31 +3139,31 @@ class ProductsBuilderService {
2895
3139
  }
2896
3140
  }
2897
3141
  if (type === productsBuilder_types_1.JobEventTypes.DATABASE_ACTION) {
2898
- const found = this.fetchDatabaseAction(event);
3142
+ const found = await this.fetchDatabaseAction(event);
2899
3143
  if (!found) {
2900
3144
  throw new Error(`Database action ${event} not found`);
2901
3145
  }
2902
3146
  }
2903
3147
  if (type === productsBuilder_types_1.JobEventTypes.FUNCTION) {
2904
- const found = this.fetchFunction(event);
3148
+ const found = await this.fetchFunction(event);
2905
3149
  if (!found) {
2906
3150
  throw new Error(`Cloud function ${event} not found`);
2907
3151
  }
2908
3152
  }
2909
3153
  if (type === productsBuilder_types_1.JobEventTypes.STORAGE) {
2910
- const found = this.fetchStorage(event);
3154
+ const found = await this.fetchStorage(event);
2911
3155
  if (!found) {
2912
3156
  throw new Error(`Storage ${event} not found`);
2913
3157
  }
2914
3158
  }
2915
3159
  if (type === productsBuilder_types_1.JobEventTypes.NOTIFICATION) {
2916
- const found = this.fetchNotification(event);
3160
+ const found = await this.fetchNotification(event);
2917
3161
  if (!found) {
2918
3162
  throw new Error(`Notification ${event} not found`);
2919
3163
  }
2920
3164
  }
2921
3165
  if (type === productsBuilder_types_1.JobEventTypes.PUBLISH) {
2922
- const found = this.fetchMessageBroker(event);
3166
+ const found = await this.fetchMessageBroker(event);
2923
3167
  if (!found) {
2924
3168
  throw new Error(`Message Broker ${event} not found`);
2925
3169
  }
@@ -2928,7 +3172,7 @@ class ProductsBuilderService {
2928
3172
  async createJob(data, throwErrorIfExists = false) {
2929
3173
  try {
2930
3174
  // TODO: figure out a way to check if this has run before, halt if it has
2931
- if (!this.fetchJob(data.tag)) {
3175
+ if (!(await this.fetchJob(data.tag))) {
2932
3176
  await validators_1.CreateProductJobSchema.validateAsync(data);
2933
3177
  await this.validateJobEvent(data);
2934
3178
  if (data.type === productsBuilder_types_1.JobEventTypes.ACTION) {
@@ -2950,7 +3194,6 @@ class ProductsBuilderService {
2950
3194
  const dbAction = await this.fetchDatabaseAction(data.event);
2951
3195
  }
2952
3196
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign({}, data), { component: enums_1.ProductComponents.JOB, action: enums_1.RequestAction.CREATE }), this.getUserAccess());
2953
- await this.initializeProduct(this.product_id);
2954
3197
  }
2955
3198
  else {
2956
3199
  if (throwErrorIfExists)
@@ -2963,7 +3206,7 @@ class ProductsBuilderService {
2963
3206
  }
2964
3207
  async updateJob(tag, data) {
2965
3208
  try {
2966
- const job = this.fetchJob(tag);
3209
+ const job = await this.fetchJob(tag);
2967
3210
  if (!job) {
2968
3211
  throw new Error(`Job ${tag} not found`);
2969
3212
  }
@@ -2973,20 +3216,18 @@ class ProductsBuilderService {
2973
3216
  throw new Error(`tag ${tag} is in use`); // TODO: also check on the backend
2974
3217
  }
2975
3218
  await this.productApi.updateProduct(this.product_id, Object.assign(Object.assign(Object.assign({}, job), data), { component: enums_1.ProductComponents.JOB, action: enums_1.RequestAction.UPDATE }), this.getUserAccess());
2976
- await this.initializeProduct(this.product_id);
2977
3219
  }
2978
3220
  catch (e) {
2979
3221
  throw e;
2980
3222
  }
2981
3223
  }
2982
- fetchJob(tag, throwError = false) {
2983
- const job = this.product.jobs.find((data) => data.tag === tag);
2984
- if (!job && throwError)
2985
- throw new Error(`Job ${tag} not found`);
2986
- return job;
3224
+ async fetchJobs() {
3225
+ const components = await this.productApi.fetchProductComponents(this.product_id, 'job', this.getUserAccess());
3226
+ return components;
2987
3227
  }
2988
- fetchJobs() {
2989
- return this.product.jobs;
3228
+ async fetchJob(tag) {
3229
+ const component = await this.productApi.fetchProductComponentByTag(this.product_id, 'job', tag, this.getUserAccess());
3230
+ return component;
2990
3231
  }
2991
3232
  getUserAccess() {
2992
3233
  return {
@@ -2996,6 +3237,9 @@ class ProductsBuilderService {
2996
3237
  public_key: this.public_key,
2997
3238
  };
2998
3239
  }
3240
+ async fetchSessionUser(ductape_user_id) {
3241
+ return await this.productApi.fetchProductSessionUser(ductape_user_id, this.getUserAccess());
3242
+ }
2999
3243
  }
3000
3244
  exports.default = ProductsBuilderService;
3001
3245
  //# sourceMappingURL=products.service.js.map