Skip to content

Commit

Permalink
Merge pull request #28 from Giveth/10-track-count-of-multisig-sessions
Browse files Browse the repository at this point in the history
Add multisigSessionCountRouter
  • Loading branch information
ae2079 committed Jul 18, 2024
1 parent 3a50e12 commit 86dbd6e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/repositories/multisigSessionRepository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import moment from 'moment';
import { Between } from 'typeorm';
import { MultisigSession } from '../entities/multisigSession';

export const firstOrCreateMultisigSession = async (
Expand Down Expand Up @@ -47,3 +48,14 @@ export const findNonExpiredMultisigSessions = async (

return session;
};

export async function getMultisigSessionsCount(
fromDate: Date,
toDate: Date,
): Promise<number> {
return await MultisigSession.count({
where: {
createdAt: Between(fromDate, toDate),
},
});
}
2 changes: 2 additions & 0 deletions src/routes/v1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
32 changes: 32 additions & 0 deletions src/routes/v1/multisigSessionCountRouter.ts
Original file line number Diff line number Diff line change
@@ -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);
}
},
);
5 changes: 5 additions & 0 deletions src/utils/errorMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};

0 comments on commit 86dbd6e

Please sign in to comment.