-
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
Showing
5 changed files
with
147 additions
and
119 deletions.
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
src/bank-statements/bank-statements-repository.service.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,125 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { nextFriday } from 'date-fns'; | ||
import { TicketRevenuesService } from 'src/ticket-revenues/ticket-revenues.service'; | ||
import { User } from 'src/users/entities/user.entity'; | ||
import { getDateYMDString, isPaymentWeekComplete } from 'src/utils/date-utils'; | ||
import { TimeIntervalEnum } from 'src/utils/enums/time-interval.enum'; | ||
import { getPagination } from 'src/utils/get-pagination'; | ||
import { PaginationOptions } from 'src/utils/types/pagination-options'; | ||
import { Pagination } from 'src/utils/types/pagination.type'; | ||
import { IBankStatement } from './interfaces/bank-statement.interface'; | ||
import { IBSCounts } from './interfaces/bs-counts.interface'; | ||
import { IBSGetMePreviousDaysValidArgs } from './interfaces/bs-get-me-previous-days-args.interface'; | ||
import { IBSGetMePreviousDaysResponse } from './interfaces/bs-get-me-previous-days-response.interface'; | ||
|
||
/** | ||
* Get weekly statements | ||
*/ | ||
@Injectable() | ||
export class BankStatementsRepositoryService { | ||
constructor(private readonly ticketRevenuesService: TicketRevenuesService) {} | ||
|
||
public async getPreviousDays( | ||
validArgs: IBSGetMePreviousDaysValidArgs, | ||
paginationOptions: PaginationOptions, | ||
): Promise<Pagination<IBSGetMePreviousDaysResponse>> { | ||
const previousDays = await this.buildPreviousDays({ | ||
user: validArgs.user, | ||
endDate: validArgs.endDate, | ||
timeInterval: validArgs.timeInterval, | ||
paginationArgs: paginationOptions, | ||
}); | ||
const statusCounts = this.generateStatusCounts(previousDays.data); | ||
|
||
return getPagination<IBSGetMePreviousDaysResponse>( | ||
{ | ||
data: previousDays.data, | ||
statusCounts: statusCounts, | ||
}, | ||
{ | ||
dataLenght: previousDays.data.length, | ||
maxCount: previousDays.count, | ||
}, | ||
paginationOptions, | ||
); | ||
} | ||
|
||
private async buildPreviousDays(validArgs: { | ||
user: User; | ||
endDate: string; | ||
timeInterval?: TimeIntervalEnum; | ||
paginationArgs?: PaginationOptions; | ||
}): Promise<Pagination<{ data: IBankStatement[] }>> { | ||
const pagination = validArgs.paginationArgs | ||
? validArgs.paginationArgs | ||
: { limit: 9999, page: 1 }; | ||
const revenues = await this.ticketRevenuesService.fetchTicketRevenues({ | ||
startDate: new Date(validArgs.endDate), | ||
endDate: new Date(validArgs.endDate), | ||
cpfCnpj: validArgs.user.getCpfCnpj(), | ||
limit: pagination.limit, | ||
offset: (pagination.page - 1) * pagination.limit, | ||
previousDays: true, | ||
}); | ||
const statements = revenues.data.map((item, index) => { | ||
const isPaid = isPaymentWeekComplete( | ||
new Date(String(item.processingDateTime)), | ||
); | ||
return { | ||
id: index, | ||
date: getDateYMDString(new Date(String(item.processingDateTime))), | ||
processingDate: getDateYMDString( | ||
new Date(String(item.processingDateTime)), | ||
), | ||
transactionDate: getDateYMDString( | ||
new Date(String(item.transactionDateTime)), | ||
), | ||
paymentOrderDate: getDateYMDString( | ||
nextFriday(new Date(String(item.processingDateTime))), | ||
), | ||
effectivePaymentDate: isPaid | ||
? getDateYMDString( | ||
nextFriday(new Date(String(item.processingDateTime))), | ||
) | ||
: null, | ||
cpfCnpj: validArgs.user.getCpfCnpj(), | ||
permitCode: validArgs.user.getPermitCode(), | ||
amount: item.transactionValue, | ||
status: isPaid ? 'Pago' : 'A pagar', | ||
statusCode: isPaid ? 'paid' : 'toPay', | ||
bankStatus: isPaid ? '00' : null, | ||
bankStatusCode: isPaid ? 'Crédito ou Débito Efetivado' : null, | ||
error: null, | ||
errorCode: null, | ||
} as IBankStatement; | ||
}); | ||
return getPagination<{ data: IBankStatement[] }>( | ||
{ | ||
data: statements, | ||
}, | ||
{ | ||
dataLenght: statements.length, | ||
maxCount: revenues.countAll, | ||
}, | ||
pagination, | ||
); | ||
} | ||
|
||
private generateStatusCounts( | ||
data: IBankStatement[], | ||
): Record<string, IBSCounts> { | ||
const statusCounts: Record<string, IBSCounts> = {}; | ||
for (const item of data) { | ||
if (!statusCounts?.[item.statusCode]) { | ||
statusCounts[item.statusCode] = { | ||
count: 1, | ||
amountSum: item.amount, | ||
}; | ||
} else { | ||
statusCounts[item.statusCode].count += 1; | ||
statusCounts[item.statusCode].amountSum += item.amount; | ||
} | ||
} | ||
return statusCounts; | ||
} | ||
} |
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
8 changes: 8 additions & 0 deletions
8
src/bank-statements/interfaces/bs-get-me-previous-days-args.interface.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 |
---|---|---|
@@ -1,7 +1,15 @@ | ||
import { User } from 'src/users/entities/user.entity'; | ||
import { TimeIntervalEnum } from 'src/utils/enums/time-interval.enum'; | ||
import { BSMePrevDaysTimeIntervalEnum } from '../enums/bs-me-prev-days-time-interval.enum'; | ||
|
||
export class IBSGetMePreviousDaysArgs { | ||
endDate?: string; | ||
timeInterval?: BSMePrevDaysTimeIntervalEnum; | ||
userId?: number; | ||
} | ||
|
||
export class IBSGetMePreviousDaysValidArgs { | ||
user: User; | ||
endDate: string; | ||
timeInterval?: TimeIntervalEnum; | ||
} |