-
Notifications
You must be signed in to change notification settings - Fork 4
/
quote.controller.ts
86 lines (84 loc) · 2.89 KB
/
quote.controller.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { Controller, Get, Query, Catch, HttpException, HttpStatus, Req, UnauthorizedException, ParseIntPipe } from '@nestjs/common';
import { QuoteService } from './quote.service';
import { ApiBearerAuth, ApiOperation, ApiTags, ApiQuery, ApiResponse } from '@nestjs/swagger';
@Controller('getQuote')
@Catch()
export class QuoteController {
constructor(private readonly quoteService: QuoteService) { }
/**
*
* @param req authorization header
* @param mode mode of transport
* @param productId
* @param quantity
* @param totalDistanceMeasured
* @param weight
* @param dimension
* @Get annotation is used for swagger implementation
*/
/**
* Swagger defination goes here
*/
@Get()
@ApiBearerAuth()
@ApiTags('Logistics Service')
@ApiOperation({ summary: 'Get Quote from freight service using technical user authentication' })
@ApiQuery({
name: 'mode',
description: 'Mode of transportation (Air, Water, Rail, Road)',
type: String
})
@ApiQuery({
name: 'quantity',
description: 'Number of items to be shipped',
type: Number
})
@ApiQuery({
name: 'productId',
description: 'Product ID',
type: String
})
@ApiQuery({
name: 'totalDistanceMeasured',
description: 'Distance for delivery',
type: String
})
@ApiQuery({
name: 'weight',
description: 'Product Weight',
type: String
})
@ApiQuery({
name: 'dimension',
description: 'Product dimension(Height,length,width)',
type: String
})
@ApiResponse({ status: 204, description: 'Out of stock' })
@ApiResponse({ status: 200, description: 'Ok' })
// end of swagger defination
// Pipe methods are used for validation
async getQuote(@Req() req: any, @Query('mode') mode: string, @Query('productId') productId: string, @Query('quantity', ParseIntPipe) quantity: number,
@Query('totalDistanceMeasured',ParseIntPipe) totalDistanceMeasured: number, @Query('weight',ParseIntPipe) weight: number, @Query('dimension') dimension: string,
): Promise<any> {
const authorization = req.headers.authorization;
const isAuthorized = req.authInfo.checkLocalScope('Supplier');
// checks for authorization before executing next block, if unauthorized, returns exception
if(isAuthorized){
return await this.quoteService.checkProductStock(authorization, mode.toUpperCase(), productId, quantity,
weight, totalDistanceMeasured, dimension).then(data => {
return { Cost: data };
}).catch(error => {
/**
* if error is false, its a out of stock error
*/
if (error === false) {
throw new HttpException('Product Out Of Stock',HttpStatus.NO_CONTENT);
}
console.log(error);
throw new HttpException({"status": 500,"error": "Unable to handle request"}, HttpStatus.INTERNAL_SERVER_ERROR);
});
}else{
throw new UnauthorizedException();
}
}
}