-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prompts: add budget prompt to get budget data
- Loading branch information
1 parent
5385bae
commit 00e2334
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { prompt } from "../deps.ts"; | ||
import { writeJsonFile } from "../repositories/FileRepository.ts"; | ||
import ExactBudgetService from "../services/ExactBudgetService.ts"; | ||
import BasePrompt from "./BasePrompt.ts"; | ||
import DebouncedInput from "./DebouncedInput.ts"; | ||
|
||
const enum Prompts { | ||
BUDGET = "budget", | ||
} | ||
|
||
export default class BudgetPrompt extends BasePrompt { | ||
constructor() { | ||
super(); | ||
this.ensureApi(); | ||
} | ||
|
||
async run() { | ||
return prompt([ | ||
{ | ||
name: Prompts.BUDGET, | ||
message: "Please select the ledger account to get transactions from:", | ||
type: DebouncedInput, | ||
list: true, | ||
info: true, | ||
suggestions: (await this.exactRepo.getBudgetScenario()).map((b) => | ||
b.Description | ||
), | ||
debounceTime: 1000, | ||
update: async (input: string) => { | ||
return (await this.exactRepo.getBudgetScenario(input)).map((b) => | ||
b.Description | ||
); | ||
}, | ||
after: async ({ budget }, next) => { | ||
if (!budget) { | ||
return next(Prompts.BUDGET); | ||
} | ||
|
||
const budgetService = new ExactBudgetService(this.exactRepo); | ||
const budgetData = await budgetService.getBudgetScenario(budget); | ||
|
||
await writeJsonFile( | ||
`budget_${budget}`, | ||
Array.from(budgetData.values()), | ||
); | ||
|
||
return next(); | ||
}, | ||
}, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import ExactRepository from "../repositories/ExactRepository.ts"; | ||
import { BudgetScenarioValue } from "../repositories/exact_models.d.ts"; | ||
|
||
export default class ExactBudgetService { | ||
#exactRepo; | ||
|
||
constructor(exactRepo: ExactRepository) { | ||
this.#exactRepo = exactRepo; | ||
} | ||
|
||
async getBudgetScenario(budgetDescription: string) { | ||
console.log(`Getting budget '${budgetDescription}'.`); | ||
const budgets = await this.#exactRepo.getBudgetScenario( | ||
budgetDescription, | ||
1, | ||
); | ||
|
||
if (!budgets.length) { | ||
throw new TypeError(`Failed to get budget for '${budgetDescription}'.`); | ||
} | ||
|
||
return aggregateBudgetScenarioValues( | ||
await this.#exactRepo.getBudgetScenarioValues(budgets[0].ID), | ||
); | ||
} | ||
} | ||
|
||
export function aggregateBudgetScenarioValues(budgets: BudgetScenarioValue[]) { | ||
const map: Map<string, Omit<BudgetScenarioValue, "ReportingPeriod">> = | ||
new Map(); | ||
|
||
for (const budget of budgets) { | ||
const total = map.get(budget.GLAccountCode); | ||
|
||
if (!total) { | ||
// deno-lint-ignore no-unused-vars | ||
const { ReportingPeriod, ...total } = budget; | ||
map.set(budget.GLAccountCode, { ...total }); | ||
continue; | ||
} | ||
|
||
total.AmountDC += budget.AmountDC; | ||
} | ||
|
||
return map; | ||
} |