This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
loader.js
63 lines (52 loc) · 1.93 KB
/
loader.js
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
const alfy = require("alfy");
const numeral = require("numeral");
const getCurrency = require("./get-currency");
const reportFetchRateError = require("./report-fetch-rate-error");
const fixRurToRub = require("./fix-rur-to-rub");
const { requestURL, dividerSymbol } = require("./config");
numeral.defaultFormat("0.00");
module.exports = async operation => {
const [amount, currency] = alfy.input.toLowerCase().split(" ");
const userAmount = numeral(amount);
const userValidCurrency = getCurrency({ name: currency });
const actionName = operation === "buy" ? "Buying" : "Selling";
if (userAmount.value() > 0 && userValidCurrency) {
const response = fixRurToRub(await alfy.fetch(requestURL));
if (response.length < 4) {
reportFetchRateError();
return;
}
const userCurrency = response.find(
({ ccy }) => ccy.toLowerCase() === currency
);
const total = userAmount.clone().multiply(userCurrency[operation]);
const targetSymbol =
userCurrency.ccy.toLowerCase() === "btc"
? getCurrency({ name: "usd" }).symbol
: getCurrency({ name: "uah" }).symbol;
const baseAmount = numeral(userCurrency[operation]).format();
const title = `${targetSymbol} ${total.format()}`;
const arg = title;
const subtitle = `${actionName} ${
userValidCurrency.symbol
} ${userAmount.format()} for ${targetSymbol} ${total.format()} ${dividerSymbol} ${
userValidCurrency.symbol
} 1 for ${targetSymbol} ${baseAmount}`;
const icon = {
path: operation === "buy" ? "./icon/buy.png" : "./icon/sale.png",
};
alfy.output([{ title, subtitle, arg, icon }]);
} else {
alfy.output([
{
title: `${actionName} ${
userAmount.value() > 0 ? userAmount.format() : "how much"
} of which currency?`,
subtitle: "The format is <amount> <usd | eur | rub | btc>",
icon: {
path: "./icon/wait.png",
},
},
]);
}
};