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}`);
|
||||
},
|
||||
{
|
||||
|
||||
@@ -37,4 +37,15 @@ class AdminAccountsService {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async getAccount(accountId: string) {
|
||||
const { data } = await this.adminClient
|
||||
.schema('medreport')
|
||||
.from('accounts')
|
||||
.select('*')
|
||||
.eq('id', accountId)
|
||||
.single().throwOnError();
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
16
packages/features/admin/src/lib/server/utils/medusa-sdk.ts
Normal file
16
packages/features/admin/src/lib/server/utils/medusa-sdk.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import Medusa from "@medusajs/js-sdk"
|
||||
|
||||
export const getAdminSdk = () => {
|
||||
const medusaBackendUrl = process.env.MEDUSA_BACKEND_PUBLIC_URL!;
|
||||
const medusaPublishableApiKey = process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY!;
|
||||
const key = process.env.MEDUSA_SECRET_API_KEY!;
|
||||
|
||||
if (!medusaBackendUrl || !medusaPublishableApiKey) {
|
||||
throw new Error('Medusa environment variables not set');
|
||||
}
|
||||
return new Medusa({
|
||||
baseUrl: medusaBackendUrl,
|
||||
debug: process.env.NODE_ENV === 'development',
|
||||
apiKey: key,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user