Skip to content

Commit eb12654

Browse files
authored
- closeAccountRequest changed from class to interface (#268)
- added create close account request helper function - added a test for close account
1 parent fb3a0f5 commit eb12654

File tree

4 files changed

+35
-23
lines changed

4 files changed

+35
-23
lines changed

helpers.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AddAuthorizedUsersRequest, MobileWalletPayloadRequest, RemoveAuthorizedUsersRequest } from "./types"
1+
import { AddAuthorizedUsersRequest, CloseAccountRequest, CloseReason, MobileWalletPayloadRequest, RemoveAuthorizedUsersRequest } from "./types"
22
import { State, Address, FullName, Phone, Status, Title, Officer, BeneficialOwner, BusinessContact, AuthorizedUser, Counterparty, Coordinates, UsAddress, InternationalAddress, Relationship, RelationshipsArrayData } from "./types/common"
33
export function createUsAddress(street: string, street2: string | null, city: string, state: State | null, postalCode: string, country: "US"): UsAddress {
44
return {
@@ -157,3 +157,15 @@ export function createMobileWalletRequest(cardId: string, signedNonce: string):
157157
}
158158
}
159159

160+
export function createCloseAccountRequest(accountId: string, closeReason: CloseReason = "ByCustomer" ): CloseAccountRequest {
161+
return {
162+
accountId: accountId,
163+
data: {
164+
type: "accountClose",
165+
attributes: {
166+
reason: closeReason
167+
}
168+
}
169+
}
170+
}
171+

resources/account.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class Accounts extends BaseResource {
2323
}
2424

2525
public async closeAccount(request: CloseAccountRequest): Promise<UnitResponse<Account>> {
26-
return this.httpPost<UnitResponse<Account>>(`/${request.accountId}/close`, request.to_json())
26+
return this.httpPost<UnitResponse<Account>>(`/${request.accountId}/close`, {data: request.data})
2727
}
2828

2929
public async reopenAccount(accountId: string): Promise<UnitResponse<Account>> {

tests/accounts.spec.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Unit } from "../unit"
22
import { AccountOwnersRequest, DepositAccount } from "../types/account"
33

44
import dotenv from "dotenv"
5-
import { createRelationshipArray } from "../helpers"
5+
import { createCloseAccountRequest, createRelationshipArray } from "../helpers"
66
import { createIndividualAccount, createIndividualCustomer } from "./testHelpers"
77
dotenv.config()
88
const unit = new Unit(process.env.UNIT_TOKEN || "test", process.env.UNIT_API_URL || "test")
@@ -40,7 +40,7 @@ describe("Create Account", () => {
4040
})
4141

4242
describe("Add Account Owners", () => {
43-
test("add account owners", async () => {
43+
test("Add and remove owners", async () => {
4444
const new_account = (await createIndividualAccount(unit)).data as DepositAccount
4545
const originated_customer_id = new_account.relationships.customer?.data.id
4646
const customer_id1 = await createIndividualCustomer(unit)
@@ -71,3 +71,15 @@ describe("Add Account Owners", () => {
7171
})
7272
})
7373

74+
describe("Close Account", () => {
75+
test("Close Deposit Account", async () => {
76+
const res = await createIndividualAccount(unit)
77+
const account = (await unit.accounts.get(res.data.id)).data
78+
expect(account.type).toBe("depositAccount")
79+
80+
const closedAccount = (await unit.accounts.closeAccount(createCloseAccountRequest(account.id))).data as DepositAccount
81+
expect(account.type).toBe("depositAccount")
82+
expect(closedAccount.attributes.status).toBe("Closed")
83+
})
84+
})
85+

types/account.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -292,28 +292,16 @@ export interface AccountDepositProduct {
292292
}
293293
}
294294

295-
type CloseReason = "ByCustomer" | "Fraud"
295+
export type CloseReason = "ByCustomer" | "Fraud"
296296

297-
export class CloseAccountRequest {
298-
public accountId: string
299-
public reason: CloseReason
300-
301-
constructor(accountId: string, reason: CloseReason = "ByCustomer") {
302-
this.accountId = accountId
303-
this.reason = reason
304-
}
297+
export interface CloseAccountRequest {
298+
accountId: string
305299

306-
public to_json(): any {
307-
const data: any = {
308-
"data": {
309-
"type": "accountClose",
310-
"attributes": {
311-
"reason": this.reason
312-
}
313-
}
300+
data: {
301+
type: "accountClose"
302+
attributes: {
303+
reason: CloseReason
314304
}
315-
316-
return data
317305
}
318306
}
319307

0 commit comments

Comments
 (0)