|
@@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
|
|
|
import { InjectConnection } from '@nestjs/typeorm';
|
|
import { InjectConnection } from '@nestjs/typeorm';
|
|
|
import { CreateChannelInput, CurrencyCode, UpdateChannelInput } from '@vendure/common/lib/generated-types';
|
|
import { CreateChannelInput, CurrencyCode, UpdateChannelInput } from '@vendure/common/lib/generated-types';
|
|
|
import { DEFAULT_CHANNEL_CODE } from '@vendure/common/lib/shared-constants';
|
|
import { DEFAULT_CHANNEL_CODE } from '@vendure/common/lib/shared-constants';
|
|
|
-import { ID } from '@vendure/common/lib/shared-types';
|
|
|
|
|
|
|
+import { ID, Type } from '@vendure/common/lib/shared-types';
|
|
|
import { unique } from '@vendure/common/lib/unique';
|
|
import { unique } from '@vendure/common/lib/unique';
|
|
|
import { Connection } from 'typeorm';
|
|
import { Connection } from 'typeorm';
|
|
|
|
|
|
|
@@ -12,6 +12,7 @@ import { ChannelNotFoundError, EntityNotFoundError, InternalServerError } from '
|
|
|
import { ChannelAware } from '../../common/types/common-types';
|
|
import { ChannelAware } from '../../common/types/common-types';
|
|
|
import { assertFound } from '../../common/utils';
|
|
import { assertFound } from '../../common/utils';
|
|
|
import { ConfigService } from '../../config/config.service';
|
|
import { ConfigService } from '../../config/config.service';
|
|
|
|
|
+import { VendureEntity } from '../../entity/base/base.entity';
|
|
|
import { Channel } from '../../entity/channel/channel.entity';
|
|
import { Channel } from '../../entity/channel/channel.entity';
|
|
|
import { Zone } from '../../entity/zone/zone.entity';
|
|
import { Zone } from '../../entity/zone/zone.entity';
|
|
|
import { getEntityOrThrow } from '../helpers/utils/get-entity-or-throw';
|
|
import { getEntityOrThrow } from '../helpers/utils/get-entity-or-throw';
|
|
@@ -36,12 +37,29 @@ export class ChannelService {
|
|
|
* Assigns a ChannelAware entity to the default Channel as well as any channel
|
|
* Assigns a ChannelAware entity to the default Channel as well as any channel
|
|
|
* specified in the RequestContext.
|
|
* specified in the RequestContext.
|
|
|
*/
|
|
*/
|
|
|
- assignToChannels<T extends ChannelAware>(entity: T, ctx: RequestContext): T {
|
|
|
|
|
|
|
+ assignToCurrentChannel<T extends ChannelAware>(entity: T, ctx: RequestContext): T {
|
|
|
const channelIds = unique([ctx.channelId, this.getDefaultChannel().id]);
|
|
const channelIds = unique([ctx.channelId, this.getDefaultChannel().id]);
|
|
|
entity.channels = channelIds.map(id => ({ id })) as any;
|
|
entity.channels = channelIds.map(id => ({ id })) as any;
|
|
|
return entity;
|
|
return entity;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Assigns the entity to the given Channel and saves.
|
|
|
|
|
+ */
|
|
|
|
|
+ async assignToChannel<T extends ChannelAware & VendureEntity>(
|
|
|
|
|
+ entityType: Type<T>,
|
|
|
|
|
+ entityId: ID,
|
|
|
|
|
+ channelId: ID,
|
|
|
|
|
+ ): Promise<T> {
|
|
|
|
|
+ const entity = await getEntityOrThrow(this.connection, entityType, entityId, {
|
|
|
|
|
+ relations: ['channels'],
|
|
|
|
|
+ });
|
|
|
|
|
+ const channel = await getEntityOrThrow(this.connection, Channel, channelId);
|
|
|
|
|
+ entity.channels.push(channel);
|
|
|
|
|
+ await this.connection.getRepository(entityType).save(entity as any);
|
|
|
|
|
+ return entity;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Given a channel token, returns the corresponding Channel if it exists.
|
|
* Given a channel token, returns the corresponding Channel if it exists.
|
|
|
*/
|
|
*/
|