forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurrency.controller.ts
More file actions
157 lines (143 loc) · 4.13 KB
/
Copy pathcurrency.controller.ts
File metadata and controls
157 lines (143 loc) · 4.13 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import {
BadRequestException,
Body,
Controller,
Get,
HttpCode,
HttpStatus,
Post,
Put,
Query,
Req,
ValidationPipe,
} from '@nestjs/common';
import {
ApiOperation,
ApiQuery,
ApiResponse,
ApiTags,
} from '@nestjs/swagger';
import type { Request } from 'express';
import { ConvertDto } from './dto/convert.dto';
import { RateQueryDto } from './dto/rate-query.dto';
import { UpdatePreferenceDto } from './dto/update-preference.dto';
import {
CurrencyService,
type ConversionResponse,
type RateLookupResponse,
} from './currency.service';
interface AuthenticatedRequest extends Request {
user: {
id: string;
};
}
@ApiTags('currency')
@Controller('currency')
export class CurrencyController {
constructor(private readonly currencyService: CurrencyService) {}
@Get('preferences')
@ApiOperation({ summary: 'Get the current user currency preferences' })
getPreferences(@Req() req: AuthenticatedRequest) {
return this.currencyService.getPreferences(req.user.id);
}
@Put('preferences')
@ApiOperation({ summary: 'Create or update the current user currency preferences' })
updatePreferences(
@Req() req: AuthenticatedRequest,
@Body(ValidationPipe) dto: UpdatePreferenceDto,
) {
return this.currencyService.updatePreferences(req.user.id, dto);
}
@Post('setup')
@ApiOperation({
summary:
'Create a first-login currency preference using geo detection with explicit fallback metadata',
})
firstLoginSetup(@Req() req: AuthenticatedRequest) {
return this.currencyService.firstLoginSetup(req.user.id, req);
}
@Get('rates')
@HttpCode(HttpStatus.OK)
@ApiOperation({
summary:
'Get exchange rates for the requested base currency, including per-target source and stale-cache metadata',
})
@ApiQuery({
name: 'base',
required: false,
type: 'string',
description: 'Base currency code. Defaults to USD.',
})
@ApiQuery({
name: 'targets',
required: false,
type: 'string',
description: 'Comma-separated list of target currency codes.',
})
@ApiResponse({
status: 200,
description: 'Exchange rates retrieved successfully',
})
getRates(
@Query(ValidationPipe) query: RateQueryDto = {},
): Promise<RateLookupResponse> {
const targets = query.targets
?.split(',')
.map((target) => target.trim())
.filter(Boolean);
return this.currencyService.getRates(query.base ?? 'USD', targets);
}
@Post('convert')
@HttpCode(HttpStatus.OK)
@ApiOperation({
summary:
'Convert an amount between currencies using the canonical rate service',
})
@ApiResponse({
status: 200,
description: 'Currency converted successfully',
})
convertCurrency(
@Body(ValidationPipe) convertDto: ConvertDto,
): Promise<ConversionResponse> {
return this.currencyService.convertCurrency(convertDto);
}
@Get('convert')
@ApiOperation({
summary:
'Compatibility GET conversion endpoint backed by the canonical rate service',
})
convertCurrencyFromQuery(
@Query(ValidationPipe) convertDto: ConvertDto,
): Promise<ConversionResponse> {
return this.currencyService.convertCurrency(convertDto);
}
@Get('supported')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'Get list of supported currencies' })
getSupportedCurrencies(): string[] {
return this.currencyService.getSupportedCurrencies();
}
@Get('format')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'Format an amount with a currency symbol or code' })
async formatCurrency(
@Query('amount') amount: string,
@Query('currency') currency: string,
): Promise<{ formatted: string }> {
const parsedAmount = Number(amount);
if (!Number.isFinite(parsedAmount)) {
throw new BadRequestException('Invalid amount parameter');
}
return {
formatted: this.currencyService.formatCurrency(parsedAmount, currency),
};
}
@Post('cache/clear')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'Clear the persisted currency-rate cache' })
async clearCache(): Promise<{ message: string }> {
await this.currencyService.clearCache();
return { message: 'Exchange rate cache cleared successfully' };
}
}