mirror of
https://github.com/logto-io/logto.git
synced 2025-01-20 21:32:31 -05:00
feat(core): get /dashboard/users/total (#936)
* feat(core): get /dashboard/users/total * test(core): get /dashboard/users/total
This commit is contained in:
parent
4abcda6820
commit
c4bb0de7d4
3 changed files with 44 additions and 0 deletions
30
packages/core/src/routes/dashboard.test.ts
Normal file
30
packages/core/src/routes/dashboard.test.ts
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
import dashboardRoutes from '@/routes/dashboard';
|
||||||
|
import { createRequester } from '@/utils/test-utils';
|
||||||
|
|
||||||
|
const totalUserCount = 1000;
|
||||||
|
const countUsers = jest.fn(async () => ({ count: totalUserCount }));
|
||||||
|
|
||||||
|
jest.mock('@/queries/user', () => ({
|
||||||
|
countUsers: async () => countUsers(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe('dashboardRoutes', () => {
|
||||||
|
const logRequest = createRequester({ authedRoutes: dashboardRoutes });
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
jest.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('GET /dashboard/users/total', () => {
|
||||||
|
it('should call countUsers with no parameters', async () => {
|
||||||
|
await logRequest.get('/dashboard/users/total');
|
||||||
|
expect(countUsers).toHaveBeenCalledWith();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('/dashboard/users/total should return correct response', async () => {
|
||||||
|
const response = await logRequest.get('/dashboard/users/total');
|
||||||
|
expect(response.status).toEqual(200);
|
||||||
|
expect(response.body).toEqual({ totalUserCount });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
12
packages/core/src/routes/dashboard.ts
Normal file
12
packages/core/src/routes/dashboard.ts
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
import { countUsers } from '@/queries/user';
|
||||||
|
|
||||||
|
import { AuthedRouter } from './types';
|
||||||
|
|
||||||
|
export default function dashboardRoutes<T extends AuthedRouter>(router: T) {
|
||||||
|
router.get('/dashboard/users/total', async (ctx, next) => {
|
||||||
|
const { count: totalUserCount } = await countUsers();
|
||||||
|
ctx.body = { totalUserCount };
|
||||||
|
|
||||||
|
return next();
|
||||||
|
});
|
||||||
|
}
|
|
@ -7,6 +7,7 @@ import koaAuth from '@/middleware/koa-auth';
|
||||||
import koaLogSession from '@/middleware/koa-log-session';
|
import koaLogSession from '@/middleware/koa-log-session';
|
||||||
import applicationRoutes from '@/routes/application';
|
import applicationRoutes from '@/routes/application';
|
||||||
import connectorRoutes from '@/routes/connector';
|
import connectorRoutes from '@/routes/connector';
|
||||||
|
import dashboardRoutes from '@/routes/dashboard';
|
||||||
import resourceRoutes from '@/routes/resource';
|
import resourceRoutes from '@/routes/resource';
|
||||||
import sessionRoutes from '@/routes/session';
|
import sessionRoutes from '@/routes/session';
|
||||||
import settingRoutes from '@/routes/setting';
|
import settingRoutes from '@/routes/setting';
|
||||||
|
@ -40,6 +41,7 @@ const createRouters = (provider: Provider) => {
|
||||||
adminUserRoutes(authedRouter);
|
adminUserRoutes(authedRouter);
|
||||||
logRoutes(authedRouter);
|
logRoutes(authedRouter);
|
||||||
roleRoutes(authedRouter);
|
roleRoutes(authedRouter);
|
||||||
|
dashboardRoutes(authedRouter);
|
||||||
|
|
||||||
return [sessionRouter, anonymousRouter, authedRouter];
|
return [sessionRouter, anonymousRouter, authedRouter];
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue