Skip to content

Commit

Permalink
Utils, fix expired ico styles.
Browse files Browse the repository at this point in the history
  • Loading branch information
somespecialone committed Nov 10, 2023
1 parent 5399efd commit 48efc18
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 24 deletions.
6 changes: 2 additions & 4 deletions api/cron/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ export async function updateCurrencyRates(event: H3Event): Promise<DbEntry[]> {
return resObj
}, {})

const nowLocal = new Date()
const nowUTC = new Date(nowLocal.getTime() + nowLocal.getTimezoneOffset() * 60 * 1000)
// @ts-ignore
const toOmit: [keyof typeof CURRENCIES] = querySetItems.reduce((resArr, { key, updated }) => {
if (key.startsWith(RATES_PREFIX)) {
if (new Date(updated * 1000).toDateString() === nowUTC.toDateString()) {
if (!rateIsExpired(updated)) {
resArr.push(key.split(RATES_PREFIX)[1] as never) // omit
}
}
Expand Down Expand Up @@ -84,7 +82,7 @@ export async function updateCurrencyRates(event: H3Event): Promise<DbEntry[]> {
}

// UTC ts in seconds
const updated = Math.round(nowUTC.getTime() / 1000)
const updated = Math.round(getUTCDate().getTime() / 1000)

if (currencyId === 1) {
originalToUSDRate = myRound(listingData['price'] / listingData['converted_price'])
Expand Down
8 changes: 8 additions & 0 deletions api/routes/update.get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// manual update trigger
import actionEventHandler from './__space/v0/actions.post'

export default defineEventHandler(async (event) => {
event._requestBody = JSON.stringify({ event: { id: 'update-rates' } })
event._method = 'POST'
return await actionEventHandler(event)
})
2 changes: 1 addition & 1 deletion api/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { myRound } from '../../utils'
export { myRound, rateIsExpired, getUTCDate } from '../../utils'

/**
* Make new array with chunks.
Expand Down
2 changes: 1 addition & 1 deletion converter/src/lib/components/Converter.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
color: var(--accent-second);
}
svg {
:global(svg) {
position: absolute;
left: 101%;
top: 50%;
Expand Down
6 changes: 1 addition & 5 deletions converter/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { myRound } from '../../../utils';
export { myRound, rateIsExpired } from '../../../utils';

/**
* Get random number between margins.
Expand All @@ -20,7 +20,3 @@ export function* getRandomColor() {
yield color;
}
}

export function rateIsExpired(ts: number): boolean {
return new Date(ts * 1000).toDateString() !== new Date().toDateString();
}
26 changes: 13 additions & 13 deletions deployment/Spacefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@ micros:
engine: nodejs16
run: node index.mjs
public_routes:
- /
- /rates
- /history
- "/"
- "/rates"
- "/history"
# actions:
# - id: update-rates
# name: Update rates
# trigger: schedule
# default_interval: 9,39 * * * ?
presets:
env:
- name: LISTING_ID
- name: NITRO_LISTING_ID
description: Market or listing id of listing. Required.
- name: ITEM_MARKET_NAME
- name: NITRO_ITEM_MARKET_NAME
description: Item market hash name. Required.
- name: LISTING_FILTER_PARAM
- name: NITRO_LISTING_FILTER_PARAM
description: Search listing query. Optional.
- name: LISTING_START_PARAM
- name: NITRO_LISTING_START_PARAM
description: Offset of listings. Optional.
- name: CURRENCIES_TO_FETCH
- name: NITRO_CURRENCIES_TO_FETCH
description: Just currencies. Don't pass USD. Optional. Default 'EUR,PLN,UAH'
- name: RATE_LIMIT
- name: NITRO_RATE_LIMIT
description: Max count of currencies to fetch in single round. Optional. Default 4
- name: STEAM_GAME_ID
- name: NITRO_STEAM_GAME_ID
description: App id of game. Optional. Default 730 (CSGO).
- name: HISTORY_SIZE
- name: NITRO_HISTORY_SIZE
description: Size of cashed history. Optional. Default 150.
- name: HISTORY_LENGTH
- name: NITRO_HISTORY_LENGTH
description: Length of history response. Optional. Default 30.
- name: ALLOW_ORIGIN
- name: NITRO_ALLOW_ORIGIN
description: CORS Header. Optional. Default '*'
13 changes: 13 additions & 0 deletions utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,16 @@
export function myRound(value: number): number {
return Math.round(value * 100) / 100
}

/**
* That's JS :)
* @see https://stackoverflow.com/a/11964609/19419998
*/
export function getUTCDate(): Date {
const now = new Date()
return new Date(now.getTime() + now.getTimezoneOffset() * 60 * 1000)
}

export function rateIsExpired(ts: number): boolean {
return new Date(ts * 1000).toDateString() !== getUTCDate().toDateString()
}

0 comments on commit 48efc18

Please sign in to comment.