@haus-tech/badge-plugin 3.0.0 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,8 @@
1
1
  import { DeepPartial } from '@vendure/common/lib/shared-types';
2
- import { VendureEntity, Collection, ID, Asset } from '@vendure/core';
3
- export declare class Badge extends VendureEntity {
2
+ import { VendureEntity, Collection, ID, Asset, ChannelAware, Channel } from '@vendure/core';
3
+ export declare class Badge extends VendureEntity implements ChannelAware {
4
4
  constructor(input?: DeepPartial<Badge>);
5
+ channels: Channel[];
5
6
  position: string;
6
7
  order: number;
7
8
  text: string;
@@ -20,6 +20,7 @@ let Badge = class Badge extends core_1.VendureEntity {
20
20
  Object.assign(this, input);
21
21
  }
22
22
  }
23
+ channels;
23
24
  position;
24
25
  order;
25
26
  text;
@@ -28,6 +29,11 @@ let Badge = class Badge extends core_1.VendureEntity {
28
29
  collection;
29
30
  collectionId;
30
31
  };
32
+ __decorate([
33
+ (0, typeorm_1.ManyToMany)((type) => core_1.Channel),
34
+ (0, typeorm_1.JoinTable)(),
35
+ __metadata("design:type", Array)
36
+ ], Badge.prototype, "channels", void 0);
31
37
  __decorate([
32
38
  (0, typeorm_1.Column)({ default: 'top-left' }),
33
39
  __metadata("design:type", String)
@@ -1,14 +1,18 @@
1
- import { AssetService, CollectionService, EntityHydrator, ErrorResult, ListQueryBuilder, ListQueryOptions, PaginatedList, Product, RequestContext, TransactionalConnection } from '@vendure/core';
1
+ import { OnApplicationBootstrap } from '@nestjs/common';
2
+ import { AssetService, ChannelService, CollectionService, EntityHydrator, ErrorResult, ListQueryBuilder, ListQueryOptions, PaginatedList, Product, RequestContext, TransactionalConnection } from '@vendure/core';
2
3
  import { Badge } from '../entity/badge.entity';
3
4
  import { CreateBadgeInput, DeletionResponse, UpdateBadgeInput } from '../gql/generated';
4
5
  import { SearchResult } from '@vendure/common/lib/generated-shop-types';
5
- export declare class BadgeService {
6
+ import { AssignBadgesToChannelInput } from '../types';
7
+ export declare class BadgeService implements OnApplicationBootstrap {
6
8
  private listQueryBuilder;
7
9
  private connection;
8
10
  private assetService;
9
11
  private entityHydratorService;
10
12
  private collectionService;
11
- constructor(listQueryBuilder: ListQueryBuilder, connection: TransactionalConnection, assetService: AssetService, entityHydratorService: EntityHydrator, collectionService: CollectionService);
13
+ private channelService;
14
+ constructor(listQueryBuilder: ListQueryBuilder, connection: TransactionalConnection, assetService: AssetService, entityHydratorService: EntityHydrator, collectionService: CollectionService, channelService: ChannelService);
15
+ onApplicationBootstrap(): Promise<void>;
12
16
  findOne(ctx: RequestContext, id: number): Promise<Badge | null>;
13
17
  findAll(ctx: RequestContext, options?: ListQueryOptions<Badge>): Promise<PaginatedList<Badge>>;
14
18
  create(ctx: RequestContext, input: CreateBadgeInput): Promise<Badge>;
@@ -18,4 +22,5 @@ export declare class BadgeService {
18
22
  findByCollectionIds(ctx: RequestContext, collectionIds: string[]): Promise<Badge[]>;
19
23
  findBadgesForProduct(ctx: RequestContext, product: Product): Promise<Badge[]>;
20
24
  findBadgesForSearchResult(ctx: RequestContext, searchResult: SearchResult): Promise<Badge[]>;
25
+ assignToChannel(ctx: RequestContext, input: AssignBadgesToChannelInput): Promise<Badge[]>;
21
26
  }
@@ -21,19 +21,37 @@ let BadgeService = class BadgeService {
21
21
  assetService;
22
22
  entityHydratorService;
23
23
  collectionService;
24
- constructor(listQueryBuilder, connection, assetService, entityHydratorService, collectionService) {
24
+ channelService;
25
+ constructor(listQueryBuilder, connection, assetService, entityHydratorService, collectionService, channelService) {
25
26
  this.listQueryBuilder = listQueryBuilder;
26
27
  this.connection = connection;
27
28
  this.assetService = assetService;
28
29
  this.entityHydratorService = entityHydratorService;
29
30
  this.collectionService = collectionService;
31
+ this.channelService = channelService;
32
+ }
33
+ async onApplicationBootstrap() {
34
+ // Assign all badges which are not assigned to any channel to the default channel
35
+ const defaultChannel = await this.channelService.getDefaultChannel();
36
+ const badgesWithoutChannels = await this.connection.rawConnection
37
+ .getRepository(badge_entity_1.Badge)
38
+ .createQueryBuilder('badge')
39
+ .leftJoinAndSelect('badge.channels', 'channel')
40
+ .where('channel.id IS NULL')
41
+ .getMany();
42
+ for (const badge of badgesWithoutChannels) {
43
+ badge.channels = [defaultChannel];
44
+ await this.connection.rawConnection.getRepository(badge_entity_1.Badge).save(badge);
45
+ }
30
46
  }
31
47
  async findOne(ctx, id) {
32
48
  return this.connection.getRepository(ctx, badge_entity_1.Badge).findOne({ where: { id } });
33
49
  }
34
50
  async findAll(ctx, options) {
35
51
  return this.listQueryBuilder
36
- .build(badge_entity_1.Badge, options, { relations: ['collection', 'asset'], ctx })
52
+ .build(badge_entity_1.Badge, options, { relations: ['collection', 'asset', 'channels'], ctx })
53
+ .leftJoin('badge.channels', 'channel')
54
+ .andWhere('channel.id = :channelId', { channelId: ctx.channel.id })
37
55
  .getManyAndCount()
38
56
  .then(([items, totalItems]) => ({ items, totalItems }));
39
57
  }
@@ -41,6 +59,7 @@ let BadgeService = class BadgeService {
41
59
  const badge = new badge_entity_1.Badge({
42
60
  position: 'top-left',
43
61
  ...input,
62
+ channels: [ctx.channel],
44
63
  });
45
64
  return this.connection.getRepository(ctx, badge_entity_1.Badge).save(badge);
46
65
  }
@@ -80,12 +99,19 @@ let BadgeService = class BadgeService {
80
99
  return badge || new core_1.ErrorResult();
81
100
  }
82
101
  async findOneByCollectionId(ctx, collectionId) {
83
- return this.connection.getRepository(ctx, badge_entity_1.Badge).findOne({ where: { collectionId } });
102
+ const badge = await this.connection
103
+ .getRepository(ctx, badge_entity_1.Badge)
104
+ .findOne({ where: { collectionId }, relations: ['channels'] });
105
+ if (badge?.channels.some((channel) => channel.id === ctx.channel.id)) {
106
+ return badge;
107
+ }
108
+ return null;
84
109
  }
85
110
  async findByCollectionIds(ctx, collectionIds) {
86
- return this.connection
111
+ const badges = await this.connection
87
112
  .getRepository(ctx, badge_entity_1.Badge)
88
- .find({ where: { collectionId: (0, typeorm_1.In)(collectionIds) } });
113
+ .find({ where: { collectionId: (0, typeorm_1.In)(collectionIds) }, relations: ['channels'] });
114
+ return badges.filter((badge) => badge.channels.some((channel) => channel.id === ctx.channel.id));
89
115
  }
90
116
  async findBadgesForProduct(ctx, product) {
91
117
  const collections = await this.collectionService.getCollectionsByProductId(ctx, product.id, true);
@@ -97,6 +123,13 @@ let BadgeService = class BadgeService {
97
123
  const collectionIds = collections.map((c) => c.id);
98
124
  return this.findByCollectionIds(ctx, collectionIds);
99
125
  }
126
+ async assignToChannel(ctx, input) {
127
+ const badges = await this.connection.findByIdsInChannel(ctx, badge_entity_1.Badge, input.badgeIds, ctx.channelId, {});
128
+ await Promise.all(badges.map(async (badge) => {
129
+ await this.channelService.assignToChannels(ctx, badge_entity_1.Badge, badge.id, [input.channelId]);
130
+ }));
131
+ return this.connection.findByIdsInChannel(ctx, badge_entity_1.Badge, badges.map((b) => b.id), ctx.channelId, {});
132
+ }
100
133
  };
101
134
  BadgeService = __decorate([
102
135
  (0, common_1.Injectable)(),
@@ -104,6 +137,7 @@ BadgeService = __decorate([
104
137
  core_1.TransactionalConnection,
105
138
  core_1.AssetService,
106
139
  core_1.EntityHydrator,
107
- core_1.CollectionService])
140
+ core_1.CollectionService,
141
+ core_1.ChannelService])
108
142
  ], BadgeService);
109
143
  exports.BadgeService = BadgeService;
@@ -0,0 +1,5 @@
1
+ import { Scalars } from '@vendure/core';
2
+ export type AssignBadgesToChannelInput = {
3
+ badgeIds: Array<Scalars['ID']>;
4
+ channelId: Scalars['ID'];
5
+ };
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -27,7 +27,7 @@
27
27
  [disabled]="true"
28
28
  [hiddenWhenOff]="true"
29
29
  ></vdr-select-toggle>
30
- <img class="asset-thumb" [src]="item.asset | assetPreview : 'thumb'" />
30
+ <img class="asset-thumb" [src]="item.asset | assetPreview : 'medium'" />
31
31
  </div>
32
32
  <div class="detail">
33
33
  <span [title]="item.position">{{ item.position }}</span>
@@ -35,6 +35,7 @@
35
35
 
36
36
  img.asset-thumb {
37
37
  aspect-ratio: 1;
38
+ object-fit: contain;
38
39
  }
39
40
 
40
41
  vdr-select-toggle {
@@ -17,7 +17,7 @@
17
17
  </ng-select>
18
18
  </vdr-labeled-data>
19
19
 
20
- <vdr-labeled-data [label]="'badge-plugin.collections' | translate">
20
+ <vdr-labeled-data [label]="'badge-plugin.collections' | translate" class="select-wrapper">
21
21
  <ng-select
22
22
  [multiple]="false"
23
23
  [clearable]="true"
@@ -14,3 +14,10 @@
14
14
  .label-title {
15
15
  font-size: 32px;
16
16
  }
17
+
18
+ :host ::ng-deep .select-wrapper {
19
+ > div {
20
+ display: block;
21
+ width: 100%;
22
+ }
23
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haus-tech/badge-plugin",
3
- "version": "3.0.0",
3
+ "version": "4.0.1",
4
4
  "description": "Adds a badge to product images",
5
5
  "author": "Haus Tech",
6
6
  "repository": "https://github.com/WeAreHausTech/haus-tech-vendure-plugins",
@@ -16,7 +16,7 @@
16
16
  ],
17
17
  "scripts": {
18
18
  "start": "yarn ts-node test/dev-server.ts",
19
- "build": "rimraf dist && tsc && copyfiles -u 1 'src/ui/**/*' dist/",
19
+ "build": "yarn run -T rimraf dist && yarn run -T tsc && yarn run -T copyfiles -u 1 'src/ui/**/*' dist/",
20
20
  "test": "jest --preset=\"ts-jest\"",
21
21
  "prepublishOnly": "yarn && yarn build"
22
22
  },