@izara_project/izara-shared-search-and-sort 1.0.1 → 1.0.2

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.
@@ -0,0 +1,264 @@
1
+ /*
2
+ Copyright (C) 2025 Sven Mason <http://izara.io>
3
+
4
+ This program is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Affero General Public License as
6
+ published by the Free Software Foundation, either version 3 of the
7
+ License, or (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Affero General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Affero General Public License
15
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ "use strict";
19
+
20
+ //* searchResultMain record
21
+ import { objectHash as hash } from '@izara_project/izara-shared-core'; //const hash = require('@izara_project/izara-shared-core').objectHash;
22
+ import { isEmpty as isEmpty } from 'lodash';
23
+ import filtersSharedLib from './FiltersSharedLib'; //const filtersSharedLib = require('./FiltersSharedLib');
24
+ import dataFieldsSharedLib from './DataFieldsSharedLib';
25
+ import searchResultShared from '@izara_project/izara-core-library-search-result';
26
+
27
+
28
+ function createSearchRequest(
29
+ _izContext,
30
+ objType,
31
+ initialLogicalElementId,
32
+ logicalElements,
33
+ requiredDataFields,
34
+ complexFilterCombinations,
35
+ perParentCombinations,
36
+ requiredDataLinkStepObjects,
37
+ requiredDataLinkSteps,
38
+ values
39
+ ) {
40
+ _izContext.logger.debug('createSearchResult: ', {
41
+ objType,
42
+ initialLogicalElementId,
43
+ logicalElements,
44
+ requiredDataFields,
45
+ complexFilterCombinations,
46
+ perParentCombinations,
47
+ requiredDataLinkStepObjects,
48
+ requiredDataLinkSteps,
49
+ values
50
+ });
51
+
52
+ let filterElements = {};
53
+ let status = 'valid'
54
+ let errorsObject = {};
55
+ let errorsFound = [];
56
+
57
+ //* create complexFilter normalize to send backend
58
+ let [
59
+ complexFilterRequest,
60
+ complexFilterStatus,
61
+ complexErrorsObject,
62
+ complexErrorsFound
63
+ ] = filtersSharedLib.createFiltersRequest(
64
+ _izContext,
65
+ objType,
66
+ initialLogicalElementId,
67
+ logicalElements,
68
+ values,
69
+ []
70
+ );
71
+ _izContext.logger.debug('return createComplexFilter: ', {
72
+ complexFilterRequest,
73
+ complexFilterStatus,
74
+ complexErrorsObject,
75
+ complexErrorsFound
76
+ });
77
+
78
+ if (complexErrorsFound.length > 0) {
79
+ errorsFound = errorsFound.concat(complexErrorsFound);
80
+ Object.assign(errorsObject, complexErrorsObject);
81
+ }
82
+
83
+ let requiredData = dataFieldsSharedLib.createRequiredData(
84
+ _izContext,
85
+ objType,
86
+ requiredDataFields,
87
+ complexFilterCombinations,
88
+ perParentCombinations,
89
+ requiredDataLinkStepObjects,
90
+ requiredDataLinkSteps,
91
+ values,
92
+ );
93
+ _izContext.logger.debug('return create requiredData: ', requiredData);
94
+
95
+ if (requiredData.status === 'invalid' || requiredData.errorsFound.length > 0) {
96
+ Object.assign(errorsObject, requiredData.errorsObject)
97
+ errorsFound.push('requiredData cannot create');
98
+ errorsFound = errorsFound.concat(requiredData.errorsFound);
99
+ }
100
+
101
+ if (errorsFound.length > 0) {
102
+ return [null, 'invalid', errorsObject, errorsFound]
103
+ }
104
+
105
+ filterElements = complexFilterRequest.filterElements;
106
+
107
+ if (!isEmpty(requiredData.perParentCombinations)) {
108
+ for (const filterObject of Object.values(requiredData.perParentCombinations)) {
109
+ Object.assign(filterElements, filterObject.filterElements);
110
+ }
111
+ }
112
+
113
+ if (!isEmpty(requiredData.complexFilterCombinations)) {
114
+ for (const filterObject of Object.values(requiredData.complexFilterCombinations)) {
115
+ Object.assign(filterElements, filterObject.filterElements);
116
+ }
117
+ }
118
+
119
+ if (!isEmpty(requiredData.filterElements)) {
120
+ Object.assign(filterElements, requiredData.filterElements);
121
+ }
122
+
123
+ _izContext.logger.debug("--------------------------- start create searchResult request -------------------------------", {
124
+ complexFilterRequest,
125
+ requiredData,
126
+ filterElements
127
+ });
128
+
129
+ let searchResultRequest = {
130
+ objType: objType,
131
+ searchResultId: complexFilterRequest.filterMainId,
132
+ searchDetailId: requiredData.requiredDataHash,
133
+ requiredDataObjects: requiredData.requiredDataObjects,
134
+ searchParams: {
135
+ filterObjectId: complexFilterRequest.filterMainId
136
+ },
137
+ filterElements: filterElements,
138
+ linkPathObjects: requiredData.linkPathObjects,
139
+ linkPathSteps: requiredData.linkPathSteps
140
+ };
141
+ _izContext.logger.debug("searchResultRequest: ", searchResultRequest);
142
+
143
+ return [searchResultRequest, status, errorsObject, errorsFound]
144
+
145
+ }
146
+
147
+ function createSortRequest(
148
+ _izContext,
149
+ objType,
150
+ initialLogicalElementId,
151
+ logicalElements,
152
+ requiredDataFields,
153
+ complexFilterCombinations,
154
+ perParentCombinations,
155
+ requiredDataLinkStepObjects,
156
+ requiredDataLinkSteps,
157
+ sortFields,
158
+ values
159
+ ) {
160
+ _izContext.logger.debug('createSortResult: ', {
161
+ objType,
162
+ initialLogicalElementId,
163
+ logicalElements,
164
+ requiredDataFields,
165
+ complexFilterCombinations,
166
+ perParentCombinations,
167
+ requiredDataLinkStepObjects,
168
+ requiredDataLinkSteps,
169
+ sortFields,
170
+ values
171
+ });
172
+
173
+ let sortFieldMain = []
174
+
175
+ //* set sortField
176
+ for (const sortField of sortFields) {
177
+ // {
178
+ // requiredDataLinkStepObjectId: "userTag_LinkStepObjectC",
179
+ // dataType: "number"
180
+ // },
181
+
182
+ let requiredDataLinkStepObjectId = sortField.requiredDataLinkStepObjectId;
183
+
184
+ let requiredDataLinkObject = requiredDataLinkStepObjects[requiredDataLinkStepObjectId];
185
+ _izContext.logger.debug('requiredDataLinkObject', requiredDataLinkObject);
186
+
187
+ let requiredDataLinkStepsSortField = requiredDataLinkObject.linkSteps;
188
+ _izContext.logger.debug('requiredDataLinkStepsSortField', requiredDataLinkStepsSortField);
189
+
190
+ let lastRequiredDataLinkStepId = requiredDataLinkStepsSortField[requiredDataLinkStepsSortField.length - 1];
191
+ _izContext.logger.debug('lastRequiredDataLinkStepId', lastRequiredDataLinkStepId);
192
+
193
+ let linkStep = requiredDataLinkSteps[lastRequiredDataLinkStepId];
194
+ _izContext.logger.debug('linkStep', linkStep);
195
+
196
+ sortFieldMain.push({
197
+ fieldName: linkStep.fieldName,
198
+ dataType: sortField.dataType
199
+ });
200
+
201
+ }
202
+ _izContext.logger.debug("sortFieldMain: ", sortFieldMain)
203
+
204
+ let [searchResultRequest, status, errorsObject, errorsFound] = createSearchRequest(
205
+ _izContext,
206
+ objType,
207
+ initialLogicalElementId,
208
+ logicalElements,
209
+ requiredDataFields,
210
+ complexFilterCombinations,
211
+ perParentCombinations,
212
+ requiredDataLinkStepObjects,
213
+ requiredDataLinkSteps,
214
+ values
215
+ );
216
+ _izContext.logger.debug('SearchResultRequest: ', {
217
+ searchResultRequest,
218
+ status,
219
+ errorsObject,
220
+ errorsFound
221
+ });
222
+
223
+ if (errorsFound.length > 0) {
224
+ return [null, 'invalid', errorsObject, errorsFound]
225
+ }
226
+
227
+ let searchResultId = searchResultRequest.searchResultId;
228
+ let searchDetailId = searchResultRequest.searchDetailId;
229
+ _izContext.logger.debug('searchResultMain: ', {
230
+ searchResultId,
231
+ searchDetailId
232
+ });
233
+
234
+ let sortResultId = searchResultShared.createSortResultId(
235
+ _izContext,
236
+ searchResultId,
237
+ searchDetailId,
238
+ sortFieldMain
239
+ );
240
+ _izContext.logger.debug('sortResultId: ', sortResultId)
241
+
242
+ let sortRequest = {
243
+ objType: objType,
244
+ searchResultId: searchResultRequest.searchResultId,
245
+ searchDetailId: searchResultRequest.searchDetailId,
246
+ requiredDataObjects: searchResultRequest.requiredDataObjects,
247
+ searchParams: searchResultRequest.searchParams,
248
+ filterElements: searchResultRequest.filterElements,
249
+ linkPathObjects: searchResultRequest.linkPathObjects,
250
+ linkPathSteps: searchResultRequest.linkPathSteps,
251
+ sortFields: sortFieldMain
252
+ };
253
+ _izContext.logger.debug('sortRequest: ', sortRequest);
254
+
255
+ return [sortRequest, status, errorsObject, errorsFound]
256
+
257
+ }
258
+
259
+
260
+ export default {
261
+ //* create request to backend to sort and search
262
+ createSearchRequest,
263
+ createSortRequest
264
+ }