diff --git a/package.json b/package.json index ae0039aba..c83e0aefb 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ "prisma": "dotenv -e .env -c -- prisma", "docker": "./scripts/docker-exec-shortcuts.sh", "ci:integration:test": "yarn pretest && dotenv -e .env.test -- ts-node -O '{\"module\":\"commonjs\"}' node_modules/jest/bin/jest.js tests/integration-tests --forceExit", - "tarDebug": "tar cf debug.tar logs/ paybutton-config.json .env*" + "tarDebug": "tar cf debug.tar logs/ paybutton-config.json .env*", + "updateAllPrices": "./scripts/update-all-prices.sh" }, "dependencies": { "@emotion/react": "^11.8.2", diff --git a/prisma/migrations/20250225123654_14_decimal_points_for_price/migration.sql b/prisma/migrations/20250225123654_14_decimal_points_for_price/migration.sql new file mode 100644 index 000000000..57b629cc2 --- /dev/null +++ b/prisma/migrations/20250225123654_14_decimal_points_for_price/migration.sql @@ -0,0 +1,8 @@ +/* + Warnings: + + - 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)`. + +*/ +-- AlterTable +ALTER TABLE `Price` MODIFY `value` DECIMAL(36, 14) NOT NULL; diff --git a/prisma/migrations/migration_lock.toml b/prisma/migrations/migration_lock.toml index e5a788a7a..8a21669ab 100644 --- a/prisma/migrations/migration_lock.toml +++ b/prisma/migrations/migration_lock.toml @@ -1,3 +1,3 @@ # Please do not edit this file manually -# It should be added in your version-control system (i.e. Git) +# It should be added in your version-control system (e.g., Git) provider = "mysql" \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 524f7a0da..d102eb0e1 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -105,7 +105,7 @@ model Quote { model Price { id Int @id @default(autoincrement()) - value Decimal @db.Decimal(36, 8) + value Decimal @db.Decimal(36, 14) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt timestamp Int diff --git a/prisma/seeds/prices.ts b/prisma/seeds/prices.ts index ed2c99eef..d0ca4a35f 100644 --- a/prisma/seeds/prices.ts +++ b/prisma/seeds/prices.ts @@ -8,7 +8,7 @@ import * as fs from 'fs' import * as path from 'path' import { promisify } from 'util' -interface PriceFileData extends KeyValueT { +export interface PriceFileData extends KeyValueT { ticker: string date: string priceInCAD: string @@ -60,7 +60,7 @@ export async function createPricesFile (): Promise { console.log(`\n\nstart: ${start.format('HH:mm:ss')}\nfinish: ${finish.format('HH:mm:ss')}\nduration: ${(finish.diff(start) / 1000).toFixed(2)} seconds`) } -async function getPricesFromFile (): Promise { +export async function getPricesFromFile (): Promise { if (await fileExists(fs, PATH_PRICE_CSV_FILE)) { const csvContent = await readCsv(fs, PATH_PRICE_CSV_FILE) const res: PriceFileData[] = [] diff --git a/scripts/paybutton-server-start.sh b/scripts/paybutton-server-start.sh index 3dfcc7c19..1d1435b7f 100755 --- a/scripts/paybutton-server-start.sh +++ b/scripts/paybutton-server-start.sh @@ -13,9 +13,9 @@ yarn || exit 1 # Clear logs logtime=$(date +%Y-%m-%d@%H:%M) -mv logs/next.log logs/history/next_"$logtime".log -mv logs/jobs.log logs/history/jobs_"$logtime".log -mv logs/ws-server.log logs/history/ws-server_"$logtime".log +[ -e logs/next.log ] && mv logs/next.log logs/history/next_"$logtime".log +[ -e logs/jobs.log ] && mv logs/jobs.log logs/history/jobs_"$logtime".log +[ -e logs/ws-server.log ] && mv logs/ws-server.log logs/history/ws-server_"$logtime".log if [ "$ENVIRONMENT" = "production" ]; then yarn prisma migrate deploy || exit 1 diff --git a/scripts/update-all-prices.sh b/scripts/update-all-prices.sh new file mode 100755 index 000000000..87efb1ce0 --- /dev/null +++ b/scripts/update-all-prices.sh @@ -0,0 +1,9 @@ +set -e + +echo Are you sure you want to update all prices? This will affect the database.[y/N] +read ans +if [[ $ans = "Y" || $ans = "y" ]]; then + dotenv -e .env -c -- ts-node -O '{"module":"commonjs"}' -r tsconfig-paths/register ./scripts/updateAllPrices.ts +else + echo Exited. +fi diff --git a/scripts/updateAllPrices.ts b/scripts/updateAllPrices.ts new file mode 100644 index 000000000..452742460 --- /dev/null +++ b/scripts/updateAllPrices.ts @@ -0,0 +1,28 @@ +import { createPricesFile, getPricesFromFile, PriceFileData } from '../prisma/seeds/prices' +import { NETWORK_IDS } from 'constants/index' +import { upsertPricesForNetworkId } from '../services/priceService' +import moment from 'moment' + +async function updatePricesFromFile (prices: PriceFileData[]): Promise { + for (const price of prices) { + const networkId = NETWORK_IDS[price.ticker] + const priceData = { + Price_in_CAD: price.priceInCAD, + Price_in_USD: price.priceInUSD + } + const timestamp = moment(price.date).unix() + await upsertPricesForNetworkId( + priceData, + networkId, + timestamp + ) + } +} + +async function run (): Promise { + await createPricesFile() + const prices = await getPricesFromFile() + await updatePricesFromFile(prices) +} + +void run()