Skip to content

Commit

Permalink
feat(wallet/frontend): add temporary card service (#1554)
Browse files Browse the repository at this point in the history
* Add initial cards api

* Add card service mock

* Format
  • Loading branch information
raducristianpopa authored Sep 6, 2024
1 parent f80db89 commit bf9844a
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 0 deletions.
175 changes: 175 additions & 0 deletions packages/wallet/frontend/src/lib/api/card.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import {
getError,
httpClient,
type ErrorResponse,
type SuccessResponse
} from '../httpClient'

// TODO: update interface - can be moved to shared folder as well
export interface IUserCard {
name: string
number: string
expiry: string
cvv: number
isFrozen: boolean
}

type GetDetailsResponse = SuccessResponse<IUserCard>
type GetDetailsResult = GetDetailsResponse | ErrorResponse

type TerminateCardResult = SuccessResponse<boolean> | ErrorResponse

type FreezeResult = SuccessResponse<boolean> | ErrorResponse

type UnfreezeResult = SuccessResponse<boolean> | ErrorResponse

type UpdateSpendingLimitResult = SuccessResponse | ErrorResponse

type ChangePinResult = SuccessResponse | ErrorResponse

interface UserCardService {
getDetails(cookies?: string): Promise<GetDetailsResult>
terminate(): Promise<TerminateCardResult>
freeze(): Promise<FreezeResult>
unfreeze(): Promise<UnfreezeResult>
updateSpendingLimit(): Promise<UpdateSpendingLimitResult>
changePin(): Promise<ChangePinResult>
}

const createCardService = (): UserCardService => ({
async getDetails(cookies) {
try {
const response = await httpClient
.get(`card`, {
headers: {
...(cookies ? { Cookie: cookies } : {})
}
})
.json<GetDetailsResponse>()
return response
} catch (error) {
return getError(error, '[TODO] UPDATE ME!')
}
},

async terminate() {
try {
const response = await httpClient
.post('card/terminate')
.json<SuccessResponse>()
return response
} catch (error) {
return getError(error, '[TODO] UPDATE ME!')
}
},

async freeze() {
try {
const response = await httpClient
.post('card/freeze')
.json<SuccessResponse>()
return response
} catch (error) {
return getError(error, '[TODO] UPDATE ME!')
}
},

async unfreeze() {
try {
const response = await httpClient
.post('card/unfreeze')
.json<SuccessResponse>()
return response
} catch (error) {
return getError(error, '[TODO] UPDATE ME!')
}
},

async updateSpendingLimit() {
try {
const response = await httpClient
.post('card/update-spending-limit')
.json<SuccessResponse>()
return response
} catch (error) {
return getError(error, '[TODO] UPDATE ME!')
}
},

async changePin() {
try {
const response = await httpClient
.post('card/change-pin')
.json<SuccessResponse>()
return response
} catch (error) {
return getError(error, '[TODO] UPDATE ME!')
}
}
})

const mock = (): UserCardService => ({
async getDetails() {
return Promise.resolve({
success: true,
message: 'Mocked getDetails',
result: {
name: 'John Doe',
number: '4242 4242 4242 4242',
expiry: '01/27',
cvv: 321,
isFrozen: Math.random() > 0.5 ? true : false
}
})
},

async terminate() {
return Promise.resolve({
success: true,
message: 'Mocked terminate',
result: true
})
},

async freeze() {
return Promise.resolve({
success: true,
message: 'Mocked freeze',
result: true
})
},

async unfreeze() {
return Promise.resolve({
success: true,
message: 'Mocked unfreeze',
result: true
})
},

async updateSpendingLimit() {
try {
const response = await httpClient
.post('card/update-spending-limit')
.json<SuccessResponse>()
return response
} catch (error) {
return getError(error, '[TODO] UPDATE ME!')
}
},

async changePin() {
try {
const response = await httpClient
.post('card/change-pin')
.json<SuccessResponse>()
return response
} catch (error) {
return getError(error, '[TODO] UPDATE ME!')
}
}
})

const cardService = createCardService()
const cardServiceMock = mock()
export { cardService, cardServiceMock }
1 change: 1 addition & 0 deletions packages/wallet/frontend/src/pages/card/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function CardContainer() {
</div>
)
}

const UserCardPage: NextPageWithLayout = () => {
return (
<>
Expand Down

0 comments on commit bf9844a

Please sign in to comment.