Skip to content

Commit 5cff832

Browse files
feat(core): Add StockLocationEvent
1 parent e362475 commit 5cff832

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { CreateStockLocationInput, UpdateStockLocationInput } from '@vendure/common/lib/generated-types';
2+
import { ID } from '@vendure/common/lib/shared-types';
3+
4+
import { RequestContext } from '../../api/common/request-context';
5+
import { StockLocation } from '../../entity';
6+
import { VendureEntityEvent } from '../vendure-entity-event';
7+
8+
type StockLocationInputTypes = CreateStockLocationInput | UpdateStockLocationInput | ID;
9+
10+
/**
11+
* @description
12+
* This event is fired whenever a {@link StockLocation} is added, updated
13+
* or deleted.
14+
*
15+
* @docsCategory events
16+
* @docsPage Event Types
17+
*/
18+
export class StockLocationEvent extends VendureEntityEvent<StockLocation, StockLocationInputTypes> {
19+
constructor(
20+
ctx: RequestContext,
21+
entity: StockLocation,
22+
type: 'created' | 'updated' | 'deleted',
23+
input?: StockLocationInputTypes,
24+
) {
25+
super(entity, type, ctx, input);
26+
}
27+
}

packages/core/src/event-bus/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export * from './events/role-event';
5454
export * from './events/search-event';
5555
export * from './events/seller-event';
5656
export * from './events/shipping-method-event';
57+
export * from './events/stock-location-event';
5758
export * from './events/stock-movement-event';
5859
export * from './events/tax-category-event';
5960
export * from './events/tax-rate-event';

packages/core/src/service/services/stock-location.service.ts

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { TransactionalConnection } from '../../connection/transactional-connecti
2222
import { OrderLine } from '../../entity/order-line/order-line.entity';
2323
import { StockLevel } from '../../entity/stock-level/stock-level.entity';
2424
import { StockLocation } from '../../entity/stock-location/stock-location.entity';
25+
import { EventBus, StockLocationEvent } from '../../event-bus/index';
2526
import { CustomFieldRelationService } from '../helpers/custom-field-relation/custom-field-relation.service';
2627
import { ListQueryBuilder } from '../helpers/list-query-builder/list-query-builder';
2728
import { RequestContextService } from '../helpers/request-context/request-context.service';
@@ -41,6 +42,7 @@ export class StockLocationService {
4142
private configService: ConfigService,
4243
private requestContextCache: RequestContextCacheService,
4344
private customFieldRelationService: CustomFieldRelationService,
45+
private eventBus: EventBus,
4446
) {}
4547

4648
async initStockLocations() {
@@ -81,6 +83,7 @@ export class StockLocationService {
8183
);
8284
await this.channelService.assignToCurrentChannel(stockLocation, ctx);
8385
await this.connection.getRepository(ctx, StockLocation).save(stockLocation);
86+
await this.eventBus.publish(new StockLocationEvent(ctx, stockLocation, 'created', input));
8487
return stockLocation;
8588
}
8689

@@ -94,6 +97,7 @@ export class StockLocationService {
9497
input,
9598
updatedStockLocation,
9699
);
100+
await this.eventBus.publish(new StockLocationEvent(ctx, updatedStockLocation, 'updated', input));
97101
return assertFound(this.findOne(ctx, updatedStockLocation.id));
98102
}
99103

@@ -147,7 +151,11 @@ export class StockLocationService {
147151
}
148152
}
149153
try {
154+
const deletedStockLocation = new StockLocation(stockLocation);
150155
await this.connection.getRepository(ctx, StockLocation).remove(stockLocation);
156+
await this.eventBus.publish(
157+
new StockLocationEvent(ctx, deletedStockLocation, 'deleted', input.id),
158+
);
151159
} catch (e: any) {
152160
return {
153161
result: DeletionResult.NOT_DELETED,

0 commit comments

Comments
 (0)