prettier fix
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
import Medusa from "@medusajs/js-sdk"
|
||||
import type { AdminProductCategory } from "@medusajs/types";
|
||||
import { listProductTypes } from "@lib/data/products";
|
||||
import { getAnalysisElements } from "~/lib/services/analysis-element.service";
|
||||
import { getAnalysisGroups } from "~/lib/services/analysis-group.service";
|
||||
import { createMedusaSyncFailEntry, createMedusaSyncSuccessEntry } from "~/lib/services/analyses.service";
|
||||
import { listProductTypes } from '@lib/data/products';
|
||||
import Medusa from '@medusajs/js-sdk';
|
||||
import type { AdminProductCategory } from '@medusajs/types';
|
||||
|
||||
import {
|
||||
createMedusaSyncFailEntry,
|
||||
createMedusaSyncSuccessEntry,
|
||||
} from '~/lib/services/analyses.service';
|
||||
import { getAnalysisElements } from '~/lib/services/analysis-element.service';
|
||||
import { getAnalysisGroups } from '~/lib/services/analysis-group.service';
|
||||
|
||||
const SYNLAB_SERVICES_CATEGORY_HANDLE = 'synlab-services';
|
||||
const SYNLAB_ANALYSIS_PRODUCT_TYPE_HANDLE = 'synlab-analysis';
|
||||
@@ -12,7 +16,8 @@ const BASE_ANALYSIS_PRODUCT_HANDLE = 'analysis-base';
|
||||
|
||||
const getAdminSdk = () => {
|
||||
const medusaBackendUrl = process.env.MEDUSA_BACKEND_PUBLIC_URL!;
|
||||
const medusaPublishableApiKey = process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY!;
|
||||
const medusaPublishableApiKey =
|
||||
process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY!;
|
||||
const key = process.env.MEDUSA_SECRET_API_KEY!;
|
||||
|
||||
if (!medusaBackendUrl || !medusaPublishableApiKey) {
|
||||
@@ -23,15 +28,14 @@ const getAdminSdk = () => {
|
||||
debug: process.env.NODE_ENV === 'development',
|
||||
apiKey: key,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
async function createProductCategories({
|
||||
medusa,
|
||||
}: {
|
||||
medusa: Medusa;
|
||||
}) {
|
||||
const { product_categories: existingProductCategories } = await medusa.admin.productCategory.list();
|
||||
const parentCategory = existingProductCategories.find(({ handle }) => handle === SYNLAB_SERVICES_CATEGORY_HANDLE);
|
||||
async function createProductCategories({ medusa }: { medusa: Medusa }) {
|
||||
const { product_categories: existingProductCategories } =
|
||||
await medusa.admin.productCategory.list();
|
||||
const parentCategory = existingProductCategories.find(
|
||||
({ handle }) => handle === SYNLAB_SERVICES_CATEGORY_HANDLE,
|
||||
);
|
||||
|
||||
if (!parentCategory) {
|
||||
throw new Error('Parent category not found');
|
||||
@@ -46,8 +50,12 @@ async function createProductCategories({
|
||||
for (const analysisGroup of analysisGroups) {
|
||||
console.info(`Processing analysis group '${analysisGroup.name}'`);
|
||||
|
||||
const isExisting = existingProductCategories.find(({ name }) => name === analysisGroup.name);
|
||||
const isNewlyCreated = createdCategories.find(({ name }) => name === analysisGroup.name);
|
||||
const isExisting = existingProductCategories.find(
|
||||
({ name }) => name === analysisGroup.name,
|
||||
);
|
||||
const isNewlyCreated = createdCategories.find(
|
||||
({ name }) => name === analysisGroup.name,
|
||||
);
|
||||
if (isExisting || isNewlyCreated) {
|
||||
console.info(`Analysis group '${analysisGroup.name}' already exists`);
|
||||
continue;
|
||||
@@ -63,18 +71,19 @@ async function createProductCategories({
|
||||
analysisGroupId: analysisGroup.id,
|
||||
},
|
||||
});
|
||||
console.info(`Successfully created category, id=${createResponse.product_category.id}`);
|
||||
console.info(
|
||||
`Successfully created category, id=${createResponse.product_category.id}`,
|
||||
);
|
||||
createdCategories.push(createResponse.product_category);
|
||||
}
|
||||
}
|
||||
|
||||
async function getChildProductCategories({
|
||||
medusa,
|
||||
}: {
|
||||
medusa: Medusa;
|
||||
}) {
|
||||
const { product_categories: allCategories } = await medusa.admin.productCategory.list();
|
||||
const childCategories = allCategories.filter(({ parent_category_id }) => parent_category_id !== null);
|
||||
async function getChildProductCategories({ medusa }: { medusa: Medusa }) {
|
||||
const { product_categories: allCategories } =
|
||||
await medusa.admin.productCategory.list();
|
||||
const childCategories = allCategories.filter(
|
||||
({ parent_category_id }) => parent_category_id !== null,
|
||||
);
|
||||
return childCategories;
|
||||
}
|
||||
|
||||
@@ -93,34 +102,34 @@ async function deleteProductCategories({
|
||||
/**
|
||||
* In case a reset is needed
|
||||
*/
|
||||
async function deleteProducts({
|
||||
medusa,
|
||||
}: {
|
||||
medusa: Medusa;
|
||||
}) {
|
||||
async function deleteProducts({ medusa }: { medusa: Medusa }) {
|
||||
const { products: existingProducts } = await medusa.admin.product.list({
|
||||
fields: 'id,collection_id',
|
||||
limit: 1000,
|
||||
});
|
||||
|
||||
await Promise.all(existingProducts.filter((a) => !a.collection_id).map(({ id }) => medusa.admin.product.delete(id)));
|
||||
|
||||
await Promise.all(
|
||||
existingProducts
|
||||
.filter((a) => !a.collection_id)
|
||||
.map(({ id }) => medusa.admin.product.delete(id)),
|
||||
);
|
||||
}
|
||||
|
||||
async function getAnalysisPackagesType() {
|
||||
const { productTypes } = await listProductTypes();
|
||||
const analysisPackagesType = productTypes.find(({ metadata }) => metadata?.handle === SYNLAB_ANALYSIS_PRODUCT_TYPE_HANDLE);
|
||||
const analysisPackagesType = productTypes.find(
|
||||
({ metadata }) => metadata?.handle === SYNLAB_ANALYSIS_PRODUCT_TYPE_HANDLE,
|
||||
);
|
||||
if (!analysisPackagesType) {
|
||||
throw new Error('Synlab analysis packages type not found');
|
||||
}
|
||||
return analysisPackagesType;
|
||||
}
|
||||
|
||||
async function getProductDefaultFields({
|
||||
medusa,
|
||||
}: {
|
||||
medusa: Medusa;
|
||||
}) {
|
||||
const baseProductsResponse = await medusa.admin.product.list({ handle: BASE_ANALYSIS_PRODUCT_HANDLE })
|
||||
async function getProductDefaultFields({ medusa }: { medusa: Medusa }) {
|
||||
const baseProductsResponse = await medusa.admin.product.list({
|
||||
handle: BASE_ANALYSIS_PRODUCT_HANDLE,
|
||||
});
|
||||
const baseProduct = baseProductsResponse.products[0];
|
||||
if (!baseProduct) {
|
||||
throw new Error('Base product not found');
|
||||
@@ -142,25 +151,18 @@ async function getProductDefaultFields({
|
||||
defaultSalesChannels,
|
||||
defaultProductOption,
|
||||
defaultProductVariant,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
async function createProducts({
|
||||
medusa,
|
||||
}: {
|
||||
medusa: Medusa;
|
||||
}) {
|
||||
const { product_categories: allCategories } = await medusa.admin.productCategory.list();
|
||||
async function createProducts({ medusa }: { medusa: Medusa }) {
|
||||
const { product_categories: allCategories } =
|
||||
await medusa.admin.productCategory.list();
|
||||
|
||||
const [
|
||||
{ products: existingProducts },
|
||||
analysisElements,
|
||||
analysisPackagesType,
|
||||
{
|
||||
defaultSalesChannels,
|
||||
defaultProductOption,
|
||||
defaultProductVariant,
|
||||
}
|
||||
{ defaultSalesChannels, defaultProductOption, defaultProductVariant },
|
||||
] = await Promise.all([
|
||||
medusa.admin.product.list({
|
||||
category_id: allCategories.map(({ id }) => id),
|
||||
@@ -168,13 +170,17 @@ async function createProducts({
|
||||
getAnalysisElements({ getAll: true }),
|
||||
getAnalysisPackagesType(),
|
||||
getProductDefaultFields({ medusa }),
|
||||
])
|
||||
]);
|
||||
|
||||
for (const analysisElement of analysisElements) {
|
||||
const { analysis_id_original: originalId } = analysisElement;
|
||||
const isExisting = existingProducts.find(({ metadata }) => metadata?.analysisIdOriginal === originalId);
|
||||
const isExisting = existingProducts.find(
|
||||
({ metadata }) => metadata?.analysisIdOriginal === originalId,
|
||||
);
|
||||
if (isExisting) {
|
||||
console.info(`Analysis element '${analysisElement.analysis_name_lab}' already exists`);
|
||||
console.info(
|
||||
`Analysis element '${analysisElement.analysis_name_lab}' already exists`,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
const { analysis_name_lab: name } = analysisElement;
|
||||
@@ -183,7 +189,10 @@ async function createProducts({
|
||||
continue;
|
||||
}
|
||||
|
||||
const category = allCategories.find(({ metadata }) => metadata?.analysisGroupId === analysisElement.parent_analysis_group_id);
|
||||
const category = allCategories.find(
|
||||
({ metadata }) =>
|
||||
metadata?.analysisGroupId === analysisElement.parent_analysis_group_id,
|
||||
);
|
||||
if (!category) {
|
||||
console.error(`Category not found for analysis element '${name}'`);
|
||||
continue;
|
||||
@@ -227,9 +236,9 @@ export default async function syncAnalysisGroupsStore() {
|
||||
// await deleteProductCategories({ medusa, categories });
|
||||
// await deleteProducts({ medusa });
|
||||
// return;
|
||||
|
||||
|
||||
await createProducts({ medusa });
|
||||
|
||||
|
||||
await createMedusaSyncSuccessEntry();
|
||||
} catch (e) {
|
||||
await createMedusaSyncFailEntry(JSON.stringify(e));
|
||||
|
||||
Reference in New Issue
Block a user