feat(MED-85): create customer group for company account in Medusa
This commit is contained in:
@@ -23,6 +23,7 @@ import { createAdminAccountsService } from './services/admin-accounts.service';
|
||||
import { createAdminAuthUserService } from './services/admin-auth-user.service';
|
||||
import { createCreateCompanyAccountService } from './services/admin-create-company-account.service';
|
||||
import { adminAction } from './utils/admin-action';
|
||||
import { getAdminSdk } from './utils/medusa-sdk';
|
||||
|
||||
/**
|
||||
* @name banUserAction
|
||||
@@ -138,7 +139,24 @@ export const deleteAccountAction = adminAction(
|
||||
|
||||
logger.info({ accountId }, `Super Admin is deleting account...`);
|
||||
|
||||
await service.deleteAccount(accountId);
|
||||
const { name: customerGroupName } = await service.getAccount(accountId);
|
||||
try {
|
||||
await service.deleteAccount(accountId);
|
||||
} catch (e) {
|
||||
logger.error({ accountId }, `Error deleting company account`);
|
||||
throw e;
|
||||
}
|
||||
const medusa = getAdminSdk();
|
||||
const { customer_groups } = await medusa.admin.customerGroup.list();
|
||||
const customerGroup = customer_groups.find(({ name }) => name === customerGroupName);
|
||||
if (customerGroup) {
|
||||
try {
|
||||
await medusa.admin.customerGroup.delete(customerGroup.id);
|
||||
} catch (e) {
|
||||
logger.error({ accountId }, `Error deleting Medusa customer group for company ${customerGroupName}`);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
logger.info(
|
||||
{ accountId },
|
||||
@@ -267,6 +285,40 @@ export const createCompanyAccountAction = enhanceAction(
|
||||
}
|
||||
|
||||
logger.info(ctx, `Company account created`);
|
||||
|
||||
logger.info(ctx, `Creating Medusa customer group`);
|
||||
const medusa = getAdminSdk();
|
||||
const { customer_groups: existingCustomerGroups } = await medusa.admin.customerGroup.list();
|
||||
const isExisting = existingCustomerGroups.find((group) => group.name === name);
|
||||
if (isExisting) {
|
||||
logger.info(ctx, `Customer group already exists`);
|
||||
} else {
|
||||
logger.info(ctx, `Creating Medusa customer group`);
|
||||
const { data: account } = await client
|
||||
.schema('medreport').from('accounts')
|
||||
.select('medusa_account_id')
|
||||
.eq('personal_code', ownerPersonalCode)
|
||||
.single().throwOnError();
|
||||
const medusaAccountId = account.medusa_account_id;
|
||||
if (!medusaAccountId) {
|
||||
logger.error(ctx, `User has no Medusa account ID`);
|
||||
} else {
|
||||
const { customer_group: { id: customerGroupId } } = await medusa.admin.customerGroup.create({ name });
|
||||
const { customers } = await medusa.admin.customer.list({
|
||||
id: medusaAccountId,
|
||||
});
|
||||
if (customers.length !== 1) {
|
||||
logger.error(ctx, `Customer not found`);
|
||||
} else {
|
||||
const customerId = customers[0]!.id;
|
||||
await medusa.admin.customer.batchCustomerGroups(customerId, {
|
||||
add: [customerGroupId],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
redirect(`/admin/accounts/${data.id}`);
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user