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(wallet/backend): add permanently block card endpoint #1662

Merged
merged 8 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions packages/wallet/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ export class App {
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.put(
'/cards/:cardId/block',
isAuth,
cardController.permanentlyBlockCard
)

// Return an error for invalid routes
router.use('*', (req: Request, res: CustomResponse) => {
Expand Down
26 changes: 25 additions & 1 deletion packages/wallet/backend/src/card/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ import {
getCardsByCustomerSchema,
getCardDetailsSchema,
lockCardSchema,
unlockCardSchema
unlockCardSchema,
permanentlyBlockCardSchema
} from './validation'

export interface ICardController {
getCardsByCustomer: Controller<ICardDetailsResponse[]>
getCardDetails: Controller<ICardResponse>
lock: Controller<ICardResponse>
unlock: Controller<ICardResponse>
permanentlyBlockCard: Controller<ICardResponse>
}

export class CardController implements ICardController {
Expand Down Expand Up @@ -97,4 +99,26 @@ export class CardController implements ICardController {
next(error)
}
}

public permanentlyBlockCard = async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
const userId = req.session.user.id
const { params, query } = await validate(permanentlyBlockCardSchema, req)
const { cardId } = params
const { reasonCode } = query

const result = await this.cardService.permanentlyBlockCard(
userId,
cardId,
reasonCode
)
res.status(200).json(toSuccessResponse(result))
} catch (error) {
next(error)
}
}
}
17 changes: 17 additions & 0 deletions packages/wallet/backend/src/card/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from './types'
import { LockReasonCode } from '@wallet/shared/src'
import { NotFound } from '@shared/backend'
import { BlockReasonCode } from '@wallet/shared/src'

export class CardService {
constructor(
Expand Down Expand Up @@ -51,4 +52,20 @@ export class CardService {
): Promise<ICardResponse> {
return this.gateHubClient.unlockCard(cardId, requestBody)
}

async permanentlyBlockCard(
userId: string,
cardId: string,
reasonCode: BlockReasonCode
): Promise<ICardResponse> {
const walletAddress = await this.walletAddressService.getByCardId(
Copy link
Contributor

Choose a reason for hiding this comment

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

replace with ensureWalletAddressExists

userId,
cardId
)
if (!walletAddress) {
throw new NotFound('Card not found or not associated with the user.')
}

return this.gateHubClient.permanentlyBlockCard(cardId, reasonCode)
}
}
20 changes: 20 additions & 0 deletions packages/wallet/backend/src/card/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,23 @@ export const unlockCardSchema = z.object({
note: z.string()
})
})

export const permanentlyBlockCardSchema = z.object({
params: z.object({
cardId: z.string()
}),
query: z.object({
reasonCode: z.enum([
'LostCard',
'StolenCard',
'IssuerRequestGeneral',
'IssuerRequestFraud',
'IssuerRequestLegal',
'IssuerRequestIncorrectOpening',
'CardDamagedOrNotWorking',
'UserRequest',
'IssuerRequestCustomerDeceased',
'ProductDoesNotRenew'
])
})
})
12 changes: 12 additions & 0 deletions packages/wallet/backend/src/gatehub/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
ICardLockRequest,
ICardUnlockRequest
} from '@/card/types'
import { BlockReasonCode } from '@wallet/shared/src'

export class GateHubClient {
private clientIds = SANDBOX_CLIENT_IDS
Expand Down Expand Up @@ -407,6 +408,17 @@ export class GateHubClient {
)
}

async permanentlyBlockCard(
cardId: string,
reasonCode: BlockReasonCode
): Promise<ICardResponse> {
let url = `${this.apiUrl}/v1/cards/${cardId}/block`

url += `?reasonCode=${encodeURIComponent(reasonCode)}`

return this.request<ICardResponse>('PUT', url)
}

private async request<T>(
method: HTTP_METHODS,
url: string,
Expand Down
52 changes: 51 additions & 1 deletion packages/wallet/backend/tests/cards/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ describe('CardController', () => {
getCardsByCustomer: jest.fn(),
getCardDetails: jest.fn(),
lock: jest.fn(),
unlock: jest.fn()
unlock: jest.fn(),
permanentlyBlockCard: jest.fn()
}

const args = mockLogInRequest().body
Expand Down Expand Up @@ -332,4 +333,53 @@ describe('CardController', () => {
})
})
})

describe('permanentlyBlockCard', () => {
it('should get block card successfully', async () => {
const next = jest.fn()

mockCardService.permanentlyBlockCard.mockResolvedValue({})

req.params = { cardId: 'test-card-id' }
req.query = { reasonCode: 'StolenCard' }

await cardController.permanentlyBlockCard(req, res, next)

expect(mockCardService.permanentlyBlockCard).toHaveBeenCalledWith(
userId,
'test-card-id',
'StolenCard'
)
expect(res.statusCode).toBe(200)
expect(res._getJSONData()).toEqual({
success: true,
message: 'SUCCESS',
result: {}
})
})
it('should return 400 if reasonCode is invalid', async () => {
const next = jest.fn()

req.params = { cardId: 'test-card-id' }
req.query = { reasonCode: 'InvalidCode' }

await cardController.permanentlyBlockCard(req, res, (err) => {
next(err)
res.status(err.statusCode).json({
success: false,
message: err.message
})
})

expect(next).toHaveBeenCalled()
const error = next.mock.calls[0][0]
expect(error).toBeInstanceOf(BadRequest)
expect(error.message).toBe('Invalid input')
expect(res.statusCode).toBe(400)
expect(res._getJSONData()).toEqual({
success: false,
message: 'Invalid input'
})
})
})
})
12 changes: 12 additions & 0 deletions packages/wallet/shared/src/types/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,15 @@ export type LockReasonCode =
| 'IssuerRequestGeneral'
| 'IssuerRequestFraud'
| 'IssuerRequestLegal'

export type BlockReasonCode =
| 'LostCard'
| 'StolenCard'
| 'IssuerRequestGeneral'
| 'IssuerRequestFraud'
| 'IssuerRequestLegal'
| 'IssuerRequestIncorrectOpening'
| 'CardDamagedOrNotWorking'
| 'UserRequest'
| 'IssuerRequestCustomerDeceased'
| 'ProductDoesNotRenew'
Loading