mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-23 16:13:50 +00:00
Ajout de commentaires supplémentaires aux motifs
This commit is contained in:
@@ -1,61 +1,61 @@
|
||||
import {
|
||||
Address,
|
||||
GeographicalUnitLayer,
|
||||
SimpleGeographicalUnit,
|
||||
Address,
|
||||
GeographicalUnitLayer,
|
||||
SimpleGeographicalUnit,
|
||||
} from "../../types";
|
||||
import { fetchResults, makeFetch } from "./apiMethods";
|
||||
|
||||
export const getAddressById = async (address_id: number): Promise<Address> => {
|
||||
const url = `/api/1.0/main/address/${address_id}.json`;
|
||||
const url = `/api/1.0/main/address/${address_id}.json`;
|
||||
|
||||
const response = await fetch(url);
|
||||
const response = await fetch(url);
|
||||
|
||||
if (response.ok) {
|
||||
return response.json();
|
||||
}
|
||||
if (response.ok) {
|
||||
return response.json();
|
||||
}
|
||||
|
||||
throw Error("Error with request resource response");
|
||||
throw Error("Error with request resource response");
|
||||
};
|
||||
|
||||
export const getGeographicalUnitsByAddress = async (
|
||||
address: Address,
|
||||
address: Address,
|
||||
): Promise<SimpleGeographicalUnit[]> => {
|
||||
return fetchResults<SimpleGeographicalUnit>(
|
||||
`/api/1.0/main/geographical-unit/by-address/${address.address_id}.json`,
|
||||
);
|
||||
return fetchResults<SimpleGeographicalUnit>(
|
||||
`/api/1.0/main/geographical-unit/by-address/${address.address_id}.json`,
|
||||
);
|
||||
};
|
||||
|
||||
export const getAllGeographicalUnitLayers = async (): Promise<
|
||||
GeographicalUnitLayer[]
|
||||
GeographicalUnitLayer[]
|
||||
> => {
|
||||
return fetchResults<GeographicalUnitLayer>(
|
||||
`/api/1.0/main/geographical-unit-layer.json`,
|
||||
);
|
||||
return fetchResults<GeographicalUnitLayer>(
|
||||
`/api/1.0/main/geographical-unit-layer.json`,
|
||||
);
|
||||
};
|
||||
|
||||
export const syncAddressWithReference = async (
|
||||
address: Address,
|
||||
address: Address,
|
||||
): Promise<Address> => {
|
||||
return makeFetch<null, Address>(
|
||||
"POST",
|
||||
`/api/1.0/main/address/reference-match/${address.address_id}/sync-with-reference`,
|
||||
);
|
||||
return makeFetch<null, Address>(
|
||||
"POST",
|
||||
`/api/1.0/main/address/reference-match/${address.address_id}/sync-with-reference`,
|
||||
);
|
||||
};
|
||||
|
||||
export const markAddressReviewed = async (
|
||||
address: Address,
|
||||
address: Address,
|
||||
): Promise<Address> => {
|
||||
return makeFetch<null, Address>(
|
||||
"POST",
|
||||
`/api/1.0/main/address/reference-match/${address.address_id}/set/reviewed`,
|
||||
);
|
||||
return makeFetch<null, Address>(
|
||||
"POST",
|
||||
`/api/1.0/main/address/reference-match/${address.address_id}/set/reviewed`,
|
||||
);
|
||||
};
|
||||
|
||||
export const markAddressToReview = async (
|
||||
address: Address,
|
||||
address: Address,
|
||||
): Promise<Address> => {
|
||||
return makeFetch<null, Address>(
|
||||
"POST",
|
||||
`/api/1.0/main/address/reference-match/${address.address_id}/set/to_review`,
|
||||
);
|
||||
return makeFetch<null, Address>(
|
||||
"POST",
|
||||
`/api/1.0/main/address/reference-match/${address.address_id}/set/to_review`,
|
||||
);
|
||||
};
|
||||
|
@@ -6,57 +6,57 @@ export type fetchOption = Record<string, boolean | string | number | null>;
|
||||
export type Params = Record<string, number | string>;
|
||||
|
||||
export interface PaginationResponse<T> {
|
||||
pagination: {
|
||||
more: boolean;
|
||||
items_per_page: number;
|
||||
};
|
||||
results: T[];
|
||||
count: number;
|
||||
pagination: {
|
||||
more: boolean;
|
||||
items_per_page: number;
|
||||
};
|
||||
results: T[];
|
||||
count: number;
|
||||
}
|
||||
|
||||
export type FetchParams = Record<string, string | number | null>;
|
||||
|
||||
export interface TransportExceptionInterface {
|
||||
name: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface ValidationExceptionInterface
|
||||
extends TransportExceptionInterface {
|
||||
name: "ValidationException";
|
||||
error: object;
|
||||
violations: string[];
|
||||
titles: string[];
|
||||
propertyPaths: string[];
|
||||
extends TransportExceptionInterface {
|
||||
name: "ValidationException";
|
||||
error: object;
|
||||
violations: string[];
|
||||
titles: string[];
|
||||
propertyPaths: string[];
|
||||
}
|
||||
|
||||
export interface ValidationErrorResponse extends TransportExceptionInterface {
|
||||
violations: {
|
||||
title: string;
|
||||
propertyPath: string;
|
||||
}[];
|
||||
violations: {
|
||||
title: string;
|
||||
propertyPath: string;
|
||||
}[];
|
||||
}
|
||||
|
||||
export interface AccessExceptionInterface extends TransportExceptionInterface {
|
||||
name: "AccessException";
|
||||
violations: string[];
|
||||
name: "AccessException";
|
||||
violations: string[];
|
||||
}
|
||||
|
||||
export interface NotFoundExceptionInterface
|
||||
extends TransportExceptionInterface {
|
||||
name: "NotFoundException";
|
||||
extends TransportExceptionInterface {
|
||||
name: "NotFoundException";
|
||||
}
|
||||
|
||||
export interface ServerExceptionInterface extends TransportExceptionInterface {
|
||||
name: "ServerException";
|
||||
message: string;
|
||||
code: number;
|
||||
body: string;
|
||||
name: "ServerException";
|
||||
message: string;
|
||||
code: number;
|
||||
body: string;
|
||||
}
|
||||
|
||||
export interface ConflictHttpExceptionInterface
|
||||
extends TransportExceptionInterface {
|
||||
name: "ConflictHttpException";
|
||||
violations: string[];
|
||||
extends TransportExceptionInterface {
|
||||
name: "ConflictHttpException";
|
||||
violations: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,223 +66,223 @@ export interface ConflictHttpExceptionInterface
|
||||
* and use of the @link{fetchResults} method.
|
||||
*/
|
||||
export const makeFetch = <Input, Output>(
|
||||
method: "POST" | "GET" | "PUT" | "PATCH" | "DELETE",
|
||||
url: string,
|
||||
body?: body | Input | null,
|
||||
options?: FetchParams,
|
||||
method: "POST" | "GET" | "PUT" | "PATCH" | "DELETE",
|
||||
url: string,
|
||||
body?: body | Input | null,
|
||||
options?: FetchParams,
|
||||
): Promise<Output> => {
|
||||
let opts = {
|
||||
method: method,
|
||||
headers: {
|
||||
"Content-Type": "application/json;charset=utf-8",
|
||||
},
|
||||
let opts = {
|
||||
method: method,
|
||||
headers: {
|
||||
"Content-Type": "application/json;charset=utf-8",
|
||||
},
|
||||
};
|
||||
|
||||
if (body !== null && typeof body !== "undefined") {
|
||||
Object.assign(opts, { body: JSON.stringify(body) });
|
||||
}
|
||||
|
||||
if (typeof options !== "undefined") {
|
||||
opts = Object.assign(opts, options);
|
||||
}
|
||||
return fetch(url, opts).then((response) => {
|
||||
if (response.status === 204) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
if (response.ok) {
|
||||
return response.json();
|
||||
}
|
||||
|
||||
if (response.status === 422) {
|
||||
return response.json().then((response) => {
|
||||
throw ValidationException(response);
|
||||
});
|
||||
}
|
||||
|
||||
if (response.status === 403) {
|
||||
throw AccessException(response);
|
||||
}
|
||||
|
||||
if (response.status === 409) {
|
||||
throw ConflictHttpException(response);
|
||||
}
|
||||
|
||||
throw {
|
||||
name: "Exception",
|
||||
sta: response.status,
|
||||
txt: response.statusText,
|
||||
err: new Error(),
|
||||
violations: response.body,
|
||||
};
|
||||
|
||||
if (body !== null && typeof body !== "undefined") {
|
||||
Object.assign(opts, { body: JSON.stringify(body) });
|
||||
}
|
||||
|
||||
if (typeof options !== "undefined") {
|
||||
opts = Object.assign(opts, options);
|
||||
}
|
||||
return fetch(url, opts).then((response) => {
|
||||
if (response.status === 204) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
if (response.ok) {
|
||||
return response.json();
|
||||
}
|
||||
|
||||
if (response.status === 422) {
|
||||
return response.json().then((response) => {
|
||||
throw ValidationException(response);
|
||||
});
|
||||
}
|
||||
|
||||
if (response.status === 403) {
|
||||
throw AccessException(response);
|
||||
}
|
||||
|
||||
if (response.status === 409) {
|
||||
throw ConflictHttpException(response);
|
||||
}
|
||||
|
||||
throw {
|
||||
name: "Exception",
|
||||
sta: response.status,
|
||||
txt: response.statusText,
|
||||
err: new Error(),
|
||||
violations: response.body,
|
||||
};
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetch results with certain parameters
|
||||
*/
|
||||
function _fetchAction<T>(
|
||||
page: number,
|
||||
uri: string,
|
||||
params?: FetchParams,
|
||||
page: number,
|
||||
uri: string,
|
||||
params?: FetchParams,
|
||||
): Promise<PaginationResponse<T>> {
|
||||
const item_per_page = 50;
|
||||
const item_per_page = 50;
|
||||
|
||||
const searchParams = new URLSearchParams();
|
||||
searchParams.append("item_per_page", item_per_page.toString());
|
||||
searchParams.append("page", page.toString());
|
||||
const searchParams = new URLSearchParams();
|
||||
searchParams.append("item_per_page", item_per_page.toString());
|
||||
searchParams.append("page", page.toString());
|
||||
|
||||
if (params !== undefined) {
|
||||
Object.keys(params).forEach((key) => {
|
||||
const v = params[key];
|
||||
if (typeof v === "string") {
|
||||
searchParams.append(key, v);
|
||||
} else if (typeof v === "number") {
|
||||
searchParams.append(key, v.toString());
|
||||
} else if (v === null) {
|
||||
searchParams.append(key, "");
|
||||
}
|
||||
if (params !== undefined) {
|
||||
Object.keys(params).forEach((key) => {
|
||||
const v = params[key];
|
||||
if (typeof v === "string") {
|
||||
searchParams.append(key, v);
|
||||
} else if (typeof v === "number") {
|
||||
searchParams.append(key, v.toString());
|
||||
} else if (v === null) {
|
||||
searchParams.append(key, "");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const url = uri + "?" + searchParams.toString();
|
||||
|
||||
return fetch(url, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json;charset=utf-8",
|
||||
},
|
||||
})
|
||||
.then((response) => {
|
||||
if (response.ok) {
|
||||
return response.json();
|
||||
}
|
||||
|
||||
if (response.status === 404) {
|
||||
throw NotFoundException(response);
|
||||
}
|
||||
|
||||
if (response.status === 422) {
|
||||
return response.json().then((response) => {
|
||||
throw ValidationException(response);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const url = uri + "?" + searchParams.toString();
|
||||
if (response.status === 403) {
|
||||
throw AccessException(response);
|
||||
}
|
||||
|
||||
return fetch(url, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json;charset=utf-8",
|
||||
},
|
||||
if (response.status >= 500) {
|
||||
return response.text().then((body) => {
|
||||
throw ServerException(response.status, body);
|
||||
});
|
||||
}
|
||||
|
||||
throw new Error("other network error");
|
||||
})
|
||||
.then((response) => {
|
||||
if (response.ok) {
|
||||
return response.json();
|
||||
}
|
||||
|
||||
if (response.status === 404) {
|
||||
throw NotFoundException(response);
|
||||
}
|
||||
|
||||
if (response.status === 422) {
|
||||
return response.json().then((response) => {
|
||||
throw ValidationException(response);
|
||||
});
|
||||
}
|
||||
|
||||
if (response.status === 403) {
|
||||
throw AccessException(response);
|
||||
}
|
||||
|
||||
if (response.status >= 500) {
|
||||
return response.text().then((body) => {
|
||||
throw ServerException(response.status, body);
|
||||
});
|
||||
}
|
||||
|
||||
throw new Error("other network error");
|
||||
})
|
||||
.catch(
|
||||
(
|
||||
reason:
|
||||
| NotFoundExceptionInterface
|
||||
| ServerExceptionInterface
|
||||
| ValidationExceptionInterface
|
||||
| TransportExceptionInterface,
|
||||
) => {
|
||||
console.error(reason);
|
||||
throw reason;
|
||||
},
|
||||
);
|
||||
.catch(
|
||||
(
|
||||
reason:
|
||||
| NotFoundExceptionInterface
|
||||
| ServerExceptionInterface
|
||||
| ValidationExceptionInterface
|
||||
| TransportExceptionInterface,
|
||||
) => {
|
||||
console.error(reason);
|
||||
throw reason;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
export const fetchResults = async <T>(
|
||||
uri: string,
|
||||
params?: FetchParams,
|
||||
uri: string,
|
||||
params?: FetchParams,
|
||||
): Promise<T[]> => {
|
||||
const promises: Promise<T[]>[] = [];
|
||||
let page = 1;
|
||||
const firstData: PaginationResponse<T> = (await _fetchAction(
|
||||
page,
|
||||
uri,
|
||||
params,
|
||||
)) as PaginationResponse<T>;
|
||||
const promises: Promise<T[]>[] = [];
|
||||
let page = 1;
|
||||
const firstData: PaginationResponse<T> = (await _fetchAction(
|
||||
page,
|
||||
uri,
|
||||
params,
|
||||
)) as PaginationResponse<T>;
|
||||
|
||||
promises.push(Promise.resolve(firstData.results));
|
||||
promises.push(Promise.resolve(firstData.results));
|
||||
|
||||
if (firstData.pagination.more) {
|
||||
do {
|
||||
page = ++page;
|
||||
promises.push(
|
||||
_fetchAction<T>(page, uri, params).then((r) =>
|
||||
Promise.resolve(r.results),
|
||||
),
|
||||
);
|
||||
} while (page * firstData.pagination.items_per_page < firstData.count);
|
||||
}
|
||||
if (firstData.pagination.more) {
|
||||
do {
|
||||
page = ++page;
|
||||
promises.push(
|
||||
_fetchAction<T>(page, uri, params).then((r) =>
|
||||
Promise.resolve(r.results),
|
||||
),
|
||||
);
|
||||
} while (page * firstData.pagination.items_per_page < firstData.count);
|
||||
}
|
||||
|
||||
return Promise.all(promises).then((values) => values.flat());
|
||||
return Promise.all(promises).then((values) => values.flat());
|
||||
};
|
||||
|
||||
export const fetchScopes = (): Promise<Scope[]> => {
|
||||
return fetchResults("/api/1.0/main/scope.json");
|
||||
return fetchResults("/api/1.0/main/scope.json");
|
||||
};
|
||||
|
||||
/**
|
||||
* Error objects to be thrown
|
||||
*/
|
||||
const ValidationException = (
|
||||
response: ValidationErrorResponse,
|
||||
response: ValidationErrorResponse,
|
||||
): ValidationExceptionInterface => {
|
||||
const error = {} as ValidationExceptionInterface;
|
||||
error.name = "ValidationException";
|
||||
error.violations = response.violations.map(
|
||||
(violation) => `${violation.title}: ${violation.propertyPath}`,
|
||||
);
|
||||
error.titles = response.violations.map((violation) => violation.title);
|
||||
error.propertyPaths = response.violations.map(
|
||||
(violation) => violation.propertyPath,
|
||||
);
|
||||
return error;
|
||||
const error = {} as ValidationExceptionInterface;
|
||||
error.name = "ValidationException";
|
||||
error.violations = response.violations.map(
|
||||
(violation) => `${violation.title}: ${violation.propertyPath}`,
|
||||
);
|
||||
error.titles = response.violations.map((violation) => violation.title);
|
||||
error.propertyPaths = response.violations.map(
|
||||
(violation) => violation.propertyPath,
|
||||
);
|
||||
return error;
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const AccessException = (response: Response): AccessExceptionInterface => {
|
||||
const error = {} as AccessExceptionInterface;
|
||||
error.name = "AccessException";
|
||||
error.violations = ["You are not allowed to perform this action"];
|
||||
const error = {} as AccessExceptionInterface;
|
||||
error.name = "AccessException";
|
||||
error.violations = ["You are not allowed to perform this action"];
|
||||
|
||||
return error;
|
||||
return error;
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const NotFoundException = (response: Response): NotFoundExceptionInterface => {
|
||||
const error = {} as NotFoundExceptionInterface;
|
||||
error.name = "NotFoundException";
|
||||
const error = {} as NotFoundExceptionInterface;
|
||||
error.name = "NotFoundException";
|
||||
|
||||
return error;
|
||||
return error;
|
||||
};
|
||||
|
||||
const ServerException = (
|
||||
code: number,
|
||||
body: string,
|
||||
code: number,
|
||||
body: string,
|
||||
): ServerExceptionInterface => {
|
||||
const error = {} as ServerExceptionInterface;
|
||||
error.name = "ServerException";
|
||||
error.code = code;
|
||||
error.body = body;
|
||||
const error = {} as ServerExceptionInterface;
|
||||
error.name = "ServerException";
|
||||
error.code = code;
|
||||
error.body = body;
|
||||
|
||||
return error;
|
||||
return error;
|
||||
};
|
||||
|
||||
const ConflictHttpException = (
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
response: Response,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
response: Response,
|
||||
): ConflictHttpExceptionInterface => {
|
||||
const error = {} as ConflictHttpExceptionInterface;
|
||||
const error = {} as ConflictHttpExceptionInterface;
|
||||
|
||||
error.name = "ConflictHttpException";
|
||||
error.violations = [
|
||||
"Sorry, but someone else has already changed this entity. Please refresh the page and apply the changes again",
|
||||
];
|
||||
error.name = "ConflictHttpException";
|
||||
error.violations = [
|
||||
"Sorry, but someone else has already changed this entity. Please refresh the page and apply the changes again",
|
||||
];
|
||||
|
||||
return error;
|
||||
return error;
|
||||
};
|
||||
|
@@ -2,17 +2,17 @@ import { makeFetch } from "ChillMainAssets/lib/api/apiMethods";
|
||||
import { ExportGeneration } from "ChillMainAssets/types";
|
||||
|
||||
export const fetchExportGenerationStatus = async (
|
||||
exportGenerationId: string,
|
||||
exportGenerationId: string,
|
||||
): Promise<ExportGeneration> =>
|
||||
makeFetch(
|
||||
"GET",
|
||||
`/api/1.0/main/export-generation/${exportGenerationId}/object`,
|
||||
);
|
||||
makeFetch(
|
||||
"GET",
|
||||
`/api/1.0/main/export-generation/${exportGenerationId}/object`,
|
||||
);
|
||||
|
||||
export const generateFromSavedExport = async (
|
||||
savedExportUuid: string,
|
||||
savedExportUuid: string,
|
||||
): Promise<ExportGeneration> =>
|
||||
makeFetch(
|
||||
"POST",
|
||||
`/api/1.0/main/export/export-generation/create-from-saved-export/${savedExportUuid}`,
|
||||
);
|
||||
makeFetch(
|
||||
"POST",
|
||||
`/api/1.0/main/export/export-generation/create-from-saved-export/${savedExportUuid}`,
|
||||
);
|
||||
|
@@ -2,7 +2,7 @@ import { fetchResults } from "./apiMethods";
|
||||
import { Location, LocationType } from "../../types";
|
||||
|
||||
export const getLocations = (): Promise<Location[]> =>
|
||||
fetchResults("/api/1.0/main/location.json");
|
||||
fetchResults("/api/1.0/main/location.json");
|
||||
|
||||
export const getLocationTypes = (): Promise<LocationType[]> =>
|
||||
fetchResults("/api/1.0/main/location-type.json");
|
||||
fetchResults("/api/1.0/main/location-type.json");
|
||||
|
@@ -1,3 +1,3 @@
|
||||
export function buildReturnPath(location: Location): string {
|
||||
return location.pathname + location.search;
|
||||
return location.pathname + location.search;
|
||||
}
|
||||
|
@@ -2,23 +2,23 @@ import { User } from "../../types";
|
||||
import { makeFetch } from "./apiMethods";
|
||||
|
||||
export const whoami = (): Promise<User> => {
|
||||
const url = `/api/1.0/main/whoami.json`;
|
||||
return fetch(url).then((response) => {
|
||||
if (response.ok) {
|
||||
return response.json();
|
||||
}
|
||||
throw {
|
||||
msg: "Error while getting whoami.",
|
||||
sta: response.status,
|
||||
txt: response.statusText,
|
||||
err: new Error(),
|
||||
body: response.body,
|
||||
};
|
||||
});
|
||||
const url = `/api/1.0/main/whoami.json`;
|
||||
return fetch(url).then((response) => {
|
||||
if (response.ok) {
|
||||
return response.json();
|
||||
}
|
||||
throw {
|
||||
msg: "Error while getting whoami.",
|
||||
sta: response.status,
|
||||
txt: response.statusText,
|
||||
err: new Error(),
|
||||
body: response.body,
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
export const whereami = (): Promise<Location | null> => {
|
||||
const url = `/api/1.0/main/user-current-location.json`;
|
||||
const url = `/api/1.0/main/user-current-location.json`;
|
||||
|
||||
return makeFetch<null, Location | null>("GET", url);
|
||||
return makeFetch<null, Location | null>("GET", url);
|
||||
};
|
||||
|
Reference in New Issue
Block a user