Update version within PUT request

Try to add api logic

check for version being the same instead of smaller

implementing optimistic locking and displaying correct message in frontend

rector fixes

adjust violation message and add translation in translation.yaml

add translator in apiController
This commit is contained in:
2023-11-29 16:14:19 +01:00
committed by Julien Fastré
parent f00b39980c
commit 3bb911b4d0
7 changed files with 70 additions and 11 deletions

View File

@@ -55,11 +55,20 @@ export interface ServerExceptionInterface extends TransportExceptionInterface {
body: string;
}
export interface ConflictHttpExceptionInterface extends TransportExceptionInterface {
name: 'ConflictHttpException';
violations: string[];
}
/**
* Generic api method that can be adapted to any fetch request
*/
export const makeFetch = <Input, Output>(method: 'POST'|'GET'|'PUT'|'PATCH'|'DELETE', url: string, body?: body | Input | null, options?: FetchParams): Promise<Output> => {
export const makeFetch = <Input, Output>(
method: 'POST'|'GET'|'PUT'|'PATCH'|'DELETE',
url: string, body?: body | Input | null,
options?: FetchParams
): Promise<Output> => {
let opts = {
method: method,
headers: {
@@ -67,6 +76,7 @@ export const makeFetch = <Input, Output>(method: 'POST'|'GET'|'PUT'|'PATCH'|'DEL
},
};
if (body !== null && typeof body !== 'undefined') {
Object.assign(opts, {body: JSON.stringify(body)})
}
@@ -90,6 +100,10 @@ export const makeFetch = <Input, Output>(method: 'POST'|'GET'|'PUT'|'PATCH'|'DEL
throw AccessException(response);
}
if (response.status === 409) {
throw ConflictHttpException(response);
}
throw {
name: 'Exception',
sta: response.status,
@@ -220,3 +234,12 @@ const ServerException = (code: number, body: string): ServerExceptionInterface =
return error;
}
const ConflictHttpException = (response: Response): 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']
return error;
}