Changeset View
Changeset View
Standalone View
Standalone View
lib/app/base-chips/field-types/value-exists-in-collection.js
"use strict"; | |||||
const locreq = require("locreq")(__dirname); | |||||
const assert = require("assert"); | const assert = require("assert"); | ||||
const Promise = require("bluebird"); | const Promise = require("bluebird"); | ||||
module.exports = function(app) { | module.exports = function(app) { | ||||
return { | return { | ||||
name: "value-exists-in-collection", | name: "value-exists-in-collection", | ||||
is_proper_value: async function(context, params, new_value) { | async is_proper_value(_context, params, new_value) { | ||||
assert.equal(typeof params.field, "string"); | assert.equal(typeof params.field, "string"); | ||||
assert( | assert( | ||||
typeof params.include_forbidden === "boolean" || | typeof params.include_forbidden === "boolean" || | ||||
params.include_forbidden === undefined | params.include_forbidden === undefined | ||||
); | ); | ||||
const collection = params.collection; | const collection = params.collection; | ||||
assert(collection instanceof app.Sealious.Collection); | assert(collection instanceof app.Sealious.Collection); | ||||
await collection.fields[params.field].is_proper_value( | await collection.fields[params.field].is_proper_value( | ||||
context, | _context, | ||||
new_value | new_value | ||||
); | ); | ||||
let context = _context; | |||||
if (params.include_forbidden) { | if (params.include_forbidden) { | ||||
context = new app.Sealious.SuperContext(); | context = new app.Sealious.SuperContext(); | ||||
} | } | ||||
const matches = await app.run_action( | const matches = await app.run_action( | ||||
context, | context, | ||||
["collections", collection.name], | ["collections", collection.name], | ||||
"show", | "show", | ||||
{ filter: { [params.field]: new_value } } | { filter: { [params.field]: new_value } } | ||||
); | ); | ||||
if (matches.length) { | if (matches.length) { | ||||
return Promise.resolve(); | return Promise.resolve(); | ||||
} else { | } | ||||
return Promise.reject( | return Promise.reject( | ||||
`No ${collection.name} with ${ | `No ${collection.name} with ${params.field} set to ${new_value}` | ||||
params.field | |||||
} set to ${new_value}` | |||||
); | ); | ||||
} | |||||
}, | }, | ||||
encode: async function(context, params, value_in_code, old_value) { | async encode(context, params, value_in_code, old_value) { | ||||
return params.collection.fields[params.field].encode( | return params.collection.fields[params.field].encode( | ||||
context, | context, | ||||
value_in_code, | value_in_code, | ||||
old_value | old_value | ||||
); | ); | ||||
}, | }, | ||||
format: function(context, params, decoded_value, format) { | format(context, params, decoded_value, format) { | ||||
return params.collection.fields[params.field].format( | return params.collection.fields[params.field].format( | ||||
context, | context, | ||||
decoded_value, | decoded_value, | ||||
format | format | ||||
); | ); | ||||
}, | }, | ||||
filter_to_query: function(context, params, field_filter) { | filter_to_query(context, params, field_filter) { | ||||
return params.collection.fields[params.field].filter_to_query( | return params.collection.fields[params.field].filter_to_query( | ||||
context, | context, | ||||
field_filter | field_filter | ||||
); | ); | ||||
}, | }, | ||||
get_aggregation_stages: function(context, params, field_name, query) { | get_aggregation_stages(context, params, field_name, query) { | ||||
return params.collection.fields[ | return params.collection.fields[ | ||||
params.field | params.field | ||||
].get_aggregation_stages(context, field_name, query); | ].get_aggregation_stages(context, field_name, query); | ||||
}, | }, | ||||
}; | }; | ||||
}; | }; |