Skip to content

Commit f2af597

Browse files
committed
Merge branch 'feat/collapse-payments-csv' of https://github.com/paybutton/paybutton-server into feat/collapse-payments-csv
2 parents a2d177f + 933ce99 commit f2af597

8 files changed

Lines changed: 54 additions & 8 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"prisma": "dotenv -e .env -c -- prisma",
1818
"docker": "./scripts/docker-exec-shortcuts.sh",
1919
"ci:integration:test": "yarn pretest && dotenv -e .env.test -- ts-node -O '{\"module\":\"commonjs\"}' node_modules/jest/bin/jest.js tests/integration-tests --forceExit",
20-
"tarDebug": "tar cf debug.tar logs/ paybutton-config.json .env*"
20+
"tarDebug": "tar cf debug.tar logs/ paybutton-config.json .env*",
21+
"updateAllPrices": "./scripts/update-all-prices.sh"
2122
},
2223
"dependencies": {
2324
"@emotion/react": "^11.8.2",
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to alter the column `value` on the `Price` table. The data in that column could be lost. The data in that column will be cast from `Decimal(36,8)` to `Decimal(36,14)`.
5+
6+
*/
7+
-- AlterTable
8+
ALTER TABLE `Price` MODIFY `value` DECIMAL(36, 14) NOT NULL;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Please do not edit this file manually
2-
# It should be added in your version-control system (i.e. Git)
2+
# It should be added in your version-control system (e.g., Git)
33
provider = "mysql"

prisma/schema.prisma

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ model Quote {
105105

106106
model Price {
107107
id Int @id @default(autoincrement())
108-
value Decimal @db.Decimal(36, 8)
108+
value Decimal @db.Decimal(36, 14)
109109
createdAt DateTime @default(now())
110110
updatedAt DateTime @updatedAt
111111
timestamp Int

prisma/seeds/prices.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as fs from 'fs'
88
import * as path from 'path'
99
import { promisify } from 'util'
1010

11-
interface PriceFileData extends KeyValueT<string> {
11+
export interface PriceFileData extends KeyValueT<string> {
1212
ticker: string
1313
date: string
1414
priceInCAD: string
@@ -60,7 +60,7 @@ export async function createPricesFile (): Promise<void> {
6060
console.log(`\n\nstart: ${start.format('HH:mm:ss')}\nfinish: ${finish.format('HH:mm:ss')}\nduration: ${(finish.diff(start) / 1000).toFixed(2)} seconds`)
6161
}
6262

63-
async function getPricesFromFile (): Promise<PriceFileData[]> {
63+
export async function getPricesFromFile (): Promise<PriceFileData[]> {
6464
if (await fileExists(fs, PATH_PRICE_CSV_FILE)) {
6565
const csvContent = await readCsv(fs, PATH_PRICE_CSV_FILE)
6666
const res: PriceFileData[] = []

scripts/paybutton-server-start.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ yarn || exit 1
1313
# Clear logs
1414

1515
logtime=$(date +%Y-%m-%d@%H:%M)
16-
mv logs/next.log logs/history/next_"$logtime".log
17-
mv logs/jobs.log logs/history/jobs_"$logtime".log
18-
mv logs/ws-server.log logs/history/ws-server_"$logtime".log
16+
[ -e logs/next.log ] && mv logs/next.log logs/history/next_"$logtime".log
17+
[ -e logs/jobs.log ] && mv logs/jobs.log logs/history/jobs_"$logtime".log
18+
[ -e logs/ws-server.log ] && mv logs/ws-server.log logs/history/ws-server_"$logtime".log
1919

2020
if [ "$ENVIRONMENT" = "production" ]; then
2121
yarn prisma migrate deploy || exit 1

scripts/update-all-prices.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
set -e
2+
3+
echo Are you sure you want to update all prices? This will affect the database.[y/N]
4+
read ans
5+
if [[ $ans = "Y" || $ans = "y" ]]; then
6+
dotenv -e .env -c -- ts-node -O '{"module":"commonjs"}' -r tsconfig-paths/register ./scripts/updateAllPrices.ts
7+
else
8+
echo Exited.
9+
fi

scripts/updateAllPrices.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { createPricesFile, getPricesFromFile, PriceFileData } from '../prisma/seeds/prices'
2+
import { NETWORK_IDS } from 'constants/index'
3+
import { upsertPricesForNetworkId } from '../services/priceService'
4+
import moment from 'moment'
5+
6+
async function updatePricesFromFile (prices: PriceFileData[]): Promise<void> {
7+
for (const price of prices) {
8+
const networkId = NETWORK_IDS[price.ticker]
9+
const priceData = {
10+
Price_in_CAD: price.priceInCAD,
11+
Price_in_USD: price.priceInUSD
12+
}
13+
const timestamp = moment(price.date).unix()
14+
await upsertPricesForNetworkId(
15+
priceData,
16+
networkId,
17+
timestamp
18+
)
19+
}
20+
}
21+
22+
async function run (): Promise<void> {
23+
await createPricesFile()
24+
const prices = await getPricesFromFile()
25+
await updatePricesFromFile(prices)
26+
}
27+
28+
void run()

0 commit comments

Comments
 (0)