/ src / api / user-events.ts
user-events.ts
 1  import type { Error as ServerError } from "~/definitions/error";
 2  import { defaultFetcher, type Fetcher, type Request } from "@literate.ink/utilities";
 3  import { CLIENT_TYPE, createRouteREST, SERVICE_VERSION } from "~/core/constants";
 4  import { type Identification, ReauthenticateError } from "~/models";
 5  
 6  // eslint-disable-next-line ts/explicit-function-return-type
 7  export const userEvents = async (identification: Identification, itemPerPage = 10, page = 0, fetcher: Fetcher = defaultFetcher) => {
 8    const request: Request = {
 9      content: JSON.stringify({
10        itemPerPage,
11        page
12      }),
13      headers: {
14        "Authorization": `Bearer ${identification.accessToken}`,
15        "channel": "AIZ",
16        "clientVersion": SERVICE_VERSION,
17        "Content-Type": "application/json",
18        "format": "T",
19        "language": "fr",
20        "model": "A",
21        "sessionId": identification.sessionID,
22        "smoneyClientType": CLIENT_TYPE,
23        "userId": identification.identifier,
24        "version": "2.0"
25      },
26      method: "POST",
27      url: createRouteREST("GetUserEventList")
28    };
29  
30    const response = await fetcher(request);
31    const json = JSON.parse(response.content) as {
32      GetUserEventListResult: Array<{
33        /**
34         * formatted as `le D/M/YYYY à H:mm AM/PM`
35         */
36        EventFormattedDate: string;
37        EventMessage: string;
38      }>;
39    } | ServerError;
40  
41    if ("ErrorMessage" in json) {
42      if (json.Code === 140 || json.Code === 570)
43        throw new ReauthenticateError();
44  
45      throw new Error(`${json.ErrorMessage} (${json.Code})`);
46    }
47  
48    return json.GetUserEventListResult;
49  };