Browse Source

add first available date

main
matthew 2 years ago
parent
commit
c2bf379485
  1. 31
      searching-front/services/domain-watcher.ts

31
searching-front/services/domain-watcher.ts

@ -19,14 +19,23 @@ interface SearchNFTItemsParams {
} }
const getFullUrl = (dmn: string) => `http://${dmn}` const getFullUrl = (dmn: string) => `http://${dmn}`
const upsertDmn = async (dmn: string, available: boolean) => const upsertDmn = async (dmn: string, available: boolean) => {
await db.nftDomain.upsert({ const domainFromDB = await db.nftDomain.findFirst({where:{address: getFullUrl(dmn)}})
const shouldUpdateFirstAvailableDate = !domainFromDB?.firstAvailableDate && available;
const upsertObj = { available, address: getFullUrl(dmn) };
if(shouldUpdateFirstAvailableDate){
upsertObj.firstAvailableDate = new Date();
}
return await db.nftDomain.upsert({
where: { where: {
address: getFullUrl(dmn), address: getFullUrl(dmn),
}, },
update: { available, address: getFullUrl(dmn) }, update: upsertObj,
create: { available, address: getFullUrl(dmn) }, create: upsertObj,
}) })
}
const wait = (time: number) => new Promise((resolve) => setTimeout(() => resolve(true), time)) const wait = (time: number) => new Promise((resolve) => setTimeout(() => resolve(true), time))
@ -76,30 +85,36 @@ const main = async () =>
limit: portion, limit: portion,
offset: count * portion, offset: count * portion,
}) })
if (nftItems.length) { if (nftItems.length) {
console.time('fetchDomain')
const promises1: Promise<unknown>[] = []
for (let i = 0; i < nftItems.length; i++) { for (let i = 0; i < nftItems.length; i++) {
const nftDomainItem = nftItems[i] const nftDomainItem = nftItems[i]
if (nftDomainItem.dns) { if (nftDomainItem.dns) {
fetchTonSite(nftDomainItem.dns)
promises1.push(fetchTonSite(nftDomainItem.dns)
.then(async (dmn) => { .then(async (dmn) => {
console.log("success dmn", dmn) console.log("success dmn", dmn)
upsertDmn(dmn, true) upsertDmn(dmn, true)
}) })
.catch((dmn) => { .catch((dmn) => {
upsertDmn(dmn,false) upsertDmn(dmn,false)
}) }))
} }
} }
count++ count++
await Promise.all(promises1)
console.timeEnd('fetchDomain')
continue continue
} }
break break
} }
console.log("Finish fetch nft") console.log("Finish fetch nft")
setTimeout(() => { setTimeout(() => {
resolve(true) resolve(true)
}, 10000) }, 3000)
}) })
export default main export default main

Loading…
Cancel
Save