Skip to content

Commit ead2cd5

Browse files
committed
refactor: streamline date handling in vault and miniapp services
- Replaced manual date construction with a method to retrieve the current budget period in MiniappService, VaultWebService, and VaultService. - Updated transaction retrieval logic to directly use provided date options, enhancing clarity and reducing redundancy in date management.
1 parent 52cc788 commit ead2cd5

File tree

4 files changed

+20
-29
lines changed

4 files changed

+20
-29
lines changed

src/bot/miniapp.service.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,8 @@ export class MiniappService {
239239
});
240240
}
241241

242-
// Usar data customizada ou o mês atual
243-
const now = new Date();
244-
const date = customDate || {
245-
month: now.getMonth() + 1,
246-
year: now.getFullYear(),
247-
};
242+
// Usar data customizada ou o período de orçamento atual
243+
const date = customDate || vault.getCurrentBudgetPeriod();
248244
const budget = vault.getBudgetsSummary(date.month, date.year);
249245

250246
return right({
@@ -285,10 +281,7 @@ export class MiniappService {
285281
try {
286282
const transactionsResult = await this.vaultService.getTransactions({
287283
vaultId,
288-
date: options.date || {
289-
year: new Date().getFullYear(),
290-
month: new Date().getMonth() + 1,
291-
},
284+
date: options.date,
292285
page: options.page || 1,
293286
categoryId: options.categoryId,
294287
description: options.description,

src/vault/domain/vault.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ export class Vault {
115115
return date >= startDate && date <= endDate;
116116
}
117117

118+
getCurrentBudgetPeriod(): { month: number; year: number } {
119+
const now = new Date();
120+
let month = now.getMonth() + 1;
121+
let year = now.getFullYear();
122+
if (now.getDate() < this._budgetStartDay) {
123+
month -= 1;
124+
if (month < 1) { month = 12; year -= 1; }
125+
}
126+
return { month, year };
127+
}
128+
118129
static create(): Vault {
119130
return new Vault(Vault.generateId(), Vault.generateToken(), new Date());
120131
}
@@ -287,8 +298,7 @@ export class Vault {
287298
totalSpentAmount(date?: { month: number; year: number }): number {
288299
let total = 0;
289300
if (!date) {
290-
const now = new Date();
291-
date = { month: now.getMonth() + 1, year: now.getFullYear() };
301+
date = this.getCurrentBudgetPeriod();
292302
}
293303

294304
for (const transaction of this.transactions.values()) {
@@ -305,8 +315,7 @@ export class Vault {
305315
totalIncomeAmount(date?: { month: number; year: number }): number {
306316
let total = 0;
307317
if (!date) {
308-
const now = new Date();
309-
date = { month: now.getMonth() + 1, year: now.getFullYear() };
318+
date = this.getCurrentBudgetPeriod();
310319
}
311320

312321
for (const transaction of this.transactions.values()) {

src/vault/vault-web.service.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,8 @@ export class VaultWebService {
200200
});
201201
}
202202

203-
// Use custom date or current month
204-
const now = new Date();
205-
const targetDate = date || {
206-
month: now.getMonth() + 1,
207-
year: now.getFullYear(),
208-
};
203+
// Use custom date or current budget period
204+
const targetDate = date || vault.getCurrentBudgetPeriod();
209205
const budget = vault.getBudgetsSummary(targetDate.month, targetDate.year);
210206

211207
return right({
@@ -240,10 +236,7 @@ export class VaultWebService {
240236
// Use VaultService directly
241237
const [error, transactions] = await this.vaultService.getTransactions({
242238
vaultId,
243-
date: params.date || {
244-
year: new Date().getFullYear(),
245-
month: new Date().getMonth() + 1,
246-
},
239+
date: params.date,
247240
page: params.page || 1,
248241
categoryId: params.categoryId,
249242
description: params.description,

src/vault/vault.service.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,7 @@ export class VaultService {
214214
return left(`Cofre não encontrado`);
215215
}
216216

217-
const now = new Date();
218-
const date = input.date ?? {
219-
month: now.getMonth() + 1,
220-
year: now.getFullYear(),
221-
};
217+
const date = input.date ?? vault.getCurrentBudgetPeriod();
222218
const { startDate, endDate } = vault.getBudgetPeriod(date.month, date.year);
223219

224220
const transactions =

0 commit comments

Comments
 (0)