diff --git a/src/api/Status.js b/src/api/Status.js index d9af0926..f39342cc 100644 --- a/src/api/Status.js +++ b/src/api/Status.js @@ -23,14 +23,12 @@ export class Status extends DataCollector { getState () { return this.formatData(this.state) } - getBlocksServiceStatus () { + async getBlocksServiceStatus () { const Status = this.getModule('Status') - return Status.find({}, { timestamp: -1 }, 1) - .then(res => { - res = res.data[0] - if (res) delete (res._id) - return res - }) + const { data } = await Status.find({}, { timestamp: -1 }, 1) + const [latestStatus] = data + + return latestStatus } async updateState () { try { diff --git a/src/converters/status.converters.js b/src/converters/status.converters.js index b3feb1e4..07cccf8f 100644 --- a/src/converters/status.converters.js +++ b/src/converters/status.converters.js @@ -12,4 +12,18 @@ function rawStatusToEntity ({ } } -export { rawStatusToEntity } +function statusEntityToRaw ({ + pendingBlocks, + requestingBlocks, + nodeDown, + timestamp +}) { + return { + pendingBlocks, + requestingBlocks, + nodeDown, + timestamp: Number(timestamp) + } +} + +export { rawStatusToEntity, statusEntityToRaw } diff --git a/src/repositories/status.repository.js b/src/repositories/status.repository.js index 4ca53cf5..1f6e9246 100644 --- a/src/repositories/status.repository.js +++ b/src/repositories/status.repository.js @@ -1,23 +1,24 @@ -import { rawStatusToEntity } from '../converters/status.converters' +import { rawStatusToEntity, statusEntityToRaw } from '../converters/status.converters' import {prismaClient} from '../lib/Setup' +import { createPrismaOrderBy, mongoQueryToPrisma } from './utils' + +const statsEntitySelect = { + pendingBlocks: true, + requestingBlocks: true, + nodeDown: true, + timestamp: true +} export const statusRepository = { - findOne (query = {}, project = {}, collection) { - return collection.findOne(query, project) - }, - find (query = {}, project = {}, collection, sort = {}, limit = 0, isArray = true) { - if (isArray) { - return collection - .find(query, project) - .sort(sort) - .limit(limit) - .toArray() - } else { - return collection - .find(query, project) - .sort(sort) - .limit(limit) - } + async find (query = {}, project = {}, collection, sort = {}, limit = 0, isArray = true) { + const statusArr = await prismaClient.status.findMany({ + where: mongoQueryToPrisma(query), + select: statsEntitySelect, + orderBy: createPrismaOrderBy(sort), + take: limit + }) + + return statusArr.map(status => statusEntityToRaw(status)) }, async insertOne (data, collection) { await prismaClient.status.create({ data: rawStatusToEntity(data) })