0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-06 20:40:08 -05:00
logto/packages/core/src/queries/user.ts

262 lines
7.5 KiB
TypeScript
Raw Normal View History

import type { User, CreateUser, UserWithRoleNames } from '@logto/schemas';
import { SearchJointMode, Users } from '@logto/schemas';
2022-10-21 00:14:17 -05:00
import type { OmitAutoSetFields } from '@logto/shared';
import { conditionalSql, convertToIdentifiers } from '@logto/shared';
import type { CommonQueryMethods } from 'slonik';
import { sql } from 'slonik';
2021-08-29 22:30:54 -05:00
import { buildUpdateWhereWithPool } from '#src/database/update-where.js';
2022-11-21 03:38:24 -05:00
import envSet from '#src/env-set/index.js';
import { DeletionError } from '#src/errors/SlonikError/index.js';
import type { Search } from '#src/utils/search.js';
import { buildConditionsFromSearch } from '#src/utils/search.js';
// TODO: @sijie remove this
import { findRolesByRoleIds } from './roles.js';
import { findUsersRolesByUserId } from './users-roles.js';
const { table, fields } = convertToIdentifiers(Users);
export const createUserQueries = (pool: CommonQueryMethods) => {
const findUserByUsername = async (username: string) =>
pool.maybeOne<User>(sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
where ${fields.username}=${username}
`);
const findUserByEmail = async (email: string) =>
pool.maybeOne<User>(sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
where lower(${fields.primaryEmail})=lower(${email})
`);
2021-07-04 04:41:46 -05:00
const findUserByPhone = async (phone: string) =>
pool.maybeOne<User>(sql`
2022-02-11 02:19:18 -05:00
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
where ${fields.primaryPhone}=${phone}
`);
const findUserById = async (id: string): Promise<UserWithRoleNames> => {
const user = await pool.one<User>(sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
where ${fields.id}=${id}
`);
const userRoles = await findUsersRolesByUserId(user.id);
const roles =
userRoles.length > 0 ? await findRolesByRoleIds(userRoles.map(({ roleId }) => roleId)) : [];
return {
...user,
roleNames: roles.map(({ name }) => name),
};
};
const findUserByIdentity = async (target: string, userId: string) =>
pool.maybeOne<User>(
sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
where ${fields.identities}::json#>>'{${sql.identifier([target])},userId}' = ${userId}
`
);
const hasUser = async (username: string, excludeUserId?: string) =>
pool.exists(sql`
select ${fields.id}
from ${table}
where ${fields.username}=${username}
${conditionalSql(excludeUserId, (id) => sql`and ${fields.id}<>${id}`)}
`);
const hasUserWithId = async (id: string) =>
pool.exists(sql`
2022-02-11 02:19:18 -05:00
select ${fields.id}
from ${table}
where ${fields.id}=${id}
`);
const hasUserWithEmail = async (email: string, excludeUserId?: string) =>
pool.exists(sql`
select ${fields.primaryEmail}
from ${table}
where lower(${fields.primaryEmail})=lower(${email})
${conditionalSql(excludeUserId, (id) => sql`and ${fields.id}<>${id}`)}
`);
const hasUserWithPhone = async (phone: string, excludeUserId?: string) =>
pool.exists(sql`
select ${fields.primaryPhone}
from ${table}
where ${fields.primaryPhone}=${phone}
${conditionalSql(excludeUserId, (id) => sql`and ${fields.id}<>${id}`)}
`);
const hasUserWithIdentity = async (target: string, userId: string) =>
pool.exists(
sql`
select ${fields.id}
from ${table}
where ${fields.identities}::json#>>'{${sql.identifier([target])},userId}' = ${userId}
`
);
const buildUserConditions = (search: Search, excludeUserIds: string[]) => {
const hasSearch = search.matches.length > 0;
const searchFields = [
Users.fields.id,
Users.fields.primaryEmail,
Users.fields.primaryPhone,
Users.fields.username,
Users.fields.name,
];
if (excludeUserIds.length > 0) {
// FIXME @sijie temp solution to filter out admin users,
// It is too complex to use join
return sql`
where ${fields.id} not in (${sql.join(excludeUserIds, sql`, `)})
${conditionalSql(
hasSearch,
() => sql`and (${buildConditionsFromSearch(search, searchFields)})`
)}
`;
}
return conditionalSql(
hasSearch,
() => sql`where ${buildConditionsFromSearch(search, searchFields)}`
);
};
const defaultUserSearch = { matches: [], isCaseSensitive: false, joint: SearchJointMode.Or };
const countUsers = async (search: Search = defaultUserSearch, excludeUserIds: string[] = []) =>
pool.one<{ count: number }>(sql`
select count(*)
from ${table}
${buildUserConditions(search, excludeUserIds)}
`);
const findUsers = async (
limit: number,
offset: number,
search: Search,
excludeUserIds: string[] = []
) =>
pool.any<User>(
sql`
select ${sql.join(
Object.values(fields).map((field) => sql`${table}.${field}`),
sql`,`
)}
from ${table}
${buildUserConditions(search, excludeUserIds)}
limit ${limit}
offset ${offset}
`
);
const findUsersByIds = async (userIds: string[]) =>
userIds.length > 0
? pool.any<User>(sql`
select ${sql.join(Object.values(fields), sql`, `)}
from ${table}
where ${fields.id} in (${sql.join(userIds, sql`, `)})
`)
: [];
const updateUser = buildUpdateWhereWithPool(pool)<CreateUser, User>(Users, true);
const updateUserById = async (
id: string,
set: Partial<OmitAutoSetFields<CreateUser>>,
jsonbMode: 'replace' | 'merge' = 'merge'
) => updateUser({ set, where: { id }, jsonbMode });
const deleteUserById = async (id: string) => {
const { rowCount } = await pool.query(sql`
delete from ${table}
where ${fields.id}=${id}
`);
if (rowCount < 1) {
throw new DeletionError(Users.table, id);
}
};
const deleteUserIdentity = async (userId: string, target: string) =>
pool.one<User>(sql`
update ${table}
set ${fields.identities}=${fields.identities}::jsonb-${target}
where ${fields.id}=${userId}
returning *
`);
const hasActiveUsers = async () =>
pool.exists(sql`
select ${fields.id}
from ${table}
limit 1
`);
const getDailyNewUserCountsByTimeInterval = async (
startTimeExclusive: number,
endTimeInclusive: number
) =>
pool.any<{ date: string; count: number }>(sql`
select date(${fields.createdAt}), count(*)
from ${table}
where ${fields.createdAt} > to_timestamp(${startTimeExclusive}::double precision / 1000)
and ${fields.createdAt} <= to_timestamp(${endTimeInclusive}::double precision / 1000)
group by date(${fields.createdAt})
`);
return {
findUserByUsername,
findUserByEmail,
findUserByPhone,
findUserById,
findUserByIdentity,
hasUser,
hasUserWithId,
hasUserWithEmail,
hasUserWithPhone,
hasUserWithIdentity,
countUsers,
findUsers,
findUsersByIds,
updateUserById,
deleteUserById,
deleteUserIdentity,
hasActiveUsers,
getDailyNewUserCountsByTimeInterval,
};
};
/** @deprecated Will be removed soon. Use createUserQueries() factory instead. */
export const {
findUserByUsername,
findUserByEmail,
findUserByPhone,
findUserById,
findUserByIdentity,
hasUser,
hasUserWithId,
hasUserWithEmail,
hasUserWithPhone,
hasUserWithIdentity,
countUsers,
findUsers,
findUsersByIds,
updateUserById,
deleteUserById,
deleteUserIdentity,
hasActiveUsers,
getDailyNewUserCountsByTimeInterval,
} = createUserQueries(envSet.pool);