Skip to content

Commit

Permalink
feat: add operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexcited committed Nov 7, 2024
1 parent 49ba225 commit 68ca475
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 0 deletions.
21 changes: 21 additions & 0 deletions examples/operations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as izly from "../src";
import { read } from "./_persisted-session";

void async function main () {
const identification = await read();

const payments = await izly.operations(identification, izly.OperationKind.Payment, 5);
const topup = await izly.operations(identification, izly.OperationKind.TopUp, 5);

console.info("- Payments -");
for (const payment of payments) {
const priceAsString = `${payment.amount.toPrecision(3)} EUR`;
console.info(payment.date.toLocaleString("fr-FR"), "paid", priceAsString);
}

console.info("- Top-up -");
for (const operation of topup) {
const priceAsString = `${operation.amount.toPrecision(3)} EUR`;
console.info(operation.date.toLocaleString("fr-FR"), "received", priceAsString);
}
}();
1 change: 1 addition & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from "./balance";
export * from "./contact";
export * from "./information";
export * from "./login";
export * from "./operations";
export * from "./qr-pay";
export * from "./refresh";
export * from "./tokenize";
50 changes: 50 additions & 0 deletions src/api/operations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { defaultFetcher, type Fetcher, type Request } from "@literate.ink/utilities";
import { CLIENT_TYPE, SERVICE_VERSION } from "~/core/constants";
import { decodeFormattedDate } from "~/decoders/date";
import type { Identification, Operation } from "~/models";

export const OperationKind = {
TopUp: 0,
Transfer: 1,
Payment: 2
} as const;

export type OperationKind = typeof OperationKind[keyof typeof OperationKind];

/**
* @returns a list of the last operations
*/
export const operations = async (identification: Identification, kind: OperationKind, limit = 15, fetcher: Fetcher = defaultFetcher): Promise<Array<Operation>> => {
// transactionGroup=2 means history of operations
const url = new URL(`https://rest.izly.fr/Service/PublicService.svc/rest/GetHomePageOperations?transactionGroup=${kind}&top=${limit}`);
const request: Request = {
url,
headers: {
version: "2.0",
channel: "AIZ",
format: "T",
model: "A",
clientVersion: SERVICE_VERSION,
smoneyClientType: CLIENT_TYPE,
language: "fr",
userId: identification.identifier,
sessionId: identification.sessionID,
"Authorization": `Bearer ${identification.accessToken}`
}
};

const response = await fetcher(request);
const json = JSON.parse(response.content);

return json.GetHomePageOperationsResult.Result.map((operation: any) => ({
id: operation.Id,
amount: operation.Amount,
date: decodeFormattedDate(operation.Date),
isCredit: operation.IsCredit,
message: operation.Message as string | null,
type: operation.OperationType,
status: operation.Status
} as Operation));
};


1 change: 1 addition & 0 deletions src/decoders/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const decodeFormattedDate = (date: string) => new Date(parseInt(date.substring(6, date.length - 2).split("+")[0]));
1 change: 1 addition & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from "./errors";
export * from "./balance";
export * from "./configuration";
export * from "./identification";
export * from "./operation";
export * from "./profile";
19 changes: 19 additions & 0 deletions src/models/operation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export type Operation = Readonly<{
id: number
amount: number
date: Date
isCredit: boolean
message: string | null
type: 2 | 7
status: number
}>;

// TODO: not sure what "type" means here, is it an enum ?

// on operation 0:
// "Message": "Carte bancaire",
// "OperationType": 7

// on operation 2:
// "Message": null,
// "OperationType": 2,

0 comments on commit 68ca475

Please sign in to comment.