Skip to content

Commit

Permalink
fix(core): Add missing SellerEvent and correctly update relations
Browse files Browse the repository at this point in the history
Relates to #2216
  • Loading branch information
michaelbromley committed Jun 12, 2023
1 parent ea92bd1 commit 4f421d3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
28 changes: 28 additions & 0 deletions packages/core/src/event-bus/events/seller-event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { CreateSellerInput, UpdateSellerInput } from '@vendure/common/lib/generated-types';
import { ID } from '@vendure/common/lib/shared-types';

import { RequestContext } from '../../api/common/request-context';
import { Role } from '../../entity';
import { Seller } from '../../entity/seller/seller.entity';
import { VendureEntityEvent } from '../vendure-entity-event';

type SellerInputTypes = CreateSellerInput | UpdateSellerInput | ID;

/**
* @description
* This event is fired whenever one {@link Seller} is added, updated or deleted.
*
* @docsCategory events
* @docsPage Event Types
* @since 2.0.1
*/
export class SellerEvent extends VendureEntityEvent<Seller, SellerInputTypes> {
constructor(
ctx: RequestContext,
entity: Seller,
type: 'created' | 'updated' | 'deleted',
input?: SellerInputTypes,
) {
super(entity, type, ctx, input);
}
}
1 change: 1 addition & 0 deletions packages/core/src/event-bus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export * from './events/refund-state-transition-event';
export * from './events/role-change-event';
export * from './events/role-event';
export * from './events/search-event';
export * from './events/seller-event';
export * from './events/shipping-method-event';
export * from './events/stock-movement-event';
export * from './events/tax-category-event';
Expand Down
31 changes: 28 additions & 3 deletions packages/core/src/service/services/seller.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import {
import { ID, PaginatedList } from '@vendure/common/lib/shared-types';

import { RequestContext } from '../../api/common/request-context';
import { assertFound } from '../../common/index';
import { ListQueryOptions } from '../../common/types/common-types';
import { TransactionalConnection } from '../../connection/transactional-connection';
import { Seller } from '../../entity/seller/seller.entity';
import { EventBus, SellerEvent } from '../../event-bus/index';
import { CustomFieldRelationService } from '../helpers/custom-field-relation/custom-field-relation.service';
import { ListQueryBuilder } from '../helpers/list-query-builder/list-query-builder';

/**
Expand All @@ -21,7 +24,12 @@ import { ListQueryBuilder } from '../helpers/list-query-builder/list-query-build
*/
@Injectable()
export class SellerService {
constructor(private connection: TransactionalConnection, private listQueryBuilder: ListQueryBuilder) {}
constructor(
private connection: TransactionalConnection,
private listQueryBuilder: ListQueryBuilder,
private eventBus: EventBus,
private customFieldRelationService: CustomFieldRelationService,
) {}

async initSellers() {
await this.ensureDefaultSellerExists();
Expand All @@ -44,8 +52,16 @@ export class SellerService {
.then(result => result ?? undefined);
}

create(ctx: RequestContext, input: CreateSellerInput) {
return this.connection.getRepository(ctx, Seller).save(new Seller(input));
async create(ctx: RequestContext, input: CreateSellerInput) {
const seller = await this.connection.getRepository(ctx, Seller).save(new Seller(input));
const sellerWithRelations = await this.customFieldRelationService.updateRelations(
ctx,
Seller,
input,
seller,
);
this.eventBus.publish(new SellerEvent(ctx, sellerWithRelations, 'created', input));
return assertFound(this.findOne(ctx, seller.id));
}

async update(ctx: RequestContext, input: UpdateSellerInput) {
Expand All @@ -54,12 +70,21 @@ export class SellerService {
seller.name = input.name;
await this.connection.getRepository(ctx, Seller).save(seller);
}
const sellerWithRelations = await this.customFieldRelationService.updateRelations(
ctx,
Seller,
input,
seller,
);
this.eventBus.publish(new SellerEvent(ctx, sellerWithRelations, 'updated', input));
return seller;
}

async delete(ctx: RequestContext, id: ID): Promise<DeletionResponse> {
const seller = await this.connection.getEntityOrThrow(ctx, Seller, id);
await this.connection.getRepository(ctx, Seller).remove(seller);
const deletedSeller = new Seller(seller);
this.eventBus.publish(new SellerEvent(ctx, deletedSeller, 'deleted', id));
return {
result: DeletionResult.DELETED,
};
Expand Down

0 comments on commit 4f421d3

Please sign in to comment.