Просмотр исходного кода

fix(core): Filter StockMovements by type when fetching by product variant (#3713)

Ariel Barabas 5 месяцев назад
Родитель
Сommit
99907fc60e

+ 160 - 0
packages/core/e2e/graphql/generated-e2e-admin-types.ts

@@ -9761,6 +9761,30 @@ export type GetStockMovementQuery = {
     } | null;
 };
 
+export type GetStockMovementByTypeQueryVariables = Exact<{
+    id: Scalars['ID']['input'];
+    type: StockMovementType;
+}>;
+
+export type GetStockMovementByTypeQuery = {
+    product?: {
+        id: string;
+        variants: Array<{
+            stockMovements: {
+                totalItems: number;
+                items: Array<
+                    | { id: string; type: StockMovementType; quantity: number }
+                    | { id: string; type: StockMovementType; quantity: number }
+                    | { id: string; type: StockMovementType; quantity: number }
+                    | { id: string; type: StockMovementType; quantity: number }
+                    | { id: string; type: StockMovementType; quantity: number }
+                    | { id: string; type: StockMovementType; quantity: number }
+                >;
+            };
+        }>;
+    } | null;
+};
+
 export type GetRunningJobsQueryVariables = Exact<{
     options?: InputMaybe<JobListOptions>;
 }>;
@@ -27294,6 +27318,142 @@ export const GetStockMovementDocument = {
         },
     ],
 } as unknown as DocumentNode<GetStockMovementQuery, GetStockMovementQueryVariables>;
+export const GetStockMovementByTypeDocument = {
+    kind: 'Document',
+    definitions: [
+        {
+            kind: 'OperationDefinition',
+            operation: 'query',
+            name: { kind: 'Name', value: 'GetStockMovementByType' },
+            variableDefinitions: [
+                {
+                    kind: 'VariableDefinition',
+                    variable: { kind: 'Variable', name: { kind: 'Name', value: 'id' } },
+                    type: {
+                        kind: 'NonNullType',
+                        type: { kind: 'NamedType', name: { kind: 'Name', value: 'ID' } },
+                    },
+                },
+                {
+                    kind: 'VariableDefinition',
+                    variable: { kind: 'Variable', name: { kind: 'Name', value: 'type' } },
+                    type: {
+                        kind: 'NonNullType',
+                        type: { kind: 'NamedType', name: { kind: 'Name', value: 'StockMovementType' } },
+                    },
+                },
+            ],
+            selectionSet: {
+                kind: 'SelectionSet',
+                selections: [
+                    {
+                        kind: 'Field',
+                        name: { kind: 'Name', value: 'product' },
+                        arguments: [
+                            {
+                                kind: 'Argument',
+                                name: { kind: 'Name', value: 'id' },
+                                value: { kind: 'Variable', name: { kind: 'Name', value: 'id' } },
+                            },
+                        ],
+                        selectionSet: {
+                            kind: 'SelectionSet',
+                            selections: [
+                                { kind: 'Field', name: { kind: 'Name', value: 'id' } },
+                                {
+                                    kind: 'Field',
+                                    name: { kind: 'Name', value: 'variants' },
+                                    selectionSet: {
+                                        kind: 'SelectionSet',
+                                        selections: [
+                                            {
+                                                kind: 'Field',
+                                                name: { kind: 'Name', value: 'stockMovements' },
+                                                arguments: [
+                                                    {
+                                                        kind: 'Argument',
+                                                        name: { kind: 'Name', value: 'options' },
+                                                        value: {
+                                                            kind: 'ObjectValue',
+                                                            fields: [
+                                                                {
+                                                                    kind: 'ObjectField',
+                                                                    name: { kind: 'Name', value: 'type' },
+                                                                    value: {
+                                                                        kind: 'Variable',
+                                                                        name: { kind: 'Name', value: 'type' },
+                                                                    },
+                                                                },
+                                                            ],
+                                                        },
+                                                    },
+                                                ],
+                                                selectionSet: {
+                                                    kind: 'SelectionSet',
+                                                    selections: [
+                                                        {
+                                                            kind: 'Field',
+                                                            name: { kind: 'Name', value: 'items' },
+                                                            selectionSet: {
+                                                                kind: 'SelectionSet',
+                                                                selections: [
+                                                                    {
+                                                                        kind: 'InlineFragment',
+                                                                        typeCondition: {
+                                                                            kind: 'NamedType',
+                                                                            name: {
+                                                                                kind: 'Name',
+                                                                                value: 'StockMovement',
+                                                                            },
+                                                                        },
+                                                                        selectionSet: {
+                                                                            kind: 'SelectionSet',
+                                                                            selections: [
+                                                                                {
+                                                                                    kind: 'Field',
+                                                                                    name: {
+                                                                                        kind: 'Name',
+                                                                                        value: 'id',
+                                                                                    },
+                                                                                },
+                                                                                {
+                                                                                    kind: 'Field',
+                                                                                    name: {
+                                                                                        kind: 'Name',
+                                                                                        value: 'type',
+                                                                                    },
+                                                                                },
+                                                                                {
+                                                                                    kind: 'Field',
+                                                                                    name: {
+                                                                                        kind: 'Name',
+                                                                                        value: 'quantity',
+                                                                                    },
+                                                                                },
+                                                                            ],
+                                                                        },
+                                                                    },
+                                                                ],
+                                                            },
+                                                        },
+                                                        {
+                                                            kind: 'Field',
+                                                            name: { kind: 'Name', value: 'totalItems' },
+                                                        },
+                                                    ],
+                                                },
+                                            },
+                                        ],
+                                    },
+                                },
+                            ],
+                        },
+                    },
+                ],
+            },
+        },
+    ],
+} as unknown as DocumentNode<GetStockMovementByTypeQuery, GetStockMovementByTypeQueryVariables>;
 export const GetRunningJobsDocument = {
     kind: 'Document',
     definitions: [

+ 21 - 0
packages/core/e2e/graphql/shared-definitions.ts

@@ -312,6 +312,27 @@ export const GET_STOCK_MOVEMENT = gql`
     }
     ${VARIANT_WITH_STOCK_FRAGMENT}
 `;
+
+export const GET_STOCK_MOVEMENT_BY_TYPE = gql`
+    query GetStockMovementByType($id: ID!, $type: StockMovementType!) {
+        product(id: $id) {
+            id
+            variants {
+                stockMovements(options: { type: $type }) {
+                    items {
+                        ... on StockMovement {
+                            id
+                            type
+                            quantity
+                        }
+                    }
+                    totalItems
+                }
+            }
+        }
+    }
+`;
+
 export const GET_RUNNING_JOBS = gql`
     query GetRunningJobs($options: JobListOptions) {
         jobs(options: $options) {

+ 26 - 0
packages/core/e2e/stock-control.e2e-spec.ts

@@ -35,6 +35,7 @@ import {
     CREATE_FULFILLMENT,
     GET_ORDER,
     GET_STOCK_MOVEMENT,
+    GET_STOCK_MOVEMENT_BY_TYPE,
     SETTLE_PAYMENT,
     UPDATE_GLOBAL_SETTINGS,
     UPDATE_PRODUCT_VARIANTS,
@@ -111,6 +112,14 @@ describe('Stock control', () => {
         return product;
     }
 
+    async function getProductWithStockMovementByType(productId: string, type: StockMovementType) {
+        const { product } = await adminClient.query<
+            Codegen.GetStockMovementByTypeQuery,
+            Codegen.GetStockMovementByTypeQueryVariables
+        >(GET_STOCK_MOVEMENT_BY_TYPE, { id: productId, type });
+        return product;
+    }
+
     async function setFirstEligibleShippingMethod() {
         const { eligibleShippingMethods } = await shopClient.query<CodegenShop.GetShippingMethodsQuery>(
             GET_ELIGIBLE_SHIPPING_METHODS,
@@ -345,6 +354,23 @@ describe('Stock control', () => {
             expect(variant3.stockMovements.items[1].quantity).toBe(4);
         });
 
+        it('returns all stockMovements filtered by type', async () => {
+            const product = await getProductWithStockMovementByType('T_2', StockMovementType.ALLOCATION);
+            const [variant1, variant2, variant3] = product!.variants;
+
+            expect(variant1.stockMovements.totalItems).toBe(1);
+            expect(variant1.stockMovements.items[0].type).toBe(StockMovementType.ALLOCATION);
+            expect(variant1.stockMovements.items[0].quantity).toBe(2);
+
+            expect(variant2.stockMovements.totalItems).toBe(1);
+            expect(variant2.stockMovements.items[0].type).toBe(StockMovementType.ALLOCATION);
+            expect(variant2.stockMovements.items[0].quantity).toBe(3);
+
+            expect(variant3.stockMovements.totalItems).toBe(1);
+            expect(variant3.stockMovements.items[0].type).toBe(StockMovementType.ALLOCATION);
+            expect(variant3.stockMovements.items[0].quantity).toBe(4);
+        });
+
         it('stockAllocated is updated according to trackInventory setting', async () => {
             const product = await getProductWithStockMovement('T_2');
             const [variant1, variant2, variant3] = product!.variants;

+ 12 - 10
packages/core/src/service/services/stock-movement.service.ts

@@ -61,19 +61,21 @@ export class StockMovementService {
     getStockMovementsByProductVariantId(
         ctx: RequestContext,
         productVariantId: ID,
-        options: StockMovementListOptions,
+        options?: StockMovementListOptions,
     ): Promise<PaginatedList<StockMovement>> {
-        return this.listQueryBuilder
+        const qb = this.listQueryBuilder
             .build<StockMovement>(StockMovement as any, options, { ctx })
             .leftJoin('stockmovement.productVariant', 'productVariant')
-            .andWhere('productVariant.id = :productVariantId', { productVariantId })
-            .getManyAndCount()
-            .then(async ([items, totalItems]) => {
-                return {
-                    items,
-                    totalItems,
-                };
-            });
+            .andWhere('productVariant.id = :productVariantId', { productVariantId });
+
+        if (options?.type) {
+            qb.andWhere('stockmovement.type = :type', { type: options.type });
+        }
+
+        return qb.getManyAndCount().then(([items, totalItems]) => ({
+            items,
+            totalItems,
+        }));
     }
 
     /**