Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fund card account after is verified #1775

Merged
merged 9 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex.schema.table('accounts', function (table) {
table.boolean('isFunded').defaultTo(false)
})
}

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex.schema.table('accounts', function (table) {
table.dropColumn('isFunded')
})
}
1 change: 1 addition & 0 deletions packages/wallet/backend/src/account/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class Account extends BaseModel {
public user!: User
public walletAddresses!: Array<WalletAddress>
public cardId?: string
public isFunded!: boolean

static relationMappings = () => ({
user: {
Expand Down
39 changes: 34 additions & 5 deletions packages/wallet/backend/src/card/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ import { ICardResponse } from '@wallet/shared'
import { UserService } from '@/user/service'
import { Logger } from 'winston'
import { User } from '@/user/model'
import { Account } from '@/account/model'
import { TransactionTypeEnum } from '@/gatehub/consts'
import { Env } from '@/config/env'

export class CardService {
constructor(
private gateHubClient: GateHubClient,
private accountService: AccountService,
private userService: UserService,
private logger: Logger
private logger: Logger,
private env: Env
) {}

async getCardsByCustomer(
Expand All @@ -40,12 +44,15 @@ export class CardService {
gateHubUserId
)

Object.assign(cards[0], {
const activeCard =
cards.find((card) => card.status !== 'SoftDelete') || cards[0]

Object.assign(activeCard, {
isPinSet: user.isPinSet,
walletAddress: user.cardWalletAddress
})

return cards
return [activeCard]
}

async getCardDetails(
Expand Down Expand Up @@ -131,11 +138,13 @@ export class CardService {
token: string,
cypher: string
): Promise<void> {
await this.ensureAccountExists(userId, cardId)
const cardAccount = await this.ensureAccountExists(userId, cardId)

await this.gateHubClient.changePin(token, cypher)

await User.query().findById(userId).patch({ isPinSet: true })

await this.fundAccount(cardAccount)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be done at sign up instead of doing it when we fetch the cards.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is on set pin

}

async lock(
Expand Down Expand Up @@ -183,11 +192,13 @@ export class CardService {
private async ensureAccountExists(
userId: string,
cardId: string
): Promise<void> {
): Promise<Account> {
const account = await this.accountService.getAccountByCardId(userId, cardId)
if (!account) {
throw new NotFound('Card not found or not associated with the user.')
}

return account
}

private async ensureGatehubUserUuid(
Expand All @@ -207,4 +218,22 @@ export class CardService {

return { user, gateHubUserId: user.gateHubUserId }
}

private async fundAccount(cardAccount: Account) {
if (cardAccount.isFunded) {
this.logger.warn(`Account was already funded ${cardAccount.id}`)
return
}

await Account.query().findById(cardAccount.id).patch({ isFunded: true })

await this.gateHubClient.createTransaction({
amount: 20,
vault_uuid: this.gateHubClient.getVaultUuid(cardAccount.assetCode),
receiving_address: cardAccount.gateHubWalletId,
sending_address: this.env.GATEHUB_SETTLEMENT_WALLET_ADDRESS,
type: TransactionTypeEnum.HOSTED,
message: 'Transfer'
})
}
}
5 changes: 4 additions & 1 deletion packages/wallet/backend/src/gatehub/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class GateHubService {
id: user.id,
gateHubUserId: user.gateHubUserId
})

if (this.gateHubClient.isProduction) {
await this.emailService.sendKYCVerifiedEmail(user.email)
}
Expand Down Expand Up @@ -319,12 +320,14 @@ export class GateHubService {
gateHubUser.id
)

const activeCard = cards.find((card) => card.status !== 'SoftDelete')

await this.createDefaultAccountAndWAForManagedUser(
userId,
true,
walletAddressName,
walletAddressPublicName,
cards[0].id
activeCard?.id
)

await User.query().findById(userId).patch({
Expand Down
20 changes: 15 additions & 5 deletions packages/wallet/backend/tests/cards/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,10 @@ describe('CardController', () => {
it('should get card details successfully', async () => {
const next = jest.fn()

const password = 'some password'
req.query = { publicKeyBase64: 'test-public-key' }
req.params = { cardId: 'test-card-id' }
req.body = { password }

const mockedCardDetails: ICardDetailsResponse = {
cipher: 'encrypted-card-data'
Expand All @@ -175,10 +177,14 @@ describe('CardController', () => {

await cardController.getCardDetails(req, res, next)

expect(mockCardService.getCardDetails).toHaveBeenCalledWith(userId, {
cardId: 'test-card-id',
publicKey: 'test-public-key'
})
expect(mockCardService.getCardDetails).toHaveBeenCalledWith(
userId,
password,
{
cardId: 'test-card-id',
publicKey: 'test-public-key'
}
)
expect(res.statusCode).toBe(200)
expect(res._getJSONData()).toEqual({
success: true,
Expand Down Expand Up @@ -517,7 +523,11 @@ describe('CardController', () => {
it('should get pin successfully', async () => {
const next = jest.fn()

const password = 'some password'
req.query = { publicKeyBase64: 'test-public-key' }
req.body = {
password
}

const mockedCardDetails: ICardDetailsResponse = {
cipher: 'encrypted-card-pin'
Expand All @@ -527,7 +537,7 @@ describe('CardController', () => {

await cardController.getPin(req, res, next)

expect(mockCardService.getPin).toHaveBeenCalledWith(userId, {
expect(mockCardService.getPin).toHaveBeenCalledWith(userId, password, {
cardId: 'test-card-id',
publicKey: 'test-public-key'
})
Expand Down
Loading