-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from LerianStudio/feature/mapper-refactor
Refactored Mappers
- Loading branch information
Showing
45 changed files
with
299 additions
and
500 deletions.
There are no files selected for viewing
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
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
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,41 +1,42 @@ | ||
import { AssetEntity } from '@/core/domain/entities/asset-entity' | ||
import { AssetResponseDto } from '../dto/asset-response-dto' | ||
import { CreateAssetDto } from '../dto/create-asset-dto' | ||
import { UpdateAssetDto } from '../dto/update-asset-dto' | ||
import { PaginationEntity } from '@/core/domain/entities/pagination-entity' | ||
import { PaginationMapper } from './pagination-mapper' | ||
|
||
export function assetEntityToDto(asset: AssetEntity): AssetResponseDto { | ||
return { | ||
id: asset.id!, | ||
organizationId: asset.organizationId!, | ||
ledgerId: asset.ledgerId!, | ||
name: asset.name, | ||
type: asset.type, | ||
code: asset.code, | ||
status: { | ||
...asset.status, | ||
description: asset.status.description ?? '' | ||
}, | ||
metadata: asset.metadata, | ||
createdAt: asset.createdAt!, | ||
updatedAt: asset.updatedAt!, | ||
deletedAt: asset.deletedAt! | ||
export class AssetMapper { | ||
public static toDomain(dto: CreateAssetDto): AssetEntity { | ||
return { | ||
name: dto.name!, | ||
type: dto.type!, | ||
code: dto.code!, | ||
status: dto.status!, | ||
metadata: dto.metadata! | ||
} | ||
} | ||
} | ||
|
||
export function assetDtoToEntity(dto: CreateAssetDto): AssetEntity { | ||
return { | ||
name: dto.name, | ||
type: dto.type, | ||
code: dto.code, | ||
status: dto.status, | ||
metadata: dto.metadata | ||
public static toResponseDto(entity: AssetEntity): AssetResponseDto { | ||
return { | ||
id: entity.id!, | ||
organizationId: entity.organizationId!, | ||
ledgerId: entity.ledgerId!, | ||
name: entity.name, | ||
type: entity.type, | ||
code: entity.code, | ||
status: { | ||
...entity.status, | ||
description: entity.status.description ?? '' | ||
}, | ||
metadata: entity.metadata, | ||
createdAt: entity.createdAt!, | ||
updatedAt: entity.updatedAt!, | ||
deletedAt: entity.deletedAt! | ||
} | ||
} | ||
} | ||
|
||
export function assetUpdateDtoToEntity( | ||
dto: Partial<UpdateAssetDto> | ||
): Partial<AssetEntity> { | ||
return { | ||
...dto | ||
public static toPaginationResponseDto( | ||
result: PaginationEntity<AssetEntity> | ||
): PaginationEntity<AssetResponseDto> { | ||
return PaginationMapper.toResponseDto(result, AssetMapper.toResponseDto) | ||
} | ||
} |
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,35 +1,40 @@ | ||
import { LedgerEntity } from '@/core/domain/entities/ledger-entity' | ||
import { CreateLedgerDto } from '../dto/create-ledger-dto' | ||
import { LedgerResponseDto } from '../dto/ledger-response-dto' | ||
import { UpdateProductDto } from '../dto/product-dto' | ||
import { PaginationEntity } from '@/core/domain/entities/pagination-entity' | ||
import { PaginationMapper } from './pagination-mapper' | ||
|
||
export function ledgerDtoToEntity(dto: CreateLedgerDto): LedgerEntity { | ||
return { | ||
name: dto.name, | ||
status: dto.status, | ||
metadata: dto.metadata | ||
export class LedgerMapper { | ||
public static toDomain( | ||
dto: CreateLedgerDto | UpdateProductDto | ||
): LedgerEntity { | ||
return { | ||
name: dto.name!, | ||
status: dto.status!, | ||
metadata: dto.metadata! | ||
} | ||
} | ||
} | ||
|
||
export function ledgerEntityToDto(entity: LedgerEntity): LedgerResponseDto { | ||
return { | ||
id: entity.id!, | ||
organizationId: entity.organizationId!, | ||
name: entity.name, | ||
status: { | ||
code: entity.status.code, | ||
description: entity.status.description ?? '' | ||
}, | ||
metadata: entity.metadata ?? {}, | ||
createdAt: entity.createdAt!, | ||
updatedAt: entity.updatedAt!, | ||
deletedAt: entity.deletedAt! | ||
public static toResponseDto(ledger: LedgerEntity): LedgerResponseDto { | ||
return { | ||
id: ledger.id!, | ||
organizationId: ledger.organizationId!, | ||
name: ledger.name, | ||
status: { | ||
code: ledger.status.code, | ||
description: ledger.status.description ?? '' | ||
}, | ||
metadata: ledger.metadata ?? {}, | ||
createdAt: ledger.createdAt!, | ||
updatedAt: ledger.updatedAt!, | ||
deletedAt: ledger.deletedAt! | ||
} | ||
} | ||
} | ||
|
||
export function ledgerUpdateDtoToEntity( | ||
dto: Partial<CreateLedgerDto> | ||
): Partial<LedgerEntity> { | ||
return { | ||
...dto | ||
public static toPaginationResponseDto( | ||
result: PaginationEntity<LedgerEntity> | ||
): PaginationEntity<LedgerResponseDto> { | ||
return PaginationMapper.toResponseDto(result, LedgerMapper.toResponseDto) | ||
} | ||
} |
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,47 +1,49 @@ | ||
import { OrganizationEntity } from '@/core/domain/entities/organization-entity' | ||
import { CreateOrganizationDto } from '../dto/create-organization-dto' | ||
import { OrganizationResponseDto } from '../dto/organization-response-dto' | ||
import { UpdateOrganizationDto } from '../dto/update-organization-dto' | ||
import { PaginationMapper } from './pagination-mapper' | ||
import { PaginationEntity } from '@/core/domain/entities/pagination-entity' | ||
|
||
export function organizationDtoToEntity( | ||
dto: CreateOrganizationDto | ||
): OrganizationEntity { | ||
return { | ||
legalName: dto.legalName, | ||
parentOrganizationId: dto.parentOrganizationId, | ||
doingBusinessAs: dto.doingBusinessAs, | ||
legalDocument: dto.legalDocument, | ||
address: dto.address, | ||
metadata: dto.metadata, | ||
status: dto.status | ||
export class OrganizationMapper { | ||
public static toDomain(dto: CreateOrganizationDto): OrganizationEntity { | ||
return { | ||
legalName: dto.legalName!, | ||
parentOrganizationId: dto.parentOrganizationId, | ||
doingBusinessAs: dto.doingBusinessAs!, | ||
legalDocument: dto.legalDocument!, | ||
address: dto.address!, | ||
metadata: dto.metadata!, | ||
status: dto.status! | ||
} | ||
} | ||
} | ||
|
||
export function organizationEntityToDto( | ||
entity: OrganizationEntity | ||
): OrganizationResponseDto { | ||
return { | ||
id: entity.id!, | ||
legalName: entity.legalName, | ||
parentOrganizationId: entity.parentOrganizationId, | ||
doingBusinessAs: entity.doingBusinessAs, | ||
legalDocument: entity.legalDocument, | ||
address: entity.address, | ||
status: { | ||
code: entity.status.code, | ||
description: entity.status.description ?? '' | ||
}, | ||
metadata: entity.metadata ?? {}, | ||
createdAt: entity.createdAt!, | ||
updatedAt: entity.updatedAt!, | ||
deletedAt: entity.deletedAt | ||
public static toResponseDto( | ||
entity: OrganizationEntity | ||
): OrganizationResponseDto { | ||
return { | ||
id: entity.id!, | ||
legalName: entity.legalName, | ||
parentOrganizationId: entity.parentOrganizationId!, | ||
doingBusinessAs: entity.doingBusinessAs, | ||
legalDocument: entity.legalDocument, | ||
address: entity.address, | ||
status: { | ||
code: entity.status.code, | ||
description: entity.status.description ?? '' | ||
}, | ||
metadata: entity.metadata ?? {}, | ||
createdAt: entity.createdAt!, | ||
updatedAt: entity.updatedAt!, | ||
deletedAt: entity.deletedAt! | ||
} | ||
} | ||
} | ||
|
||
export function organizationUpdateDtoToEntity( | ||
dto: Partial<UpdateOrganizationDto> | ||
): Partial<OrganizationEntity> { | ||
return { | ||
...dto | ||
public static toPaginationResponseDto( | ||
result: PaginationEntity<OrganizationEntity> | ||
): PaginationEntity<OrganizationResponseDto> { | ||
return PaginationMapper.toResponseDto( | ||
result, | ||
OrganizationMapper.toResponseDto | ||
) | ||
} | ||
} |
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,15 +1,20 @@ | ||
import { PaginationEntity } from '@/core/domain/entities/pagination-entity' | ||
import { PaginationDto } from '../dto/pagination-dto' | ||
|
||
// export async function paginationEntityToDto<T>( | ||
// paginationEntity: PaginationEntity<T>, | ||
// mapperItem = (item: T) => Promise<T> | ||
// ): Promise<PaginationDto<T>> { | ||
export class PaginationMapper { | ||
static toResponseDto<T, R = T>( | ||
paginationEntity: PaginationEntity<T>, | ||
mapper = (item: T) => item as unknown as R | ||
): PaginationDto<R> { | ||
const items = | ||
paginationEntity.items && paginationEntity.items !== null | ||
? paginationEntity.items.map(mapper) | ||
: [] | ||
|
||
// const items = paginationEntity.items.map(mapperItem) | ||
// return { | ||
// items: items, | ||
// limit: paginationEntity.limit, | ||
// page: paginationEntity.page | ||
// } | ||
// } | ||
return { | ||
items: items, | ||
limit: paginationEntity.limit, | ||
page: paginationEntity.page | ||
} | ||
} | ||
} |
Oops, something went wrong.