From 5c32532c6d570ee9fc47cc093315a3f9ba7862a7 Mon Sep 17 00:00:00 2001 From: Lautaro Petaccio <1120791+LautaroPetaccio@users.noreply.github.com> Date: Fri, 19 May 2023 19:44:25 -0300 Subject: [PATCH] fix: Use seconds (#91) 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. --- src/adapters/time.ts | 7 +++++++ src/controllers/routes.ts | 2 +- src/modules/api/component.ts | 31 +++++++++++++++---------------- src/modules/api/types.ts | 7 ++++++- src/modules/api/utils.ts | 2 +- tests/modules/api.spec.ts | 9 +++++---- 6 files changed, 35 insertions(+), 23 deletions(-) create mode 100644 src/adapters/time.ts diff --git a/src/adapters/time.ts b/src/adapters/time.ts new file mode 100644 index 0000000..afab393 --- /dev/null +++ b/src/adapters/time.ts @@ -0,0 +1,7 @@ +export function fromMillisecondsToSeconds(timeInMilliseconds: number): number { + return Math.floor(timeInMilliseconds / 1000) +} + +export function fromSecondsToMilliseconds(timeInSeconds: number): number { + return timeInSeconds * 1000 +} diff --git a/src/controllers/routes.ts b/src/controllers/routes.ts index e252aa9..c1a4196 100644 --- a/src/controllers/routes.ts +++ b/src/controllers/routes.ts @@ -30,7 +30,7 @@ export async function setupRouter( const router = new Router() const { district, map } = components - const getLastModifiedTime = () => map.getLastUpdatedAt() + const getLastModifiedTime = () => map.getLastUpdatedAt() * 1000 const lastModifiedMiddlewareByMapDate = lastModifiedMiddleware(getLastModifiedTime) diff --git a/src/modules/api/component.ts b/src/modules/api/component.ts index c1fd915..e79e011 100644 --- a/src/modules/api/component.ts +++ b/src/modules/api/component.ts @@ -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' @@ -206,16 +207,13 @@ 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), ]) @@ -223,8 +221,9 @@ export async function createApiComponent(components: { : {} return nfts.reduce( (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) @@ -455,12 +454,12 @@ export async function createApiComponent(components: { Math.max(updatedAt, parseInt(estate.updatedAt, 10)), 0 ) - const rentalListingsUpdatedAt = Object.values( - rentalListingByNftId - ).reduce( - (updatedAt, rentalListing) => - Math.max(updatedAt, rentalListing.updatedAt), - 0 + const rentalListingsUpdatedAt = fromMillisecondsToSeconds( + Object.values(rentalListingByNftId).reduce( + (updatedAt, rentalListing) => + Math.max(updatedAt, rentalListing.updatedAt), + 0 + ) ) // Gets the minimum last updated time or the original updatedAfter time. diff --git a/src/modules/api/types.ts b/src/modules/api/types.ts index 30412b7..6dfb7df 100644 --- a/src/modules/api/types.ts +++ b/src/modules/api/types.ts @@ -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 = { @@ -36,6 +40,7 @@ export interface IApiComponent { export type OrderFragment = { price: string + /** The time in milliseconds when the order expires */ expiresAt: string } diff --git a/src/modules/api/utils.ts b/src/modules/api/utils.ts index bc3ac02..ba0722d 100644 --- a/src/modules/api/utils.ts +++ b/src/modules/api/utils.ts @@ -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 = ( diff --git a/tests/modules/api.spec.ts b/tests/modules/api.spec.ts index edcf40f..70262be 100644 --- a/tests/modules/api.spec.ts +++ b/tests/modules/api.spec.ts @@ -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 { @@ -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(), @@ -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(), @@ -101,7 +102,7 @@ beforeEach(async () => { price: '2000000000000000000', expiresAt: (date + 100000000).toString(), }, - updatedAt: date.toString(), + updatedAt: fromMillisecondsToSeconds(date).toString(), }, }, }, @@ -776,7 +777,7 @@ describe('when fetching update data', () => { beforeEach(() => { fstEstate = { - updatedAt: date.toString(), + updatedAt: fromMillisecondsToSeconds(date).toString(), estate: { parcels: [ { nft: defaultFstParcelEstate },