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

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,265 @@
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 lodash from 'lodash';
23
+ const { isEmpty } = lodash;
24
+ import filtersSharedLib from './FiltersSharedLib.js'; //const filtersSharedLib = require('./FiltersSharedLib');
25
+ import dataFieldsSharedLib from './DataFieldsSharedLib.js';
26
+ import searchResultShared from '@izara_project/izara-core-library-search-result';
27
+
28
+
29
+ function createSearchRequest(
30
+ _izContext,
31
+ objType,
32
+ initialLogicalElementId,
33
+ logicalElements,
34
+ requiredDataFields,
35
+ complexFilterCombinations,
36
+ perParentCombinations,
37
+ requiredDataLinkStepObjects,
38
+ requiredDataLinkSteps,
39
+ values
40
+ ) {
41
+ _izContext.logger.debug('createSearchResult: ', {
42
+ objType,
43
+ initialLogicalElementId,
44
+ logicalElements,
45
+ requiredDataFields,
46
+ complexFilterCombinations,
47
+ perParentCombinations,
48
+ requiredDataLinkStepObjects,
49
+ requiredDataLinkSteps,
50
+ values
51
+ });
52
+
53
+ let filterElements = {};
54
+ let status = 'valid'
55
+ let errorsObject = {};
56
+ let errorsFound = [];
57
+
58
+ //* create complexFilter normalize to send backend
59
+ let [
60
+ complexFilterRequest,
61
+ complexFilterStatus,
62
+ complexErrorsObject,
63
+ complexErrorsFound
64
+ ] = filtersSharedLib.createFiltersRequest(
65
+ _izContext,
66
+ objType,
67
+ initialLogicalElementId,
68
+ logicalElements,
69
+ values,
70
+ []
71
+ );
72
+ _izContext.logger.debug('return createComplexFilter: ', {
73
+ complexFilterRequest,
74
+ complexFilterStatus,
75
+ complexErrorsObject,
76
+ complexErrorsFound
77
+ });
78
+
79
+ if (complexErrorsFound.length > 0) {
80
+ errorsFound = errorsFound.concat(complexErrorsFound);
81
+ Object.assign(errorsObject, complexErrorsObject);
82
+ }
83
+
84
+ let requiredData = dataFieldsSharedLib.createRequiredData(
85
+ _izContext,
86
+ objType,
87
+ requiredDataFields,
88
+ complexFilterCombinations,
89
+ perParentCombinations,
90
+ requiredDataLinkStepObjects,
91
+ requiredDataLinkSteps,
92
+ values,
93
+ );
94
+ _izContext.logger.debug('return create requiredData: ', requiredData);
95
+
96
+ if (requiredData.status === 'invalid' || requiredData.errorsFound.length > 0) {
97
+ Object.assign(errorsObject, requiredData.errorsObject)
98
+ errorsFound.push('requiredData cannot create');
99
+ errorsFound = errorsFound.concat(requiredData.errorsFound);
100
+ }
101
+
102
+ if (errorsFound.length > 0) {
103
+ return [null, 'invalid', errorsObject, errorsFound]
104
+ }
105
+
106
+ filterElements = complexFilterRequest.filterElements;
107
+
108
+ if (!isEmpty(requiredData.perParentCombinations)) {
109
+ for (const filterObject of Object.values(requiredData.perParentCombinations)) {
110
+ Object.assign(filterElements, filterObject.filterElements);
111
+ }
112
+ }
113
+
114
+ if (!isEmpty(requiredData.complexFilterCombinations)) {
115
+ for (const filterObject of Object.values(requiredData.complexFilterCombinations)) {
116
+ Object.assign(filterElements, filterObject.filterElements);
117
+ }
118
+ }
119
+
120
+ if (!isEmpty(requiredData.filterElements)) {
121
+ Object.assign(filterElements, requiredData.filterElements);
122
+ }
123
+
124
+ _izContext.logger.debug("--------------------------- start create searchResult request -------------------------------", {
125
+ complexFilterRequest,
126
+ requiredData,
127
+ filterElements
128
+ });
129
+
130
+ let searchResultRequest = {
131
+ objType: objType,
132
+ searchResultId: complexFilterRequest.filterMainId,
133
+ searchDetailId: requiredData.requiredDataHash,
134
+ requiredDataObjects: requiredData.requiredDataObjects,
135
+ searchParams: {
136
+ filterObjectId: complexFilterRequest.filterMainId
137
+ },
138
+ filterElements: filterElements,
139
+ linkPathObjects: requiredData.linkPathObjects,
140
+ linkPathSteps: requiredData.linkPathSteps
141
+ };
142
+ _izContext.logger.debug("searchResultRequest: ", searchResultRequest);
143
+
144
+ return [searchResultRequest, status, errorsObject, errorsFound]
145
+
146
+ }
147
+
148
+ function createSortRequest(
149
+ _izContext,
150
+ objType,
151
+ initialLogicalElementId,
152
+ logicalElements,
153
+ requiredDataFields,
154
+ complexFilterCombinations,
155
+ perParentCombinations,
156
+ requiredDataLinkStepObjects,
157
+ requiredDataLinkSteps,
158
+ sortFields,
159
+ values
160
+ ) {
161
+ _izContext.logger.debug('createSortResult: ', {
162
+ objType,
163
+ initialLogicalElementId,
164
+ logicalElements,
165
+ requiredDataFields,
166
+ complexFilterCombinations,
167
+ perParentCombinations,
168
+ requiredDataLinkStepObjects,
169
+ requiredDataLinkSteps,
170
+ sortFields,
171
+ values
172
+ });
173
+
174
+ let sortFieldMain = []
175
+
176
+ //* set sortField
177
+ for (const sortField of sortFields) {
178
+ // {
179
+ // requiredDataLinkStepObjectId: "userTag_LinkStepObjectC",
180
+ // dataType: "number"
181
+ // },
182
+
183
+ let requiredDataLinkStepObjectId = sortField.requiredDataLinkStepObjectId;
184
+
185
+ let requiredDataLinkObject = requiredDataLinkStepObjects[requiredDataLinkStepObjectId];
186
+ _izContext.logger.debug('requiredDataLinkObject', requiredDataLinkObject);
187
+
188
+ let requiredDataLinkStepsSortField = requiredDataLinkObject.linkSteps;
189
+ _izContext.logger.debug('requiredDataLinkStepsSortField', requiredDataLinkStepsSortField);
190
+
191
+ let lastRequiredDataLinkStepId = requiredDataLinkStepsSortField[requiredDataLinkStepsSortField.length - 1];
192
+ _izContext.logger.debug('lastRequiredDataLinkStepId', lastRequiredDataLinkStepId);
193
+
194
+ let linkStep = requiredDataLinkSteps[lastRequiredDataLinkStepId];
195
+ _izContext.logger.debug('linkStep', linkStep);
196
+
197
+ sortFieldMain.push({
198
+ fieldName: linkStep.fieldName,
199
+ dataType: sortField.dataType
200
+ });
201
+
202
+ }
203
+ _izContext.logger.debug("sortFieldMain: ", sortFieldMain)
204
+
205
+ let [searchResultRequest, status, errorsObject, errorsFound] = createSearchRequest(
206
+ _izContext,
207
+ objType,
208
+ initialLogicalElementId,
209
+ logicalElements,
210
+ requiredDataFields,
211
+ complexFilterCombinations,
212
+ perParentCombinations,
213
+ requiredDataLinkStepObjects,
214
+ requiredDataLinkSteps,
215
+ values
216
+ );
217
+ _izContext.logger.debug('SearchResultRequest: ', {
218
+ searchResultRequest,
219
+ status,
220
+ errorsObject,
221
+ errorsFound
222
+ });
223
+
224
+ if (errorsFound.length > 0) {
225
+ return [null, 'invalid', errorsObject, errorsFound]
226
+ }
227
+
228
+ let searchResultId = searchResultRequest.searchResultId;
229
+ let searchDetailId = searchResultRequest.searchDetailId;
230
+ _izContext.logger.debug('searchResultMain: ', {
231
+ searchResultId,
232
+ searchDetailId
233
+ });
234
+
235
+ let sortResultId = searchResultShared.createSortResultId(
236
+ _izContext,
237
+ searchResultId,
238
+ searchDetailId,
239
+ sortFieldMain
240
+ );
241
+ _izContext.logger.debug('sortResultId: ', sortResultId)
242
+
243
+ let sortRequest = {
244
+ objType: objType,
245
+ searchResultId: searchResultRequest.searchResultId,
246
+ searchDetailId: searchResultRequest.searchDetailId,
247
+ requiredDataObjects: searchResultRequest.requiredDataObjects,
248
+ searchParams: searchResultRequest.searchParams,
249
+ filterElements: searchResultRequest.filterElements,
250
+ linkPathObjects: searchResultRequest.linkPathObjects,
251
+ linkPathSteps: searchResultRequest.linkPathSteps,
252
+ sortFields: sortFieldMain
253
+ };
254
+ _izContext.logger.debug('sortRequest: ', sortRequest);
255
+
256
+ return [sortRequest, status, errorsObject, errorsFound]
257
+
258
+ }
259
+
260
+
261
+ export default {
262
+ //* create request to backend to sort and search
263
+ createSearchRequest,
264
+ createSortRequest
265
+ }