diff --git a/packages/wallet/backend/src/gatehub/controller.ts b/packages/wallet/backend/src/gatehub/controller.ts index 6c00ead39..9cd02681c 100644 --- a/packages/wallet/backend/src/gatehub/controller.ts +++ b/packages/wallet/backend/src/gatehub/controller.ts @@ -19,7 +19,18 @@ export class GateHubController implements IGateHubController { try { const userId = req.session.user.id const iframeType: IFRAME_TYPE = req.params.type as IFRAME_TYPE - const url = await this.gateHubService.getIframeUrl(iframeType, userId) + const { url, isApproved, customerId } = + await this.gateHubService.getIframeUrl(iframeType, userId) + + if (isApproved) { + req.session.user.needsIDProof = false + + if (customerId) { + req.session.user.customerId = customerId + } + + await req.session.save() + } res.status(200).json(toSuccessResponse({ url })) } catch (e) { next(e) diff --git a/packages/wallet/backend/src/gatehub/service.ts b/packages/wallet/backend/src/gatehub/service.ts index 652e89971..7f95d474b 100644 --- a/packages/wallet/backend/src/gatehub/service.ts +++ b/packages/wallet/backend/src/gatehub/service.ts @@ -3,6 +3,7 @@ import { IFRAME_TYPE } from '@wallet/shared/src' import { User } from '@/user/model' import { NotFound } from '@shared/backend' import { + IAddUserToGatewayResponse, ICardTransactionWebhookData, IDepositWebhookData, IWebhookData @@ -30,18 +31,35 @@ export class GateHubService { private emailService: EmailService ) {} - async getIframeUrl(iframeType: IFRAME_TYPE, userId: string): Promise { + async getIframeUrl( + iframeType: IFRAME_TYPE, + userId: string + ): Promise<{ url: string; isApproved?: boolean; customerId?: string }> { const user = await User.query().findById(userId) if (!user || !user.gateHubUserId) { throw new NotFound() } + let addUserToGatewayResponse = {} + if (iframeType === 'onboarding' && !this.gateHubClient.isProduction) { + const userState = await this.gateHubClient.getUserState( + user.gateHubUserId + ) + + if (userState.verifications[0]?.status === 1) { + addUserToGatewayResponse = await this.addUserToGateway(userId) + } + } + const url = await this.gateHubClient.getIframeUrl( iframeType, user.gateHubUserId ) - return url + return { + url, + ...addUserToGatewayResponse + } } async handleWebhook(data: IWebhookData) { @@ -203,7 +221,7 @@ export class GateHubService { async addUserToGateway( userId: string, isApproved = false - ): Promise<{ isApproved: boolean; customerId?: string }> { + ): Promise { const user = await User.query().findById(userId) if (!user || !user.gateHubUserId) { throw new NotFound() diff --git a/packages/wallet/backend/src/gatehub/types.ts b/packages/wallet/backend/src/gatehub/types.ts index b424d6a0f..208b2ae91 100644 --- a/packages/wallet/backend/src/gatehub/types.ts +++ b/packages/wallet/backend/src/gatehub/types.ts @@ -44,6 +44,13 @@ export interface ICreateManagedUserResponse { managedBy: string } +interface IVerification { + uuid: string + status: number + state: number + provider_type: 'sumsub' +} + export interface IGetUserStateResponse { profile: { first_name: string @@ -53,6 +60,7 @@ export interface IGetUserStateResponse { address_street1: string address_street2: string } + verifications: IVerification[] } export interface ICreateWalletRequest { @@ -170,3 +178,8 @@ export interface IDepositWebhookData { deposit_type: DepositTypeEnum message?: string } + +export interface IAddUserToGatewayResponse { + isApproved: boolean + customerId?: string +} diff --git a/packages/wallet/backend/tests/gatehub/controller.test.ts b/packages/wallet/backend/tests/gatehub/controller.test.ts index b1187438a..88b4195c1 100644 --- a/packages/wallet/backend/tests/gatehub/controller.test.ts +++ b/packages/wallet/backend/tests/gatehub/controller.test.ts @@ -85,7 +85,9 @@ describe('GateHub Controller', () => { type: 'withdrawal' } const mockedIframeUrl = 'URL' - mockGateHubService.getIframeUrl.mockResolvedValue(mockedIframeUrl) + mockGateHubService.getIframeUrl.mockResolvedValue({ + url: mockedIframeUrl + }) await gateHubController.getIframeUrl(req, res, next) diff --git a/packages/wallet/backend/tests/gatehub/service.test.ts b/packages/wallet/backend/tests/gatehub/service.test.ts index ceb4e32e7..adb8b9ca0 100644 --- a/packages/wallet/backend/tests/gatehub/service.test.ts +++ b/packages/wallet/backend/tests/gatehub/service.test.ts @@ -154,7 +154,7 @@ describe('GateHub Service', (): void => { mockGateHubClient.getIframeUrl.mockReturnValue(mockedIframeUrl) const result = await gateHubService.getIframeUrl('withdrawal', user.id) - expect(result).toMatch(mockedIframeUrl) + expect(result).toMatchObject({ url: mockedIframeUrl }) }) it('should return NotFound if no user found', async () => {