@@ -10,7 +10,7 @@ import { KycService } from 'src/subdomains/generic/kyc/services/kyc.service';
1010import { MailContext , MailType } from 'src/subdomains/supporting/notification/enums' ;
1111import { MailKey , MailTranslationKey } from 'src/subdomains/supporting/notification/factories/mail.factory' ;
1212import { NotificationService } from 'src/subdomains/supporting/notification/services/notification.service' ;
13- import { IsNull , MoreThan } from 'typeorm' ;
13+ import { FindOptionsWhere , IsNull , MoreThan } from 'typeorm' ;
1414import { UserData } from '../user-data/user-data.entity' ;
1515import { KycLevel , KycType , TradeApprovalReason , UserDataStatus } from '../user-data/user-data.enum' ;
1616import { UserDataService } from '../user-data/user-data.service' ;
@@ -75,9 +75,6 @@ export class RecommendationService {
7575 } )
7676 : undefined ;
7777
78- if ( recommended ?. tradeApprovalDate )
79- await this . userDataService . createTradeApprovalLog ( recommended , TradeApprovalReason . MAIL_INVITATION ) ;
80-
8178 const entity = await this . createRecommendationInternal (
8279 RecommendationType . INVITATION ,
8380 dto . recommendedMail ? RecommendationMethod . MAIL : RecommendationMethod . RECOMMENDATION_CODE ,
@@ -102,6 +99,12 @@ export class RecommendationService {
10299 isConfirmed : true ,
103100 confirmationDate : new Date ( ) ,
104101 } ) ;
102+
103+ if ( recommended . tradeApprovalDate ) {
104+ await this . userDataService . createTradeApprovalLog ( recommended , TradeApprovalReason . MAIL_INVITATION ) ;
105+
106+ await this . setRecommenderRefCode ( entity ) ;
107+ }
105108 }
106109
107110 if ( dto . recommendedMail ) await this . sendInvitationMail ( entity ) ;
@@ -228,20 +231,24 @@ export class RecommendationService {
228231 TradeApprovalReason . RECOMMENDATION_CONFIRMED ,
229232 ) ;
230233
231- const refCode =
232- entity . kycStep && entity . method === RecommendationMethod . REF_CODE
233- ? entity . kycStep . getResult < KycRecommendationData > ( ) . key
234- : ( entity . recommender . users . find ( ( u ) => u . ref ) ?. ref ?? Config . defaultRef ) ;
235-
236- for ( const user of entity . recommended . users ??
237- ( await this . userService . getAllUserDataUsers ( entity . recommended . id ) ) ) {
238- if ( user . usedRef === Config . defaultRef ) await this . userService . updateUserInternal ( user , { usedRef : refCode } ) ;
239- }
234+ await this . setRecommenderRefCode ( entity ) ;
240235 }
241236
242237 return this . recommendationRepo . save ( entity ) ;
243238 }
244239
240+ async setRecommenderRefCode ( entity : Recommendation ) : Promise < void > {
241+ const refCode =
242+ entity . kycStep && entity . method === RecommendationMethod . REF_CODE
243+ ? entity . kycStep . getResult < KycRecommendationData > ( ) . key
244+ : ( entity . recommender . users . find ( ( u ) => u . ref ) ?. ref ?? Config . defaultRef ) ;
245+
246+ for ( const user of entity . recommended . users ??
247+ ( await this . userService . getAllUserDataUsers ( entity . recommended . id ) ) ) {
248+ if ( user . usedRef === Config . defaultRef ) await this . userService . updateUserInternal ( user , { usedRef : refCode } ) ;
249+ }
250+ }
251+
245252 async getAndCheckRecommendationByCode ( code : string ) : Promise < Recommendation > {
246253 const entity = await this . recommendationRepo . findOne ( {
247254 where : { code } ,
@@ -260,13 +267,26 @@ export class RecommendationService {
260267 return entity ;
261268 }
262269
263- async getAllRecommendationForUserData ( userDataId : number ) : Promise < Recommendation [ ] > {
270+ async getAllRecommendationForUserData ( recommenderId : number ) : Promise < Recommendation [ ] > {
264271 return this . recommendationRepo . find ( {
265- where : { recommender : { id : userDataId } } ,
272+ where : { recommender : { id : recommenderId } } ,
266273 relations : { recommended : true , recommender : true } ,
267274 } ) ;
268275 }
269276
277+ async getUserDataRecommendation (
278+ userDataId : number ,
279+ where : FindOptionsWhere < Recommendation > ,
280+ ) : Promise < Recommendation > {
281+ return this . recommendationRepo . findOne ( {
282+ where : { recommended : { id : userDataId } , ...where } ,
283+ relations : {
284+ recommended : { users : true } ,
285+ recommender : { users : true } ,
286+ } ,
287+ } ) ;
288+ }
289+
270290 async checkAndConfirmRecommendInvitation ( recommendedId : number ) : Promise < Recommendation > {
271291 const entity = await this . recommendationRepo . findOne ( {
272292 where : { recommended : { id : recommendedId } , isConfirmed : IsNull ( ) , expirationDate : MoreThan ( new Date ( ) ) } ,
0 commit comments