diff --git a/packages/core/src/__mocks__/index.ts b/packages/core/src/__mocks__/index.ts index 5404e4eb1..e608c8b06 100644 --- a/packages/core/src/__mocks__/index.ts +++ b/packages/core/src/__mocks__/index.ts @@ -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, -}; diff --git a/packages/core/src/queries/user-log.test.ts b/packages/core/src/queries/user-log.test.ts deleted file mode 100644 index cf8ef6835..000000000 --- a/packages/core/src/queries/user-log.test.ts +++ /dev/null @@ -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 = 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); - }); -}); diff --git a/packages/core/src/queries/user-log.ts b/packages/core/src/queries/user-log.ts deleted file mode 100644 index bc29de2e0..000000000 --- a/packages/core/src/queries/user-log.ts +++ /dev/null @@ -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(UserLogs); - -export const findLogsByUserId = async (userId: string) => - envSet.pool.many(sql` - select ${sql.join(Object.values(fields), sql`,`)} - from ${table} - where ${fields.userId}=${userId} - order by created_at desc - `); diff --git a/packages/schemas/src/db-entries/custom-types.ts b/packages/schemas/src/db-entries/custom-types.ts index e9a686cd0..da6a53bfd 100644 --- a/packages/schemas/src/db-entries/custom-types.ts +++ b/packages/schemas/src/db-entries/custom-types.ts @@ -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', } diff --git a/packages/schemas/src/db-entries/index.ts b/packages/schemas/src/db-entries/index.ts index 52f34253c..e27c52bb1 100644 --- a/packages/schemas/src/db-entries/index.ts +++ b/packages/schemas/src/db-entries/index.ts @@ -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'; diff --git a/packages/schemas/src/db-entries/user-log.ts b/packages/schemas/src/db-entries/user-log.ts deleted file mode 100644 index f9284f2f8..000000000 --- a/packages/schemas/src/db-entries/user-log.ts +++ /dev/null @@ -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 = 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 = 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, -}); diff --git a/packages/schemas/src/foundations/jsonb-types.ts b/packages/schemas/src/foundations/jsonb-types.ts index 3478c0ce6..252f94688 100644 --- a/packages/schemas/src/foundations/jsonb-types.ts +++ b/packages/schemas/src/foundations/jsonb-types.ts @@ -67,22 +67,6 @@ export const identitiesGuard = z.record(identityGuard); export type Identity = z.infer; export type Identities = z.infer; -/** - * 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; - /** * Settings */ diff --git a/packages/schemas/tables/user_logs.sql b/packages/schemas/tables/user_logs.sql deleted file mode 100644 index d8a578fc2..000000000 --- a/packages/schemas/tables/user_logs.sql +++ /dev/null @@ -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) -);