fix BO account check in case account id refers to deleted BO account
This commit is contained in:
@@ -206,14 +206,7 @@ class AuthCallbackService {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If user already has Medusa account, we're done
|
|
||||||
if (accountData?.medusa_account_id) {
|
|
||||||
console.log('Keycloak user already has Medusa account:', accountData.medusa_account_id);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const { medusaLoginOrRegister } = await import('../../features/medusa-storefront/src/lib/data/customer');
|
const { medusaLoginOrRegister } = await import('../../features/medusa-storefront/src/lib/data/customer');
|
||||||
|
|
||||||
const medusaAccountId = await medusaLoginOrRegister({
|
const medusaAccountId = await medusaLoginOrRegister({
|
||||||
email: user.email,
|
email: user.email,
|
||||||
supabaseUserId: user.id,
|
supabaseUserId: user.id,
|
||||||
@@ -221,20 +214,24 @@ class AuthCallbackService {
|
|||||||
lastName: accountData?.last_name ?? '-',
|
lastName: accountData?.last_name ?? '-',
|
||||||
});
|
});
|
||||||
|
|
||||||
// Update the account with the Medusa account ID
|
const currentMedusaAccountId = accountData?.medusa_account_id;
|
||||||
const { error: updateError } = await this.client
|
if (!currentMedusaAccountId || currentMedusaAccountId !== medusaAccountId) {
|
||||||
.schema('medreport')
|
const { error: updateError } = await this.client
|
||||||
.from('accounts')
|
.schema('medreport')
|
||||||
.update({ medusa_account_id: medusaAccountId })
|
.from('accounts')
|
||||||
.eq('primary_owner_user_id', user.id)
|
.update({ medusa_account_id: medusaAccountId })
|
||||||
.eq('is_personal_account', true);
|
.eq('primary_owner_user_id', user.id)
|
||||||
|
.eq('is_personal_account', true);
|
||||||
|
|
||||||
if (updateError) {
|
if (updateError) {
|
||||||
console.error('Error updating account with Medusa ID:', updateError);
|
console.error('Error updating account with Medusa ID:', updateError);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Successfully set up Medusa account for Keycloak user:', medusaAccountId);
|
||||||
|
} else {
|
||||||
|
console.log('Keycloak user already has Medusa account:', accountData.medusa_account_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('Successfully set up Medusa account for Keycloak user:', medusaAccountId);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error setting up Medusa account for Keycloak user:', error);
|
console.error('Error setting up Medusa account for Keycloak user:', error);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user