Changeset View
Changeset View
Standalone View
Standalone View
lib/app/base-chips/field-types/derived-value.js
const locreq = require("locreq")(__dirname); | |||||
const Errors = locreq("lib/response/error.js"); | |||||
const Promise = require("bluebird"); | const Promise = require("bluebird"); | ||||
module.exports = app => ({ | module.exports = app => ({ | ||||
name: "derived-value", | name: "derived-value", | ||||
get_description: () => | get_description: () => | ||||
"A value derived from an array of fields of the given collection.", | "A value derived from an array of fields of the given collection.", | ||||
get_default_value: async () => null, | get_default_value: async () => null, | ||||
is_proper_value: function(context, params, new_value) { | is_proper_value(context, params, new_value) { | ||||
return this._call_base_method( | return this._call_base_method( | ||||
"is_proper_value", | "is_proper_value", | ||||
context, | context, | ||||
params, | params, | ||||
new_value | new_value | ||||
); | ); | ||||
}, | }, | ||||
filter_to_query: async function(context, params, field_filter) { | async filter_to_query(context, params, field_filter) { | ||||
return this._call_base_method( | return this._call_base_method( | ||||
"filter_to_query", | "filter_to_query", | ||||
context, | context, | ||||
params, | params, | ||||
field_filter | field_filter | ||||
); | ); | ||||
}, | }, | ||||
init: function(collection, field_name, params) { | init(collection, field_name, params) { | ||||
const { fields, derived_fn, base_field_type } = params; | const { fields, derived_fn } = params; | ||||
if (typeof derived_fn !== "function") { | if (typeof derived_fn !== "function") { | ||||
throw new Error( | throw new Error( | ||||
`'derived_fn' param in ${field_name} derived-value field is not a function.` | `'derived_fn' param in ${field_name} derived-value field is not a function.` | ||||
); | ); | ||||
} | } | ||||
if (!Array.isArray(fields)) { | if (!Array.isArray(fields)) { | ||||
throw new Error( | throw new Error( | ||||
Show All 16 Lines | init(collection, field_name, params) { | ||||
} | } | ||||
app.addHook( | app.addHook( | ||||
new app.Sealious.EventMatchers.Collection({ | new app.Sealious.EventMatchers.Collection({ | ||||
when: "before", | when: "before", | ||||
collection_name: collection.name, | collection_name: collection.name, | ||||
action: "create", | action: "create", | ||||
}), | }), | ||||
async (emitted_event, params) => { | async (emitted_event, _params) => { | ||||
this._call_is_proper_value_for_params(params, collection); | this._call_is_proper_value_for_params(_params, collection); | ||||
const derived_fn_args = fields.map( | const derived_fn_args = fields.map( | ||||
field => | field => | ||||
params[field] === undefined || params[field] === null | _params[field] === undefined || _params[field] === null | ||||
? "" | ? "" | ||||
: params[field] | : _params[field] | ||||
); | ); | ||||
const derived_value = await derived_fn(...derived_fn_args); | const derived_value = await derived_fn(...derived_fn_args); | ||||
return { | return { | ||||
[field_name]: derived_value, | [field_name]: derived_value, | ||||
...params, | ..._params, | ||||
}; | }; | ||||
} | } | ||||
); | ); | ||||
app.addHook( | app.addHook( | ||||
new app.Sealious.EventMatchers.Resource({ | new app.Sealious.EventMatchers.Resource({ | ||||
when: "before", | when: "before", | ||||
collection_name: collection.name, | collection_name: collection.name, | ||||
action: "edit", | action: "edit", | ||||
}), | }), | ||||
async ({ metadata, subject_path }, params) => { | async ({ metadata, subject_path }, _params) => { | ||||
this._call_is_proper_value_for_params( | this._call_is_proper_value_for_params( | ||||
metadata.context, | metadata.context, | ||||
params, | _params, | ||||
collection | collection | ||||
); | ); | ||||
if (fields.some(field => Object.keys(params).includes(field))) { | if ( | ||||
fields.some(field => Object.keys(_params).includes(field)) | |||||
) { | |||||
const derived_fn_args = await Promise.map( | const derived_fn_args = await Promise.map( | ||||
fields, | fields, | ||||
async current_field => { | async current_field => { | ||||
if (Object.keys(params).includes(current_field)) { | if (Object.keys(_params).includes(current_field)) { | ||||
return params[current_field]; | return _params[current_field]; | ||||
} | } | ||||
const resource = await app.run_action( | const resource = await app.run_action( | ||||
new app.Sealious.SuperContext(), | new app.Sealious.SuperContext(), | ||||
subject_path.split("."), | subject_path.split("."), | ||||
"show" | "show" | ||||
); | ); | ||||
return resource[current_field]; | return resource[current_field]; | ||||
} | } | ||||
); | ); | ||||
const derived_value = await derived_fn(...derived_fn_args); | const derived_value = await derived_fn(...derived_fn_args); | ||||
return { | return { | ||||
...params, | ..._params, | ||||
[field_name]: derived_value, | [field_name]: derived_value, | ||||
}; | }; | ||||
} | } | ||||
return params; | return _params; | ||||
} | } | ||||
); | ); | ||||
}, | }, | ||||
encode: async function(context, params, value) { | async encode(context, params, value) { | ||||
return this._call_base_method("encode", context, params, value); | return this._call_base_method("encode", context, params, value); | ||||
}, | }, | ||||
decode: function(context, params, value_in_db) { | decode(context, params, value_in_db) { | ||||
return this._call_base_method("decode", context, params, value_in_db); | return this._call_base_method("decode", context, params, value_in_db); | ||||
}, | }, | ||||
format: function(context, params, decoded_value, format) { | format(context, params, decoded_value, format) { | ||||
return this._call_base_method( | return this._call_base_method( | ||||
"format", | "format", | ||||
context, | context, | ||||
params, | params, | ||||
decoded_value, | decoded_value, | ||||
format | format | ||||
); | ); | ||||
}, | }, | ||||
async _call_is_proper_value_for_params(context, event_params, collection) { | async _call_is_proper_value_for_params(context, event_params, collection) { | ||||
for (param_field in event_params) { | for (const param_field in event_params) { | ||||
try { | try { | ||||
await collection.fields[param_field].type.is_proper_value( | await collection.fields[param_field].type.is_proper_value( | ||||
context, | context, | ||||
collection.fields[param_field].params, | collection.fields[param_field].params, | ||||
event_params[param_field] | event_params[param_field] | ||||
); | ); | ||||
} catch (e) { | } catch (e) { | ||||
return; | return; | ||||
Show All 13 Lines |