-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
650ee5b
commit 7395a67
Showing
17 changed files
with
1,091 additions
and
39 deletions.
There are no files selected for viewing
Binary file not shown.
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 |
---|---|---|
@@ -1,6 +1,20 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { join } from 'path'; | ||
|
||
import { AccountPersistenceModule } from './modules/account-persistence/account-persistence.module'; | ||
import { AccountWebModule } from './modules/account-web/account-web.module'; | ||
|
||
@Module({ | ||
imports: [], | ||
imports: [ | ||
TypeOrmModule.forRoot({ | ||
type: 'sqlite', | ||
database: join(__dirname, '..', 'data', 'data.sqlite'), | ||
logging: true, | ||
autoLoadEntities: true, | ||
}), | ||
AccountPersistenceModule, | ||
AccountWebModule, | ||
], | ||
}) | ||
export class AppModule {} |
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
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import { SendMoneyCommand } from './send-money.command'; | ||
|
||
export const SendMoneyUseCaseSymbol = Symbol('SendMoneyUseCase'); | ||
|
||
export interface SendMoneyUseCase { | ||
sendMoney(command: SendMoneyCommand); | ||
sendMoney(command: SendMoneyCommand): Promise<boolean>; | ||
} |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { AccountEntity, AccountId } from '../../entities/account.entity'; | ||
|
||
export interface LoadAccountPort { | ||
loadAccount(accountId: AccountId): AccountEntity; | ||
loadAccount(accountId: AccountId): Promise<AccountEntity>; | ||
} |
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
51 changes: 51 additions & 0 deletions
51
src/modules/account-persistence/account-persistence.adapter.ts
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,51 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { LoadAccountPort } from '../../domains/ports/out/load-account.port'; | ||
import { UpdateAccountStatePort } from '../../domains/ports/out/update-account-state.port'; | ||
import { | ||
AccountEntity, | ||
AccountId, | ||
} from '../../domains/entities/account.entity'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { AccountOrmEntity } from './account.orm-entity'; | ||
import { Repository } from 'typeorm'; | ||
import { ActivityOrmEntity } from './activity.orm-entity'; | ||
import { AccountMapper } from './account.mapper'; | ||
|
||
@Injectable() | ||
export class AccountPersistenceAdapter | ||
implements LoadAccountPort, UpdateAccountStatePort | ||
{ | ||
constructor( | ||
@InjectRepository(AccountOrmEntity) | ||
private readonly _accountRepository: Repository<AccountOrmEntity>, | ||
|
||
@InjectRepository(ActivityOrmEntity) | ||
private readonly _activityRepository: Repository<ActivityOrmEntity>, | ||
) {} | ||
|
||
async loadAccount(accountId: AccountId): Promise<AccountEntity> { | ||
const account = await this._accountRepository.findOne({ | ||
userId: accountId, | ||
}); | ||
|
||
if (account === undefined) { | ||
throw new Error('Account not found'); | ||
} | ||
|
||
const activities = await this._activityRepository.find({ | ||
ownerAccountId: accountId, | ||
}); | ||
|
||
return AccountMapper.mapToDomain(account, activities); | ||
} | ||
|
||
updateActivities(account: AccountEntity) { | ||
account.activityWindow.activities.forEach((activity) => { | ||
if (activity.id === null) { | ||
this._activityRepository.save( | ||
AccountMapper.mapToActivityOrmEntity(activity), | ||
); | ||
} | ||
}); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/modules/account-persistence/account-persistence.module.ts
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,27 @@ | ||
import { Global, Module } from '@nestjs/common'; | ||
import { AccountPersistenceAdapter } from './account-persistence.adapter'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { AccountOrmEntity } from './account.orm-entity'; | ||
import { ActivityOrmEntity } from './activity.orm-entity'; | ||
import { SendMoneyUseCaseSymbol } from '../../domains/ports/in/send-money.use-case'; | ||
import { SendMoneyService } from '../../domains/servicies/send-money.service'; | ||
|
||
@Global() | ||
@Module({ | ||
imports: [TypeOrmModule.forFeature([AccountOrmEntity, ActivityOrmEntity])], | ||
providers: [ | ||
AccountPersistenceAdapter, | ||
{ | ||
provide: SendMoneyUseCaseSymbol, | ||
useFactory: (AccountPersistenceAdapter) => { | ||
return new SendMoneyService( | ||
AccountPersistenceAdapter, | ||
AccountPersistenceAdapter, | ||
); | ||
}, | ||
inject: [AccountPersistenceAdapter], | ||
}, | ||
], | ||
exports: [SendMoneyUseCaseSymbol], | ||
}) | ||
export class AccountPersistenceModule {} |
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,51 @@ | ||
import { AccountOrmEntity } from './account.orm-entity'; | ||
import { ActivityOrmEntity } from './activity.orm-entity'; | ||
import { AccountEntity } from '../../domains/entities/account.entity'; | ||
import { ActivityWindowEntity } from '../../domains/entities/activity-window.entity'; | ||
import { ActivityEntity } from '../../domains/entities/activity.entity'; | ||
import { MoneyEntity } from '../../domains/entities/money.entity'; | ||
|
||
export class AccountMapper { | ||
static mapToDomain( | ||
account: AccountOrmEntity, | ||
activities: ActivityOrmEntity[], | ||
) { | ||
const activityWindow = this.mapToActivityWindow(activities); | ||
const balance = activityWindow.calculateBalance(account.userId); | ||
|
||
return new AccountEntity(account.userId, balance, activityWindow); | ||
} | ||
|
||
static mapToActivityWindow( | ||
activities: ActivityOrmEntity[], | ||
): ActivityWindowEntity { | ||
const activityWindowEntity = new ActivityWindowEntity(); | ||
|
||
activities.forEach((activity) => { | ||
const activityEntity = new ActivityEntity( | ||
activity.ownerAccountId, | ||
activity.sourceAccountId, | ||
activity.targetAccountId, | ||
new Date(activity.timestamp), | ||
MoneyEntity.of(activity.amount), | ||
activity.id, | ||
); | ||
activityWindowEntity.addActivity(activityEntity); | ||
}); | ||
return activityWindowEntity; | ||
} | ||
|
||
static mapToActivityOrmEntity(activity: ActivityEntity): ActivityOrmEntity { | ||
const activityOrmEntity = new ActivityOrmEntity(); | ||
|
||
activityOrmEntity.timestamp = activity.timestamp.getTime(); | ||
activityOrmEntity.ownerAccountId = activity.ownerAccountId; | ||
activityOrmEntity.sourceAccountId = activity.sourceAccountId; | ||
activityOrmEntity.targetAccountId = activity.targetAccountId; | ||
activityOrmEntity.amount = activity.money.amount.toNumber(); | ||
if (activity.id !== null) { | ||
activityOrmEntity.id = activity.id; | ||
} | ||
return activityOrmEntity; | ||
} | ||
} |
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,9 @@ | ||
import { Column, PrimaryGeneratedColumn, Entity } from 'typeorm'; | ||
|
||
@Entity('Account', {}) | ||
export class AccountOrmEntity { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
@Column() | ||
userId: string; | ||
} |
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,22 @@ | ||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; | ||
|
||
@Entity('Activity', {}) | ||
export class ActivityOrmEntity { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
timestamp: number; | ||
|
||
@Column() | ||
ownerAccountId: string; | ||
|
||
@Column() | ||
sourceAccountId: string; | ||
|
||
@Column() | ||
targetAccountId: string; | ||
|
||
@Column() | ||
amount: number; | ||
} |
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,7 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { SendMoneyController } from './send-money.controller'; | ||
|
||
@Module({ | ||
controllers: [SendMoneyController], | ||
}) | ||
export class AccountWebModule {} |
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,31 @@ | ||
import { Controller, Get, Inject, Query } from '@nestjs/common'; | ||
import { | ||
SendMoneyUseCase, | ||
SendMoneyUseCaseSymbol, | ||
} from '../../domains/ports/in/send-money.use-case'; | ||
import { SendMoneyCommand } from '../../domains/ports/in/send-money.command'; | ||
import { MoneyEntity } from '../../domains/entities/money.entity'; | ||
|
||
@Controller('/account/send') | ||
export class SendMoneyController { | ||
constructor( | ||
@Inject(SendMoneyUseCaseSymbol) | ||
private readonly _sendMoneyUseCase: SendMoneyUseCase, | ||
) {} | ||
|
||
@Get('/') | ||
async sendMoney( | ||
@Query('sourceAccountId') sourceAccountId: string, | ||
@Query('targetAccountId') targetAccountId: string, | ||
@Query('amount') amount: number, | ||
) { | ||
const command = new SendMoneyCommand( | ||
sourceAccountId, | ||
targetAccountId, | ||
MoneyEntity.of(amount), | ||
); | ||
const result = await this._sendMoneyUseCase.sendMoney(command); | ||
|
||
return { result }; | ||
} | ||
} |
Oops, something went wrong.