@@ -6,18 +6,19 @@ import {
6
6
NotFoundException ,
7
7
Param ,
8
8
Post ,
9
+ Put ,
9
10
UseGuards ,
10
11
ValidationPipe ,
11
- Put ,
12
+ Delete ,
12
13
} from '@nestjs/common' ;
13
14
import { ApiTags } from '@nestjs/swagger' ;
14
- import mongoose , { Types } from 'mongoose' ;
15
+ import { Types } from 'mongoose' ;
15
16
import { BasicProductDto , CreateProductDto , ProductDto , UpdateProductDto } from 'shared-types' ;
16
17
import { AuthenticatedGuard } from '../../auth/guards/authenticated.guard' ;
18
+ import { ParseObjectIdPipe } from '../../pipes/prase-object-id.pipe' ;
17
19
import { OrganizationsStatsService } from '../organizations/organizations-stats.service' ;
18
20
import { ProductsService } from './products.service' ;
19
21
import { Product } from './schemas/product.schema' ;
20
- import { ParseObjectIdPipe } from '../../pipes/prase-object-id.pipe' ;
21
22
22
23
@ApiTags ( 'products' )
23
24
@Controller ( 'products' )
@@ -45,12 +46,6 @@ export class ProductsController {
45
46
return Product . toDto ( product ) ;
46
47
}
47
48
48
- @Get ( 'by-org/:id' )
49
- async findAll ( @Param ( 'id' , ParseObjectIdPipe ) id : Types . ObjectId ) : Promise < BasicProductDto [ ] > {
50
- const products = await this . productsService . findAll ( id ) ;
51
- return products . map ( ( product ) => Product . toBasicDto ( product ) ) ;
52
- }
53
-
54
49
@Put ( ':id' )
55
50
async update (
56
51
@Param ( 'id' , ParseObjectIdPipe ) id : Types . ObjectId ,
@@ -63,6 +58,21 @@ export class ProductsController {
63
58
return Product . toDto ( product ) ;
64
59
}
65
60
61
+ @Delete ( ':id' )
62
+ async delete ( @Param ( 'id' , ParseObjectIdPipe ) id : Types . ObjectId ) : Promise < ProductDto > {
63
+ const product = await this . productsService . delete ( id ) ;
64
+ if ( ! product ) {
65
+ throw new NotFoundException ( ) ;
66
+ }
67
+ return Product . toDto ( product ) ;
68
+ }
69
+
70
+ @Get ( 'by-org/:id' )
71
+ async findAll ( @Param ( 'id' , ParseObjectIdPipe ) id : Types . ObjectId ) : Promise < BasicProductDto [ ] > {
72
+ const products = await this . productsService . findAll ( id ) ;
73
+ return products . map ( ( product ) => Product . toBasicDto ( product ) ) ;
74
+ }
75
+
66
76
async updateTotalProductsCount ( organizationId : Types . ObjectId ) {
67
77
const newCount = await this . productsService . countAll ( organizationId ) ;
68
78
await this . organizationStatsService . updateProductsCount ( organizationId , newCount ) ;
0 commit comments