@@ -15,6 +15,7 @@ import { CreditDuration } from './interface/credit-duration.interface';
1515import { ReadCreditError } from './error/read-credit.error' ;
1616import { CreateCreditError } from './error/create-credit.error' ;
1717import { IncrementCreditError } from './error/increment-credit.error' ;
18+ import { ReadActiveCreditError } from './error/read-active-credit.error' ;
1819
1920@Injectable ( )
2021export class CreditService {
@@ -65,10 +66,25 @@ export class CreditService {
6566 }
6667 }
6768
68- public async incrementActiveCreditUsage ( user : User ) : Promise < void > {
69+ public async getActiveCredit ( user : User ) : Promise < Credit > {
6970 try {
70- const credit = await this . getActiveCredit ( user ) ;
71+ const presentDate = new Date ( ) ;
7172
73+ return await this . creditRepository . findOneOrFail ( {
74+ where : {
75+ user,
76+ fromDate : LessThan ( presentDate ) ,
77+ toDate : MoreThan ( presentDate ) ,
78+ } ,
79+ } ) ;
80+ } catch ( error ) {
81+ this . loggerService . log ( `No active credit is available. ${ error } ` ) ;
82+ throw new ReadActiveCreditError ( error ) ;
83+ }
84+ }
85+
86+ public async incrementCreditUsage ( credit : Credit ) : Promise < void > {
87+ try {
7288 this . checkLimit ( credit ) ;
7389 credit . incrementUsage ( ) ;
7490
@@ -85,18 +101,6 @@ export class CreditService {
85101 }
86102 }
87103
88- private async getActiveCredit ( user : User ) : Promise < Credit > {
89- const presentDate = new Date ( ) ;
90-
91- return await this . creditRepository . findOneOrFail ( {
92- where : {
93- user,
94- fromDate : LessThan ( presentDate ) ,
95- toDate : MoreThan ( presentDate ) ,
96- } ,
97- } ) ;
98- }
99-
100104 private calculateCreditDuration ( duration : CreditDuration ) : {
101105 fromDate : Date ;
102106 toDate : Date ;
0 commit comments