const axios = require('axios') class Deck { constructor(url, user, pass, board, defaultstack) { // Nextcloud's base URL, not the api! this.api = `${url}/index.php/apps/deck/api/v1.0/boards/${board}` this.user = user this.pass = pass this.board = board this.defaultstack = defaultstack } checkLabelExist(label) { let tmpVal = false return new Promise((resolve, reject) => { axios.get( this.api, {auth: {username: this.user, password: this.pass}} ).then((res) => { res.data.labels.forEach((el) => { if (el.title == label) { resolve(el) tmpVal = true } }) }).then(() => { if(!tmpVal) resolve(false) }) .catch(e => console.log(e)) }) } createLabel(label, color) { this.checkLabelExist(label).then((exist) => { if (!exist) { axios.post(`${this.api}/labels`, { title: label, color: color }, {auth: {username: this.user, password: this.pass}} ) .then(console.log(`[${this.api}] - CREATE LABEL: '${label}' added`)) .catch(e => console.log(e)) } else { console.log(`[${this.api}] - CREATE LABEL: '${label}' already exists.`) } }) } checkCardExist(tracking) { let tmpVal = false return new Promise((resolve, reject) => { axios.get( `${this.api}/stacks`, {auth: {username: this.user, password: this.pass}} ).then((res) => { res.data.forEach((el) => { if (el.cards) el.cards.forEach((el) => { if (el.description && el.description.match(tracking) != null) { resolve(el) tmpVal = true } }) }) }).then(() => { //Check archived stack aswell... Wrapped inside .then because we only want resolve(false) ADTER we checked everywhere, not simoultaniously axios.get( `${this.api}/stacks/archived`, {auth: {username: this.user, password: this.pass}} ).then((res) => { res.data.forEach((el) => { if (el.cards) el.cards.forEach((el) => { if (el.description && el.description.match(tracking) != null) { resolve(el) tmpVal = true } }) }) }).then(() => { if(!tmpVal) resolve(false) }) }) .catch(e => console.log(e)) }) } createCard(title,description, tracking, tags, stack) { let tmpVal return new Promise((resolve, reject) => { this.checkCardExist(tracking).then((exist) => { console.log("checkcardexist then:" + exist) if (!exist) { axios.post( `${this.api}/stacks/${this.defaultstack}/cards`, {title: 'Giteabot comming trough... please reload :-)'}, {auth: {username: this.user, password: this.pass}} ).then((res) => { res.data.title = `🍵 ${title}` res.data.description = `[tracking]:${tracking}:\n\n${description}\n` axios.put( `${this.api}/stacks/${this.defaultstack}/cards/${res.data.id}`, res.data, {auth: {username: this.user, password: this.pass}} ).then(() => { console.log(`[${this.api}] - ADD CARD: '${tracking}' added to stack '${this.defaultstack}'`) resolve(true) tmpVal = true }) .catch(e => console.log(e)) }) .catch(e => console.log(e)) } else { if(!tmpVal){ resolve(false) console.log(`[${this.api}] - ADD CARD: '${tracking}' already exists in board '${this.board}'`) } } }) }) } archiveCard(tracking) { this.checkCardExist(tracking).then((card) => { if (card) { card.archived = true axios.put( `${this.api}/stacks/${this.defaultstack}/cards/${card.id}`, card, {auth: {username: this.user, password: this.pass}} ).then(() => { console.log(`[${this.api}] - ARCHIVE CARD: '${tracking}' archived`) }) .catch(e => console.log(e)) } else { console.log(`[${this.api}] - ARCHIVE CARD: '${tracking}' doesn't exist`) } }) } unarchiveCard(tracking) { this.checkCardExist(tracking).then((card) => { if (card) { card.archived = false axios.put( `${this.api}/stacks/${this.defaultstack}/cards/${card.id}`, card, {auth: {username: this.user, password: this.pass}} ).then(() => { console.log(`[${this.api}] - UNARCHIVE CARD: '${tracking}' archived`) }) .catch(e => console.log(e)) } else { console.log(`[${this.api}] - UNARCHIVE CARD: '${tracking}' doesn't exist`) } }) } cardSet(tracking, property, value) { let tmpVal = false return new Promise((resolve,reject) => { this.checkCardExist(tracking).then((card) => { if (card) { card[property] = value axios.put( `${this.api}/stacks/${this.defaultstack}/cards/${card.id}`, card, {auth: {username: this.user, password: this.pass}} ).then(() => { console.log(`[${this.api}] - CARD PROPERTY SET: '${property}' to '${value}' on '${tracking}'`) resolve(true) tmpVal = true }) .catch(e => console.log(e)) } else { if(!tmpVal) console.log(`[${this.api}] - CARD PROPERTY SET: '${tracking}' doesn't exist`) resolve(false) } }) }) } deleteCard(tracking) { return new Promise((resolve,reject) => { this.checkCardExist(tracking).then((card) => { if(card) { axios.delete( `${this.api}/stacks/${this.defaultstack}/cards/${card.id}`, {auth: {username: this.user, password: this.pass}} ).then(() => { console.log(`[${this.api}] - DELETE CARD: '${tracking}' deleted`) resolve(true) }) } else{ console.log(`[${this.api}] - DELETE CARD: '${tracking}' doesn't exist`) resolve(false) } }) }) } } module.exports = Deck