Skip to content

Commit

Permalink
fix(core): Fix featuredAsset error when adding item to Order
Browse files Browse the repository at this point in the history
Fixes #756
  • Loading branch information
michaelbromley committed Mar 11, 2021
1 parent 28b096c commit e635f25
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
31 changes: 21 additions & 10 deletions packages/core/src/service/services/asset.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { RequestContext } from '../../api/common/request-context';
import { isGraphQlErrorResult } from '../../common/error/error-result';
import { ForbiddenError, InternalServerError } from '../../common/error/errors';
import { MimeTypeError } from '../../common/error/generated-graphql-admin-errors';
import { ChannelAware } from '../../common/types/common-types';
import { getAssetType, idsAreEqual } from '../../common/utils';
import { ConfigService } from '../../config/config.service';
import { Logger } from '../../config/logger/vendure-logger';
Expand Down Expand Up @@ -117,16 +118,26 @@ export class AssetService {
ctx: RequestContext,
entity: T,
): Promise<Asset | undefined> {
const entityType: Type<EntityWithAssets> = Object.getPrototypeOf(entity).constructor;
const entityWithFeaturedAsset = await this.connection.findOneInChannel(
ctx,
entityType,
entity.id,
ctx.channelId,
{
relations: ['featuredAsset'],
},
);
const entityType: Type<T> = Object.getPrototypeOf(entity).constructor;
let entityWithFeaturedAsset: T | undefined;

if (this.channelService.isChannelAware(entity)) {
entityWithFeaturedAsset = await this.connection.findOneInChannel(
ctx,
entityType as Type<T & ChannelAware>,
entity.id,
ctx.channelId,
{
relations: ['featuredAsset'],
},
);
} else {
entityWithFeaturedAsset = await this.connection
.getRepository(ctx, entityType)
.findOne(entity.id, {
relations: ['featuredAsset'],
});
}
return (entityWithFeaturedAsset && entityWithFeaturedAsset.featuredAsset) || undefined;
}

Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/service/services/channel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from '@vendure/common/lib/generated-types';
import { DEFAULT_CHANNEL_CODE } from '@vendure/common/lib/shared-constants';
import { ID, Type } from '@vendure/common/lib/shared-types';
import { notNullOrUndefined } from '@vendure/common/lib/shared-utils';
import { unique } from '@vendure/common/lib/unique';

import { RequestContext } from '../../api/common/request-context';
Expand Down Expand Up @@ -213,6 +212,13 @@ export class ChannelService {
};
}

public isChannelAware(entity: VendureEntity): entity is VendureEntity & ChannelAware {
const entityType = Object.getPrototypeOf(entity).constructor;
return !!this.connection.rawConnection
.getMetadata(entityType)
.relations.find(r => r.type === Channel && r.propertyName === 'channels');
}

/**
* There must always be a default Channel. If none yet exists, this method creates one.
* Also ensures the default Channel token matches the defaultChannelToken config setting.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class TransactionalConnection {
const { channelId, ...optionsWithoutChannelId } = options;
entity = await this.findOneInChannel(
ctx,
entityType,
entityType as Type<T & ChannelAware>,
id,
options.channelId,
optionsWithoutChannelId,
Expand All @@ -163,7 +163,7 @@ export class TransactionalConnection {
* Like the TypeOrm `Repository.findOne()` method, but limits the results to
* the given Channel.
*/
findOneInChannel<T extends ChannelAware | VendureEntity>(
findOneInChannel<T extends ChannelAware & VendureEntity>(
ctx: RequestContext,
entity: Type<T>,
id: ID,
Expand Down

0 comments on commit e635f25

Please sign in to comment.