Repository navigation
feat: add wallets model #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,79 @@ | ||
| type WalletProps = { | ||
| id?: string; | ||
| userId: string; | ||
| name: string; | ||
| initialBalance: number; | ||
| isDefault: boolean; | ||
|
|
||
| createdAt: Date; | ||
| updatedAt: Date; | ||
| deletedAt?: Date; | ||
| }; | ||
|
|
||
| export class Wallet { | ||
| constructor(private readonly props: WalletProps) {} | ||
|
|
||
| get id(): string | undefined { | ||
| return this.props.id; | ||
| } | ||
|
|
||
| set id(id: string | undefined) { | ||
| this.props.id = id; | ||
| } | ||
|
|
||
| get userId(): string { | ||
| return this.props.userId; | ||
| } | ||
|
|
||
| set userId(userId: string) { | ||
| this.props.userId = userId; | ||
| } | ||
|
|
||
| get name(): string { | ||
| return this.props.name; | ||
| } | ||
|
|
||
| set name(name: string) { | ||
| this.props.name = name; | ||
| } | ||
|
|
||
| get initialBalance(): number { | ||
| return this.props.initialBalance; | ||
| } | ||
|
|
||
| set initialBalance(balance: number) { | ||
| this.props.initialBalance = balance; | ||
| } | ||
|
|
||
| get isDefault(): boolean { | ||
| return this.props.isDefault; | ||
| } | ||
|
|
||
| set isDefault(isDefault: boolean) { | ||
| this.props.isDefault = isDefault; | ||
| } | ||
|
|
||
| get createdAt(): Date { | ||
| return this.props.createdAt; | ||
| } | ||
|
|
||
| set createdAt(createdAt: Date) { | ||
| this.props.createdAt = createdAt; | ||
| } | ||
|
|
||
| get updatedAt(): Date { | ||
| return this.props.updatedAt; | ||
| } | ||
|
|
||
| set updatedAt(updatedAt: Date) { | ||
| this.props.updatedAt = updatedAt; | ||
| } | ||
|
|
||
| get deletedAt(): Date | undefined { | ||
| return this.props.deletedAt; | ||
| } | ||
|
|
||
| set deletedAt(deletedAt: Date | undefined) { | ||
| this.props.deletedAt = deletedAt; | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
src/infra/database/migrations/1784669759347_create-wallets-table.js
This file contains hidden or 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,29 @@ | ||
| exports.up = (pgm) => { | ||
| pgm.sql(` | ||
| CREATE TABLE wallets ( | ||
| id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||
| user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE, | ||
| name VARCHAR(100) NOT NULL, | ||
| initial_balance DECIMAL(15, 2) NOT NULL DEFAULT 0.00, | ||
| is_default BOOLEAN NOT NULL DEFAULT false, | ||
| created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||
| updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||
| deleted_at TIMESTAMP | ||
| ); | ||
| `); | ||
|
|
||
| pgm.sql(` | ||
| CREATE INDEX idx_wallets_user_id | ||
| ON wallets (user_id); | ||
| `); | ||
|
|
||
| pgm.sql(` | ||
| CREATE UNIQUE INDEX idx_wallets_default_per_user | ||
| ON wallets (user_id) | ||
| WHERE is_default = true AND deleted_at IS NULL; | ||
| `); | ||
| }; | ||
|
|
||
| exports.down = (pgm) => { | ||
| pgm.sql('DROP TABLE wallets'); | ||
| }; | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| export enum Providers { | ||
| USERS_REPOSITORY = 'UsersRepository', | ||
| TOKENS_REPOSITORY = 'TokensRepository', | ||
| WALLETS_REPOSITORY = 'WalletsRepository', | ||
| } |
This file contains hidden or 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,26 @@ | ||
| import { v7 as uuid } from 'uuid'; | ||
|
|
||
| import { WalletsRepositoryInterface } from './wallets.repository.interface'; | ||
| import { Wallet } from '@/entities/wallet/wallet.entity'; | ||
|
|
||
| export class WalletsInMemoryRepository implements WalletsRepositoryInterface { | ||
| private wallets: Wallet[] = []; | ||
|
|
||
| async save(wallet: Wallet): Promise<Wallet> { | ||
| if (wallet.id) { | ||
| const index = this.wallets.findIndex((w) => w.id === wallet.id); | ||
| this.wallets[index] = wallet; | ||
| } else { | ||
| wallet.id = uuid(); | ||
| this.wallets.push(wallet); | ||
| } | ||
|
|
||
| return wallet; | ||
| } | ||
|
daniloamsilva marked this conversation as resolved.
|
||
|
|
||
| async findByUserId(userId: string): Promise<Wallet[]> { | ||
| return this.wallets.filter( | ||
| (wallet) => wallet.userId === userId && !wallet.deletedAt, | ||
| ); | ||
| } | ||
|
daniloamsilva marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or 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,76 @@ | ||
| import { Inject } from '@nestjs/common'; | ||
|
|
||
| import { WalletsRepositoryInterface } from './wallets.repository.interface'; | ||
| import { DatabaseService } from '@/infra/database/database.service'; | ||
| import { Wallet } from '@/entities/wallet/wallet.entity'; | ||
| import { mapSnakeToCamel } from '@/utils/map-snake-to-camel'; | ||
|
|
||
| export class WalletsPostgresRepository implements WalletsRepositoryInterface { | ||
| constructor( | ||
| @Inject(DatabaseService) | ||
| private readonly database: DatabaseService, | ||
| ) {} | ||
|
|
||
| async save(wallet: Wallet): Promise<Wallet> { | ||
| const findWallet = (await this.findByUserId(wallet.userId)).find( | ||
| (w) => w.id === wallet.id, | ||
| ); | ||
|
|
||
| let query = ''; | ||
| let values = []; | ||
|
|
||
| if (findWallet) { | ||
| query = ` | ||
| UPDATE wallets | ||
| SET | ||
| user_id = $1, | ||
| name = $2, | ||
| initial_balance = $3, | ||
| is_default = $4, | ||
| updated_at = $5 | ||
| WHERE id = $6 | ||
| RETURNING *; | ||
| `; | ||
|
|
||
| values = [ | ||
| wallet.userId, | ||
| wallet.name, | ||
| wallet.initialBalance, | ||
| wallet.isDefault, | ||
| wallet.updatedAt, | ||
| findWallet.id, | ||
| ]; | ||
| } else { | ||
| query = ` | ||
| INSERT INTO wallets (user_id, name, initial_balance, is_default, created_at, updated_at) | ||
| VALUES ($1, $2, $3, $4, $5, $6) | ||
| RETURNING *; | ||
| `; | ||
|
|
||
| values = [ | ||
| wallet.userId, | ||
| wallet.name, | ||
| wallet.initialBalance, | ||
| wallet.isDefault, | ||
| wallet.createdAt, | ||
| wallet.updatedAt, | ||
| ]; | ||
| } | ||
|
|
||
| const rows = await this.database.query(query, values); | ||
| return new Wallet(mapSnakeToCamel(rows[0])); | ||
| } | ||
|
daniloamsilva marked this conversation as resolved.
|
||
|
|
||
| async findByUserId(userId: string): Promise<Wallet[]> { | ||
| const rows = await this.database.query( | ||
| ` | ||
| SELECT * FROM wallets | ||
| WHERE user_id = $1 | ||
| AND deleted_at IS NULL; | ||
| `, | ||
| [userId], | ||
| ); | ||
|
|
||
| return rows.map((row) => new Wallet(mapSnakeToCamel(row))); | ||
| } | ||
|
daniloamsilva marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or 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,6 @@ | ||
| import { Wallet } from '@/entities/wallet/wallet.entity'; | ||
|
|
||
| export interface WalletsRepositoryInterface { | ||
| save(wallet: Wallet): Promise<Wallet>; | ||
| findByUserId(userId: string): Promise<Wallet[]>; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.