fix: Fixes to product module and improving tests (#6898)
This commit is contained in:
@@ -58,7 +58,7 @@ export const updateProductVariantsWorkflow = createWorkflow(
|
||||
|
||||
return {
|
||||
selector: {
|
||||
ids: data.variantPriceSetLinks.map((link) => link.price_set_id),
|
||||
id: data.variantPriceSetLinks.map((link) => link.price_set_id),
|
||||
} as PricingTypes.FilterablePriceSetProps,
|
||||
update: {
|
||||
prices: data.input.update.prices,
|
||||
@@ -78,11 +78,11 @@ export const updateProductVariantsWorkflow = createWorkflow(
|
||||
},
|
||||
(data) => {
|
||||
return data.updatedVariants.map((variant, i) => {
|
||||
const linkForVariant = data.variantPriceSetLinks.find(
|
||||
const linkForVariant = data.variantPriceSetLinks?.find(
|
||||
(link) => link.variant_id === variant.id
|
||||
)
|
||||
|
||||
const priceSetForVariant = data.updatedPriceSets.find(
|
||||
const priceSetForVariant = data.updatedPriceSets?.find(
|
||||
(priceSet) => priceSet.id === linkForVariant?.price_set_id
|
||||
)
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ export const updateProductsWorkflow = createWorkflow(
|
||||
(
|
||||
input: WorkflowData<WorkflowInput>
|
||||
): WorkflowData<ProductTypes.ProductDTO[]> => {
|
||||
// TODO: Delete price sets for removed variants
|
||||
return updateProductsStep(input)
|
||||
}
|
||||
)
|
||||
|
||||
+42
-10
@@ -19,35 +19,38 @@ moduleIntegrationTestRunner({
|
||||
let productTwo: Product
|
||||
|
||||
beforeEach(async () => {
|
||||
const testManager = await MikroOrmWrapper.forkManager()
|
||||
|
||||
productOne = testManager.create(Product, {
|
||||
productOne = await service.create({
|
||||
id: "product-1",
|
||||
title: "product 1",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
options: [
|
||||
{
|
||||
title: "size",
|
||||
values: ["large"],
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
productTwo = testManager.create(Product, {
|
||||
productTwo = await service.create({
|
||||
id: "product-2",
|
||||
title: "product 2",
|
||||
status: ProductTypes.ProductStatus.PUBLISHED,
|
||||
})
|
||||
|
||||
variantOne = testManager.create(ProductVariant, {
|
||||
variantOne = await service.createVariants({
|
||||
id: "test-1",
|
||||
title: "variant 1",
|
||||
inventory_quantity: 10,
|
||||
product: productOne,
|
||||
product_id: productOne.id,
|
||||
options: { size: "large" },
|
||||
})
|
||||
|
||||
variantTwo = testManager.create(ProductVariant, {
|
||||
variantTwo = await service.createVariants({
|
||||
id: "test-2",
|
||||
title: "variant",
|
||||
inventory_quantity: 10,
|
||||
product: productTwo,
|
||||
product_id: productTwo.id,
|
||||
})
|
||||
|
||||
await testManager.persistAndFlush([variantOne, variantTwo])
|
||||
})
|
||||
|
||||
describe("listAndCountVariants", () => {
|
||||
@@ -165,6 +168,35 @@ moduleIntegrationTestRunner({
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("softDelete variant", () => {
|
||||
it("should soft delete a variant and its relations", async () => {
|
||||
const beforeDeletedVariants = await service.listVariants(
|
||||
{ id: variantOne.id },
|
||||
{
|
||||
relations: ["options", "options.option_value", "options.variant"],
|
||||
}
|
||||
)
|
||||
|
||||
await service.softDeleteVariants([variantOne.id])
|
||||
const deletedVariants = await service.listVariants(
|
||||
{ id: variantOne.id },
|
||||
{
|
||||
relations: ["options", "options.option_value", "options.variant"],
|
||||
withDeleted: true,
|
||||
}
|
||||
)
|
||||
|
||||
expect(deletedVariants).toHaveLength(1)
|
||||
expect(deletedVariants[0].deleted_at).not.toBeNull()
|
||||
|
||||
for (const variantOption of deletedVariants[0].options) {
|
||||
expect(variantOption.deleted_at).not.toBeNull()
|
||||
// The value itself should not be affected
|
||||
expect(variantOption?.option_value?.deleted_at).toBeNull()
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
-1
@@ -738,7 +738,6 @@ moduleIntegrationTestRunner({
|
||||
relations: [
|
||||
"variants",
|
||||
"variants.options",
|
||||
"variants.options",
|
||||
"options",
|
||||
"options.values",
|
||||
],
|
||||
|
||||
@@ -246,6 +246,7 @@ export default class ProductModuleService<
|
||||
},
|
||||
{
|
||||
take: null,
|
||||
relations: ["values"],
|
||||
},
|
||||
sharedContext
|
||||
)
|
||||
|
||||
@@ -579,6 +579,18 @@ export interface ProductVariantOptionDTO {
|
||||
* The associated product variant id.
|
||||
*/
|
||||
variant_id?: string | null
|
||||
/**
|
||||
* When the product variant option was created.
|
||||
*/
|
||||
created_at: string | Date
|
||||
/**
|
||||
* When the product variant option was updated.
|
||||
*/
|
||||
updated_at: string | Date
|
||||
/**
|
||||
* When the product variant option was deleted.
|
||||
*/
|
||||
deleted_at?: string | Date
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -259,10 +259,7 @@ export interface IProductModuleService extends IModuleService {
|
||||
* title: "Shirt",
|
||||
* })
|
||||
*/
|
||||
upsert(
|
||||
data: UpsertProductDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<ProductDTO[]>
|
||||
upsert(data: UpsertProductDTO, sharedContext?: Context): Promise<ProductDTO>
|
||||
|
||||
/**
|
||||
* This method is used to update a product.
|
||||
|
||||
Reference in New Issue
Block a user