forked from Shared/esg
moallem all endpoints working
This commit is contained in:
70
src/common/helpers/sayah-response.helper.ts
Normal file
70
src/common/helpers/sayah-response.helper.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
export interface SayahApiResponse {
|
||||
ReturnValue?: boolean;
|
||||
HasError?: boolean;
|
||||
IsSucceed?: boolean;
|
||||
isSucceed?: boolean;
|
||||
Errors?: Record<string, string> | Array<{ Code?: string; Message?: string }> | null;
|
||||
errors?: Record<string, string> | Array<{ Code?: string; Message?: string }> | null;
|
||||
Result?: {
|
||||
ErrorMessage?: string | null;
|
||||
[key: string]: unknown;
|
||||
};
|
||||
}
|
||||
|
||||
export function formatSayahErrors(
|
||||
errors?: Record<string, string> | Array<{ Code?: string; Message?: string }> | null,
|
||||
): string {
|
||||
if (!errors) {
|
||||
return 'Request failed';
|
||||
}
|
||||
|
||||
if (Array.isArray(errors)) {
|
||||
if (errors.length === 0) {
|
||||
return 'Request failed';
|
||||
}
|
||||
return errors.map((error) => `${error.Code}: ${error.Message}`).join(', ');
|
||||
}
|
||||
|
||||
const entries = Object.entries(errors);
|
||||
if (entries.length === 0) {
|
||||
return 'Request failed';
|
||||
}
|
||||
|
||||
return entries.map(([code, field]) => `${field} (code: ${code})`).join(', ');
|
||||
}
|
||||
|
||||
export function getSayahProviderError(
|
||||
body: SayahApiResponse,
|
||||
): { message: string; code: string } | null {
|
||||
if ('ReturnValue' in body || 'HasError' in body) {
|
||||
if (body.HasError) {
|
||||
return {
|
||||
message: formatSayahErrors(body.Errors),
|
||||
code: 'SAYAH_ERROR',
|
||||
};
|
||||
}
|
||||
|
||||
if (body.ReturnValue === false) {
|
||||
return {
|
||||
message: formatSayahErrors(body.Errors),
|
||||
code: 'SHEBA_MISMATCH',
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
const isSucceed = body.IsSucceed ?? body.isSucceed;
|
||||
if (isSucceed === false) {
|
||||
const errors = body.Errors ?? body.errors;
|
||||
return {
|
||||
message:
|
||||
(Array.isArray(errors) || (errors && Object.keys(errors).length > 0)
|
||||
? formatSayahErrors(errors)
|
||||
: body.Result?.ErrorMessage) ?? 'Request failed',
|
||||
code: 'API_ERROR',
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user