Skip to content

Commit d9cd155

Browse files
committed
wip
1 parent 516880f commit d9cd155

File tree

7 files changed

+139
-69
lines changed

7 files changed

+139
-69
lines changed

apps/engine/src/providers/pluggy/pluggy-api.ts

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { PluggyClient } from "pluggy-sdk";
2-
import type { ProviderParams } from "../types";
3-
import type { GetTransactionsParams } from "./types";
2+
import type {
3+
GetAccountBalanceResponse,
4+
GetConnectionStatusRequest,
5+
ProviderParams,
6+
} from "../types";
7+
import type { GetStatusResponse, GetTransactionsParams } from "./types";
48

59
export class PluggyApi {
610
#client: PluggyClient;
@@ -26,35 +30,57 @@ export class PluggyApi {
2630
}
2731

2832
async getAccounts(id: string) {
29-
return this.#client.fetchAccounts(id);
33+
const response = await this.#client.fetchAccounts(id);
34+
35+
return response.results;
3036
}
3137

3238
async getTransactions({ accountId, latest }: GetTransactionsParams) {
3339
if (latest) {
34-
return this.#client.fetchTransactions(accountId);
40+
const response = await this.#client.fetchTransactions(accountId);
41+
42+
return response.results;
3543
}
3644

37-
return this.#client.fetchAllTransactions(accountId);
45+
const response = await this.#client.fetchAllTransactions(accountId);
46+
47+
return response;
3848
}
3949

4050
async getHealthCheck() {
41-
// https://status.pluggy.ai/api/v2/status.json
42-
// return this.#client.healthCheck();
51+
try {
52+
const response = await fetch(
53+
"https://status.pluggy.ai/api/v2/status.json",
54+
);
55+
56+
const data = (await response.json()) as GetStatusResponse;
57+
58+
return (
59+
data.status.indicator === "none" ||
60+
data.status.indicator === "maintenance"
61+
);
62+
} catch {
63+
return false;
64+
}
4365
}
4466

45-
async getAccountBalance({
46-
accessToken,
47-
accountId,
48-
}: GetAccountBalanceRequest): Promise<
49-
GetAccountBalanceResponse | undefined
50-
> {}
67+
async getAccountBalance(
68+
accountId: string,
69+
): Promise<GetAccountBalanceResponse | undefined> {
70+
const response = await this.#client.fetchAccount(accountId);
71+
72+
return {
73+
currency: response.currencyCode,
74+
amount: response.balance,
75+
};
76+
}
5177

5278
async getInstitutions() {
5379
return this.#client.fetchConnectors();
5480
}
5581

56-
async getInstitutionById(id: string) {
57-
return this.#client.fetchConnectorById(id);
82+
async getInstitutionById(id: number) {
83+
return this.#client.fetchConnector(id);
5884
}
5985

6086
async getConnectionStatus({ id }: GetConnectionStatusRequest) {

apps/engine/src/providers/pluggy/pluggy-provider.ts

Lines changed: 32 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@ import type {
1010
ProviderParams,
1111
} from "../types";
1212
import { PluggyApi } from "./pluggy-api";
13-
import {
14-
transformAccount,
15-
transformAccountBalance,
16-
transformInstitution,
17-
transformTransaction,
18-
} from "./transform";
13+
import { transformAccount, transformTransaction } from "./transform";
1914

2015
export class PluggyProvider implements Provider {
2116
#api: PluggyApi;
@@ -24,62 +19,46 @@ export class PluggyProvider implements Provider {
2419
this.#api = new PluggyApi(params);
2520
}
2621

27-
// async getTransactions({
28-
// accessToken,
29-
// accountId,
30-
// accountType,
31-
// latest,
32-
// }: GetTransactionsRequest) {
33-
// if (!accessToken || !accountId) {
34-
// throw Error("accessToken or accountId is missing");
35-
// }
22+
async getTransactions({ accountId, latest }: GetTransactionsRequest) {
23+
if (!accountId) {
24+
throw Error("accountId is missing");
25+
}
3626

37-
// const response = await this.#api.getTransactions({
38-
// accessToken,
39-
// accountId,
40-
// latest,
41-
// });
27+
const response = await this.#api.getTransactions({
28+
accountId,
29+
latest,
30+
});
4231

43-
// return (response ?? []).map((transaction) =>
44-
// transformTransaction({
45-
// transaction,
46-
// accountType,
47-
// }),
48-
// );
49-
// }
32+
return (response ?? []).map(transformTransaction);
33+
}
5034

51-
// async getHealthCheck() {
52-
// return this.#api.getHealthCheck();
53-
// }
35+
async getHealthCheck() {
36+
return this.#api.getHealthCheck();
37+
}
5438

55-
// async getAccounts({ accessToken, institutionId }: GetAccountsRequest) {
56-
// if (!accessToken || !institutionId) {
57-
// throw Error("accessToken or institutionId is missing");
58-
// }
39+
async getAccounts({ id }: GetAccountsRequest) {
40+
if (!id) {
41+
throw Error("id is missing");
42+
}
5943

60-
// const response = await this.#api.getAccounts({
61-
// accessToken,
62-
// institutionId,
63-
// });
44+
const response = await this.#api.getAccounts(id);
6445

65-
// return (response ?? []).map(transformAccount);
66-
// }
46+
return (response ?? []).map(transformAccount);
47+
}
6748

68-
// async getAccountBalance({
69-
// accessToken,
70-
// accountId,
71-
// }: GetAccountBalanceRequest) {
72-
// if (!accessToken || !accountId) {
73-
// throw Error("Missing params");
74-
// }
49+
async getAccountBalance({ accountId }: GetAccountBalanceRequest) {
50+
if (!accountId) {
51+
throw Error("Missing params");
52+
}
7553

76-
// const response = await this.#api.getAccountBalance({
77-
// accessToken,
78-
// accountId,
79-
// });
54+
const response = await this.#api.getAccountBalance(accountId);
8055

81-
// return transformAccountBalance(response);
82-
// }
56+
if (!response) {
57+
throw Error("Account not found");
58+
}
59+
60+
return response;
61+
}
8362

8463
// async getInstitutions({ countryCode }: GetInstitutionsRequest) {
8564
// const response = await this.#api.getInstitutions({
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { getType } from "@/utils/account";
2+
import { capitalCase } from "change-case";
3+
import type { Account, AccountType, Transaction } from "pluggy-sdk";
4+
import type {
5+
Account as BaseAccount,
6+
Balance as BaseBalance,
7+
Transaction as BaseTransaction,
8+
} from "../types";
9+
10+
export const transformTransaction = (
11+
transaction: Transaction,
12+
): BaseTransaction => {
13+
return {
14+
id: transaction.id,
15+
amount: transaction.amount,
16+
date: transaction.date.toISOString(),
17+
currency: transaction.currencyCode,
18+
status: transaction.status === "POSTED" ? "posted" : "pending",
19+
balance: transaction.balance,
20+
category: transaction.category,
21+
method: transaction.type,
22+
description: null,
23+
currency_rate: null,
24+
currency_source: null,
25+
name: capitalCase(transaction.description),
26+
};
27+
};
28+
29+
export const transformAccount = (account: Account): BaseAccount => {
30+
return {
31+
id: account.id,
32+
name: account.name,
33+
currency: account.currencyCode,
34+
type: getType(account.type),
35+
institution: {
36+
id: "",
37+
name: "",
38+
logo: "",
39+
provider: "pluggy",
40+
},
41+
enrollment_id: null,
42+
balance: {
43+
amount: account.balance,
44+
currency: account.currencyCode,
45+
},
46+
};
47+
};

apps/engine/src/providers/pluggy/types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,17 @@ export type GetTransactionsParams = {
22
accountId: string;
33
latest?: boolean;
44
};
5+
6+
export type GetStatusResponse = {
7+
page: {
8+
id: string;
9+
name: string;
10+
url: string;
11+
time_zone: string;
12+
updated_at: string;
13+
};
14+
status: {
15+
indicator: string;
16+
description: string;
17+
};
18+
};

apps/engine/src/providers/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export type GetTransactionsRequest = {
6666
};
6767

6868
export type GetAccountsRequest = {
69-
id?: string; // GoCardLess
69+
id?: string; // GoCardLess & Pluggy
7070
accessToken?: string; // Teller & Plaid
7171
institutionId?: string; // Plaid
7272
};

apps/engine/src/utils/account.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import { type AccountType, getType } from "./account";
44
describe("getType function", () => {
55
it("should return 'depository' for 'depository' input", () => {
66
expect(getType("depository")).toBe("depository");
7+
expect(getType("BANK")).toBe("depository");
78
});
89

910
it("should return 'credit' for 'credit' input", () => {
1011
expect(getType("credit")).toBe("credit");
12+
expect(getType("CREDIT")).toBe("credit");
1113
});
1214

1315
it("should return 'other_asset' for any other input", () => {

apps/engine/src/utils/account.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ export type AccountType =
77

88
export function getType(type: string): AccountType {
99
switch (type) {
10+
case "BANK":
1011
case "depository":
1112
return "depository";
13+
case "CREDIT":
1214
case "credit":
1315
return "credit";
1416
default:

0 commit comments

Comments
 (0)