Skip to content

Commit 1e5230a

Browse files
axshaniilyamerman
andauthored
Check deposits: Get front/back image (#226)
* getImages, tests needed * tests * another try to getImage * getImage will return string * remove comments * lint-fix * update test * add response encoding and type Co-authored-by: Ilya Merman <[email protected]>
1 parent 09443b2 commit 1e5230a

File tree

4 files changed

+74
-10
lines changed

4 files changed

+74
-10
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"homepage": "https://github.com/unit-finance/unit-node-sdk#readme",
2323
"devDependencies": {
2424
"@types/jest": "^27.0.1",
25-
"@types/node": "^15.0.2",
25+
"@types/node": "^15.14.9",
2626
"@typescript-eslint/eslint-plugin": "^5.1.0",
2727
"@typescript-eslint/parser": "^5.1.0",
2828
"dotenv": "^10.0.0",

resources/checkDeposit.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Account, Customer, Transaction } from "../types"
2-
import { CheckDeposit, CreateCheckDepositRequest, PatchCheckDepositRequest, UploadCheckDepositRequest } from "../types/checkDeposit"
3-
import { UnitResponse, Include, UnitConfig, BaseListParams } from "../types/common"
2+
import { CheckDeposit, CreateCheckDepositRequest, PatchCheckDepositRequest, UploadCheckDepositRequest } from "../types"
3+
import { UnitResponse, Include, UnitConfig, BaseListParams } from "../types"
44
import { BaseResource } from "./baseResource"
5+
import {responseEncoding, ResponseType} from "axios"
56

67
export class CheckDeposits extends BaseResource {
78

@@ -44,6 +45,11 @@ export class CheckDeposits extends BaseResource {
4445

4546
return this.httpPut<UnitResponse<CheckDeposit>>(path, request.file, {headers})
4647
}
48+
49+
public async getImage(id: string, front = true, responseEncoding: responseEncoding = "binary", responseType: ResponseType = "blob"): Promise<string> {
50+
const p = front ? "front" : "back"
51+
return this.httpGet<string>(`/${id}/${p}`, {responseEncoding, responseType})
52+
}
4753
}
4854

4955
export interface CheckDepositListParams extends BaseListParams {

tests/checkDeposits.spec.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { CreateCheckDepositRequest, Unit, UploadCheckDepositRequest } from "../unit"
2+
import axios from "axios"
3+
import dotenv from "dotenv"
4+
import { createIndividualAccount } from "./testHelpers"
5+
dotenv.config()
6+
const unit = new Unit(process.env.UNIT_TOKEN || "test", process.env.UNIT_API_URL || "test")
7+
8+
async function createCheckDepositReqeust(amount = 20000): Promise<CreateCheckDepositRequest> {
9+
const accountId = (await createIndividualAccount(unit)).data.id
10+
return {
11+
"type": "checkDeposit",
12+
"attributes": {
13+
"amount": amount,
14+
"description": "Check deposit"
15+
},
16+
"relationships": {
17+
"account": {
18+
"data": {
19+
"type": "depositAccount",
20+
"id": accountId
21+
}
22+
}
23+
}
24+
}
25+
}
26+
27+
describe("CheckDeposit Create Test", () => {
28+
test("Create CheckDeposit", async () => {
29+
const req = await createCheckDepositReqeust()
30+
const res = await unit.checkDeposits.create(req)
31+
expect(res.data.type).toBe("checkDeposit")
32+
expect(res.data.attributes.amount).toBe(20000)
33+
})
34+
})
35+
36+
describe("Upload Check", () => {
37+
test("Create CheckDeposit and upload image", async () => {
38+
const createReq = await createCheckDepositReqeust(15000)
39+
const checkDeposit = await unit.checkDeposits.create(createReq)
40+
expect(checkDeposit.data.type).toBe("checkDeposit")
41+
expect(checkDeposit.data.attributes.amount).toBe(15000)
42+
43+
const checkDepositId = checkDeposit.data.id
44+
45+
const response = await axios.get("https://image.shutterstock.com/image-illustration/blank-check-design-open-spacing-260nw-76703974.jpg", { responseType: "arraybuffer" })
46+
const buffer = Buffer.from(response.data, "utf-8")
47+
const uploadReq: UploadCheckDepositRequest = {
48+
checkDepositId: checkDepositId,
49+
file: buffer
50+
}
51+
52+
const uploadRes = await unit.checkDeposits.upload(uploadReq)
53+
expect(uploadRes.data.type).toBe("checkDeposit")
54+
55+
const image = await unit.checkDeposits.getImage(checkDepositId)
56+
expect(image).not.toBeNull()
57+
})
58+
})

0 commit comments

Comments
 (0)