Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #841/generate product variants
- Loading branch information
1 parent
711519c
commit b30fb04
Showing
41 changed files
with
588 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { Module } from '@nestjs/common'; | ||
import { ProductOption } from './product-option.entity'; | ||
import { ProductOptionService } from './product-option.service'; | ||
import { ProductOptionController } from './product-option.controller'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([ProductOption])], | ||
controllers: [ProductOptionController], | ||
providers: [ProductOptionService], | ||
exports: [ProductOptionService] | ||
}) | ||
export class ProductOptionModule {} |
14 changes: 14 additions & 0 deletions
14
apps/api/src/app/product-option/product-option.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { ApiTags } from '@nestjs/swagger'; | ||
import { CrudController } from '../core'; | ||
import { Controller } from '@nestjs/common'; | ||
import { ProductOption } from './product-option.entity'; | ||
import { ProductOptionService } from './product-option.service'; | ||
|
||
@ApiTags('Product-Options') | ||
// @UseGuards(AuthGuard('jwt')) | ||
@Controller() | ||
export class ProductOptionController extends CrudController<ProductOption> { | ||
constructor(private readonly productOptionService: ProductOptionService) { | ||
super(productOptionService); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { CrudService } from '../core'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
import { ProductOption } from './product-option.entity'; | ||
|
||
@Injectable() | ||
export class ProductOptionService extends CrudService<ProductOption> { | ||
constructor( | ||
@InjectRepository(ProductOption) | ||
private readonly productOptionRepository: Repository<ProductOption> | ||
) { | ||
super(productOptionRepository); | ||
} | ||
|
||
async createBulk( | ||
productOptionsInput: ProductOption[] | ||
): Promise<ProductOption[]> { | ||
return this.productOptionRepository.create(productOptionsInput); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
apps/api/src/app/product-settings/product-settings.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ApiTags } from '@nestjs/swagger'; | ||
import { CrudController } from '../core'; | ||
import { Controller } from '@nestjs/common'; | ||
import { ProductVariantSettings } from './product-settings.entity'; | ||
import { ProductVariantSettingService } from './product-settings.service'; | ||
|
||
@ApiTags('Product-Variant-Price') | ||
// @UseGuards(AuthGuard('jwt')) | ||
@Controller() | ||
export class ProductVariantSettingsController extends CrudController< | ||
ProductVariantSettings | ||
> { | ||
constructor( | ||
private readonly productVariantSettingService: ProductVariantSettingService | ||
) { | ||
super(productVariantSettingService); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
apps/api/src/app/product-settings/product-settings.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { Module } from '@nestjs/common'; | ||
import { ProductVariantSettings } from './product-settings.entity'; | ||
import { ProductVariantSettingService } from './product-settings.service'; | ||
import { ProductVariantSettingsController } from './product-settings.controller'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([ProductVariantSettings])], | ||
controllers: [ProductVariantSettingsController], | ||
providers: [ProductVariantSettingService], | ||
exports: [ProductVariantSettingService] | ||
}) | ||
export class ProductVariantSettingsModule {} |
Empty file.
26 changes: 26 additions & 0 deletions
26
apps/api/src/app/product-settings/product-settings.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { CrudService } from '../core'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
import { ProductVariantSettings } from './product-settings.entity'; | ||
|
||
@Injectable() | ||
export class ProductVariantSettingService extends CrudService< | ||
ProductVariantSettings | ||
> { | ||
constructor( | ||
@InjectRepository(ProductVariantSettings) | ||
private readonly productVariantSettingsRepository: Repository< | ||
ProductVariantSettings | ||
> | ||
) { | ||
super(productVariantSettingsRepository); | ||
} | ||
|
||
async createDefaultVariantSettings(): Promise<ProductVariantSettings> { | ||
const newProductVariantSettings = new ProductVariantSettings(); | ||
return this.productVariantSettingsRepository.save( | ||
newProductVariantSettings | ||
); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
apps/api/src/app/product-variant-price/product-variant-price-module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { Module } from '@nestjs/common'; | ||
import { ProductVariantPrice } from './product-variant-price.entity'; | ||
import { ProductVariantPriceController } from './product-variant-price.controller'; | ||
import { ProductVariantPriceService } from './product-variant-price.service'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([ProductVariantPrice])], | ||
controllers: [ProductVariantPriceController], | ||
providers: [ProductVariantPriceService], | ||
exports: [ProductVariantPriceService] | ||
}) | ||
export class ProductVariantPriceModule {} |
18 changes: 18 additions & 0 deletions
18
apps/api/src/app/product-variant-price/product-variant-price.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ApiTags } from '@nestjs/swagger'; | ||
import { CrudController } from '../core'; | ||
import { Controller } from '@nestjs/common'; | ||
import { ProductVariantPrice } from './product-variant-price.entity'; | ||
import { ProductVariantPriceService } from './product-variant-price.service'; | ||
|
||
@ApiTags('Product-Variant-Price') | ||
// @UseGuards(AuthGuard('jwt')) | ||
@Controller() | ||
export class ProductVariantPriceController extends CrudController< | ||
ProductVariantPrice | ||
> { | ||
constructor( | ||
private readonly productVariantPriceService: ProductVariantPriceService | ||
) { | ||
super(productVariantPriceService); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
29 changes: 29 additions & 0 deletions
29
apps/api/src/app/product-variant-price/product-variant-price.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { CrudService } from '../core'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
import { ProductVariantPrice } from './product-variant-price.entity'; | ||
import { CurrenciesEnum } from '@gauzy/models'; | ||
|
||
@Injectable() | ||
export class ProductVariantPriceService extends CrudService< | ||
ProductVariantPrice | ||
> { | ||
constructor( | ||
@InjectRepository(ProductVariantPrice) | ||
private readonly productVariantPriceRepository: Repository< | ||
ProductVariantPrice | ||
> | ||
) { | ||
super(productVariantPriceRepository); | ||
} | ||
|
||
async createDefaultProductVariantPrice(): Promise<ProductVariantPrice> { | ||
const newProductVariantPrice = new ProductVariantPrice(); | ||
|
||
newProductVariantPrice.retailPriceCurrency = CurrenciesEnum.BGN; | ||
newProductVariantPrice.unitCostCurrency = CurrenciesEnum.BGN; | ||
|
||
return this.productVariantPriceRepository.save(newProductVariantPrice); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { ProductVariantCreateHandler } from './product-variant.create.handler'; | ||
|
||
export const CommandHandlers = [ProductVariantCreateHandler]; |
45 changes: 45 additions & 0 deletions
45
apps/api/src/app/product-variant/commands/handlers/product-variant.create.handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { ICommandHandler, CommandHandler } from '@nestjs/cqrs'; | ||
import { ProductVariant } from '../../product-variant.entity'; | ||
import { ProductVariantCreateCommand } from '../product-variant.create.command'; | ||
import { ProductVariantService } from '../../product-variant.service'; | ||
import { ProductVariantPriceService } from '../../../product-variant-price/product-variant-price.service'; | ||
import { ProductVariantSettingService } from '../../../product-settings/product-settings.service'; | ||
|
||
@CommandHandler(ProductVariantCreateCommand) | ||
export class ProductVariantCreateHandler | ||
implements ICommandHandler<ProductVariantCreateCommand> { | ||
constructor( | ||
private productVariantService: ProductVariantService, | ||
private productVariantPriceService: ProductVariantPriceService, | ||
private productVariantSettingsService: ProductVariantSettingService | ||
) {} | ||
|
||
public async execute( | ||
command?: ProductVariantCreateCommand | ||
): Promise<ProductVariant[]> { | ||
const { productInput } = command; | ||
|
||
const arrVariants = []; | ||
|
||
for await (const productOption of productInput.options) { | ||
const newProductVariant = new ProductVariant(); | ||
|
||
//tstodo | ||
newProductVariant.billingInvoicingPolicy = 'policy'; | ||
newProductVariant.internalReference = | ||
productOption.name + productOption.code; | ||
|
||
newProductVariant.settings = await this.productVariantSettingsService.createDefaultVariantSettings(); | ||
newProductVariant.price = await this.productVariantPriceService.createDefaultProductVariantPrice(); | ||
newProductVariant.product = productInput; | ||
|
||
arrVariants.push( | ||
await this.productVariantService.createVariant( | ||
newProductVariant | ||
) | ||
); | ||
} | ||
|
||
return arrVariants; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { ProductVariantCreateCommand } from './product-variant.create.command'; |
8 changes: 8 additions & 0 deletions
8
apps/api/src/app/product-variant/commands/product-variant.create.command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ICommand } from '@nestjs/cqrs'; | ||
import { Product } from '../../product/product.entity'; | ||
|
||
export class ProductVariantCreateCommand implements ICommand { | ||
static readonly type = '[ProductVariant] Register'; | ||
|
||
constructor(public readonly productInput: Product) {} | ||
} |
32 changes: 29 additions & 3 deletions
32
apps/api/src/app/product-variant/product-variant.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,39 @@ | ||
import { Controller } from '@nestjs/common'; | ||
import { ApiTags } from '@nestjs/swagger'; | ||
import { Controller, HttpStatus, Post, Body } from '@nestjs/common'; | ||
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger'; | ||
import { CrudController } from '../core'; | ||
import { ProductVariant } from './product-variant.entity'; | ||
import { ProductVariantService } from './product-variant.service'; | ||
import { ProductVariantCreateCommand } from './commands'; | ||
import { CommandBus } from '@nestjs/cqrs'; | ||
import { Product } from '../product/product.entity'; | ||
|
||
@ApiTags('ProductVariant') | ||
@Controller() | ||
export class ProductVariantController extends CrudController<ProductVariant> { | ||
constructor(private readonly productVariantService: ProductVariantService) { | ||
constructor( | ||
private readonly productVariantService: ProductVariantService, | ||
private readonly commandBus: CommandBus | ||
) { | ||
super(productVariantService); | ||
} | ||
|
||
@ApiOperation({ summary: 'Create product variants' }) | ||
@ApiResponse({ | ||
status: HttpStatus.CREATED, | ||
description: | ||
'These records have been successfully created.' /*, type: T*/ | ||
}) | ||
@ApiResponse({ | ||
status: HttpStatus.BAD_REQUEST, | ||
description: | ||
'Invalid input, The response body may contain clues as to what went wrong' | ||
}) | ||
// @UseGuards(PermissionGuard) | ||
@Post('/create-variants') | ||
async createProductVariants( | ||
@Body() entity: Product, | ||
...options: any[] | ||
): Promise<ProductVariant[]> { | ||
return this.commandBus.execute(new ProductVariantCreateCommand(entity)); | ||
} | ||
} |
Oops, something went wrong.