Skip to content

Commit

Permalink
Merge pull request #14 from LerianStudio/feature/asset-module
Browse files Browse the repository at this point in the history
Refactoring Asset Module
  • Loading branch information
caioaletroca authored Nov 12, 2024
2 parents 1fd4103 + 000e4ad commit 077e737
Show file tree
Hide file tree
Showing 23 changed files with 139 additions and 147 deletions.
2 changes: 1 addition & 1 deletion src/app/(routes)/ledgers/[id]/assets/assets-sheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export const AssetsSheet = ({

React.useEffect(() => {
if (mode === 'edit' && !isNil(data)) {
setMetadataEnabled(Object.entries(data.metadata).length > 0)
setMetadataEnabled(Object.entries(data.metadata ?? {}).length > 0)
form.reset(data, { keepDefaultValues: true })
} else {
setMetadataEnabled(false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import { container } from '@/core/infrastructure/container-registry/container-registry'
import { apiErrorHandler } from '@/app/api/utils/api-error-handler'
import { DeleteAsset } from '@/core/application/use-cases/assets/delete-asset-use-case'
import { FetchAssetById } from '@/core/application/use-cases/assets/fetch-asset-by-id-use-case'
import { UpdateAsset } from '@/core/application/use-cases/assets/update-asset-use-case'
import {
container,
Registry
} from '@/core/infrastructure/container-registry/container-registry'
DeleteAsset,
DeleteAssetUseCase
} from '@/core/application/use-cases/assets/delete-asset-use-case'
import {
FetchAssetById,
FetchAssetByIdUseCase
} from '@/core/application/use-cases/assets/fetch-asset-by-id-use-case'
import {
UpdateAsset,
UpdateAssetUseCase
} from '@/core/application/use-cases/assets/update-asset-use-case'
import { NextResponse } from 'next/server'

const fetchAssetByIdUseCase: FetchAssetById = container.get<FetchAssetById>(
Registry.FetchAssetByIdUseCase
FetchAssetByIdUseCase
)

const updateAssetUseCase = container.get<UpdateAsset>(
Registry.UpdateAssetUseCase
)
const updateAssetUseCase = container.get<UpdateAsset>(UpdateAssetUseCase)

const deleteAssetUseCase = container.get<DeleteAsset>(
Registry.DeleteAssetUseCase
)
const deleteAssetUseCase = container.get<DeleteAsset>(DeleteAssetUseCase)

export async function GET(
request: Request,
Expand Down
20 changes: 11 additions & 9 deletions src/app/api/organizations/[id]/ledgers/[ledgerId]/assets/route.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { container } from '@/core/infrastructure/container-registry/container-registry'
import { apiErrorHandler } from '@/app/api/utils/api-error-handler'
import { CreateAsset } from '@/core/application/use-cases/assets/create-asset-use-case'
import { FetchAllAssets } from '@/core/application/use-cases/assets/fetch-all-assets-use-case'
import {
container,
Registry
} from '@/core/infrastructure/container-registry/container-registry'
CreateAsset,
CreateAssetUseCase
} from '@/core/application/use-cases/assets/create-asset-use-case'
import {
FetchAllAssets,
FetchAllAssetsUseCase
} from '@/core/application/use-cases/assets/fetch-all-assets-use-case'
import { NextResponse } from 'next/server'

const createAssetUseCase: CreateAsset = container.get<CreateAsset>(
Registry.CreateAssetUseCase
)
const createAssetUseCase: CreateAsset =
container.get<CreateAsset>(CreateAssetUseCase)

const fetchAllAssetsUseCase: FetchAllAssets = container.get<FetchAllAssets>(
Registry.FetchAllAssetsUseCase
FetchAllAssetsUseCase
)

export async function POST(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AssetResponseDto } from '../../dto/asset-response-dto'
import { CreateAssetRepository } from '@/core/domain/repositories/assets/create-asset-repository'
import { assetDtoToEntity, assetEntityToDto } from '../../mappers/asset-mapper'
import { CreateAssetDto } from '../../dto/create-asset-dto'
import { inject, injectable } from 'inversify'

export interface CreateAsset {
execute: (
Expand All @@ -12,8 +13,12 @@ export interface CreateAsset {
) => Promise<AssetResponseDto>
}

@injectable()
export class CreateAssetUseCase implements CreateAsset {
constructor(private readonly createAssetRepository: CreateAssetRepository) {}
constructor(
@inject(CreateAssetRepository)
private readonly createAssetRepository: CreateAssetRepository
) {}

async execute(
organizationId: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DeleteAssetRepository } from '@/core/domain/repositories/assets/delete-asset-repository'
import { inject, injectable } from 'inversify'

export interface DeleteAsset {
execute: (
Expand All @@ -8,8 +9,12 @@ export interface DeleteAsset {
) => Promise<void>
}

@injectable()
export class DeleteAssetUseCase implements DeleteAsset {
constructor(private readonly deleteAssetRepository: DeleteAssetRepository) {}
constructor(
@inject(DeleteAssetRepository)
private readonly deleteAssetRepository: DeleteAssetRepository
) {}

async execute(
organizationId: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { PaginationDto } from '../../dto/pagination-dto'
import { AssetEntity } from '@/core/domain/entities/asset-entity'
import { PaginationEntity } from '@/core/domain/entities/pagination-entity'
import { assetEntityToDto } from '../../mappers/asset-mapper'
import { inject, injectable } from 'inversify'

export interface FetchAllAssets {
execute: (
Expand All @@ -17,8 +18,10 @@ export interface FetchAllAssets {
) => Promise<PaginationDto<AssetResponseDto>>
}

@injectable()
export class FetchAllAssetsUseCase implements FetchAllAssets {
constructor(
@inject(FetchAllAssetsRepository)
private readonly fetchAllAssetsRepository: FetchAllAssetsRepository
) {}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FetchAssetByIdRepository } from '@/core/domain/repositories/assets/fetch-asset-by-id-repository'
import { AssetResponseDto } from '../../dto/asset-response-dto'
import { assetEntityToDto } from '../../mappers/asset-mapper'
import { inject, injectable } from 'inversify'

export interface FetchAssetById {
execute: (
Expand All @@ -10,8 +11,10 @@ export interface FetchAssetById {
) => Promise<AssetResponseDto>
}

@injectable()
export class FetchAssetByIdUseCase implements FetchAssetById {
constructor(
@inject(FetchAssetByIdRepository)
private readonly fetchAssetByIdRepository: FetchAssetByIdRepository
) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
assetEntityToDto,
assetUpdateDtoToEntity
} from '../../mappers/asset-mapper'
import { inject, injectable } from 'inversify'

export interface UpdateAsset {
execute: (
Expand All @@ -16,8 +17,12 @@ export interface UpdateAsset {
) => Promise<AssetResponseDto>
}

@injectable()
export class UpdateAssetUseCase implements UpdateAsset {
constructor(private readonly updateAssetRepository: UpdateAssetRepository) {}
constructor(
@inject(UpdateAssetRepository)
private readonly updateAssetRepository: UpdateAssetRepository
) {}
async execute(
organizationId: string,
ledgerId: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AssetEntity } from '../../entities/asset-entity'

export interface CreateAssetRepository {
create: (
export abstract class CreateAssetRepository {
abstract create: (
organizationId: string,
ledgerId: string,
asset: AssetEntity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface DeleteAssetRepository {
delete: (
export abstract class DeleteAssetRepository {
abstract delete: (
organizationId: string,
ledgerId: string,
assetId: string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AssetEntity } from '../../entities/asset-entity'
import { PaginationEntity } from '../../entities/pagination-entity'

export interface FetchAllAssetsRepository {
fetchAll: (
export abstract class FetchAllAssetsRepository {
abstract fetchAll: (
organizationId: string,
ledgerId: string,
limit: number,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AssetEntity } from '../../entities/asset-entity'

export interface FetchAssetByIdRepository {
fetchById: (
export abstract class FetchAssetByIdRepository {
abstract fetchById: (
organizationId: string,
ledgerId: string,
assetId: string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AssetEntity } from '../../entities/asset-entity'

export interface UpdateAssetRepository {
update: (
export abstract class UpdateAssetRepository {
abstract update: (
organizationId: string,
ledgerId: string,
assetId: string,
Expand Down
107 changes: 0 additions & 107 deletions src/core/infrastructure/container-registry/asset-module.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {
FetchAllPortfoliosAccountsUseCase
} from '@/core/application/use-cases/portfolios-accounts/fetch-portfolios-accounts-use-case'
import { AuthModule, AuthRegistry } from './auth-module'

import { AssetModule, AssetRegistry } from './asset-module'
import { ProductRegistry, ProductModule } from './product-module'

import { Container } from '../utils/di/container'
Expand All @@ -23,10 +21,10 @@ import { MidazFetchAllPortfoliosRepository } from '../midaz/portfolios/midaz-fet
import { MidazFetchAllAccountsRepository } from '../midaz/accounts/midaz-fetch-all-accounts-repository'
import { PortfolioUseCaseModule } from './use-cases/portfolios-module'
import { AccountUseCaseModule } from './use-cases/account-module'
import { AssetUseCaseModule } from './use-cases/asset-module'

export const Registry = {
...AuthRegistry,
...AssetRegistry,
...ProductRegistry,

// Portfolio-Accounts
Expand All @@ -43,9 +41,9 @@ container.load(OrganizationUseCaseModule)
container.load(LedgerUseCaseModule)
container.load(PortfolioUseCaseModule)
container.load(AccountUseCaseModule)
container.load(AssetUseCaseModule)

container.container.load(AuthModule)
container.container.load(AssetModule)
container.container.load(ProductModule)

container
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Container, ContainerModule } from '../../utils/di/container'

import {
CreateAsset,
CreateAssetUseCase
} from '@/core/application/use-cases/assets/create-asset-use-case'
import {
DeleteAsset,
DeleteAssetUseCase
} from '@/core/application/use-cases/assets/delete-asset-use-case'
import {
FetchAllAssets,
FetchAllAssetsUseCase
} from '@/core/application/use-cases/assets/fetch-all-assets-use-case'
import { FetchAssetByIdUseCase } from '@/core/application/use-cases/assets/fetch-asset-by-id-use-case'
import {
UpdateAsset,
UpdateAssetUseCase
} from '@/core/application/use-cases/assets/update-asset-use-case'

export const AssetUseCaseModule = new ContainerModule(
(container: Container) => {
container.bind<CreateAsset>(CreateAssetUseCase).toSelf()
container.bind<FetchAllAssets>(FetchAllAssetsUseCase).toSelf()
container.bind<FetchAssetByIdUseCase>(FetchAssetByIdUseCase).toSelf()
container.bind<UpdateAsset>(UpdateAssetUseCase).toSelf()
container.bind<DeleteAsset>(DeleteAssetUseCase).toSelf()
}
)
Loading

0 comments on commit 077e737

Please sign in to comment.