Skip to content

Instantly share code, notes, and snippets.

@Aleksandr-ru
Last active July 7, 2020 14:53
Show Gist options
  • Save Aleksandr-ru/b2dc4402a023c5a72182775f73eec543 to your computer and use it in GitHub Desktop.
Save Aleksandr-ru/b2dc4402a023c5a72182775f73eec543 to your computer and use it in GitHub Desktop.
Сумма кастомных полей указанного столбца в Trello
// на открытой доске выполнить в консоли
(function () {
const bid = window.location.pathname.split('/')[2];
const listName = prompt('Enter list name');
const fetchJson = url => new Promise(resolve => setTimeout(resolve, Math.random() * 300))
.then(() => fetch(url))
.then(response => response.json());
const loading = title => {
let i = 0;
return setInterval(() => console.clear() || console.log(title, ['|', '/', '-', '\\'][(i++ % 4)]), 300);
};
const interval = loading(listName);
fetchJson(`https://trello.com/1/boards/${bid}/lists`)
.then(list => list.filter(a => a.name === listName).shift())
.then(list => `https://trello.com/1/lists/${list.id}/cards?fields=id`)
.then(url => fetchJson(url))
.then(cards => cards.map(card => card.id))
.then(ids => ids.map(id => `https://trello.com/1/cards/${id}/customFieldItems`))
.then(urls => {
let cf = [];
return urls.reduce((promise, url) => promise.then(() => fetchJson(url).then(json => cf.push(json))), Promise.resolve()).then(() => cf.flat());
})
.then(cf => cf.filter(f => f.value && f.value.text).map(f => ({ 'id': f.idCustomField, 'value': parseFloat(f.value.text.replace(',', '.')) || 0 })))
.then(a => a.reduce((acc, val) => {
if (!acc[val.id]) acc[val.id] = 0;
acc[val.id] += val.value;
return acc;
}, {}))
.then(cfv => fetchJson(`https://trello.com/1/boards/${bid}/customFields`)
.then(a => a.reduce((acc, val) => {
if (!acc[val.id]) acc[val.id] = val.name;
return acc;
}, {}))
.then(cfn => {
const res = {};
for (let [k, v] of Object.entries(cfv)) res[cfn[k]] = v;
return res;
}))
.then(o => {
clearInterval(interval);
for (let [k, v] of Object.entries(o)) console.log(k, v);
});
return listName;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment