-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRowsReport.ts
66 lines (56 loc) · 1.77 KB
/
RowsReport.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
import { DataFetcher } from './DataFetcher';
import { CURRENCY, Rows } from "./types";
// GET: http://example.com/get-rows
// [
// {
// "title": "Foo",
// "price": 123,
// "currency": "RUB",
// },
// {
// "title": "Foo",
// "price": 5,
// "currency": "USD",
// },
// ]
const convertor: Record<CURRENCY, string> = {
[CURRENCY.AED]: 'AED',
[CURRENCY.USD]: "$"
}
export class RowsReport {
data_fetcher: DataFetcher;
private readonly CONVERSION_RATE = 34.13;
constructor() {
this.data_fetcher = new DataFetcher();
}
private get_format_price (price: number, currency: string): string {
switch (currency) {
case CURRENCY.AED:
return price + convertor[currency]
case CURRENCY.USD:
return convertor[currency] + price
default:
return String(price)
}
}
private get_formated_report (rows:Rows[]) {
const {report_sum, report_text} = rows.reduce((acc,row) => {
return {
report_sum: row.currency === CURRENCY.AED? acc.report_sum + row.price: acc.report_sum + row.price * this.CONVERSION_RATE,
report_text: acc.report_text + '\n' + row.title + ': ' + this.get_format_price(row.price, row.currency)
}
},{
report_sum: 0,
report_text: ''
})
return report_text + '\nTotal Amount: ' + this.get_format_price(report_sum, "AED")
}
async get_report(dateFrom: string): Promise<string> {
try{
const rows = await this.data_fetcher.get<Rows[]>('/get-rows', {dateFrom})
return this.get_formated_report(rows)
}catch (error){
throw error
}
}
}