Skip to content

Commit b1b8cd8

Browse files
committed
feat: script to update price connections
1 parent 7fa4ef8 commit b1b8cd8

3 files changed

Lines changed: 109 additions & 1 deletion

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
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",
2020
"tarDebug": "tar cf debug.tar logs/ paybutton-config.json .env*",
21-
"updateAllPrices": "./scripts/update-all-prices.sh"
21+
"updateAllPrices": "./scripts/update-all-prices.sh",
22+
"updateAllPriceConnections": "./scripts/update-all-price-connections.sh"
2223
},
2324
"dependencies": {
2425
"@emotion/react": "^11.8.2",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
set -e
2+
3+
echo This will affect the database. Are you sure of what you\'re doing? [y/N]
4+
read ans
5+
if [[ "$ans" = "Y" || "$ans" = "y" ]]; then
6+
JOBS_ENV=true dotenv -e .env -c -- ts-node -O '{"module":"commonjs"}' -r tsconfig-paths/register ./scripts/updateAllPriceConnections.ts
7+
else
8+
echo Exited.
9+
fi
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { flattenTimestamp } from '../services/priceService'
2+
import prisma from 'prisma/clientInstance'
3+
import { connectTransactionsListToPrices } from 'services/transactionService'
4+
import { Transaction } from '@prisma/client'
5+
import moment from 'moment'
6+
7+
async function misconnectTxs (txsIds: string[]): Promise<void> {
8+
if (process.env.ENVIRONMENT === 'production') {
9+
return
10+
}
11+
if (txsIds.length === 0) {
12+
return
13+
}
14+
for (const txId of txsIds) {
15+
const tx = await prisma.transaction.findUniqueOrThrow({
16+
where: {
17+
id: txId
18+
}
19+
})
20+
const txDayTimestamp = flattenTimestamp(tx.timestamp)
21+
let signal = 1
22+
if (txDayTimestamp === flattenTimestamp(moment.utc().unix())) {
23+
signal = -1
24+
}
25+
await prisma.transaction.update({
26+
where: {
27+
id: txId
28+
},
29+
data: {
30+
timestamp: tx.timestamp + (signal * 86400)
31+
}
32+
})
33+
}
34+
}
35+
36+
async function fixMisconnectedTxs (): Promise<void> {
37+
const total = await prisma.transaction.count()
38+
const pageSize = 1000
39+
let page = 0
40+
41+
await misconnectTxs([
42+
// ADD TXS IDS HERE TO TEST IT
43+
])
44+
45+
console.log('Fixing misconnected txs...')
46+
while (true) {
47+
const txsToFix: Transaction[] = []
48+
// Get txs page
49+
const txs = await prisma.transaction.findMany({
50+
orderBy: {
51+
timestamp: 'asc'
52+
},
53+
include: {
54+
prices: {
55+
select: {
56+
price: {
57+
select: {
58+
timestamp: true
59+
}
60+
}
61+
}
62+
}
63+
},
64+
skip: page * pageSize,
65+
take: pageSize
66+
})
67+
68+
const viewedCount = page * pageSize + txs.length
69+
70+
// Finish if empty
71+
if (txs.length === 0) {
72+
break
73+
}
74+
75+
// Find txs with misaligned timestamps
76+
txs.forEach(tx => {
77+
const txFlattenedTimestamp = flattenTimestamp(tx.timestamp)
78+
const txPriceTimestamps = tx.prices.map(p => p.price.timestamp)
79+
if (txPriceTimestamps.filter(t => t !== txFlattenedTimestamp).length > 0) {
80+
txsToFix.push(tx)
81+
}
82+
})
83+
if (txsToFix.length !== 0) {
84+
console.log(`[${viewedCount}/${total}] Fixing ${txsToFix.length} txs...`)
85+
await connectTransactionsListToPrices(txsToFix)
86+
console.log(`[${viewedCount}/${total}] Finished fixing ${txsToFix.length} txs.`)
87+
}
88+
console.log(`[${viewedCount}/${total}]`)
89+
page++
90+
}
91+
console.log('FINISHED')
92+
}
93+
94+
async function run (): Promise<void> {
95+
await fixMisconnectedTxs()
96+
}
97+
98+
void run()

0 commit comments

Comments
 (0)