oa-jira
Version:
Octet Agile's JIRA connectivity project.
53 lines (46 loc) • 2.1 kB
JavaScript
const commons = require('../../commons');
const jira = require('../../jira');
const Sprint = require('../classes/sprint.class');
exports.create = ({ id, state, name, startDate, endDate, completeDate, createdDate, originBoardId, goal } = {}) => {
return Sprint.create({ id, state, name, startDate, endDate, completeDate, createdDate, originBoardId, goal });
};
exports.resolve = sprint => Sprint.resolve(sprint);
const format = ({ id, state, name, startDate, endDate, completeDate, createdDate, originBoardId, goal }) => {
const json = { id, state, name, originBoardId, goal };
if (startDate) json.startDate = new Date(startDate);
if (endDate) json.endDate = new Date(endDate);
if (completeDate) json.completeDate = new Date(completeDate);
if (createdDate) json.createdDate = new Date(createdDate);
return Promise.resolve(json);
};
const getById = ({ connection, id, cache }) => {
if (cache.sprints.has(id)) return cache.sprints.get(id);
return jira
.getSprintById(connection, id)
.then(data => (data ? format(data) : Promise.resolve()))
.then(data => (data ? Sprint.create(data) : Promise.resolve()))
.then(category => cache.sprints.set(id, category))
.catch(error => Promise.reject(error));
};
exports.getById = getById;
const getByIds = ({ connection, ids, cache }) => {
return Promise.all(ids.map(id => getById({ connection, id, cache })))
.then(sprints => {
const result = {};
for (const sprint of sprints) {
if (sprint && !result[sprint.getId()]) result[sprint.getId()] = sprint;
}
return Promise.resolve(Object.values(result));
})
.catch(error => Promise.reject(error));
};
exports.getByIds = getByIds;
exports.getLastByIds = ({ connection, ids, cache }) => {
return getByIds({ connection, ids, cache })
.then(sprints => {
return commons.array.isEmpty(sprints)
? Promise.resolve(null)
: sprints.reduce((last, current) => (current.compare(last) > 0 ? current : last));
})
.catch(error => Promise.reject(error));
};