-
Notifications
You must be signed in to change notification settings - Fork 490
/
product.create.handler.ts
32 lines (25 loc) · 1.08 KB
/
product.create.handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { ICommandHandler, CommandHandler } from '@nestjs/cqrs';
import { ProductService } from '../../../product/product.service';
import { ProductCreateCommand } from '../product.create.command';
import { ProductOptionService } from '../../../product-option/product-option.service';
import { ProductOption } from '../../../product-option/product-option.entity';
import { Product } from '../../product.entity';
@CommandHandler(ProductCreateCommand)
export class ProductCreateHandler
implements ICommandHandler<ProductCreateCommand> {
constructor(
private productOptionService: ProductOptionService,
private productService: ProductService
) {}
public async execute(command?: ProductCreateCommand): Promise<Product> {
const { productInput } = command;
const product = await this.productService.create(productInput);
const optionsInput = productInput.options.map((optionInput) => {
const option = new ProductOption();
Object.assign(option, { ...optionInput, product });
return option;
});
await this.productOptionService.createBulk(optionsInput);
return product;
}
}