Merge pull request #146 from MR-medreport/feature/MED-238-240

MED-238,240: Fix errors from logs
This commit is contained in:
2025-11-07 09:27:05 +02:00
committed by GitHub
6 changed files with 96 additions and 15 deletions

View File

@@ -1,5 +1,5 @@
import { createUserAnalysesApi } from '@/packages/features/user-analyses/src/server/api'; import { createUserAnalysesApi } from '@/packages/features/user-analyses/src/server/api';
import { getSupabaseServerClient } from '@/packages/supabase/src/clients/server-client'; import { getSupabaseServerAdminClient } from '@/packages/supabase/src/clients/server-admin-client';
import { readPrivateMessageResponse } from '~/lib/services/medipost/medipostPrivateMessage.service'; import { readPrivateMessageResponse } from '~/lib/services/medipost/medipostPrivateMessage.service';
@@ -19,7 +19,7 @@ type GroupedResults = {
export default async function syncAnalysisResults() { export default async function syncAnalysisResults() {
console.info('Syncing analysis results'); console.info('Syncing analysis results');
const supabase = getSupabaseServerClient(); const supabase = getSupabaseServerAdminClient();
const api = createUserAnalysesApi(supabase); const api = createUserAnalysesApi(supabase);
const processedMessages: ProcessedMessage[] = []; const processedMessages: ProcessedMessage[] = [];

View File

@@ -7,6 +7,7 @@ import { ServiceCategory } from '../../_components/service-categories';
async function ttoServicesLoader() { async function ttoServicesLoader() {
const response = await getProductCategories({ const response = await getProductCategories({
fields: '*products, is_active, metadata', fields: '*products, is_active, metadata',
limit: 100,
}); });
const heroCategories = response.product_categories?.filter( const heroCategories = response.product_categories?.filter(

View File

@@ -7,7 +7,7 @@ async function getAssignedOrderIds() {
.schema('medreport') .schema('medreport')
.from('doctor_analysis_feedback') .from('doctor_analysis_feedback')
.select('analysis_order_id') .select('analysis_order_id')
.not('status', 'is', 'COMPLETED') .not('status', 'eq', 'COMPLETED')
.not('doctor_user_id', 'is', null) .not('doctor_user_id', 'is', null)
.throwOnError(); .throwOnError();

View File

@@ -15,6 +15,12 @@ import {
removeCartId, removeCartId,
setAuthToken, setAuthToken,
} from './cookies'; } from './cookies';
import { withRetries } from '../util/with-retries';
const MedusaApiRetriesDefaultConfig = {
attempts: 3,
baseDelayMs: 1000,
};
export const retrieveCustomer = export const retrieveCustomer =
async (): Promise<HttpTypes.StoreCustomer | null> => { async (): Promise<HttpTypes.StoreCustomer | null> => {
@@ -268,11 +274,21 @@ export const updateCustomerAddress = async (
}; };
async function medusaLogin(email: string, password: string) { async function medusaLogin(email: string, password: string) {
const token = await sdk.auth.login('customer', 'emailpass', { const token = await withRetries(async () => {
const loginToken = await sdk.auth.login('customer', 'emailpass', {
email, email,
password, password,
}); });
await setAuthToken(token as string); if (typeof loginToken !== 'string') {
throw new Error('Failed to login Medusa account');
}
return loginToken;
}, {
...MedusaApiRetriesDefaultConfig,
label: 'medusa.auth.login',
});
await setAuthToken(token);
try { try {
await transferCart(); await transferCart();
@@ -303,20 +319,38 @@ async function medusaRegister({
`Creating new Medusa account for Keycloak user with email=${email}`, `Creating new Medusa account for Keycloak user with email=${email}`,
); );
const registerToken = await sdk.auth.register('customer', 'emailpass', { const registerToken = await withRetries(
async () => {
const token = await sdk.auth.register('customer', 'emailpass', {
email, email,
password, password,
}); });
if (typeof token !== 'string') {
throw new Error('Failed to register Medusa account');
}
return token;
},
{
...MedusaApiRetriesDefaultConfig,
label: 'medusa.auth.register',
},
);
await setAuthToken(registerToken); await setAuthToken(registerToken);
console.info( console.info(
`Creating new Medusa customer profile for Keycloak user with email=${email} and name=${name} and lastName=${lastName}`, `Creating new Medusa customer profile for Keycloak user with email=${email} and name=${name} and lastName=${lastName}`,
); );
await withRetries(
async () => {
await sdk.store.customer.create( await sdk.store.customer.create(
{ email, first_name: name, last_name: lastName }, { email, first_name: name, last_name: lastName },
{}, {},
{ ...(await getAuthHeaders()) },
);
},
{ {
...(await getAuthHeaders()), ...MedusaApiRetriesDefaultConfig,
label: 'medusa.customer.create',
}, },
); );
} }

View File

@@ -8,3 +8,4 @@ export * from './money';
export * from './product'; export * from './product';
export * from './repeat'; export * from './repeat';
export * from './sort-products'; export * from './sort-products';
export * from './with-retries';

View File

@@ -0,0 +1,45 @@
type RetryOptions = {
attempts?: number;
baseDelayMs?: number;
label?: string;
};
function sleep(delay: number) {
return new Promise((resolve) => setTimeout(resolve, delay));
}
export async function withRetries<T>(
operation: () => Promise<T>,
{ attempts = 3, baseDelayMs = 500, label }: RetryOptions = {}
): Promise<T> {
let lastError: unknown;
for (let attempt = 1; attempt <= attempts; attempt++) {
try {
return await operation();
} catch (error) {
lastError = error;
if (attempt === attempts) {
break;
}
const delay = baseDelayMs * 2 ** (attempt - 1);
if (label) {
console.warn(
`Retrying ${label}, attempt ${attempt + 1}/${attempts} in ${delay}ms`,
error
);
}
if (delay > 0) {
await sleep(delay);
}
}
}
throw lastError instanceof Error
? lastError
: new Error("Operation failed after retries");
}