Skip to content

Commit

Permalink
fix: Use seconds (#91)
Browse files Browse the repository at this point in the history
This PR changes the code to use milliseconds when returning the
`If-Modified-Since` header.
It also makes sure that the `getLastUpdatedAt` always returns a number
representing the unix timestamp in seconds.
  • Loading branch information
LautaroPetaccio authored May 19, 2023
1 parent f7a3b3e commit 5c32532
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 23 deletions.
7 changes: 7 additions & 0 deletions src/adapters/time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function fromMillisecondsToSeconds(timeInMilliseconds: number): number {
return Math.floor(timeInMilliseconds / 1000)
}

export function fromSecondsToMilliseconds(timeInSeconds: number): number {
return timeInSeconds * 1000
}
2 changes: 1 addition & 1 deletion src/controllers/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function setupRouter(
const router = new Router<GlobalContext>()

const { district, map } = components
const getLastModifiedTime = () => map.getLastUpdatedAt()
const getLastModifiedTime = () => map.getLastUpdatedAt() * 1000
const lastModifiedMiddlewareByMapDate =
lastModifiedMiddleware(getLastModifiedTime)

Expand Down
31 changes: 15 additions & 16 deletions src/modules/api/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
TileRentalListing,
} from '../../adapters/rentals'
import { isRentalListingOpen } from '../../logic/rental'
import { fromMillisecondsToSeconds } from '../../adapters/time'
import { Metrics } from '../../metrics'
import { Tile, TileType } from '../map/types'
import { coordsToId, specialTiles } from '../map/utils'
Expand Down Expand Up @@ -206,25 +207,23 @@ export async function createApiComponent(components: {
) ${parcelFields}
}`
)
const rentalListings =
const rentalListings = await rentals.getRentalsListingsOfNFTs(
Array.from(new Set(nfts.map((nft) => nft.searchParcelEstateId ?? nft.id)))
)
const tileRentalListings =
nfts.length > 0
? Object.fromEntries(
Object.entries(
await rentals.getRentalsListingsOfNFTs(
Array.from(
new Set(nfts.map((nft) => nft.searchParcelEstateId ?? nft.id))
)
)
).map(([key, value]) => [
Object.entries(rentalListings).map(([key, value]) => [
key,
convertRentalListingToTileRentalListing(value),
])
)
: {}
return nfts.reduce<Batch>(
(batch, nft) => {
const rentalListing = rentalListings[nft.searchParcelEstateId ?? nft.id]
const tile = buildTile(nft, rentalListing)
const tileRentalListing =
tileRentalListings[nft.searchParcelEstateId ?? nft.id]
const tile = buildTile(nft, tileRentalListing)
const parcel = buildParcel(nft)
const estate = buildEstate(nft)
batch.tiles.push(tile)
Expand Down Expand Up @@ -455,12 +454,12 @@ export async function createApiComponent(components: {
Math.max(updatedAt, parseInt(estate.updatedAt, 10)),
0
)
const rentalListingsUpdatedAt = Object.values(
rentalListingByNftId
).reduce<number>(
(updatedAt, rentalListing) =>
Math.max(updatedAt, rentalListing.updatedAt),
0
const rentalListingsUpdatedAt = fromMillisecondsToSeconds(
Object.values(rentalListingByNftId).reduce<number>(
(updatedAt, rentalListing) =>
Math.max(updatedAt, rentalListing.updatedAt),
0
)
)

// Gets the minimum last updated time or the original updatedAfter time.
Expand Down
7 changes: 6 additions & 1 deletion src/modules/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ export enum ApiEvents {
PROGRESS = 'progress',
}

export type Batch = { tiles: Tile[]; parcels: NFT[]; estates: NFT[] }
export type Batch = {
tiles: Tile[]
parcels: NFT[]
estates: NFT[]
}
export type Result = Batch & { updatedAt: number }

export type NFT = {
Expand Down Expand Up @@ -36,6 +40,7 @@ export interface IApiComponent {

export type OrderFragment = {
price: string
/** The time in milliseconds when the order expires */
expiresAt: string
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import proximities from './data/proximity.json'

export function isExpired(order: OrderFragment) {
return parseInt(order.expiresAt) <= Date.now()
return parseInt(order.expiresAt) <= Math.round(Date.now() / 1000)
}

export const getProximity = (
Expand Down
9 changes: 5 additions & 4 deletions tests/modules/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '@well-known-components/interfaces'
import { ISubgraphComponent } from '@well-known-components/thegraph-component'
import { convertRentalListingToTileRentalListing } from '../../src/adapters/rentals'
import { fromMillisecondsToSeconds } from '../../src/adapters/time'
import { Metrics } from '../../src/metrics'
import { createApiComponent } from '../../src/modules/api/component'
import {
Expand Down Expand Up @@ -51,7 +52,7 @@ beforeEach(async () => {
searchParcelY: '1',
searchParcelEstateId: null,
tokenId: '0',
updatedAt: date.toString(),
updatedAt: fromMillisecondsToSeconds(date).toString(),
activeOrder: {
price: '1000000000000000000',
expiresAt: (date + 100000000).toString(),
Expand All @@ -71,7 +72,7 @@ beforeEach(async () => {
searchParcelX: '1',
searchParcelY: '1',
tokenId: '1',
updatedAt: date.toString(),
updatedAt: fromMillisecondsToSeconds(date).toString(),
activeOrder: {
price: '2000000000000000000',
expiresAt: (date + 100000000).toString(),
Expand Down Expand Up @@ -101,7 +102,7 @@ beforeEach(async () => {
price: '2000000000000000000',
expiresAt: (date + 100000000).toString(),
},
updatedAt: date.toString(),
updatedAt: fromMillisecondsToSeconds(date).toString(),
},
},
},
Expand Down Expand Up @@ -776,7 +777,7 @@ describe('when fetching update data', () => {

beforeEach(() => {
fstEstate = {
updatedAt: date.toString(),
updatedAt: fromMillisecondsToSeconds(date).toString(),
estate: {
parcels: [
{ nft: defaultFstParcelEstate },
Expand Down

0 comments on commit 5c32532

Please sign in to comment.