mirror of
https://github.com/logto-io/logto.git
synced 2025-01-20 21:32:31 -05:00
chore(core,schemas): remove old user logs (#597)
* chore(core): remove user logs * chore(schemas): remove user logs
This commit is contained in:
parent
7990b813ab
commit
e21d8d38f3
8 changed files with 0 additions and 190 deletions
|
@ -6,9 +6,6 @@ import {
|
|||
Resource,
|
||||
Role,
|
||||
Setting,
|
||||
UserLog,
|
||||
UserLogResult,
|
||||
UserLogType,
|
||||
} from '@logto/schemas';
|
||||
|
||||
export * from './connector';
|
||||
|
@ -61,12 +58,3 @@ export const mockPasscode: Passcode = {
|
|||
tryCount: 2,
|
||||
createdAt: 10,
|
||||
};
|
||||
|
||||
export const mockUserLog: UserLog = {
|
||||
id: 'foo',
|
||||
userId: 'foo',
|
||||
type: UserLogType.RegisterEmail,
|
||||
result: UserLogResult.Success,
|
||||
payload: {},
|
||||
createdAt: 10,
|
||||
};
|
||||
|
|
|
@ -1,67 +0,0 @@
|
|||
import { UserLogs } from '@logto/schemas';
|
||||
import { createMockPool, createMockQueryResult, sql } from 'slonik';
|
||||
import { snakeCase } from 'snake-case';
|
||||
|
||||
import { mockUserLog } from '@/__mocks__';
|
||||
import {
|
||||
convertToIdentifiers,
|
||||
excludeAutoSetFields,
|
||||
convertToPrimitiveOrSql,
|
||||
} from '@/database/utils';
|
||||
import envSet from '@/env-set';
|
||||
import { expectSqlAssert, QueryType } from '@/utils/test-utils';
|
||||
|
||||
import { insertUserLog, findLogsByUserId } from './user-log';
|
||||
|
||||
const mockQuery: jest.MockedFunction<QueryType> = jest.fn();
|
||||
|
||||
jest.spyOn(envSet, 'pool', 'get').mockReturnValue(
|
||||
createMockPool({
|
||||
query: async (sql, values) => {
|
||||
return mockQuery(sql, values);
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
describe('user-log query', () => {
|
||||
const { table, fields } = convertToIdentifiers(UserLogs);
|
||||
const dbvalue = { ...mockUserLog, payload: JSON.stringify(mockUserLog.payload) };
|
||||
|
||||
it('findLogsByUserId', async () => {
|
||||
const userId = 'foo';
|
||||
const expectSql = sql`
|
||||
select ${sql.join(Object.values(fields), sql`,`)}
|
||||
from ${table}
|
||||
where ${fields.userId}=${userId}
|
||||
order by created_at desc
|
||||
`;
|
||||
|
||||
mockQuery.mockImplementationOnce(async (sql, values) => {
|
||||
expectSqlAssert(sql, expectSql.sql);
|
||||
expect(values).toEqual([userId]);
|
||||
|
||||
return createMockQueryResult([dbvalue]);
|
||||
});
|
||||
|
||||
await expect(findLogsByUserId(userId)).resolves.toEqual([dbvalue]);
|
||||
});
|
||||
|
||||
it('insertUserLog', async () => {
|
||||
const keys = excludeAutoSetFields(UserLogs.fieldKeys);
|
||||
|
||||
// eslint-disable-next-line sql/no-unsafe-query
|
||||
const expectSql = `
|
||||
insert into "user_logs" (${keys.map((k) => `"${snakeCase(k)}"`).join(', ')})
|
||||
values (${keys.map((_, index) => `$${index + 1}`).join(', ')})
|
||||
`;
|
||||
|
||||
mockQuery.mockImplementationOnce(async (sql, values) => {
|
||||
expectSqlAssert(sql, expectSql);
|
||||
expect(values).toEqual(keys.map((k) => convertToPrimitiveOrSql(k, mockUserLog[k])));
|
||||
|
||||
return createMockQueryResult([]);
|
||||
});
|
||||
|
||||
await insertUserLog(mockUserLog);
|
||||
});
|
||||
});
|
|
@ -1,18 +0,0 @@
|
|||
import { CreateUserLog, UserLogs } from '@logto/schemas';
|
||||
import { sql } from 'slonik';
|
||||
|
||||
import { buildInsertInto } from '@/database/insert-into';
|
||||
import { convertToIdentifiers } from '@/database/utils';
|
||||
import envSet from '@/env-set';
|
||||
|
||||
const { table, fields } = convertToIdentifiers(UserLogs);
|
||||
|
||||
export const insertUserLog = buildInsertInto<CreateUserLog>(UserLogs);
|
||||
|
||||
export const findLogsByUserId = async (userId: string) =>
|
||||
envSet.pool.many<CreateUserLog>(sql`
|
||||
select ${sql.join(Object.values(fields), sql`,`)}
|
||||
from ${table}
|
||||
where ${fields.userId}=${userId}
|
||||
order by created_at desc
|
||||
`);
|
|
@ -15,21 +15,6 @@ export enum PasscodeType {
|
|||
Register = 'Register',
|
||||
ForgotPassword = 'ForgotPassword',
|
||||
}
|
||||
export enum UserLogType {
|
||||
SignInUsernameAndPassword = 'SignInUsernameAndPassword',
|
||||
SignInEmail = 'SignInEmail',
|
||||
SignInPhone = 'SignInPhone',
|
||||
SignInSocial = 'SignInSocial',
|
||||
RegisterUsernameAndPassword = 'RegisterUsernameAndPassword',
|
||||
RegisterEmail = 'RegisterEmail',
|
||||
RegisterPhone = 'RegisterPhone',
|
||||
RegisterSocial = 'RegisterSocial',
|
||||
ExchangeAccessToken = 'ExchangeAccessToken',
|
||||
}
|
||||
export enum UserLogResult {
|
||||
Success = 'Success',
|
||||
Failed = 'Failed',
|
||||
}
|
||||
export enum UsersPasswordEncryptionMethod {
|
||||
SaltAndPepper = 'SaltAndPepper',
|
||||
}
|
||||
|
|
|
@ -10,5 +10,4 @@ export * from './resource';
|
|||
export * from './role';
|
||||
export * from './setting';
|
||||
export * from './sign-in-experience';
|
||||
export * from './user-log';
|
||||
export * from './user';
|
||||
|
|
|
@ -1,48 +0,0 @@
|
|||
// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
|
||||
|
||||
import { z } from 'zod';
|
||||
|
||||
import { UserLogPayload, userLogPayloadGuard, GeneratedSchema, Guard } from '../foundations';
|
||||
import { UserLogType, UserLogResult } from './custom-types';
|
||||
|
||||
export type CreateUserLog = {
|
||||
id: string;
|
||||
userId: string;
|
||||
type: UserLogType;
|
||||
result: UserLogResult;
|
||||
payload: UserLogPayload;
|
||||
createdAt?: number;
|
||||
};
|
||||
|
||||
export type UserLog = {
|
||||
id: string;
|
||||
userId: string;
|
||||
type: UserLogType;
|
||||
result: UserLogResult;
|
||||
payload: UserLogPayload;
|
||||
createdAt: number;
|
||||
};
|
||||
|
||||
const createGuard: Guard<CreateUserLog> = z.object({
|
||||
id: z.string(),
|
||||
userId: z.string(),
|
||||
type: z.nativeEnum(UserLogType),
|
||||
result: z.nativeEnum(UserLogResult),
|
||||
payload: userLogPayloadGuard,
|
||||
createdAt: z.number().optional(),
|
||||
});
|
||||
|
||||
export const UserLogs: GeneratedSchema<CreateUserLog> = Object.freeze({
|
||||
table: 'user_logs',
|
||||
tableSingular: 'user_log',
|
||||
fields: {
|
||||
id: 'id',
|
||||
userId: 'user_id',
|
||||
type: 'type',
|
||||
result: 'result',
|
||||
payload: 'payload',
|
||||
createdAt: 'created_at',
|
||||
},
|
||||
fieldKeys: ['id', 'userId', 'type', 'result', 'payload', 'createdAt'],
|
||||
createGuard,
|
||||
});
|
|
@ -67,22 +67,6 @@ export const identitiesGuard = z.record(identityGuard);
|
|||
export type Identity = z.infer<typeof identityGuard>;
|
||||
export type Identities = z.infer<typeof identitiesGuard>;
|
||||
|
||||
/**
|
||||
* User Logs
|
||||
*/
|
||||
|
||||
/** @deprecated */
|
||||
export const userLogPayloadGuard = z.object({
|
||||
ip: z.string().optional(),
|
||||
userAgent: z.string().optional(),
|
||||
applicationId: z.string().optional(),
|
||||
applicationName: z.string().optional(),
|
||||
details: z.object({}).optional(), // NOT intend to be parsed
|
||||
});
|
||||
|
||||
/** @deprecated */
|
||||
export type UserLogPayload = z.infer<typeof userLogPayloadGuard>;
|
||||
|
||||
/**
|
||||
* Settings
|
||||
*/
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
create type user_log_type as enum ('SignInUsernameAndPassword', 'SignInEmail', 'SignInPhone', 'SignInSocial', 'RegisterUsernameAndPassword', 'RegisterEmail', 'RegisterPhone', 'RegisterSocial', 'ExchangeAccessToken');
|
||||
|
||||
create type user_log_result as enum ('Success', 'Failed');
|
||||
|
||||
create table user_logs (
|
||||
id varchar(24) not null,
|
||||
user_id varchar(24) not null,
|
||||
type user_log_type not null,
|
||||
result user_log_result not null, /* not using boolean, may have more result types in the future */
|
||||
payload jsonb /* @use UserLogPayload */ not null,
|
||||
created_at timestamptz not null default(now()),
|
||||
primary key (id)
|
||||
);
|
Loading…
Add table
Reference in a new issue