@gudhub/core 1.0.37

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 (67) hide show
  1. package/GUDHUB/Auth/Auth.js +137 -0
  2. package/GUDHUB/Auth/Auth.test.js +15 -0
  3. package/GUDHUB/Managers/ChunksManager/ChunksManager.js +80 -0
  4. package/GUDHUB/Managers/ChunksManager/ChunksManager.test.js +46 -0
  5. package/GUDHUB/Managers/DocumentManager/DocumentManager.js +48 -0
  6. package/GUDHUB/Managers/FileManager/FileManager.js +208 -0
  7. package/GUDHUB/Managers/FileManager/file_manager.test.js +17 -0
  8. package/GUDHUB/Managers/managers.js +5 -0
  9. package/GUDHUB/PipeService/PipeService.js +167 -0
  10. package/GUDHUB/PipeService/PipeService.test.js +64 -0
  11. package/GUDHUB/PipeService/utils.js +32 -0
  12. package/GUDHUB/Processors/AppProcessor/AppProcessor.js +433 -0
  13. package/GUDHUB/Processors/AppProcessor/AppProcessor.test.js +38 -0
  14. package/GUDHUB/Processors/AppProcessor/AppProcessorMocks.js +83 -0
  15. package/GUDHUB/Processors/FieldProcessor/FieldProcessor.js +139 -0
  16. package/GUDHUB/Processors/FieldProcessor/FieldProcessor.test.js +16 -0
  17. package/GUDHUB/Processors/FieldProcessor/Untitled-1.json +17 -0
  18. package/GUDHUB/Processors/FieldProcessor/field_processor.md +85 -0
  19. package/GUDHUB/Processors/ItemProcessor/ItemProcessor.js +247 -0
  20. package/GUDHUB/Processors/ItemProcessor/item_processor.md +50 -0
  21. package/GUDHUB/Processors/processors.js +5 -0
  22. package/GUDHUB/Storage/Storage.js +115 -0
  23. package/GUDHUB/Storage/Storage.test.js +112 -0
  24. package/GUDHUB/Utils/Utils.js +153 -0
  25. package/GUDHUB/Utils/compare_items_lists_worker/compare_items_lists.worker.js +50 -0
  26. package/GUDHUB/Utils/filter/filter.js +308 -0
  27. package/GUDHUB/Utils/filter/filter.test.js +94 -0
  28. package/GUDHUB/Utils/filter/filterPreparation.js +103 -0
  29. package/GUDHUB/Utils/filter/group.js +41 -0
  30. package/GUDHUB/Utils/filter/utils.js +119 -0
  31. package/GUDHUB/Utils/get_date/get_date.js +90 -0
  32. package/GUDHUB/Utils/get_date/get_date.test.js +53 -0
  33. package/GUDHUB/Utils/interpretation/interpretation.js +100 -0
  34. package/GUDHUB/Utils/json_constructor/json_constructor.js +130 -0
  35. package/GUDHUB/Utils/json_constructor/json_constructor.test.js +70 -0
  36. package/GUDHUB/Utils/json_to_items/json_to_items.js +161 -0
  37. package/GUDHUB/Utils/json_to_items/json_to_items.test.js +85 -0
  38. package/GUDHUB/Utils/merge_chunks/merge_chunks.js +30 -0
  39. package/GUDHUB/Utils/merge_compare_items/merge_compare_items.js +316 -0
  40. package/GUDHUB/Utils/merge_compare_items/merge_compare_items.test.js +632 -0
  41. package/GUDHUB/Utils/merge_objects/merge_objects.js +120 -0
  42. package/GUDHUB/Utils/merge_objects/merge_objects.test.js +137 -0
  43. package/GUDHUB/Utils/nested_list/nested_list.js +38 -0
  44. package/GUDHUB/Utils/nested_list/nested_list.test.js +70 -0
  45. package/GUDHUB/Utils/populate_items/populate_items.js +66 -0
  46. package/GUDHUB/WebSocket/WebSocket.js +136 -0
  47. package/GUDHUB/config.js +8 -0
  48. package/GUDHUB/consts.js +1 -0
  49. package/GUDHUB/gudhub-https-service.js +193 -0
  50. package/GUDHUB/gudhub.js +477 -0
  51. package/GUDHUB/gudhub.test.js +218 -0
  52. package/GUDHUB/utils.js +25 -0
  53. package/Readme.md +537 -0
  54. package/fake_server/fake_java_server.js +112 -0
  55. package/fake_server/fake_server_data/app_132.js +3037 -0
  56. package/fake_server/fake_server_data/app_16259.js +1152 -0
  57. package/fake_server/fake_server_data/app_214.js +2057 -0
  58. package/fake_server/fake_server_data/app_8263.js +4167 -0
  59. package/fake_server/fake_server_data/chunks_mocks/bcacjbgqorherqmtykrj_8263.js +44561 -0
  60. package/fake_server/fake_server_data/chunks_mocks/bcjyuoqaewybudfyhir_8263.js +44197 -0
  61. package/fake_server/fake_server_data/chunks_mocks/tpajkpolmzklodcba_8263.js +44977 -0
  62. package/fake_server/fake_server_data/fake_server_data.js +111 -0
  63. package/index.js +3 -0
  64. package/indexUMD.js +5 -0
  65. package/package.json +45 -0
  66. package/umd/library.min.js +186 -0
  67. package/umd/library.min.js.map +1 -0
@@ -0,0 +1,90 @@
1
+ /*
2
+ |====================================== GETTING DATE ================================|
3
+ |=========================== getDate (queryKey) ======================|
4
+ |====================================================================================|
5
+ | Arguments:
6
+ | - queryKey - according to 'queryKey' we decide with date should be returned to user
7
+ |
8
+ | Return:
9
+ | - date in miliseconds
10
+ |
11
+ |==================================================================================|
12
+ */
13
+ import add from "date-fns/add/index.js";
14
+ import setDay from "date-fns/setDay/index.js";
15
+
16
+ //********************** POPULATE WITH DATE ************************//
17
+ export function populateWithDate(items, model) {
18
+ items.forEach((item) => {
19
+ model.forEach((modelField) => {
20
+ let field = item.fields.find(
21
+ (sourceField) => sourceField.element_id == modelField.element_id
22
+ );
23
+ if (field) {
24
+ field.field_value = getDate(modelField.date_type);
25
+ } else {
26
+ //-- if we didn't find field in item then we add it
27
+ item.fields.push({
28
+ field_id: modelField.element_id,
29
+ element_id: modelField.element_id,
30
+ field_value: getDate(modelField.date_type),
31
+ });
32
+ }
33
+ });
34
+ });
35
+
36
+ return items;
37
+ }
38
+
39
+ //********************** GET DATE ************************//
40
+ export function getDate(queryKey) {
41
+ const date = new Date();
42
+ switch (queryKey) {
43
+
44
+ case "next_day":
45
+ return add(date, {days: 1}).getTime();
46
+
47
+ case "two_days_after":
48
+ return add(date, {days: 2}).getTime();
49
+
50
+ case "three_days_after":
51
+ return add(date, {days: 3}).getTime();
52
+
53
+ case "four_days_after":
54
+ return add(date, {days: 4}).getTime();
55
+
56
+ case "next_week":
57
+ return add(date, {weeks: 1}).getTime();
58
+
59
+ case "two_weeks_after":
60
+ return add(date, {weeks: 2}).getTime();
61
+
62
+ case "three_weeks_after":
63
+ return add(date, {weeks: 3}).getTime();
64
+
65
+ case "this_sunday":
66
+ return setDay(date, 0, {weekStartsOn: 1});
67
+
68
+ case "this_monday":
69
+ return setDay(date, 1, {weekStartsOn: 1});
70
+
71
+ case "this_tuesday":
72
+ return setDay(date, 2, {weekStartsOn: 1});
73
+
74
+ case "this_wednesday":
75
+ return setDay(date, 3, {weekStartsOn: 1});
76
+
77
+ case "this_thursday":
78
+ return setDay(date, 4, {weekStartsOn: 1});
79
+
80
+ case "this_friday":
81
+ return setDay(date, 5, {weekStartsOn: 1});
82
+
83
+ case "this_saturday":
84
+ return setDay(date, 6, {weekStartsOn: 1});
85
+
86
+ case "now":
87
+ default:
88
+ return date.getTime();
89
+ }
90
+ }
@@ -0,0 +1,53 @@
1
+ import should from "should";
2
+ import {GudHub} from '../../gudhub.js';
3
+
4
+ describe("GET DATE", function () {
5
+ const gudhub = new GudHub();
6
+
7
+
8
+ it("NOW : should return current time", function () {
9
+ const timeDiff = Math.round( (new Date().getTime())/60 - gudhub.getDate('now')/60 );
10
+ timeDiff.should.equal(0);
11
+ });
12
+
13
+
14
+ it("NEXT DAY : should return current day + 24hours", function () {
15
+ const timeDiff = Math.round( (gudhub.getDate('next_day') - (new Date().getTime()))/3600000 );
16
+ timeDiff.should.equal(24);
17
+ });
18
+
19
+ it("THIS SUNDAY : should return sunday", function () {
20
+ let day = gudhub.getDate('this_sunday');
21
+ day.getDay().should.equal(0);
22
+ });
23
+
24
+ it("THIS MONDAY : should return monday", function () {
25
+ let day = gudhub.getDate('this_monday');
26
+ day.getDay().should.equal(1);
27
+ });
28
+
29
+ it("THIS Tuesday : should return Tuesday", function () {
30
+ let day = gudhub.getDate('this_tuesday');
31
+ day.getDay().should.equal(2);
32
+ });
33
+
34
+ it("THIS Wednesday : should return wednesday", function () {
35
+ let day = gudhub.getDate('this_wednesday');
36
+ day.getDay().should.equal(3);
37
+ });
38
+
39
+ it("THIS Thursday : should return thursday", function () {
40
+ let day = gudhub.getDate('this_thursday');
41
+ day.getDay().should.equal(4);
42
+ });
43
+
44
+ it("THIS Friday : should return friday", function () {
45
+ let day = gudhub.getDate('this_friday');
46
+ day.getDay().should.equal(5);
47
+ });
48
+
49
+ it("THIS Saturday : should return saturday", function () {
50
+ let day = gudhub.getDate('this_saturday');
51
+ day.getDay().should.equal(6);
52
+ });
53
+ });
@@ -0,0 +1,100 @@
1
+ export async function getInterpretedValue(gudhub, app_id, item_id, field_id) {
2
+ const fieldModel = await gudhub.getField(app_id, field_id);
3
+ const fieldValue = await gudhub.getFieldValue(app_id, item_id, field_id);
4
+
5
+ if (!fieldModel || !fieldModel.data_type || !fieldValue) return "";
6
+
7
+ const fieldsValueArray = fieldValue.split(",");
8
+
9
+ switch (fieldModel.data_type) {
10
+ case "radio_button":
11
+ case "text_opt":
12
+ case "tag":
13
+ const fieldNames = fieldsValueArray
14
+ .map((value) => {
15
+ const option = fieldModel.data_model.options.find(
16
+ (modelOption) => modelOption.value == value
17
+ );
18
+ return option ? option.name : "";
19
+ })
20
+ .join(" ");
21
+ return fieldNames;
22
+
23
+ case 'file':
24
+ case "image":
25
+ let fileUrl = '';
26
+ if(fieldModel.data_model.display_mode == 'multiple') {
27
+ await gudhub.getFiles(app_id, fieldsValueArray).then(
28
+ files =>
29
+ {
30
+ files.forEach(file => {
31
+ return fileUrl += file.url + ',';
32
+ });
33
+ },
34
+ err => {return 'no img'}
35
+ )
36
+ fileUrl = fileUrl.slice(0,-1)
37
+ }
38
+ else {
39
+ fileUrl = await gudhub.getFile(app_id, fieldValue).then(
40
+ file => {
41
+ return file.url;
42
+ },
43
+ err => {return 'no img'}
44
+ )
45
+ }
46
+ return fileUrl;
47
+
48
+ case "text_editor":
49
+ case 'tinymse':
50
+ case 'code_editor':
51
+ case 'grapes_html_editor':
52
+ let htmlUrl = await gudhub.getFile(app_id, fieldValue).then(
53
+ file => {
54
+ return file.url + '?timestamp=' + file.last_update;
55
+ },
56
+ err => {return 'no text'}
57
+ )
58
+ return htmlUrl;
59
+
60
+ case "item_ref":
61
+ const interpretedValue = [];
62
+ for (const field of fieldsValueArray) {
63
+ for (const ref of fieldModel.data_model.refs) {
64
+ const [appId, itemId] = field.split(".");
65
+ const value = await this.getInterpretedValue(
66
+ appId,
67
+ itemId,
68
+ ref.field_id
69
+ );
70
+ interpretedValue.push(value);
71
+ }
72
+ }
73
+ return interpretedValue.join(", ");
74
+
75
+ case "data_ref":
76
+ for (const caseItem of fieldModel.data_model.cases) {
77
+ const [appId, itemId] = fieldValue.split(".");
78
+ if (caseItem.app_id == appId) {
79
+ const value = await this.getInterpretedValue(
80
+ appId,
81
+ itemId,
82
+ caseItem.field_id
83
+ );
84
+ return value;
85
+ }
86
+ }
87
+ return "";
88
+ case "field":
89
+ const fieldsName = [];
90
+ for (const value of fieldsValueArray) {
91
+ const model = await gudhub.getField(app_id, value);
92
+ if (model.field_name != undefined) {
93
+ fieldsName.push(model.field_name);
94
+ }
95
+ }
96
+ return fieldsName.join(" ");
97
+ default:
98
+ return fieldValue;
99
+ }
100
+ }
@@ -0,0 +1,130 @@
1
+
2
+ export function compiler(scheme, item, util, variables) {
3
+ async function schemeCompiler(scheme, item, app) {
4
+ let value = null;
5
+ if (scheme.type === "array") {
6
+ // get properties value for array childs for each application item
7
+ if (Number(scheme.is_static)) {
8
+ value = new Array(
9
+ await getChildsPropertiesObject(scheme.childs, item, app)
10
+ );
11
+ } else {
12
+ value = await getArrayOfItemsWithProperties(scheme, item, app);
13
+ }
14
+ }
15
+
16
+ if (scheme.type === "object") {
17
+ // get properties value for object childs
18
+ if (Number(scheme.current_item)) {
19
+ value = await getItemWithProperties(scheme, item);
20
+ } else {
21
+ value = await getChildsPropertiesObject(scheme.childs, item, app);
22
+ }
23
+ }
24
+ if (scheme.type === "property") {
25
+ // get properties value
26
+ value = await getFieldValue(scheme, item, app);
27
+ }
28
+
29
+ return { [scheme.property_name]: value };
30
+ }
31
+
32
+ /* Get properties value for array childs in scheme, for each application filtered item. */
33
+ async function getArrayOfItemsWithProperties(scheme, item, recievedApp) {
34
+ const app = await util.gudhub.getApp(Number(scheme.app_id));
35
+ const filteredItems = await filterItems(
36
+ scheme.filter,
37
+ app.items_list,
38
+ recievedApp,
39
+ item
40
+ );
41
+
42
+ const arrayOfItemsWithProperties = filteredItems.map(async (item) => {
43
+ return getChildsPropertiesObject(scheme.childs, item, app);
44
+ });
45
+ return Promise.all(arrayOfItemsWithProperties);
46
+ }
47
+
48
+ async function getItemWithProperties(scheme, item) {
49
+ const app = await util.gudhub.getApp(scheme.app_id);
50
+ const items = app.items_list || [];
51
+
52
+ return getChildsPropertiesObject(scheme.childs, item || items[0], app);
53
+ }
54
+
55
+ /* Get properties value for object childs in scheme. */
56
+ async function getChildsPropertiesObject(properties, item, app) {
57
+ const propertiesArray = await properties.map(async (child) => {
58
+ return schemeCompiler(child, item, app);
59
+ });
60
+ const resolvedPropertiesArray = await Promise.all(propertiesArray);
61
+ return resolvedPropertiesArray.reduce((acc, object) => {
62
+ return { ...acc, ...object };
63
+ }, {});
64
+ }
65
+
66
+ /* Get property value based on interpretation value */
67
+ async function getFieldValue(scheme, item, app) {
68
+ switch (scheme.property_type) {
69
+ case "static":
70
+ return scheme.static_field_value;
71
+ case "variable":
72
+ switch (scheme.variable_type) {
73
+ case "app_id":
74
+ return app.app_id;
75
+ // case "user_id":
76
+ // resolve(storage.getUser().user_id);
77
+ // break;
78
+ case "current_item":
79
+ default:
80
+ return `${app.app_id}.${item.item_id}`;
81
+ }
82
+
83
+ case "field_id":
84
+ return scheme.field_id;
85
+
86
+ case "field_value":
87
+ default:
88
+ if (Boolean(Number(scheme.interpretation))) {
89
+ return util.gudhub.getInterpretedValue(
90
+ Number(app.app_id),
91
+ Number(item.item_id),
92
+ Number(scheme.field_id)
93
+ );
94
+ } else {
95
+ return util.gudhub.getFieldValue(
96
+ Number(app.app_id),
97
+ Number(item.item_id),
98
+ Number(scheme.field_id)
99
+ );
100
+ }
101
+ }
102
+ }
103
+
104
+ async function getFilteredItems(filtersList, app_id, item_id, itemsList) {
105
+ const modified_filters_list = await util.gudhub.prefilter(
106
+ filtersList,
107
+ app_id,
108
+ item_id,
109
+ variables
110
+ );
111
+ return util.gudhub.filter(itemsList, [...modified_filters_list, ...filtersList]);
112
+ }
113
+
114
+ /* Filter items by scheme filter */
115
+ async function filterItems(
116
+ filtersList = [],
117
+ itemsList = [],
118
+ app = {},
119
+ item = {}
120
+ ) {
121
+ return filtersList.length ? getFilteredItems(
122
+ filtersList,
123
+ app.app_id,
124
+ item.item_id,
125
+ itemsList
126
+ ) : [...itemsList ];
127
+ }
128
+
129
+ return schemeCompiler(scheme, item);
130
+ }
@@ -0,0 +1,70 @@
1
+ import should from "should";
2
+ import { GudHub } from "./../../gudhub.js";
3
+
4
+ describe("JSON CONSTRUCTOR", function () {
5
+ const gudhub = new GudHub();
6
+
7
+ it("JSON FROM ONE APP : Here we construct simple json from one App", async function () {
8
+ //-- checking if json was generated properly
9
+ let json = await gudhub.jsonConstructor(fishtank_scheme);
10
+ json.fishtank[0].should.have.property("name", "Big Fish Tank");
11
+ json.fishtank[0].should.have.property("state", "Normal");
12
+ });
13
+ it("CHECK INTERPRETATION VALUE FOR FILES: output: we should get url's", async function () {
14
+ //-- checking interpretation mode
15
+ let json = await gudhub.jsonConstructor(fishtank_scheme);
16
+ json.fishtank[0].should.have.property("image","https://gudhub.com/userdata/16259/809199.jpg,https://gudhub.com/userdata/16259/811768.png");
17
+ json.fishtank[0].should.have.property("url","https://google.com,https://test.com");
18
+ json.fishtank[0].should.have.property("notes","https://gudhub.com/userdata/16259/811770.html?timestamp=0");
19
+ });
20
+ });
21
+
22
+ var fishtank_scheme = {
23
+ type: "array",
24
+ id: 1,
25
+ childs: [
26
+ {
27
+ type: "property",
28
+ id: 5,
29
+ property_name: "name",
30
+ property_type: "field_value",
31
+ field_id: "254056",
32
+ interpretation: 1,
33
+ },
34
+ {
35
+ type: "property",
36
+ id: 6,
37
+ property_name: "state",
38
+ property_type: "field_value",
39
+ field_id: "270607",
40
+ interpretation: 1,
41
+ },
42
+ {
43
+ type: "property",
44
+ id: 7,
45
+ property_name: "image",
46
+ property_type: "field_value",
47
+ field_id: "551380",
48
+ interpretation: 1,
49
+ },
50
+ {
51
+ type: "property",
52
+ id: 3,
53
+ property_name: "url",
54
+ property_type: "field_value",
55
+ field_id: "551381",
56
+ interpretation: 1,
57
+ },
58
+ {
59
+ type: "property",
60
+ id: 4,
61
+ property_name: "notes",
62
+ property_type: "field_value",
63
+ field_id: "551382",
64
+ interpretation: 1,
65
+ },
66
+ ],
67
+ property_name: "fishtank",
68
+ app_id: "16259",
69
+ filter: [],
70
+ };
@@ -0,0 +1,161 @@
1
+ import jsonpath from 'jsonpath';
2
+
3
+ /*
4
+ |==================================== JSON TO ITEMS ==================================|
5
+ |====================== jsonToItems (json, fieldsMap) =================|
6
+ |=====================================================================================|
7
+ | Arguments:
8
+ | - json - is an object that is going to be converted to items
9
+ |
10
+ |
11
+ | - fieldsMap - this map is an object array that contains field_id and jsonPath to get data from
12
+ | [
13
+ | {
14
+ | field_id: 431,
15
+ | json_path : "$..item_id"
16
+ | }
17
+ | ]
18
+ |
19
+ |
20
+ | - Returns items_list:
21
+ | [
22
+ | fields: {
23
+ | field_id: 12356,
24
+ | field_value: "test value"
25
+ | }
26
+ | ]
27
+ |
28
+ |=====================================================================================|
29
+ */
30
+
31
+ export function jsonToItems(json, fieldsMap){
32
+ let proccessedMap = [];
33
+ let itemsPath = [];
34
+
35
+ //-- Step 1 --//
36
+ // iterating through map and generating list of paths for each field_id
37
+ fieldsMap.forEach( fieldMap =>{
38
+ let json_paths = [];
39
+ try {
40
+ json_paths = jsonpath.paths(json, fieldMap.json_path)
41
+ proccessedMap.push({
42
+ field_id: fieldMap.field_id,
43
+ json_paths: json_paths,
44
+ json_paths_length: json_paths.length,
45
+ });
46
+ } catch (err){
47
+ console.log(err)
48
+ }
49
+
50
+ });
51
+
52
+ //-- Step 2 --//
53
+ // we are sorrting by json_paths_length to find a field with biggest number of json_paths
54
+ // we are going to use the first element of the proccessedMap array as a main iterator
55
+ proccessedMap.sort((b, a) => {
56
+ return a.json_paths_length - b.json_paths_length;
57
+ });
58
+
59
+
60
+ //-- Step 3 --//
61
+ // creating items array based on proccessedMap
62
+ let items = getPathForItems(proccessedMap).map(itemPath => {
63
+ return { fields :
64
+ itemPath.fields.map(field => {
65
+ return {
66
+ field_id : field.field_id,
67
+ field_value : jsonpath.value(json, field.json_path)
68
+ }
69
+ })
70
+ }
71
+ });
72
+
73
+
74
+ return items;
75
+ }
76
+
77
+
78
+
79
+
80
+
81
+
82
+
83
+ //**************** BEST MATCH ****************//
84
+ // it looks for jsonPath in the pathsToCompare that is mutching the best
85
+ function bestMatch(targetPath, pathsToCompare){
86
+ let matchedPaths = [];
87
+
88
+
89
+ //-- Creating matchedPaths for compearing targetPath with pathsToCompare
90
+ pathsToCompare.forEach(path =>{
91
+ matchedPaths.push({
92
+ path_to_compare : path,
93
+ match_coefficient : 0
94
+ })
95
+ })
96
+
97
+
98
+ //-- Assigning match_coefficient to each path from matchedPaths arrey to determine which path match the best
99
+ // Bigger coefficient means better munch
100
+ targetPath.forEach( (trgElem, key) => {
101
+
102
+ for(var i=0; i<matchedPaths.length; i++){
103
+ if(matchedPaths[i].path_to_compare[key] == trgElem){
104
+ matchedPaths[i].match_coefficient += 1/(key+1);
105
+ }
106
+ }
107
+ });
108
+
109
+
110
+ //-- Sorting matchedPaths by match_coefficient in order to return puth that match the best
111
+ matchedPaths.sort((b, a) => {
112
+ return a.match_coefficient - b.match_coefficient;
113
+ });
114
+
115
+
116
+ //-- Returning first element of matchedPaths[] with biggest match_coefficient with is a best match result
117
+ return matchedPaths[0].path_to_compare;
118
+ }
119
+
120
+
121
+
122
+
123
+
124
+
125
+
126
+
127
+
128
+ //*************** GET PATH FOR ITEMS ****************//
129
+ /* Here we create array of items those are contains jsonePaths
130
+ /* at the next step we are going to create field_value from json_path
131
+ | [
132
+ | fields:
133
+ | {
134
+ | field_id : 123,
135
+ | json_path : $..test_path
136
+ | }
137
+ | ]
138
+ |*/
139
+ function getPathForItems( proccessedMap ){
140
+ let itemsPath = [];
141
+
142
+ proccessedMap.length && proccessedMap[0].json_paths.forEach( jsonPath =>{
143
+ let itemPath = {
144
+ fields : []
145
+ }
146
+
147
+ proccessedMap.forEach(pMap => {
148
+ itemPath.fields.push({
149
+ field_id : pMap.field_id,
150
+ json_path : bestMatch(jsonPath, pMap.json_paths),
151
+ })
152
+
153
+ });
154
+ itemsPath.push(itemPath);
155
+ });
156
+
157
+ return itemsPath;
158
+ }
159
+
160
+
161
+
@@ -0,0 +1,85 @@
1
+ import should from "should";
2
+ import { jsonToItems } from "./json_to_items.js";
3
+
4
+ describe("JSON TO ITEMS", function () {
5
+ it("Creats items list from jsone based on map", function () {
6
+ let items = jsonToItems(json, fieldsMap);
7
+ items.length.should.equal(5);
8
+
9
+ items[0].fields.forEach((field) => {
10
+ if (field.field_id == 431) field.should.have.property("field_value", 1);
11
+ if (field.field_id == 421) field.should.have.property("field_value", 111);
12
+ });
13
+
14
+ items[3].fields.forEach((field) => {
15
+ if (field.field_id == 431) field.should.have.property("field_value", 2);
16
+ if (field.field_id == 421) field.should.have.property("field_value", 222);
17
+ });
18
+ });
19
+ });
20
+
21
+ //----------------------------------- MOCKS ---------------------------------//
22
+
23
+ var fieldsMap = [
24
+ {
25
+ field_id: 431,
26
+ json_path: "$..item_id",
27
+ },
28
+ {
29
+ field_id: 432,
30
+ json_path: "$..tax",
31
+ },
32
+ {
33
+ field_id: 421,
34
+ json_path: "$..quote_id",
35
+ },
36
+ {
37
+ field_id: 422,
38
+ json_path: "$..quote_date",
39
+ },
40
+ ];
41
+
42
+ var json = [
43
+ {
44
+ quote_id: 111,
45
+ quote_date: "11/21/2020",
46
+ quote_items: [
47
+ {
48
+ item_id: 1,
49
+ quanity: 1,
50
+ tax: 1.1,
51
+ price: 15,
52
+ },
53
+ {
54
+ item_id: 2,
55
+ quanity: 2,
56
+ tax: 1.1,
57
+ price: 10,
58
+ },
59
+ ],
60
+ },
61
+ {
62
+ quote_id: 222,
63
+ quote_date: "12/22/2020",
64
+ quote_items: [
65
+ {
66
+ item_id: 1,
67
+ quanity: 3,
68
+ tax: 2.2,
69
+ price: 15,
70
+ },
71
+ {
72
+ item_id: 2,
73
+ tax: 2.2,
74
+ quanity: 1,
75
+ price: 10,
76
+ },
77
+ {
78
+ tax: 2.2,
79
+ item_id: 3,
80
+ quanity: 4,
81
+ price: 11,
82
+ },
83
+ ],
84
+ },
85
+ ];