oa-jira
Version:
Octet Agile's JIRA connectivity project.
78 lines (65 loc) • 2.32 kB
JavaScript
const commons = require('../../commons');
const constants = require('../constants');
const mapToJSON = new Map([
['complexity', value => value],
['project', value => value.toJSON()],
['resolution', value => value.toJSON()],
['sprint', value => value.toJSON()],
['status', value => value.toJSON()],
['summary', value => value],
['type', value => value.toJSON()],
['updated', value => value],
[
'versions',
value => {
const json = [];
for (const version of value) json.push(version.toJSON());
return json;
}
]
]);
class Change {
#name;
#source;
#target;
constructor({ name, source, target } = {}) {
this.#name = commons.string.check(name, constants.change.settings.name);
this.#source = source;
this.#target = target;
}
getName = () => this.#name;
getSource = () => this.#source;
getTarget = () => this.#target;
toString = () => `Change[${this.#name} : ${this.#source} ==> ${this.#target}]`;
static check(change) {
return commons.object.check(change, { name: 'change', className: 'Change', _class: Change });
}
static resolve(change) {
return commons.object.resolve(change, { name: 'change', className: 'Change', _class: Change });
}
static checkBulk(changes) {
const checked = [];
if (commons.array.not(changes)) throw commons.errors.invalid.type.new('bulk of changes', 'array');
for (const change of changes) checked.push(Change.check(change));
return checked;
}
static resolveBulk(changes) {
return commons.array.is(changes)
? Promise.all(Object.values(changes).map(change => this.resolve(change)))
: commons.errors.invalid.type.reject('bulk of changes', 'array');
}
static create({ name, source, target } = {}) {
try {
return source || target ? Promise.resolve(new Change({ name, source, target })) : Promise.resolve();
} catch (error) {
return Promise.reject(error);
}
}
toJSON = ({ unit = true } = {}) => {
const json = unit ? { name: this.#name } : {};
if (this.#source) json.source = mapToJSON.get(this.#name)(this.#source);
if (this.#target) json.target = mapToJSON.get(this.#name)(this.#target);
return json;
};
}
module.exports = Change;