|
| 1 | +import { CreateBusinessCreditCardRequest, CreateBusinessVirtualCreditCardRequest, Unit } from "../unit" |
| 2 | +import { CloseAccountRequest, CreditAccount } from "../types/account" |
| 3 | + |
| 4 | +import dotenv from "dotenv" |
| 5 | +import { createCreditAccount } from "./testHelpers" |
| 6 | +import { createAddress, createFullName, createPhone } from "../helpers" |
| 7 | +dotenv.config() |
| 8 | +const unit = new Unit(process.env.UNIT_TOKEN || "test", process.env.UNIT_API_URL || "test") |
| 9 | +const accountsId: string[] = [] |
| 10 | + |
| 11 | +async function createAccount() { |
| 12 | + const res = await createCreditAccount(unit) |
| 13 | + const account = await unit.accounts.get(res.data.id) |
| 14 | + expect(account.data.type).toBe("creditAccount") |
| 15 | + return account.data.id |
| 16 | +} |
| 17 | + |
| 18 | +async function closeAccount(accountId: string) { |
| 19 | + const req: CloseAccountRequest = { |
| 20 | + accountId, |
| 21 | + data: { |
| 22 | + type: "creditAccountClose", |
| 23 | + attributes: { |
| 24 | + reason: "Overdue" |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + const res = await unit.accounts.closeAccount(req) |
| 30 | + expect(res.data.type).toBe("creditAccount") |
| 31 | + return res |
| 32 | +} |
| 33 | + |
| 34 | +describe("Credit Accounts List", () => { |
| 35 | + let accountId = "" |
| 36 | + |
| 37 | + beforeAll(async () => { |
| 38 | + accountId = await createAccount() |
| 39 | + }) |
| 40 | + |
| 41 | + afterAll(async () => { |
| 42 | + await closeAccount(accountId) |
| 43 | + }) |
| 44 | + |
| 45 | + test("Get Credit Accounts List", async () => { |
| 46 | + const res = await unit.accounts.list({type: "credit"}) |
| 47 | + res.data.forEach(element => { |
| 48 | + expect(element.type).toBe("creditAccount") |
| 49 | + accountsId.push(element.id) |
| 50 | + }) |
| 51 | + |
| 52 | + accountsId.forEach(async id => { |
| 53 | + const res = await unit.accounts.get(id) |
| 54 | + expect(res.data.type).toBe("creditAccount") |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + test("Get accounts list with included customer", async () => { |
| 59 | + accountsId.forEach(async id => { |
| 60 | + const res = await unit.accounts.get(id, "customer") |
| 61 | + expect(res.included && res.included.length > 0).toBeTruthy() |
| 62 | + }) |
| 63 | + }) |
| 64 | +}) |
| 65 | + |
| 66 | +describe("Create Account", () => { |
| 67 | + test("Create Credit Account", async () => { |
| 68 | + const accountId = await createAccount() |
| 69 | + const res = await closeAccount(accountId) |
| 70 | + expect(res.data.id).toBe(accountId) |
| 71 | + |
| 72 | + }) |
| 73 | +}) |
| 74 | + |
| 75 | +describe("Test DTO structure", () => { |
| 76 | + test("Test Credit Account DTO", () => { |
| 77 | + const creditAccount: CreditAccount = { |
| 78 | + "type": "creditAccount", |
| 79 | + "id": "42", |
| 80 | + "attributes": { |
| 81 | + "createdAt": "2000-05-11T10:19:30.409Z", |
| 82 | + "name": "Peter Parker", |
| 83 | + "status": "Open", |
| 84 | + "creditTerms": "credit_terms_1", |
| 85 | + "currency": "USD", |
| 86 | + "balance": 10000, |
| 87 | + "hold": 0, |
| 88 | + "available": 10000, |
| 89 | + "tags": { |
| 90 | + "purpose": "some_purpose" |
| 91 | + }, |
| 92 | + "creditLimit": 200000 |
| 93 | + }, |
| 94 | + "relationships": { |
| 95 | + "customer": { |
| 96 | + "data": { |
| 97 | + "type": "customer", |
| 98 | + "id": "45555" |
| 99 | + } |
| 100 | + }, |
| 101 | + "org": { |
| 102 | + "data": { |
| 103 | + "type": "org", |
| 104 | + "id": "1" |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + expect(creditAccount.type).toBe("creditAccount") |
| 111 | + }) |
| 112 | +}) |
| 113 | + |
| 114 | +describe("Credit Credit Card", () => { |
| 115 | + test("Create Business Credit Card", async () => { |
| 116 | + const accountId = await createAccount() |
| 117 | + |
| 118 | + const req: CreateBusinessCreditCardRequest = { |
| 119 | + type: "businessCreditCard", |
| 120 | + attributes: { |
| 121 | + fullName: createFullName("Richard", "Hendricks"), |
| 122 | + ssn: "123456789", |
| 123 | + address: createAddress("5230 Newell Rd", null, "Palo Alto", "CA", "94303", "US"), |
| 124 | + shippingAddress: createAddress("5230 Newell Rd", null, "Palo Alto", "CA", "94303", "US"), |
| 125 | + dateOfBirth: "2001-08-10", |
| 126 | + |
| 127 | + phone: createPhone("1", "5555555555") |
| 128 | + }, |
| 129 | + relationships: { |
| 130 | + account: { |
| 131 | + data: { |
| 132 | + type: "creditAccount", |
| 133 | + id: accountId |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + const res = await unit.cards.create(req) |
| 140 | + expect(res.data.type).toBe("businessCreditCard") |
| 141 | + |
| 142 | + const closeRes = await unit.cards.closeCard(res.data.id) |
| 143 | + expect(closeRes.data.id).toBe(res.data.id) |
| 144 | + }) |
| 145 | + |
| 146 | + test("Create Business Virtual Credit Card", async () => { |
| 147 | + const accountId = await createAccount() |
| 148 | + |
| 149 | + const req: CreateBusinessVirtualCreditCardRequest = { |
| 150 | + type: "businessVirtualCreditCard", |
| 151 | + attributes: { |
| 152 | + fullName: createFullName("Richard", "Hendricks"), |
| 153 | + ssn: "123456789", |
| 154 | + address: createAddress("5230 Newell Rd", null, "Palo Alto", "CA", "94303", "US"), |
| 155 | + dateOfBirth: "2001-08-10", |
| 156 | + |
| 157 | + phone: createPhone("1", "5555555555") |
| 158 | + }, |
| 159 | + relationships: { |
| 160 | + account: { |
| 161 | + data: { |
| 162 | + type: "creditAccount", |
| 163 | + id: accountId |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + const res = await unit.cards.create(req) |
| 170 | + expect(res.data.type).toBe("businessVirtualCreditCard") |
| 171 | + |
| 172 | + const closeRes = await unit.cards.closeCard(res.data.id) |
| 173 | + expect(closeRes.data.id).toBe(res.data.id) |
| 174 | + }) |
| 175 | +}) |
| 176 | + |
0 commit comments