|
|
|
"use strict";
|
|
|
|
|
|
|
|
const Promise = require("bluebird");
|
|
|
|
|
|
|
|
const wrapValueAsOption = require("@validatem/wrap-value-as-option");
|
|
|
|
const required = require("@validatem/required");
|
|
|
|
const isString = require("@validatem/is-string");
|
|
|
|
const defaultTo = require("@validatem/default-to");
|
|
|
|
const validateOptions = require("@validatem/core/src/api/validate-options");
|
|
|
|
const isFunction = require("@validatem/is-function");
|
|
|
|
const arrayOf = require("@validatem/array-of");
|
|
|
|
const defaultValue = require("default-value");
|
|
|
|
|
|
|
|
// FIXME: Remaining validators
|
|
|
|
|
|
|
|
module.exports = function wrapMutationAPI({ item, task, taskDependents }, api) {
|
|
|
|
return {
|
|
|
|
createItem: function (options) {
|
|
|
|
// FIXME: Require tags to be set, even if to an empty array, to avoid accidentally forgetting the tags
|
|
|
|
return api.createItem(options);
|
|
|
|
},
|
|
|
|
renameItem: function (_options) {
|
|
|
|
let options = validateOptions(arguments, [
|
|
|
|
wrapValueAsOption("to"), {
|
|
|
|
to: [ required, isString ],
|
|
|
|
from: [ defaultTo(item.id), isString ]
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
|
|
|
return api.renameItem(options);
|
|
|
|
},
|
|
|
|
mergeItem: function (options) {
|
|
|
|
return api.mergeItem(options);
|
|
|
|
},
|
|
|
|
deleteItem: function (_options) {
|
|
|
|
let options = validateOptions(arguments, [
|
|
|
|
defaultTo({}),
|
|
|
|
wrapValueAsOption("id"), {
|
|
|
|
id: [ defaultTo(item.id), isString ]
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
|
|
|
return api.deleteItem(options);
|
|
|
|
},
|
|
|
|
createAlias: function (options) {
|
|
|
|
// TODO: Single-parameter variant?
|
|
|
|
return api.createAlias(options);
|
|
|
|
},
|
|
|
|
deleteAlias: function (from) {
|
|
|
|
// FIXME: options wrapper
|
|
|
|
return api.deleteAlias(from);
|
|
|
|
},
|
|
|
|
updateData: function (_options) {
|
|
|
|
let options = validateOptions(arguments, [
|
|
|
|
wrapValueAsOption("update"), {
|
|
|
|
id: [ defaultTo(item.id), isString ],
|
|
|
|
update: [ required, isFunction ]
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
|
|
|
return api.updateData(options);
|
|
|
|
},
|
|
|
|
updateMetadata: function (_options) {
|
|
|
|
let options = validateOptions(arguments, [
|
|
|
|
wrapValueAsOption("update"), {
|
|
|
|
id: [ defaultTo(item.id), isString ],
|
|
|
|
taskName: [ defaultTo(task), isString ],
|
|
|
|
update: [ required, isFunction ]
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
|
|
|
return api.updateMetadata(options);
|
|
|
|
},
|
|
|
|
expire: function (_options) {
|
|
|
|
let options = validateOptions(arguments, [
|
|
|
|
defaultTo({}), {
|
|
|
|
id: [ defaultTo(item.id), isString ],
|
|
|
|
taskName: [ defaultTo(task), isString ]
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
|
|
|
return api.expire(options);
|
|
|
|
},
|
|
|
|
expireDependents: function (_options) {
|
|
|
|
let options = validateOptions(arguments, [
|
|
|
|
defaultTo({}),
|
|
|
|
wrapValueAsOption("dependents"), {
|
|
|
|
id: [ defaultTo(item.id), isString ],
|
|
|
|
dependents: [ arrayOf(isString) ]
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
|
|
|
let selectedDependents = (options.dependents != null)
|
|
|
|
? new Set(options.dependents)
|
|
|
|
: null;
|
|
|
|
|
|
|
|
let allDependents = defaultValue(taskDependents, []);
|
|
|
|
|
|
|
|
let affectedDependents = (selectedDependents != null)
|
|
|
|
? allDependents.filter((dependent) => selectedDependents.has(dependent.task))
|
|
|
|
: allDependents;
|
|
|
|
|
|
|
|
return Promise.map(affectedDependents, (dependent) => {
|
|
|
|
return this.expire({
|
|
|
|
id: options.id,
|
|
|
|
taskName: dependent.task
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|