Skip to content

Commit

Permalink
feat(core): Add StockLocationEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Nov 29, 2024
1 parent e362475 commit 5cff832
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/core/src/event-bus/events/stock-location-event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { CreateStockLocationInput, UpdateStockLocationInput } from '@vendure/common/lib/generated-types';
import { ID } from '@vendure/common/lib/shared-types';

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

type StockLocationInputTypes = CreateStockLocationInput | UpdateStockLocationInput | ID;

/**
* @description
* This event is fired whenever a {@link StockLocation} is added, updated
* or deleted.
*
* @docsCategory events
* @docsPage Event Types
*/
export class StockLocationEvent extends VendureEntityEvent<StockLocation, StockLocationInputTypes> {
constructor(
ctx: RequestContext,
entity: StockLocation,
type: 'created' | 'updated' | 'deleted',
input?: StockLocationInputTypes,
) {
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 @@ -54,6 +54,7 @@ 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-location-event';
export * from './events/stock-movement-event';
export * from './events/tax-category-event';
export * from './events/tax-rate-event';
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/service/services/stock-location.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { TransactionalConnection } from '../../connection/transactional-connecti
import { OrderLine } from '../../entity/order-line/order-line.entity';
import { StockLevel } from '../../entity/stock-level/stock-level.entity';
import { StockLocation } from '../../entity/stock-location/stock-location.entity';
import { EventBus, StockLocationEvent } 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';
import { RequestContextService } from '../helpers/request-context/request-context.service';
Expand All @@ -41,6 +42,7 @@ export class StockLocationService {
private configService: ConfigService,
private requestContextCache: RequestContextCacheService,
private customFieldRelationService: CustomFieldRelationService,
private eventBus: EventBus,
) {}

async initStockLocations() {
Expand Down Expand Up @@ -81,6 +83,7 @@ export class StockLocationService {
);
await this.channelService.assignToCurrentChannel(stockLocation, ctx);
await this.connection.getRepository(ctx, StockLocation).save(stockLocation);
await this.eventBus.publish(new StockLocationEvent(ctx, stockLocation, 'created', input));
return stockLocation;
}

Expand All @@ -94,6 +97,7 @@ export class StockLocationService {
input,
updatedStockLocation,
);
await this.eventBus.publish(new StockLocationEvent(ctx, updatedStockLocation, 'updated', input));
return assertFound(this.findOne(ctx, updatedStockLocation.id));
}

Expand Down Expand Up @@ -147,7 +151,11 @@ export class StockLocationService {
}
}
try {
const deletedStockLocation = new StockLocation(stockLocation);
await this.connection.getRepository(ctx, StockLocation).remove(stockLocation);
await this.eventBus.publish(
new StockLocationEvent(ctx, deletedStockLocation, 'deleted', input.id),
);
} catch (e: any) {
return {
result: DeletionResult.NOT_DELETED,
Expand Down

0 comments on commit 5cff832

Please sign in to comment.