diff --git a/src/modules/transaction/refund/data/services/refund-read.service.ts b/src/modules/transaction/refund/data/services/refund-read.service.ts new file mode 100644 index 0000000..7bd5ec4 --- /dev/null +++ b/src/modules/transaction/refund/data/services/refund-read.service.ts @@ -0,0 +1,17 @@ +import { Injectable } from '@nestjs/common'; +import { RefundEntity } from '../../domain/entities/refund.entity'; +import { InjectRepository } from '@nestjs/typeorm'; +import { RefundModel } from '../models/refund.model'; +import { CONNECTION_NAME } from 'src/core/strings/constants/base.constants'; +import { Repository } from 'typeorm'; +import { BaseReadService } from 'src/core/modules/data/service/base-read.service'; + +@Injectable() +export class RefundReadService extends BaseReadService { + constructor( + @InjectRepository(RefundModel, CONNECTION_NAME.DEFAULT) + private repo: Repository, + ) { + super(repo); + } +} diff --git a/src/modules/transaction/refund/domain/entities/filter-refund.entity.ts b/src/modules/transaction/refund/domain/entities/filter-refund.entity.ts new file mode 100644 index 0000000..42aba39 --- /dev/null +++ b/src/modules/transaction/refund/domain/entities/filter-refund.entity.ts @@ -0,0 +1,18 @@ +import { BaseFilterEntity } from 'src/core/modules/domain/entities/base-filter.entity'; + +export interface FilterRefundEntity extends BaseFilterEntity { + codes: string[]; + invoice_codes: string[]; + bank_names: string[]; + bank_account_names: string[]; + bank_account_numbers: string[]; + contact_names: string[]; + creator_names: string[]; + + refund_date_from: Date; + refund_date_to: Date; + request_date_from: Date; + request_date_to: Date; + settlement_date_from: Date; + settlement_date_to: Date; +} diff --git a/src/modules/transaction/refund/domain/usecases/managers/detail-refund.manager.ts b/src/modules/transaction/refund/domain/usecases/managers/detail-refund.manager.ts new file mode 100644 index 0000000..88c3e89 --- /dev/null +++ b/src/modules/transaction/refund/domain/usecases/managers/detail-refund.manager.ts @@ -0,0 +1,65 @@ +import { Injectable } from '@nestjs/common'; +import { BaseDetailManager } from 'src/core/modules/domain/usecase/managers/base-detail.manager'; +import { RefundEntity } from '../../entities/refund.entity'; +import { RelationParam } from 'src/core/modules/domain/entities/base-filter.entity'; +import { mappingTransaction } from 'src/modules/transaction/transaction/domain/usecases/managers/helpers/mapping-transaction.helper'; + +@Injectable() +export class DetailRefundManager extends BaseDetailManager { + async prepareData(): Promise { + return; + } + + async beforeProcess(): Promise { + return; + } + + async afterProcess(): Promise { + mappingTransaction(this.result['transaction']); + return; + } + + get relations(): RelationParam { + return { + // relation only join (for query purpose) + joinRelations: [], + + // relation join and select (relasi yang ingin ditampilkan), + selectRelations: ['transaction', 'transaction.items', 'items.refund'], + + // relation yang hanya ingin dihitung (akan return number) + countRelations: [], + }; + } + + get selects(): string[] { + return [ + `${this.tableName}.id`, + `${this.tableName}.code`, + `${this.tableName}.status`, + `${this.tableName}.type`, + + `${this.tableName}.request_date`, + `${this.tableName}.refund_date`, + `${this.tableName}.created_at`, + `${this.tableName}.updated_at`, + `${this.tableName}.creator_name`, + `${this.tableName}.editor_name`, + + `${this.tableName}.refund_total`, + `${this.tableName}.bank_name`, + `${this.tableName}.bank_account_name`, + `${this.tableName}.bank_account_number`, + + 'transaction', + 'items', + 'refund', + ]; + } + + get setFindProperties(): any { + return { + id: this.dataId, + }; + } +} diff --git a/src/modules/transaction/refund/domain/usecases/managers/index-refund.manager.ts b/src/modules/transaction/refund/domain/usecases/managers/index-refund.manager.ts new file mode 100644 index 0000000..2fd80a0 --- /dev/null +++ b/src/modules/transaction/refund/domain/usecases/managers/index-refund.manager.ts @@ -0,0 +1,145 @@ +import { Injectable } from '@nestjs/common'; +import { BaseIndexManager } from 'src/core/modules/domain/usecase/managers/base-index.manager'; +import { RefundEntity } from '../../entities/refund.entity'; +import { SelectQueryBuilder } from 'typeorm'; +import { + Param, + RelationParam, +} from 'src/core/modules/domain/entities/base-filter.entity'; +import { BetweenQueryHelper } from 'src/core/helpers/query/between-query.helper'; + +@Injectable() +export class IndexRefundManager extends BaseIndexManager { + async prepareData(): Promise { + return; + } + + async beforeProcess(): Promise { + return; + } + + async afterProcess(): Promise { + this.result?.data?.map((item) => { + delete item['transaction']?.id; + Object.assign(item, { + ...item['transaction'], + }); + + delete item['transaction']; + }); + return; + } + + get relations(): RelationParam { + return { + // relation only join (for query purpose) + joinRelations: [], + + // relation join and select (relasi yang ingin ditampilkan), + selectRelations: ['transaction'], + + // relation yang hanya ingin dihitung (akan return number) + countRelations: [], + }; + } + + get selects(): string[] { + return [ + `${this.tableName}.id`, + `${this.tableName}.code`, + `${this.tableName}.status`, + `${this.tableName}.type`, + + `${this.tableName}.request_date`, + `${this.tableName}.refund_date`, + `${this.tableName}.created_at`, + `${this.tableName}.updated_at`, + `${this.tableName}.creator_name`, + `${this.tableName}.editor_name`, + + `${this.tableName}.refund_total`, + `${this.tableName}.bank_name`, + `${this.tableName}.bank_account_name`, + `${this.tableName}.bank_account_number`, + + `transaction.id`, + `transaction.invoice_code`, + `transaction.settlement_date`, + `transaction.payment_total`, + `transaction.customer_name`, + `transaction.customer_phone`, + `transaction.customer_email`, + `transaction.customer_description`, + ]; + } + + get specificFilter(): Param[] { + return [ + { + cols: `${this.tableName}.code`, + data: this.filterParam.codes, + }, + { + cols: `transaction.invoice_code`, + data: this.filterParam.invoice_codes, + }, + { + cols: `${this.tableName}.bank_name`, + data: this.filterParam.bank_names, + }, + { + cols: `${this.tableName}.bank_account_name`, + data: this.filterParam.bank_account_names, + }, + { + cols: `${this.tableName}.bank_account_number`, + data: this.filterParam.bank_account_numbers, + }, + { + cols: `transaction.customer_name`, + data: this.filterParam.contact_names, + }, + { + cols: `${this.tableName}.creator_name`, + data: this.filterParam.creator_names, + }, + ]; + } + + setQueryFilter( + queryBuilder: SelectQueryBuilder, + ): SelectQueryBuilder { + if (!!this.filterParam.refund_date_from) + new BetweenQueryHelper( + queryBuilder, + this.tableName, + 'refund_date', + this.filterParam.refund_date_from, + this.filterParam.refund_date_to ?? this.filterParam.refund_date_from, + 'refund_date', + ).getQuery(); + + if (!!this.filterParam.request_date_from) + new BetweenQueryHelper( + queryBuilder, + this.tableName, + 'request_date', + this.filterParam.request_date_from, + this.filterParam.request_date_to ?? this.filterParam.request_date_from, + 'request_date', + ).getQuery(); + + if (!!this.filterParam.settlement_date_from) + new BetweenQueryHelper( + queryBuilder, + 'transaction', + 'settlement_date', + this.filterParam.settlement_date_from, + this.filterParam.settlement_date_to ?? + this.filterParam.settlement_date_from, + 'settlement_date', + ).getQuery(); + + return queryBuilder; + } +} diff --git a/src/modules/transaction/refund/domain/usecases/refund-read.orchestrator.ts b/src/modules/transaction/refund/domain/usecases/refund-read.orchestrator.ts new file mode 100644 index 0000000..74af965 --- /dev/null +++ b/src/modules/transaction/refund/domain/usecases/refund-read.orchestrator.ts @@ -0,0 +1,33 @@ +import { Injectable } from '@nestjs/common'; +import { IndexRefundManager } from './managers/index-refund.manager'; +import { RefundReadService } from '../../data/services/refund-read.service'; +import { RefundEntity } from '../entities/refund.entity'; +import { PaginationResponse } from 'src/core/response/domain/ok-response.interface'; +import { BaseReadOrchestrator } from 'src/core/modules/domain/usecase/orchestrators/base-read.orchestrator'; +import { DetailRefundManager } from './managers/detail-refund.manager'; +import { TABLE_NAME } from 'src/core/strings/constants/table.constants'; + +@Injectable() +export class RefundReadOrchestrator extends BaseReadOrchestrator { + constructor( + private indexManager: IndexRefundManager, + private detailManager: DetailRefundManager, + private serviceData: RefundReadService, + ) { + super(); + } + + async index(params): Promise> { + this.indexManager.setFilterParam(params); + this.indexManager.setService(this.serviceData, TABLE_NAME.REFUND); + await this.indexManager.execute(); + return this.indexManager.getResult(); + } + + async detail(dataId: string): Promise { + this.detailManager.setData(dataId); + this.detailManager.setService(this.serviceData, TABLE_NAME.REFUND); + await this.detailManager.execute(); + return this.detailManager.getResult(); + } +} diff --git a/src/modules/transaction/refund/infrastructure/dto/filter-refund.dto.ts b/src/modules/transaction/refund/infrastructure/dto/filter-refund.dto.ts new file mode 100644 index 0000000..aa2cb41 --- /dev/null +++ b/src/modules/transaction/refund/infrastructure/dto/filter-refund.dto.ts @@ -0,0 +1,67 @@ +import { BaseFilterDto } from 'src/core/modules/infrastructure/dto/base-filter.dto'; +import { FilterRefundEntity } from '../../domain/entities/filter-refund.entity'; +import { ApiProperty } from '@nestjs/swagger'; +import { Transform } from 'class-transformer'; + +export class FilterRefundDto + extends BaseFilterDto + implements FilterRefundEntity +{ + @ApiProperty({ type: ['string'], required: false }) + @Transform((body) => { + return Array.isArray(body.value) ? body.value : [body.value]; + }) + codes: string[]; + @ApiProperty({ type: ['string'], required: false }) + @Transform((body) => { + return Array.isArray(body.value) ? body.value : [body.value]; + }) + invoice_codes: string[]; + + @ApiProperty({ type: ['string'], required: false }) + @Transform((body) => { + return Array.isArray(body.value) ? body.value : [body.value]; + }) + bank_names: string[]; + @ApiProperty({ type: ['string'], required: false }) + @Transform((body) => { + return Array.isArray(body.value) ? body.value : [body.value]; + }) + bank_account_names: string[]; + + @ApiProperty({ type: ['string'], required: false }) + @Transform((body) => { + return Array.isArray(body.value) ? body.value : [body.value]; + }) + bank_account_numbers: string[]; + + @ApiProperty({ type: ['string'], required: false }) + @Transform((body) => { + return Array.isArray(body.value) ? body.value : [body.value]; + }) + contact_names: string[]; + + @ApiProperty({ type: ['string'], required: false }) + @Transform((body) => { + return Array.isArray(body.value) ? body.value : [body.value]; + }) + creator_names: string[]; + + @ApiProperty({ type: Date, required: false }) + refund_date_from: Date; + + @ApiProperty({ type: Date, required: false }) + refund_date_to: Date; + + @ApiProperty({ type: Date, required: false }) + request_date_from: Date; + + @ApiProperty({ type: Date, required: false }) + request_date_to: Date; + + @ApiProperty({ type: Date, required: false }) + settlement_date_from: Date; + + @ApiProperty({ type: Date, required: false }) + settlement_date_to: Date; +} diff --git a/src/modules/transaction/refund/infrastructure/refund-read.controller.ts b/src/modules/transaction/refund/infrastructure/refund-read.controller.ts new file mode 100644 index 0000000..c3ec924 --- /dev/null +++ b/src/modules/transaction/refund/infrastructure/refund-read.controller.ts @@ -0,0 +1,30 @@ +import { Controller, Get, Param, Query } from '@nestjs/common'; +import { FilterRefundDto } from './dto/filter-refund.dto'; +import { Pagination } from 'src/core/response'; +import { PaginationResponse } from 'src/core/response/domain/ok-response.interface'; +import { RefundEntity } from '../domain/entities/refund.entity'; +import { RefundReadOrchestrator } from '../domain/usecases/refund-read.orchestrator'; +import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'; +import { MODULE_NAME } from 'src/core/strings/constants/module.constants'; +import { Public } from 'src/core/guards'; + +@ApiTags(`${MODULE_NAME.REFUND.split('-').join(' ')} - read`) +@Controller(`v1/${MODULE_NAME.REFUND}`) +@Public(false) +@ApiBearerAuth('JWT') +export class RefundReadController { + constructor(private orchestrator: RefundReadOrchestrator) {} + + @Get() + @Pagination() + async index( + @Query() params: FilterRefundDto, + ): Promise> { + return await this.orchestrator.index(params); + } + + @Get(':id') + async detail(@Param('id') id: string): Promise { + return await this.orchestrator.detail(id); + } +} diff --git a/src/modules/transaction/transaction/domain/usecases/managers/detail-transaction.manager.ts b/src/modules/transaction/transaction/domain/usecases/managers/detail-transaction.manager.ts index 01e731b..4b591d8 100644 --- a/src/modules/transaction/transaction/domain/usecases/managers/detail-transaction.manager.ts +++ b/src/modules/transaction/transaction/domain/usecases/managers/detail-transaction.manager.ts @@ -25,7 +25,7 @@ export class DetailTransactionManager extends BaseDetailManager } async afterProcess(): Promise { + this.result?.data?.map((item) => { + Object.assign(item, { + refund_code: item['refund']?.code ?? null, + refund_date: item['refund']?.refund_date ?? null, + }); + + delete item['refund']; + }); return; } @@ -27,7 +35,7 @@ export class IndexTransactionManager extends BaseIndexManager joinRelations: [], // relation join and select (relasi yang ingin ditampilkan), - selectRelations: ['items'], + selectRelations: ['items', 'refund'], // relation yang hanya ingin dihitung (akan return number) countRelations: [], @@ -63,6 +71,10 @@ export class IndexTransactionManager extends BaseIndexManager `${this.tableName}.payment_type_method_id`, `${this.tableName}.payment_type_method_name`, `${this.tableName}.payment_type_method_number`, + + `refund.id`, + `refund.code`, + `refund.refund_date`, ]; }