Skip to content

Commit 8dc9259

Browse files
committed
fix: reading transactions with date
1 parent 96e72db commit 8dc9259

File tree

5 files changed

+76
-83
lines changed

5 files changed

+76
-83
lines changed

src/bot/telegram-message-generator.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export class TelegramMessageGenerator {
157157
minimumFractionDigits: 2,
158158
});
159159

160-
const date = transaction.createdAt.toLocaleDateString('pt-BR');
160+
const date = transaction.date.toLocaleDateString('pt-BR');
161161
const balance = vault.getBalance().toLocaleString('pt-BR', {
162162
style: 'currency',
163163
currency: 'BRL',
@@ -280,7 +280,7 @@ export class TelegramMessageGenerator {
280280
currency: 'BRL',
281281
minimumFractionDigits: 2,
282282
});
283-
const dateStr = transaction.createdAt.toLocaleDateString('pt-BR');
283+
const dateStr = transaction.date.toLocaleDateString('pt-BR');
284284
text += `• ${transaction.type === 'income' ? '🟢' : '🔴'} \`#${this.escapeMarkdownV2(transaction.code)}\` \\| ${this.escapeMarkdownV2(value)} \\|${transaction.category ? ` ${this.escapeMarkdownV2(transaction.category.name)} \\|` : ''} ${this.escapeMarkdownV2(dateStr)}${transaction.description ? `\n${this.escapeMarkdownV2(transaction.description)}` : ''}\n\n`;
285285
}
286286

@@ -403,7 +403,7 @@ export class TelegramMessageGenerator {
403403
}),
404404
)}\n` +
405405
`*Categoria:* ${TelegramMessageGenerator.escapeMarkdownV2(transaction.category?.name ?? 'Nenhuma categoria especificada')}\n` +
406-
`*Data:* ${TelegramMessageGenerator.escapeMarkdownV2(transaction.createdAt.toLocaleDateString('pt-BR'))}\n` +
406+
`*Data:* ${TelegramMessageGenerator.escapeMarkdownV2(transaction.date.toLocaleDateString('pt-BR'))}\n` +
407407
`*Descrição:* ${TelegramMessageGenerator.escapeMarkdownV2(transaction.description ?? 'Sem descrição')}\n`
408408
);
409409
}

src/vault/domain/transaction.ts

Lines changed: 60 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,90 +3,75 @@ import { Either, left, right } from './either';
33
import { TransactionDTO } from '../dto/transaction.dto,';
44
import { Category } from './category';
55

6+
type ConstructorParams = {
7+
id: string;
8+
code: string;
9+
vaultId: string;
10+
amount: number;
11+
isCommitted: boolean;
12+
description?: string;
13+
createdAt: Date;
14+
categoryId: string | null;
15+
type: 'expense' | 'income';
16+
date: Date;
17+
};
18+
19+
type CreateParams = {
20+
amount: number;
21+
vaultId: string;
22+
description?: string;
23+
type?: 'expense' | 'income';
24+
date: Date;
25+
categoryId?: string | null;
26+
createdAt?: Date;
27+
};
28+
629
export class Transaction {
7-
static create({
8-
amount,
9-
createdAt = new Date(),
10-
description,
11-
vaultId,
12-
type = 'expense',
13-
categoryId = null,
14-
date = new Date(),
15-
}: {
16-
amount: number;
17-
vaultId: string;
18-
description?: string;
19-
type?: 'expense' | 'income';
20-
date?: Date;
21-
categoryId?: string | null;
22-
createdAt?: Date;
23-
}): Transaction {
30+
static create(params: CreateParams): Transaction {
2431
const id = crypto.randomUUID();
2532
const code = crypto.randomBytes(2).toString('hex');
26-
return new Transaction(
33+
return new Transaction({
2734
id,
2835
code,
29-
vaultId,
30-
amount,
31-
false,
32-
description,
33-
createdAt,
34-
categoryId,
35-
type,
36-
date,
37-
);
36+
vaultId: params.vaultId,
37+
amount: params.amount,
38+
isCommitted: false,
39+
description: params.description,
40+
createdAt: params.createdAt ?? new Date(),
41+
categoryId: params.categoryId ?? null,
42+
type: params.type ?? 'expense',
43+
date: params.date,
44+
});
3845
}
3946

40-
static restore({
41-
id,
42-
code,
43-
amount,
44-
isCommitted = false,
45-
description,
46-
createdAt = new Date(),
47-
categoryId = null,
48-
type = 'expense',
49-
vaultId,
50-
date = new Date(),
51-
}: {
52-
id: string;
53-
code: string;
54-
amount: number;
55-
vaultId: string;
56-
isCommitted?: boolean;
57-
description?: string;
58-
createdAt?: Date;
59-
categoryId?: string | null;
60-
type?: 'expense' | 'income';
61-
date?: Date;
62-
}): Transaction {
63-
return new Transaction(
64-
id,
65-
code,
66-
vaultId,
67-
amount,
68-
isCommitted,
69-
description,
70-
createdAt,
71-
categoryId,
72-
type,
73-
date,
74-
);
47+
static restore(params: ConstructorParams): Transaction {
48+
return new Transaction({
49+
...params,
50+
});
7551
}
52+
readonly id: string;
53+
readonly code: string;
54+
public readonly vaultId: string;
55+
public amount: number;
56+
public isCommitted: boolean = false;
57+
public description?: string;
58+
public createdAt: Date = new Date();
59+
public categoryId: string | null = null;
60+
public type: 'expense' | 'income' = 'expense';
61+
public date: Date = new Date();
7662

77-
private constructor(
78-
readonly id: string,
79-
readonly code: string,
80-
public readonly vaultId: string,
81-
public amount: number,
82-
public isCommitted: boolean = false,
83-
public description?: string,
84-
public createdAt: Date = new Date(),
85-
public categoryId: string | null = null,
86-
public type: 'expense' | 'income' = 'expense',
87-
public date: Date = new Date(),
88-
) {}
89-
63+
private constructor(params: ConstructorParams) {
64+
this.id = params.id;
65+
this.code = params.code;
66+
this.vaultId = params.vaultId;
67+
this.amount = params.amount;
68+
this.isCommitted = params.isCommitted;
69+
this.description = params.description;
70+
this.createdAt = params.createdAt;
71+
this.categoryId = params.categoryId;
72+
this.type = params.type;
73+
this.date = params.date;
74+
}
9075
commit(): Either<string, boolean> {
9176
if (this.isCommitted) {
9277
return left(`Transação #${this.code} já efetivada`);

src/vault/domain/vault.spec.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe('Vault', () => {
1717
createdAt: new Date(),
1818
categoryId: 'any',
1919
type: 'income',
20+
date: new Date(),
2021
}),
2122
);
2223
expect(vault.getBalance()).toBe(0);
@@ -33,6 +34,7 @@ describe('Vault', () => {
3334
createdAt: new Date(),
3435
categoryId: 'any',
3536
type: 'expense',
37+
date: new Date(),
3638
}),
3739
);
3840
expect(vault.getBalance()).toBe(100);
@@ -53,6 +55,7 @@ describe('Vault', () => {
5355
createdAt: new Date(),
5456
categoryId: 'any',
5557
type: 'income',
58+
date: new Date(),
5659
}),
5760
);
5861
vault.commitTransaction('1');
@@ -75,6 +78,7 @@ describe('Vault', () => {
7578
createdAt: new Date(),
7679
categoryId: 'any',
7780
type: 'income',
81+
date: new Date(),
7882
}),
7983
);
8084
vault.commitTransaction('1');
@@ -97,7 +101,7 @@ describe('Vault', () => {
97101
amount: 100,
98102
isCommitted: false,
99103
categoryId: category1.id,
100-
createdAt: new Date('2023-05-15'),
104+
createdAt: new Date('2022-05-15'),
101105
type: 'expense',
102106
date: new Date('2023-05-15'),
103107
}),
@@ -110,7 +114,7 @@ describe('Vault', () => {
110114
vaultId: vault.id,
111115
isCommitted: false,
112116
categoryId: category1.id,
113-
createdAt: new Date('2023-05-20'),
117+
createdAt: new Date('2022-05-20'),
114118
date: new Date('2023-05-20'),
115119
type: 'expense',
116120
}),
@@ -123,7 +127,7 @@ describe('Vault', () => {
123127
amount: 50,
124128
isCommitted: false,
125129
categoryId: category2.id,
126-
createdAt: new Date('2023-05-02'),
130+
createdAt: new Date('2022-05-02'),
127131
type: 'expense',
128132
date: new Date('2023-05-20'),
129133
}),
@@ -136,7 +140,7 @@ describe('Vault', () => {
136140
amount: 200,
137141
isCommitted: false,
138142
categoryId: category2.id,
139-
createdAt: new Date('2023-06-05'),
143+
createdAt: new Date('2022-06-05'),
140144
date: new Date('2023-06-05'),
141145
type: 'expense',
142146
}),

src/vault/repositories/sqlite/vault-sqlite.repository.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export class VaultSqliteRepository extends VaultRepository {
124124
createdAt: new Date(t.created_at),
125125
categoryId: t.category_id,
126126
type: t.type,
127+
date: new Date(t.date),
127128
}),
128129
);
129130
}
@@ -187,6 +188,7 @@ export class VaultSqliteRepository extends VaultRepository {
187188
createdAt: new Date(t.created_at),
188189
categoryId: t.category_id,
189190
type: t.type,
191+
date: new Date(t.date),
190192
}),
191193
);
192194
}

src/vault/vault.service.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export class VaultService {
135135
description: action.payload.description,
136136
categoryId: action.payload.categoryId,
137137
shouldCommit: true,
138+
date: new Date(),
138139
type: 'income',
139140
},
140141
});
@@ -162,6 +163,7 @@ export class VaultService {
162163
amount: action.payload.amount,
163164
description: action.payload.description,
164165
categoryId: action.payload.categoryId,
166+
date: new Date(),
165167
shouldCommit: true,
166168
type: 'expense',
167169
},
@@ -234,7 +236,7 @@ export class VaultService {
234236
amount: number;
235237
description?: string;
236238
categoryId?: string;
237-
date?: Date;
239+
date: Date;
238240
shouldCommit?: boolean;
239241
type: 'expense' | 'income';
240242
};

0 commit comments

Comments
 (0)