From 86dbd6ee7307c024df553b001a70c412f86b2665 Mon Sep 17 00:00:00 2001 From: Ali Ebrahimi <65724329+ae2079@users.noreply.github.com> Date: Thu, 27 Jun 2024 06:36:28 +0330 Subject: [PATCH] Merge pull request #28 from Giveth/10-track-count-of-multisig-sessions Add multisigSessionCountRouter --- src/repositories/multisigSessionRepository.ts | 12 +++++++ src/routes/v1/index.ts | 2 ++ src/routes/v1/multisigSessionCountRouter.ts | 32 +++++++++++++++++++ src/utils/errorMessages.ts | 5 +++ 4 files changed, 51 insertions(+) create mode 100644 src/routes/v1/multisigSessionCountRouter.ts diff --git a/src/repositories/multisigSessionRepository.ts b/src/repositories/multisigSessionRepository.ts index aab3140..18138eb 100644 --- a/src/repositories/multisigSessionRepository.ts +++ b/src/repositories/multisigSessionRepository.ts @@ -1,4 +1,5 @@ import moment from 'moment'; +import { Between } from 'typeorm'; import { MultisigSession } from '../entities/multisigSession'; export const firstOrCreateMultisigSession = async ( @@ -47,3 +48,14 @@ export const findNonExpiredMultisigSessions = async ( return session; }; + +export async function getMultisigSessionsCount( + fromDate: Date, + toDate: Date, +): Promise { + return await MultisigSession.count({ + where: { + createdAt: Between(fromDate, toDate), + }, + }); +} diff --git a/src/routes/v1/index.ts b/src/routes/v1/index.ts index a74995f..1d08b4a 100644 --- a/src/routes/v1/index.ts +++ b/src/routes/v1/index.ts @@ -7,6 +7,7 @@ import { nonceRouter } from './nonceRouter'; import { passportNonceRouter } from '@/src/routes/v1/passportNonceRouter'; import { passportAuthenticationRouter } from '@/src/routes/v1/passportAuthenticationRouter'; import { multisigAuthenticationRouter } from './multisigAuthenticationRouter'; +import { multisigSessionCountRouter } from '@/src/routes/v1/multisigSessionCountRouter'; export const v1Router = express.Router(); v1Router.use('/v1', healthRouter); @@ -15,6 +16,7 @@ v1Router.use('/v1', authorizationRouter); v1Router.use('/v1', authenticationRouter); v1Router.use('/v1', logoutRouter); v1Router.use('/v1', multisigAuthenticationRouter); +v1Router.use('/v1', multisigSessionCountRouter); // Passport routes v1Router.use('/v1', passportNonceRouter); diff --git a/src/routes/v1/multisigSessionCountRouter.ts b/src/routes/v1/multisigSessionCountRouter.ts new file mode 100644 index 0000000..148bea7 --- /dev/null +++ b/src/routes/v1/multisigSessionCountRouter.ts @@ -0,0 +1,32 @@ +import express, { Request, Response } from 'express'; +import { errorMessagesEnum } from '@/src/utils/errorMessages'; +import { logger } from '@/src/utils/logger'; +import { getMultisigSessionsCount } from '@/src/repositories/multisigSessionRepository'; + +export const multisigSessionCountRouter = express.Router(); +multisigSessionCountRouter.get( + '/multisigSessionCount', + async (req: Request, res: Response, next) => { + try { + const { from, to } = req.query; + if (!from || !to) { + res.status(422).json({ + message: errorMessagesEnum.MULTISIG_SESSION_COUNTS_INVALID_REQUEST, + }); + return; + } + + const count = await getMultisigSessionsCount( + new Date(String(from)), + new Date(String(to)), + ); + + res.send({ + count, + }); + } catch (e) { + logger.error('get multisigSessionCount error', e); + next(e); + } + }, +); diff --git a/src/utils/errorMessages.ts b/src/utils/errorMessages.ts index 5afe29f..66f08ae 100644 --- a/src/utils/errorMessages.ts +++ b/src/utils/errorMessages.ts @@ -137,4 +137,9 @@ export const errorMessagesEnum = { httpStatusCode: 404, code: 2008, }, + MULTISIG_SESSION_COUNTS_INVALID_REQUEST: { + message: 'Multisig session counts invalid request, params not found', + httpStatusCode: 404, + code: 2009, + }, };