moallem all endpoints working

This commit is contained in:
2026-06-14 16:42:52 +03:30
parent 7a6e482586
commit 2212f6da41
18 changed files with 833 additions and 234 deletions

View 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;
}