Skip to content

Commit

Permalink
revert allowing everyone to sign up for cards
Browse files Browse the repository at this point in the history
  • Loading branch information
Tymmmy committed Jan 9, 2025
1 parent e10e684 commit b440c53
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
11 changes: 11 additions & 0 deletions packages/wallet/backend/src/auth/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getRandomToken, hashToken } from '@/utils/helpers'
import { EmailService } from '@/email/service'
import { Logger } from 'winston'
import { Unauthorized, NotVerified, BadRequest } from '@shared/backend'
import { GateHubClient } from '@/gatehub/client'

interface resendVerifyEmailArgs {
email: string
Expand Down Expand Up @@ -34,6 +35,7 @@ export class AuthService implements IAuthService {
constructor(
private userService: UserService,
private emailService: EmailService,
private gateHubClient: GateHubClient,
private logger: Logger,
private env: Env
) {}
Expand All @@ -50,6 +52,15 @@ export class AuthService implements IAuthService {
this.env.NODE_ENV === 'production' &&
this.env.GATEHUB_ENV === 'production'
) {
const existingManagedUsers = await this.gateHubClient.getManagedUsers()
const gateHubUser = existingManagedUsers.find(
(user) => user.email === email
)

if (!gateHubUser) {
throw new Error('You are not allowed to sign up.')
}

if (!acceptedCardTerms) {
throw new BadRequest(
'Additional terms and condition should be accepted'
Expand Down
17 changes: 13 additions & 4 deletions packages/wallet/backend/src/card/controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Request, Response, NextFunction } from 'express'
import { BadRequest, Controller, NotFound } from '@shared/backend'
import {
BadRequest,
Controller,
InternalServerError,
NotFound
} from '@shared/backend'
import { CardService } from '@/card/service'
import { toSuccessResponse } from '@shared/backend'
import {
Expand All @@ -24,6 +29,7 @@ import {
getTokenForPinChange
} from './validation'
import { UserService } from '@/user/service'
import { Logger } from 'winston'

export interface ICardController {
getCardsByCustomer: Controller<ICardResponse[]>
Expand All @@ -41,7 +47,8 @@ export interface ICardController {
export class CardController implements ICardController {
constructor(
private cardService: CardService,
private userService: UserService
private userService: UserService,
private logger: Logger
) {}

public getCardsByCustomer = async (
Expand All @@ -53,8 +60,10 @@ export class CardController implements ICardController {
const customerId = req.session.user.customerId

if (!customerId) {
res.status(200).json(toSuccessResponse([]))
return
this.logger.error(
`Customer id was not found on session object for user ${req.session.user.id}`
)
throw new InternalServerError()
}

const cards = await this.cardService.getCardsByCustomer(
Expand Down
4 changes: 2 additions & 2 deletions packages/wallet/backend/src/gatehub/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,14 @@ export class GateHubService {
userId: string,
userEmail: string,
walletAddressPublicName: string
): Promise<string | undefined> {
): Promise<string> {
const existingManagedUsers = await this.gateHubClient.getManagedUsers()
// Managed user will always be found here since this check is also performed on sign up
const gateHubUser = existingManagedUsers.find(
(gateHubUser) => gateHubUser.email === userEmail
)
if (!gateHubUser) {
return
throw new Error(`GateHub user with email ${userEmail} not found`)
}

const customerId = gateHubUser.meta.meta.customerId
Expand Down
7 changes: 6 additions & 1 deletion packages/wallet/backend/src/user/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,12 @@ export class UserService implements IUserService {
)

if (!gateHubUser) {
gateHubUser = await this.gateHubClient.createManagedUser(user.email)
// This shouldn't really happen when GATEHUB_ENV=production because we
// already created all managed users.
this.logger.error(
`Could not find GateHub managed user with email: ${user.email})`
)
throw new Error('Could not find GateHub managed user')
}
} else {
gateHubUser = await this.gateHubClient.createManagedUser(user.email)
Expand Down
12 changes: 5 additions & 7 deletions packages/wallet/backend/tests/cards/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
MockResponse
} from 'node-mocks-http'
import { CardController } from '@/card/controller'
import { BadRequest } from '@shared/backend'
import { BadRequest, InternalServerError } from '@shared/backend'
import {
ICardDetailsResponse,
ICardLimitRequest,
Expand Down Expand Up @@ -153,12 +153,10 @@ describe('CardController', () => {
})
})

expect(res.statusCode).toBe(200)
expect(res._getJSONData()).toEqual({
success: true,
message: 'SUCCESS',
result: []
})
expect(next).toHaveBeenCalled()
const error = next.mock.calls[0][0]
expect(error).toBeInstanceOf(InternalServerError)
expect(res.statusCode).toBe(500)
})
})

Expand Down

0 comments on commit b440c53

Please sign in to comment.