Skip to content

Commit 72b9250

Browse files
committed
Allow configuring list of pools to consider
1 parent ef03f24 commit 72b9250

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ All plugin configuration takes place in base configuration.
2424
* *Path to parent field*: The path to the nested field that contains the ID field and the base fields.
2525
* *ID field name*: The name of the ID field to be filled out by the plugin. This has to be a numeric field. The field will only be updated if it is empty, if the parent field has been newly created and if all base fields have been filled out by the user.
2626
* *Base field names*: The names of the base fields to consider when setting the ID. These can be text fields or fields of the [custom data type DANTE](https://github.com/programmfabrik/fylr-plugin-custom-data-type-dante).
27+
* *IDs of pools to consider*: If not empty, IDs are only updated for resources in the specified pools. Also, resources in other pools are not considered during ID generation.
2728

2829
## Example
2930

l10n/numeric-id-auto-incrementer.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ server.config.parameter.system.numericIdAutoIncrementer.object_types.nested_fiel
66
server.config.parameter.system.numericIdAutoIncrementer.object_types.nested_fields.field_path.label,Pfad zum übergeordneten Feld,Path to parent field
77
server.config.parameter.system.numericIdAutoIncrementer.object_types.nested_fields.id_field_name.label,Name des ID-Feldes,ID field name
88
server.config.parameter.system.numericIdAutoIncrementer.object_types.nested_fields.base_fields.label,Namen der Basisfelder,Base field names
9+
server.config.parameter.system.numericIdAutoIncrementer.object_types.nested_fields.pool_ids.label,IDs der berücksichtigten Pools,IDs of pools to consider

manifest.master.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ base_config:
3434
type: text
3535
position: 0
3636
position: 2
37+
- name: pool_ids
38+
type: table
39+
fields:
40+
- name: pool_id
41+
type: text
42+
position: 0
43+
position: 3
3744
position: 1
3845
position: 0
3946

src/server/setIds.js

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ async function processObject(object, configuration) {
3838
let changed = false;
3939

4040
for (let nestedFieldConfiguration of nestedFieldsConfiguration) {
41+
const poolIds = nestedFieldConfiguration.pool_ids?.map(pool => pool.pool_id);
42+
if (poolIds?.length && !poolIds.includes(object[object._objecttype]._pool.pool._id.toString())) continue;
4143
if (await processNestedFields(object, nestedFieldConfiguration)) changed = true;
4244
}
4345

@@ -65,6 +67,7 @@ async function processNestedFields(object, nestedFieldConfiguration) {
6567
nestedFieldConfiguration.field_path,
6668
nestedFieldConfiguration.id_field_name,
6769
nestedFieldConfiguration.base_fields?.map(field => field.field_name),
70+
nestedFieldConfiguration.pool_ids?.map(pool => pool.pool_id)
6871
)) changed = true;
6972
}
7073

@@ -89,23 +92,23 @@ function getFieldValues(object, fieldPath) {
8992
}
9093
}
9194

92-
async function addId(objectType, nestedFields, nestedField, nestedFieldPath, idFieldName, baseFieldNames) {
95+
async function addId(objectType, nestedFields, nestedField, nestedFieldPath, idFieldName, baseFieldNames, poolIds) {
9396
if (!idFieldName?.length
9497
|| !baseFieldNames
9598
|| baseFieldNames.find(baseFieldName => !nestedField[baseFieldName])
9699
|| nestedField[idFieldName]
97100
|| nestedField._uuid) return false;
98101

99102
nestedField[idFieldName] = await getIdValue(
100-
objectType, nestedFields, nestedField, nestedFieldPath, idFieldName, baseFieldNames
103+
objectType, nestedFields, nestedField, nestedFieldPath, idFieldName, baseFieldNames, poolIds
101104
);
102105

103106
return true;
104107
}
105108

106-
async function getIdValue(objectType, nestedFields, nestedField, nestedFieldPath, idFieldName, baseFieldNames) {
109+
async function getIdValue(objectType, nestedFields, nestedField, nestedFieldPath, idFieldName, baseFieldNames, poolIds) {
107110
const existingIdValues = await findExistingIdValues(
108-
objectType, nestedFields, nestedField, nestedFieldPath, idFieldName, baseFieldNames
111+
objectType, nestedFields, nestedField, nestedFieldPath, idFieldName, baseFieldNames, poolIds
109112
);
110113
existingIdValues.sort((value1, value2) => value1 - value2);
111114

@@ -115,12 +118,12 @@ async function getIdValue(objectType, nestedFields, nestedField, nestedFieldPath
115118
}
116119

117120
async function findExistingIdValues(objectType, nestedFields, nestedField, nestedFieldPath, idFieldName,
118-
baseFieldNames) {
121+
baseFieldNames, poolIds) {
119122
const idValuesInCurrentObject = findExistingIdValuesInNestedFields(
120123
nestedFields, nestedField, idFieldName, baseFieldNames
121124
);
122125
const idValuesInOtherObjects = await findExistingIdValuesInOtherObjects(
123-
objectType, nestedField, nestedFieldPath, idFieldName, baseFieldNames
126+
objectType, nestedField, nestedFieldPath, idFieldName, baseFieldNames, poolIds
124127
);
125128

126129
return idValuesInCurrentObject.concat(idValuesInOtherObjects)
@@ -138,8 +141,8 @@ function findExistingIdValuesInNestedFields(nestedFields, nestedField, idFieldNa
138141
}
139142

140143
async function findExistingIdValuesInOtherObjects(objectType, nestedField, nestedFieldPath, idFieldName,
141-
baseFieldNames) {
142-
const objects = await findOtherObjects(objectType, nestedField, nestedFieldPath, idFieldName, baseFieldNames);
144+
baseFieldNames, poolIds) {
145+
const objects = await findOtherObjects(objectType, nestedField, nestedFieldPath, idFieldName, baseFieldNames, poolIds);
143146

144147
return objects.reduce((result, object) => {
145148
const idValues = findExistingIdValuesInNestedFields(
@@ -149,18 +152,27 @@ async function findExistingIdValuesInOtherObjects(objectType, nestedField, neste
149152
}, []);
150153
}
151154

152-
async function findOtherObjects(objectType, nestedField, nestedFieldPath, idFieldName, baseFieldNames, offset = 0) {
155+
async function findOtherObjects(objectType, nestedField, nestedFieldPath, idFieldName, baseFieldNames, poolIds, offset = 0) {
153156
const url = info.api_url + '/api/v1/search?access_token=' + info.api_user_access_token;
157+
const query = baseFieldNames.map(baseFieldName => {
158+
return {
159+
type: 'match',
160+
bool: 'must',
161+
fields: [getFullFieldPath(objectType, nestedField, nestedFieldPath, baseFieldName)],
162+
string: getBaseFieldValue(nestedField, baseFieldName)
163+
};
164+
});
165+
if (poolIds?.length) {
166+
query.push({
167+
type: 'in',
168+
bool: 'must',
169+
fields: [objectType + '._pool.pool._id'],
170+
in: poolIds
171+
});
172+
}
154173
const chunkSize = 100;
155174
const searchRequest = {
156-
search: baseFieldNames.map(baseFieldName => {
157-
return {
158-
type: 'match',
159-
bool: 'must',
160-
fields: [getFullFieldPath(objectType, nestedField, nestedFieldPath, baseFieldName)],
161-
string: getBaseFieldValue(nestedField, baseFieldName)
162-
};
163-
}),
175+
search: query,
164176
include_fields: [objectType + '.' + nestedFieldPath + '.' + idFieldName].concat(
165177
baseFieldNames.map(baseFieldName => getFullFieldPath(objectType, nestedField, nestedFieldPath, baseFieldName))
166178
),

0 commit comments

Comments
 (0)