Selaa lähdekoodia

fix(core): Assign assets when assigning Collection to channel

Fixes #2122, relates to #2478
Michael Bromley 2 vuotta sitten
vanhempi
sitoutus
a8481bfb1f

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 573 - 594
packages/asset-server-plugin/e2e/graphql/generated-e2e-asset-server-plugin-types.ts


Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 642 - 662
packages/common/src/generated-shop-types.ts


+ 90 - 0
packages/core/e2e/asset-channel.e2e-spec.ts

@@ -13,12 +13,14 @@ import { CurrencyCode, LanguageCode } from './graphql/generated-e2e-admin-types'
 import * as Codegen from './graphql/generated-e2e-admin-types';
 import { DeletionResult } from './graphql/generated-e2e-shop-types';
 import {
+    ASSIGN_COLLECTIONS_TO_CHANNEL,
     ASSIGN_PRODUCT_TO_CHANNEL,
     CREATE_ASSETS,
     CREATE_CHANNEL,
     DELETE_ASSET,
     GET_ASSET,
     GET_PRODUCT_WITH_VARIANTS,
+    UPDATE_COLLECTION,
     UPDATE_PRODUCT,
 } from './graphql/shared-definitions';
 import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
@@ -299,6 +301,94 @@ describe('Product related assets', () => {
     });
 });
 
+describe('Collection related assets', () => {
+    let collectionFeaturedAssetId: string;
+    it('Featured asset is available in default channel', async () => {
+        adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
+
+        await adminClient.query<Codegen.UpdateCollectionMutation, Codegen.UpdateCollectionMutationVariables>(
+            UPDATE_COLLECTION,
+            {
+                input: {
+                    id: 'T_2',
+                    featuredAssetId: 'T_3',
+                    assetIds: ['T_3'],
+                },
+            },
+        );
+
+        const { collection } = await adminClient.query<
+            Codegen.GetCollectionWithAssetsQuery,
+            Codegen.GetCollectionWithAssetsQueryVariables
+        >(GET_COLLECTION_WITH_ASSETS, {
+            id: 'T_2',
+        });
+        collectionFeaturedAssetId = collection!.featuredAsset!.id;
+        expect(collectionFeaturedAssetId).toBeDefined();
+
+        adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
+        const { asset } = await adminClient.query<Codegen.GetAssetQuery, Codegen.GetAssetQueryVariables>(
+            GET_ASSET,
+            {
+                id: collectionFeaturedAssetId,
+            },
+        );
+        expect(asset?.id).toEqual(collectionFeaturedAssetId);
+    });
+
+    it('Featured asset is not available in channel2', async () => {
+        adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
+        const { asset } = await adminClient.query<Codegen.GetAssetQuery, Codegen.GetAssetQueryVariables>(
+            GET_ASSET,
+            {
+                id: collectionFeaturedAssetId,
+            },
+        );
+        expect(asset?.id).toBeUndefined();
+    });
+
+    it('Add Collection to channel2', async () => {
+        adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
+        const { assignCollectionsToChannel } = await adminClient.query<
+            Codegen.AssignCollectionsToChannelMutation,
+            Codegen.AssignCollectionsToChannelMutationVariables
+        >(ASSIGN_COLLECTIONS_TO_CHANNEL, {
+            input: {
+                channelId: channel2Id,
+                collectionIds: ['T_2'],
+            },
+        });
+        expect(assignCollectionsToChannel[0].id).toEqual('T_2');
+    });
+
+    it('Get featured asset from channel2', async () => {
+        adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
+        const { asset } = await adminClient.query<Codegen.GetAssetQuery, Codegen.GetAssetQueryVariables>(
+            GET_ASSET,
+            {
+                id: collectionFeaturedAssetId,
+            },
+        );
+        expect(asset?.id).toEqual(collectionFeaturedAssetId);
+    });
+});
+
+const GET_COLLECTION_WITH_ASSETS = gql`
+    query GetCollectionWithAssets($id: ID!) {
+        collection(id: $id) {
+            id
+            name
+            featuredAsset {
+                ...Asset
+            }
+            assets {
+                ...Asset
+            }
+        }
+    }
+    ${ASSET_FRAGMENT}
+`;
+
 export const ASSIGN_ASSET_TO_CHANNEL = gql`
     mutation assignAssetsToChannel($input: AssignAssetsToChannelInput!) {
         assignAssetsToChannel(input: $input) {

+ 2 - 10
packages/core/e2e/collection.e2e-spec.ts

@@ -13,7 +13,7 @@ import path from 'path';
 import { afterAll, beforeAll, describe, expect, it } from 'vitest';
 
 import { initialData } from '../../../e2e-common/e2e-initial-data';
-import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
+import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
 import { pick } from '../../common/lib/pick';
 
 import { COLLECTION_FRAGMENT, FACET_VALUE_FRAGMENT } from './graphql/fragments';
@@ -28,6 +28,7 @@ import {
     SortOrder,
 } from './graphql/generated-e2e-admin-types';
 import {
+    ASSIGN_COLLECTIONS_TO_CHANNEL,
     CREATE_CHANNEL,
     CREATE_COLLECTION,
     DELETE_PRODUCT,
@@ -2579,15 +2580,6 @@ const PREVIEW_COLLECTION_VARIANTS = gql`
     }
 `;
 
-const ASSIGN_COLLECTIONS_TO_CHANNEL = gql`
-    mutation AssignCollectionsToChannel($input: AssignCollectionsToChannelInput!) {
-        assignCollectionsToChannel(input: $input) {
-            ...Collection
-        }
-    }
-    ${COLLECTION_FRAGMENT}
-`;
-
 const REMOVE_COLLECTIONS_FROM_CHANNEL = gql`
     mutation RemoveCollectionsFromChannel($input: RemoveCollectionsFromChannelInput!) {
         removeCollectionsFromChannel(input: $input) {

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 573 - 594
packages/core/e2e/graphql/generated-e2e-admin-types.ts


Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 614 - 634
packages/core/e2e/graphql/generated-e2e-shop-types.ts


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

@@ -1029,3 +1029,12 @@ export const UPDATE_ADMINISTRATOR = gql`
     }
     ${ADMINISTRATOR_FRAGMENT}
 `;
+
+export const ASSIGN_COLLECTIONS_TO_CHANNEL = gql`
+    mutation AssignCollectionsToChannel($input: AssignCollectionsToChannelInput!) {
+        assignCollectionsToChannel(input: $input) {
+            ...Collection
+        }
+    }
+    ${COLLECTION_FRAGMENT}
+`;

+ 7 - 1
packages/core/src/service/services/collection.service.ts

@@ -15,6 +15,7 @@ import {
 import { pick } from '@vendure/common/lib/pick';
 import { ROOT_COLLECTION_NAME } from '@vendure/common/lib/shared-constants';
 import { ID, PaginatedList } from '@vendure/common/lib/shared-types';
+import { unique } from '@vendure/common/lib/unique';
 import { merge } from 'rxjs';
 import { debounceTime } from 'rxjs/operators';
 import { In, IsNull } from 'typeorm';
@@ -798,7 +799,7 @@ export class CollectionService implements OnModuleInit {
         }
         const collectionsToAssign = await this.connection
             .getRepository(ctx, Collection)
-            .find({ where: { id: In(input.collectionIds) } });
+            .find({ where: { id: In(input.collectionIds) }, relations: { assets: true } });
 
         await Promise.all(
             collectionsToAssign.map(collection =>
@@ -806,6 +807,11 @@ export class CollectionService implements OnModuleInit {
             ),
         );
 
+        const assetIds: ID[] = unique(
+            ([] as ID[]).concat(...collectionsToAssign.map(c => c.assets.map(a => a.assetId))),
+        );
+        await this.assetService.assignToChannel(ctx, { channelId: input.channelId, assetIds });
+
         await this.applyFiltersQueue.add({
             ctx: ctx.serialize(),
             collectionIds: collectionsToAssign.map(collection => collection.id),

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 573 - 594
packages/elasticsearch-plugin/e2e/graphql/generated-e2e-elasticsearch-plugin-types.ts


Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 573 - 594
packages/payments-plugin/e2e/graphql/generated-admin-types.ts


Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 614 - 634
packages/payments-plugin/e2e/graphql/generated-shop-types.ts


Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 646 - 666
packages/payments-plugin/src/mollie/graphql/generated-shop-types.ts


Kaikkia tiedostoja ei voida näyttää, sillä liian monta tiedostoa muuttui tässä diffissä