Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Dec 8, 2024
1 parent d9cd155 commit 793a693
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 47 deletions.
2 changes: 1 addition & 1 deletion apps/engine/src/common/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const GeneralErrorSchema = z.object({
}),
});

export const Providers = z.enum(["teller", "plaid", "gocardless"]);
export const Providers = z.enum(["teller", "plaid", "gocardless", "pluggy"]);

export const HeadersSchema = z.object({
authorization: z.string().openapi({
Expand Down
24 changes: 19 additions & 5 deletions apps/engine/src/providers/pluggy/pluggy-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import type {
GetConnectionStatusRequest,
ProviderParams,
} from "../types";
import type { GetStatusResponse, GetTransactionsParams } from "./types";
import type {
GetAccountsRequest,
GetInstitutionsRequest,
GetStatusResponse,
GetTransactionsParams,
} from "./types";

export class PluggyApi {
#client: PluggyClient;
Expand All @@ -29,10 +34,15 @@ export class PluggyApi {
return "https://app.midday.ai/api/webhook/pluggy";
}

async getAccounts(id: string) {
async getAccounts({ id, institutionId }: GetAccountsRequest) {
const response = await this.#client.fetchAccounts(id);

return response.results;
const institution = await this.getInstitutionById(Number(institutionId));

return response.results.map((account) => ({
...account,
institution,
}));
}

async getTransactions({ accountId, latest }: GetTransactionsParams) {
Expand Down Expand Up @@ -75,8 +85,12 @@ export class PluggyApi {
};
}

async getInstitutions() {
return this.#client.fetchConnectors();
async getInstitutions({ countries }: GetInstitutionsRequest) {
const response = await this.#client.fetchConnectors({
countries,
});

return response.results;
}

async getInstitutionById(id: number) {
Expand Down
76 changes: 41 additions & 35 deletions apps/engine/src/providers/pluggy/pluggy-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import type {
ProviderParams,
} from "../types";
import { PluggyApi } from "./pluggy-api";
import { transformAccount, transformTransaction } from "./transform";
import {
transformAccount,
transformBalance,
transformInstitution,
transformTransaction,
} from "./transform";

export class PluggyProvider implements Provider {
#api: PluggyApi;
Expand All @@ -36,19 +41,22 @@ export class PluggyProvider implements Provider {
return this.#api.getHealthCheck();
}

async getAccounts({ id }: GetAccountsRequest) {
if (!id) {
throw Error("id is missing");
async getAccounts({ id, institutionId }: GetAccountsRequest) {
if (!id || !institutionId) {
throw Error("id or institutionId is missing");
}

const response = await this.#api.getAccounts(id);
const response = await this.#api.getAccounts({
id,
institutionId,
});

return (response ?? []).map(transformAccount);
}

async getAccountBalance({ accountId }: GetAccountBalanceRequest) {
if (!accountId) {
throw Error("Missing params");
throw Error("accountId is missing");
}

const response = await this.#api.getAccountBalance(accountId);
Expand All @@ -57,42 +65,40 @@ export class PluggyProvider implements Provider {
throw Error("Account not found");
}

return response;
return transformBalance(response);
}

// async getInstitutions({ countryCode }: GetInstitutionsRequest) {
// const response = await this.#api.getInstitutions({
// countryCode,
// });

// return response.map(transformInstitution);
// }
async getInstitutions({ countryCode }: GetInstitutionsRequest) {
if (!countryCode) {
throw Error("countryCode is missing");
}

// async deleteAccounts({ accessToken }: DeleteAccountsRequest) {
// if (!accessToken) {
// throw Error("accessToken is missing");
// }
const response = await this.#api.getInstitutions({
countries: [countryCode],
});

// await this.#api.deleteAccounts({
// accessToken,
// });
// }
return response.map(transformInstitution);
}

// async getConnectionStatus({ accessToken }: GetConnectionStatusRequest) {
// if (!accessToken) {
// throw Error("accessToken is missing");
// }
async deleteAccounts({ accessToken }: DeleteAccountsRequest) {
if (!accessToken) {
throw Error("accessToken is missing");
}

// const response = await this.#api.getConnectionStatus({ accessToken });
return;
}

// return response;
// }
async getConnectionStatus({ accessToken }: GetConnectionStatusRequest) {
if (!accessToken) {
throw Error("accessToken is missing");
}

// async deleteConnection({ accessToken }: DeleteConnectionRequest) {
// if (!accessToken) {
// throw Error("accessToken is missing");
// }
return null;
}

// await this.#api.deleteAccounts({ accessToken });
// }
async deleteConnection({ accessToken }: DeleteConnectionRequest) {
if (!accessToken) {
throw Error("accessToken is missing");
}
}
}
28 changes: 22 additions & 6 deletions apps/engine/src/providers/pluggy/transform.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Providers } from "@/common/schema";
import { getType } from "@/utils/account";
import { getLogoURL } from "@/utils/logo";
import { capitalCase } from "change-case";
import type { Account, AccountType, Transaction } from "pluggy-sdk";
import type { Account, Connector, Transaction } from "pluggy-sdk";
import type {
Account as BaseAccount,
Balance as BaseBalance,
Expand All @@ -26,17 +28,19 @@ export const transformTransaction = (
};
};

export const transformAccount = (account: Account): BaseAccount => {
export const transformAccount = (
account: Account & { institution: Connector },
): BaseAccount => {
return {
id: account.id,
name: account.name,
currency: account.currencyCode,
type: getType(account.type),
institution: {
id: "",
name: "",
logo: "",
provider: "pluggy",
id: account.institution.id.toString(),
name: account.institution.name,
logo: account.institution.imageUrl,
provider: Providers.Enum.pluggy,
},
enrollment_id: null,
balance: {
Expand All @@ -45,3 +49,15 @@ export const transformAccount = (account: Account): BaseAccount => {
},
};
};

export const transformInstitution = (institution: Connector) => ({
id: institution.id.toString(),
name: institution.name,
logo: getLogoURL(institution.id.toString()),
provider: Providers.Enum.pluggy,
});

export const transformBalance = (account: Account): BaseBalance => ({
currency: account.currencyCode,
amount: account.balance,
});
9 changes: 9 additions & 0 deletions apps/engine/src/providers/pluggy/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ export type GetStatusResponse = {
description: string;
};
};

export type GetInstitutionsRequest = {
countries: string[];
};

export type GetAccountsRequest = {
id: string;
institutionId: string;
};
Binary file modified bun.lockb
Binary file not shown.

0 comments on commit 793a693

Please sign in to comment.