Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/seven-hornets-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@oaknetwork/api": minor
---

added sync and balance API
33 changes: 33 additions & 0 deletions packages/api/__tests__/unit/services.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,27 @@ describe("Crowdsplit services (Unit)", () => {
authConfig,
],
});
await expectSuccess({
client,
call: () =>
service.sync("cust-1", { providers: ["stripe"], fields: ["shipping"] }),
httpMethod: "post",
expectedArgs: [
`${SANDBOX_URL}/api/v1/customers/cust-1/sync`,
{ providers: ["stripe"], fields: ["shipping"] },
authConfig,
],
});
await expectSuccess({
client,
call: () =>
service.balance("cust-1", { provider: "stripe", role: "customer" }),
httpMethod: "get",
expectedArgs: [
`${SANDBOX_URL}/api/v1/customers/cust-1/balance?provider=stripe&role=customer`,
authConfig,
],
});
await expectFailure({
call: () => service.update("cust-1", { email: "new@example.com" }),
httpMethod: "put",
Expand All @@ -216,6 +237,18 @@ describe("Crowdsplit services (Unit)", () => {
await expectTokenFailure(() =>
tokenErrorService.update("cust-1", { email: "t@t.com" }),
);
await expectTokenFailure(() =>
tokenErrorService.sync("cust-1", {
providers: ["stripe"],
fields: ["shipping"],
}),
);
await expectTokenFailure(() =>
tokenErrorService.balance("cust-1", {
provider: "stripe",
role: "customer",
}),
);
});

it("payment service methods", async () => {
Expand Down
50 changes: 50 additions & 0 deletions packages/api/src/services/customerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ export interface CustomerService {
id: string,
customer: Customer.Request,
): Promise<Result<Customer.Response>>;

sync(id: string, sync: Customer.Sync): Promise<Result<Customer.SyncResponse>>;

balance(
customer_id: string,
filter: Customer.BalanceFilter,
): Promise<Result<Customer.BalanceResponse>>;
}

export const createCustomerService = (client: OakClient): CustomerService => ({
Expand Down Expand Up @@ -91,4 +98,47 @@ export const createCustomerService = (client: OakClient): CustomerService => ({
},
);
},

async sync(
id: string,
sync: Customer.Sync,
): Promise<Result<Customer.SyncResponse>> {
const token = await client.getAccessToken();
if (!token.ok) {
return err(token.error);
}

return httpClient.post<Customer.SyncResponse>(
`${client.config.baseUrl}/api/v1/customers/${id}/sync`,
sync,
{
headers: {
Authorization: `Bearer ${token.value}`,
},
retryOptions: client.retryOptions,
},
);
},

async balance(
customer_id: string,
filter: Customer.BalanceFilter,
): Promise<Result<Customer.BalanceResponse>> {
const token = await client.getAccessToken();
if (!token.ok) {
return err(token.error);
}

const queryString = buildQueryString(filter);

return httpClient.get<Customer.BalanceResponse>(
`${client.config.baseUrl}/api/v1/customers/${customer_id}/balance${queryString}`,
{
headers: {
Authorization: `Bearer ${token.value}`,
},
retryOptions: client.retryOptions,
},
);
},
});
48 changes: 48 additions & 0 deletions packages/api/src/types/customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ export namespace Customer {
account_type?: string | null;
}

type Provider =
| "stripe"
| "bridge"
| "pagar_me"
| "brla"
| "avenia"
| "mercado_pago";

type SyncField = "shipping" | "email" | "first_name" | "last_name";

export interface Sync {
providers: [Provider]; // exactly 1
fields: SyncField[];
}

export type SyncResponse = ApiResponse<null>;

export interface Request extends Partial<Base> {}

export type Response = ApiResponse<Data>;
Expand All @@ -72,4 +89,35 @@ export namespace Customer {
document_type?: string;
country_code?: string;
}
export interface BalanceFilter {
provider: string;
role: string;
}

export interface BalanceResponse
extends ApiResponse<{
as_of: string;
filters: {
customer_id: string;
provider?: string;
role?: string;
};

balances: {
account_id: string;
provider: string;
customer: {
id: string;
role: string;
};
as_of: string;
totals: {
currency: string;
amount: number;
pending: number;
reserved: number;
instant_payouts: number;
}[];
}[];
}> {}
}