Skip to content

Commit

Permalink
implement cancel command,
Browse files Browse the repository at this point in the history
implement single data source logic,
implement better output messages logic
  • Loading branch information
belotserkovtsev committed Jul 31, 2021
1 parent 44619c6 commit a63e5e8
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 15 deletions.
25 changes: 21 additions & 4 deletions Data/expenses.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
{
"entertainment": {
"limit": 1000,
"expenses": 0
"expenses": 0,
"displayName": "Развлечения",
"abbreviations": [
"разв"
]
},
"extra": {
"limit": 1000,
"expenses": 0
"expenses": 0,
"displayName": "Резерв",
"abbreviations": [
"ост"
]
},
"transport": {
"limit": 1000,
"expenses": 0
"expenses": 0,
"displayName": "Транспорт",
"abbreviations": [
"транс",
"транспорт"
]
},
"food": {
"limit": 1000,
"expenses": 0
"expenses": 0,
"displayName": "Еда",
"abbreviations": [
"еда"
]
}
}
20 changes: 20 additions & 0 deletions Service/FileService.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ class FileService {
})
})
}

cancel(element) {
fs.readFile(appRoot + '/Data/expenses.json', 'utf-8', (err, data) => {
const json = JSON.parse(data)
const keys = Object.keys(json)

for (let i = 0; i < keys.length; i++) {
if (keys[i] === element.type) {
json[keys[i]].expenses -= parseInt(element.amount)
break
}
}

const jsonToWrite = JSON.stringify(json, null, 2)

fs.writeFile(appRoot + '/Data/expenses.json', jsonToWrite, err => {
if (err) throw err
})
})
}
}

module.exports = FileService
54 changes: 43 additions & 11 deletions Service/SpendingsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class SpendingsService {
try {
const parsedMessage = this._parseMessage(message)
this.fileService.write(parsedMessage)
this.spendingsHistory.push(parsedMessage)
return 'good'
} catch {
return 'error'
Expand All @@ -17,9 +18,9 @@ class SpendingsService {

let message = ''

Object.keys(json).forEach((key, i) => {
Object.keys(json).forEach(key => {
const percentage = Math.round(json[key].expenses / json[key].limit * 100)
message += `${key}: ${json[key].expenses} / ${json[key].limit} (${percentage} %)\n`
message += `${json[key].displayName}: ${json[key].expenses} / ${json[key].limit} (${percentage} %)\n`
})

return message
Expand All @@ -36,7 +37,33 @@ class SpendingsService {
}

itemsAvailible() {
return JSON.stringify(this.types, null, 2)
try {
const data = this.fileService.read()
const json = JSON.parse(data)
const keys = Object.keys(json)
let message = ''

for (let i = 0; i < keys.length; i++) {
message += `${json[keys[i]].displayName} : ${json[keys[i]].abbreviations}\n`
}

return message
} catch {
return 'error'
}
}

cancelLastOperation() {
try {
if (this.spendingsHistory.length === 0) {
return 'no history. nothing to cancel'
}
let element = this.spendingsHistory.pop()
this.fileService.cancel(element)
return 'good'
} catch {
return 'error'
}
}

_parseMessage(message) {
Expand All @@ -51,19 +78,24 @@ class SpendingsService {
}

_typeForAbbreviation(abbreviation) {
return this.types[abbreviation]
}
const data = this.fileService.read()
const json = JSON.parse(data)
const keys = Object.keys(json)

constructor() {
this.types = {
'разв': 'entertainment',
'транс': 'transport',
'ост': 'extra',
'еда': 'food'
for (let i = 0; i < keys.length; i++) {
if (json[keys[i]].abbreviations.includes(abbreviation)) {
return keys[i]
}
}

return null
}

constructor() {
this.fileService = new FileService()
this.spendingsHistory = Array()
}

}

module.exports = SpendingsService
5 changes: 5 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ bot.command('items', ctx => {
ctx.reply(message)
})

bot.command('cancel', ctx => {
const message = spendingsService.cancelLastOperation()
ctx.reply(message)
})

bot.launch().catch(e => {
console.log(e.message)
})

0 comments on commit a63e5e8

Please sign in to comment.