mirror of
https://github.com/logto-io/logto.git
synced 2025-03-31 22:51:25 -05:00
refactor(core): fix log
This commit is contained in:
parent
c24a57f2c4
commit
27c80f7d56
5 changed files with 20 additions and 20 deletions
|
@ -58,7 +58,7 @@ describe('koaLog middleware', () => {
|
|||
|
||||
expect(insertLog).toBeCalledWith({
|
||||
id: nanoIdMock,
|
||||
type,
|
||||
key: type,
|
||||
payload: {
|
||||
...mockPayload,
|
||||
...additionalMockPayload,
|
||||
|
@ -105,7 +105,7 @@ describe('koaLog middleware', () => {
|
|||
|
||||
expect(insertLog).toBeCalledWith({
|
||||
id: nanoIdMock,
|
||||
type,
|
||||
key: type,
|
||||
payload: {
|
||||
...mockPayload,
|
||||
result: LogResult.Error,
|
||||
|
@ -139,7 +139,7 @@ describe('koaLog middleware', () => {
|
|||
|
||||
expect(insertLog).toBeCalledWith({
|
||||
id: nanoIdMock,
|
||||
type,
|
||||
key: type,
|
||||
payload: {
|
||||
...mockPayload,
|
||||
result: LogResult.Error,
|
||||
|
|
|
@ -54,7 +54,7 @@ describe('koaAuditLog middleware', () => {
|
|||
|
||||
expect(insertLog).toBeCalledWith({
|
||||
id: nanoIdMock,
|
||||
type: logKey,
|
||||
key: logKey,
|
||||
payload: {
|
||||
...mockPayload,
|
||||
...additionalMockPayload,
|
||||
|
@ -93,12 +93,12 @@ describe('koaAuditLog middleware', () => {
|
|||
|
||||
expect(insertLog).toHaveBeenCalledWith({
|
||||
id: nanoIdMock,
|
||||
type: logKey,
|
||||
key: logKey,
|
||||
payload: basePayload,
|
||||
});
|
||||
expect(insertLog).toHaveBeenCalledWith({
|
||||
id: nanoIdMock,
|
||||
type: logKey,
|
||||
key: logKey,
|
||||
payload: {
|
||||
...basePayload,
|
||||
...additionalMockPayload,
|
||||
|
@ -139,7 +139,7 @@ describe('koaAuditLog middleware', () => {
|
|||
|
||||
expect(insertLog).toBeCalledWith({
|
||||
id: nanoIdMock,
|
||||
type: logKey,
|
||||
key: logKey,
|
||||
payload: {
|
||||
...mockPayload,
|
||||
key: logKey,
|
||||
|
@ -176,7 +176,7 @@ describe('koaAuditLog middleware', () => {
|
|||
expect(insertLog).toHaveBeenCalledTimes(2);
|
||||
expect(insertLog).toBeCalledWith({
|
||||
id: nanoIdMock,
|
||||
type: logKey,
|
||||
key: logKey,
|
||||
payload: {
|
||||
...mockPayload,
|
||||
key: logKey,
|
||||
|
|
|
@ -12,15 +12,15 @@ const { table, fields } = convertToIdentifiers(Logs);
|
|||
export const insertLog = buildInsertInto<CreateLog>(Logs);
|
||||
|
||||
export type LogCondition = {
|
||||
logType?: string;
|
||||
logKey?: string;
|
||||
applicationId?: string;
|
||||
userId?: string;
|
||||
};
|
||||
|
||||
const buildLogConditionSql = (logCondition: LogCondition) =>
|
||||
conditionalSql(logCondition, ({ logType, applicationId, userId }) => {
|
||||
conditionalSql(logCondition, ({ logKey, applicationId, userId }) => {
|
||||
const subConditions = [
|
||||
conditionalSql(logType, (logType) => sql`${fields.key}=${logType}`),
|
||||
conditionalSql(logKey, (logKey) => sql`${fields.key}=${logKey}`),
|
||||
conditionalSql(userId, (userId) => sql`${fields.payload}->>'userId'=${userId}`),
|
||||
conditionalSql(
|
||||
applicationId,
|
||||
|
|
|
@ -4,7 +4,7 @@ import { createRequester } from '#src/utils/test-utils.js';
|
|||
|
||||
const { jest } = import.meta;
|
||||
|
||||
const mockBody = { type: 'a', payload: {}, createdAt: 123 };
|
||||
const mockBody = { key: 'a', payload: {}, createdAt: 123 };
|
||||
const mockLog = { id: '1', ...mockBody };
|
||||
const mockLogs = [mockLog, { id: '2', ...mockBody }];
|
||||
|
||||
|
@ -28,15 +28,15 @@ describe('logRoutes', () => {
|
|||
it('should call countLogs and findLogs with correct parameters', async () => {
|
||||
const userId = 'userIdValue';
|
||||
const applicationId = 'foo';
|
||||
const logType = 'SignInUsernamePassword';
|
||||
const logKey = 'SignInUsernamePassword';
|
||||
const page = 1;
|
||||
const pageSize = 5;
|
||||
|
||||
await logRequest.get(
|
||||
`/logs?userId=${userId}&applicationId=${applicationId}&logType=${logType}&page=${page}&page_size=${pageSize}`
|
||||
`/logs?userId=${userId}&applicationId=${applicationId}&logKey=${logKey}&page=${page}&page_size=${pageSize}`
|
||||
);
|
||||
expect(countLogs).toHaveBeenCalledWith({ userId, applicationId, logType });
|
||||
expect(findLogs).toHaveBeenCalledWith(5, 0, { userId, applicationId, logType });
|
||||
expect(countLogs).toHaveBeenCalledWith({ userId, applicationId, logKey });
|
||||
expect(findLogs).toHaveBeenCalledWith(5, 0, { userId, applicationId, logKey });
|
||||
});
|
||||
|
||||
it('should return correct response', async () => {
|
||||
|
|
|
@ -15,19 +15,19 @@ export default function logRoutes<T extends AuthedRouter>(router: T) {
|
|||
query: object({
|
||||
userId: string().optional(),
|
||||
applicationId: string().optional(),
|
||||
logType: string().optional(),
|
||||
logKey: string().optional(),
|
||||
}),
|
||||
}),
|
||||
async (ctx, next) => {
|
||||
const { limit, offset } = ctx.pagination;
|
||||
const {
|
||||
query: { userId, applicationId, logType },
|
||||
query: { userId, applicationId, logKey },
|
||||
} = ctx.guard;
|
||||
|
||||
// TODO: @Gao refactor like user search
|
||||
const [{ count }, logs] = await Promise.all([
|
||||
countLogs({ logType, applicationId, userId }),
|
||||
findLogs(limit, offset, { logType, userId, applicationId }),
|
||||
countLogs({ logKey, applicationId, userId }),
|
||||
findLogs(limit, offset, { logKey, userId, applicationId }),
|
||||
]);
|
||||
|
||||
// Return totalCount to pagination middleware
|
||||
|
|
Loading…
Add table
Reference in a new issue