Skip to content

Commit

Permalink
Rearrange functions and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sanducb committed Oct 2, 2024
1 parent bef1cbf commit 3f87a39
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 226 deletions.
13 changes: 7 additions & 6 deletions packages/wallet/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,20 +313,21 @@ export class App {
cardController.getCardsByCustomer
)
router.get('/cards/:cardId/details', isAuth, cardController.getCardDetails)
router.put('/cards/:cardId/lock', isAuth, cardController.lock)
router.put('/cards/:cardId/unlock', isAuth, cardController.unlock)
router.get(
'/cards/:cardId/transactions',
isAuth,
cardController.getCardTransactions
)
router.get('/cards/:cardId/limits', isAuth, cardController.getCardLimits)
router.post(
'/cards/:cardId/limits',
isAuth,
cardController.createOrOverrideCardLimits
) router.get(
'/cards/:cardId/transactions',
isAuth,
cardController.getCardTransactions
)
router.get('/cards/:cardId/pin', isAuth, cardController.getPin)
router.post('/cards/:cardId/change-pin', isAuth, cardController.changePin)
router.put('/cards/:cardId/lock', isAuth, cardController.lock)
router.put('/cards/:cardId/unlock', isAuth, cardController.unlock)
router.put(
'/cards/:cardId/block',
isAuth,
Expand Down
60 changes: 30 additions & 30 deletions packages/wallet/backend/src/card/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,76 +103,76 @@ export class CardController implements ICardController {
}
}

public getPin = async (req: Request, res: Response, next: NextFunction) => {
public getCardLimits = async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
const userId = req.session.user.id
const { params, query } = await validate(getCardDetailsSchema, req)
const { params } = await validate(getCardLimitsSchema, req)
const { cardId } = params
const { publicKeyBase64 } = query

const requestBody: ICardDetailsRequest = { cardId, publicKeyBase64 }
const cardPin = await this.cardService.getPin(userId, requestBody)
res.status(200).json(toSuccessResponse(cardPin))
const limits = await this.cardService.getCardLimits(userId, cardId)
res.status(200).json(toSuccessResponse(limits))
} catch (error) {
next(error)
}
}

public changePin = async (
public createOrOverrideCardLimits = async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
const userId = req.session.user.id
const { params, body } = await validate(changePinSchema, req)
const { params, body } = await validate(
createOrOverrideCardLimitsSchema,
req
)
const { cardId } = params
const { cypher } = body
const requestBody: ICardLimitRequest[] = body

const result = await this.cardService.createOrOverrideCardLimits(
userId,
cardId,
requestBody
)

const result = await this.cardService.changePin(userId, cardId, cypher)
res.status(201).json(toSuccessResponse(result))
} catch (error) {
next(error)
}
}

public getCardLimits = async (
req: Request,
res: Response,
next: NextFunction
) => {
public getPin = async (req: Request, res: Response, next: NextFunction) => {
try {
const userId = req.session.user.id
const { params } = await validate(getCardLimitsSchema, req)
const { params, query } = await validate(getCardDetailsSchema, req)
const { cardId } = params
const { publicKeyBase64 } = query

const limits = await this.cardService.getCardLimits(userId, cardId)
res.status(200).json(toSuccessResponse(limits))
const requestBody: ICardDetailsRequest = { cardId, publicKeyBase64 }
const cardPin = await this.cardService.getPin(userId, requestBody)
res.status(200).json(toSuccessResponse(cardPin))
} catch (error) {
next(error)
}
}

public createOrOverrideCardLimits = async (
public changePin = async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
const userId = req.session.user.id
const { params, body } = await validate(
createOrOverrideCardLimitsSchema,
req
)
const { params, body } = await validate(changePinSchema, req)
const { cardId } = params
const requestBody: ICardLimitRequest[] = body

const result = await this.cardService.createOrOverrideCardLimits(
userId,
cardId,
requestBody
)
const { cypher } = body

const result = await this.cardService.changePin(userId, cardId, cypher)
res.status(201).json(toSuccessResponse(result))
} catch (error) {
next(error)
Expand Down
35 changes: 11 additions & 24 deletions packages/wallet/backend/src/card/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ export class CardService {
return this.gateHubClient.getCardDetails(requestBody)
}

async getCardTransactions(
userId: string,
cardId: string,
pageSize?: number,
pageNumber?: number
): Promise<IGetTransactionsResponse> {
await this.ensureWalletAddressExists(userId, cardId)

return this.gateHubClient.getCardTransactions(cardId, pageSize, pageNumber)
}

async getCardLimits(
userId: string,
cardId: string
Expand All @@ -53,17 +64,6 @@ export class CardService {
return this.gateHubClient.createOrOverrideCardLimits(cardId, requestBody)
}

async getCardTransactions(
userId: string,
cardId: string,
pageSize?: number,
pageNumber?: number
): Promise<IGetTransactionsResponse> {
await this.ensureWalletAddressExists(userId, cardId)

return this.gateHubClient.getCardTransactions(cardId, pageSize, pageNumber)
}

async getPin(
userId: string,
requestBody: ICardDetailsRequest
Expand Down Expand Up @@ -127,17 +127,4 @@ export class CardService {
throw new NotFound('Card not found or not associated with the user.')
}
}

private async ensureWalletAddressExists(
userId: string,
cardId: string
): Promise<void> {
const walletAddress = await this.walletAddressService.getByCardId(
userId,
cardId
)
if (!walletAddress) {
throw new NotFound('Card not found or not associated with the user.')
}
}
}
48 changes: 24 additions & 24 deletions packages/wallet/backend/src/gatehub/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,30 @@ export class GateHubClient {
return this.request<IGetTransactionsResponse>('GET', url)
}

async getCardLimits(cardId: string): Promise<ICardLimitResponse[]> {
const url = `${this.apiUrl}/v1/cards/${cardId}/limits`

return this.request<ICardLimitResponse[]>('GET', url, undefined, {
cardAppId: this.env.GATEHUB_CARD_APP_ID
})
}

async createOrOverrideCardLimits(
cardId: string,
requestBody: ICardLimitRequest[]
): Promise<ICardLimitResponse[]> {
const url = `${this.apiUrl}/v1/cards/${cardId}/limits`

return this.request<ICardLimitResponse[]>(
'POST',
url,
JSON.stringify(requestBody),
{
cardAppId: this.env.GATEHUB_CARD_APP_ID
}
)
}

async getPin(
requestBody: ICardDetailsRequest
): Promise<ICardDetailsResponse> {
Expand Down Expand Up @@ -471,30 +495,6 @@ export class GateHubClient {
)
}

async getCardLimits(cardId: string): Promise<ICardLimitResponse[]> {
const url = `${this.apiUrl}/v1/cards/${cardId}/limits`

return this.request<ICardLimitResponse[]>('GET', url, undefined, {
cardAppId: this.env.GATEHUB_CARD_APP_ID
})
}

async createOrOverrideCardLimits(
cardId: string,
requestBody: ICardLimitRequest[]
): Promise<ICardLimitResponse[]> {
const url = `${this.apiUrl}/v1/cards/${cardId}/limits`

return this.request<ICardLimitResponse[]>(
'POST',
url,
JSON.stringify(requestBody),
{
cardAppId: this.env.GATEHUB_CARD_APP_ID
}
)
}

async lockCard(
cardId: string,
reasonCode: LockReasonCode,
Expand Down
Loading

0 comments on commit 3f87a39

Please sign in to comment.