@ductape/sdk 0.0.4-v5 → 0.0.4-v7

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.
@@ -113,7 +113,7 @@ export default class ProductsBuilderService implements IProductsBuilderService {
113
113
  private createNewProduct;
114
114
  checkIfProductExists(name: string): Promise<false | IProduct>;
115
115
  fetchProduct(): IProduct;
116
- createSession(data: IProductSession): Promise<void>;
116
+ createSession(data: Partial<IProductSession>, throwErrorIfExists?: boolean): Promise<void>;
117
117
  updateSession(tag: string, data: Partial<IProductSession>): Promise<void>;
118
118
  fetchSession(tag: string): IProductSession | null;
119
119
  fetchSessions(): Array<IProductSession>;
@@ -192,19 +192,46 @@ class ProductsBuilderService {
192
192
  }
193
193
  return this.product;
194
194
  }
195
- async createSession(data) {
195
+ async createSession(data, throwErrorIfExists = false) {
196
196
  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({
197
+ await validators_1.CreateProductSessionSchema.validateAsync(data);
198
+ if (!data.tag) {
199
+ throw new Error('tag field is required');
200
+ }
201
+ const exists = this.fetchSession(data.tag);
202
+ if (!exists) {
203
+ if (!data.selector.startsWith('$Schema{')) {
204
+ throw new Error('Selector should be in the format $Schema{...}{key}');
205
+ }
206
+ const stages = (0, string_utils_1.extractStages)(data.selector);
207
+ let current = data.schema;
208
+ for (const stage of stages) {
209
+ if (current && typeof current === 'object' && stage in current) {
210
+ current = current[stage];
211
+ }
212
+ else {
213
+ throw new Error(`${data.selector} not found in event sample`);
214
+ }
215
+ }
216
+ if (current === null ||
217
+ typeof current === "undefined" ||
218
+ typeof current === "object") {
219
+ throw new Error("Selector value is not allowed to be an object|array|null|undefined");
220
+ }
221
+ data.schema_data = await this.inputsService.parseJson({
201
222
  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());
223
+ expected: inputs_types_1.ExpectedValues.PARSESAMPLE
224
+ });
225
+ data.selectorValue = current;
226
+ delete data.tag;
227
+ const payload = Object.assign(Object.assign({}, data), { component: enums_1.ProductComponents.SESSION, action: enums_1.RequestAction.CREATE });
228
+ await this.productApi.updateProduct(this.product_id, payload, this.getUserAccess());
206
229
  await this.initializeProduct(this.product_id);
207
230
  }
231
+ else {
232
+ if (throwErrorIfExists)
233
+ throw new Error(`Session ${data.tag} already exists`);
234
+ }
208
235
  }
209
236
  catch (e) {
210
237
  throw e;
@@ -212,13 +239,13 @@ class ProductsBuilderService {
212
239
  }
213
240
  async updateSession(tag, data) {
214
241
  try {
215
- const auth = this.fetchSession(tag);
216
- if (!auth) {
217
- throw new Error(`Session ${tag} not found`);
242
+ const session = this.fetchSession(tag);
243
+ if (!session) {
244
+ throw new Error(`Session with tag: ${tag} not found`);
218
245
  }
219
246
  //const { _id } = auth;
220
247
  await validators_1.UpdateProductSessionSchema.validateAsync(data); // Change to update;
221
- if (data.tag && this.fetchEnv(data.tag) && data.tag !== tag) {
248
+ if (data.tag && this.fetchSession(data.tag) && data.tag !== tag) {
222
249
  throw new Error(`tag ${data.tag} is in use`); // TODO: also check on the backend
223
250
  }
224
251
  if (!data.tag) {
@@ -230,8 +257,28 @@ class ProductsBuilderService {
230
257
  expected: inputs_types_1.ExpectedValues.PARSESAMPLE,
231
258
  category: enums_1.Categories.DATA,
232
259
  }));
260
+ if (!data.selector) {
261
+ throw new Error('Selector is expected when updating schema');
262
+ }
263
+ }
264
+ if (data.selector) {
265
+ const stages = (0, string_utils_1.extractStages)(data.selector);
266
+ let current = data.schema;
267
+ for (const stage of stages) {
268
+ if (current && typeof current === 'object' && stage in current) {
269
+ current = current[stage];
270
+ }
271
+ else {
272
+ throw new Error(`${data.selector} not found in event sample`);
273
+ }
274
+ }
275
+ if (current === null ||
276
+ typeof current === "undefined" ||
277
+ typeof current === "object") {
278
+ throw new Error("Selector value is not allowed to be an object|array|null|undefined");
279
+ }
233
280
  }
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());
281
+ 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());
235
282
  await this.initializeProduct(this.product_id);
236
283
  }
237
284
  catch (e) {