2022-01-17 23:44:08 -05:00
|
|
|
import { User, CreateUser, Users } from '@logto/schemas';
|
2021-07-02 08:14:18 -05:00
|
|
|
import { sql } from 'slonik';
|
2021-08-29 22:30:54 -05:00
|
|
|
|
|
|
|
import { buildInsertInto } from '@/database/insert-into';
|
2021-07-02 09:55:14 -05:00
|
|
|
import pool from '@/database/pool';
|
2021-11-17 22:26:34 -05:00
|
|
|
import { buildUpdateWhere } from '@/database/update-where';
|
2022-02-23 23:29:34 -05:00
|
|
|
import { conditionalSql, convertToIdentifiers, OmitAutoSetFields } from '@/database/utils';
|
2022-03-02 02:44:57 -05:00
|
|
|
import { DeletionError, UpdateError } from '@/errors/SlonikError';
|
2021-07-02 08:14:18 -05:00
|
|
|
|
|
|
|
const { table, fields } = convertToIdentifiers(Users);
|
|
|
|
|
2021-07-26 09:32:18 -05:00
|
|
|
export const findUserByUsername = async (username: string) =>
|
2021-12-07 22:11:27 -05:00
|
|
|
pool.one<User>(sql`
|
2021-12-20 01:20:23 -05:00
|
|
|
select ${sql.join(Object.values(fields), sql`,`)}
|
|
|
|
from ${table}
|
|
|
|
where ${fields.username}=${username}
|
|
|
|
`);
|
2021-07-26 09:32:18 -05:00
|
|
|
|
2022-01-30 22:04:55 -05:00
|
|
|
export const findUserByEmail = async (email: string) =>
|
|
|
|
pool.one<User>(sql`
|
|
|
|
select ${sql.join(Object.values(fields), sql`,`)}
|
|
|
|
from ${table}
|
|
|
|
where ${fields.primaryEmail}=${email}
|
|
|
|
`);
|
|
|
|
|
2022-02-09 03:14:42 -05:00
|
|
|
export const findUserByPhone = async (phone: string) =>
|
|
|
|
pool.one<User>(sql`
|
|
|
|
select ${sql.join(Object.values(fields), sql`,`)}
|
|
|
|
from ${table}
|
|
|
|
where ${fields.primaryPhone}=${phone}
|
|
|
|
`);
|
|
|
|
|
2021-07-02 08:14:18 -05:00
|
|
|
export const findUserById = async (id: string) =>
|
2021-12-07 22:11:27 -05:00
|
|
|
pool.one<User>(sql`
|
2021-12-20 01:20:23 -05:00
|
|
|
select ${sql.join(Object.values(fields), sql`,`)}
|
|
|
|
from ${table}
|
|
|
|
where ${fields.id}=${id}
|
|
|
|
`);
|
2021-07-04 04:41:46 -05:00
|
|
|
|
2022-02-11 02:19:18 -05:00
|
|
|
export const findUserByIdentity = async (connectorId: string, userId: string) =>
|
|
|
|
pool.one<User>(
|
|
|
|
sql`
|
|
|
|
select ${sql.join(Object.values(fields), sql`,`)}
|
|
|
|
from ${table}
|
|
|
|
where ${fields.identities}::json#>>'{${sql.identifier([connectorId])},userId}' = ${userId}
|
|
|
|
`
|
|
|
|
);
|
|
|
|
|
2021-07-04 04:41:46 -05:00
|
|
|
export const hasUser = async (username: string) =>
|
|
|
|
pool.exists(sql`
|
2021-12-20 01:20:23 -05:00
|
|
|
select ${fields.id}
|
|
|
|
from ${table}
|
|
|
|
where ${fields.username}=${username}
|
|
|
|
`);
|
2021-07-04 04:41:46 -05:00
|
|
|
|
|
|
|
export const hasUserWithId = async (id: string) =>
|
|
|
|
pool.exists(sql`
|
2021-12-20 01:20:23 -05:00
|
|
|
select ${fields.id}
|
|
|
|
from ${table}
|
|
|
|
where ${fields.id}=${id}
|
|
|
|
`);
|
2021-07-04 04:41:46 -05:00
|
|
|
|
2022-02-07 01:14:42 -05:00
|
|
|
export const hasUserWithEmail = async (email: string) =>
|
|
|
|
pool.exists(sql`
|
|
|
|
select ${fields.primaryEmail}
|
|
|
|
from ${table}
|
|
|
|
where ${fields.primaryEmail}=${email}
|
|
|
|
`);
|
|
|
|
|
2022-02-09 03:14:42 -05:00
|
|
|
export const hasUserWithPhone = async (phone: string) =>
|
|
|
|
pool.exists(sql`
|
|
|
|
select ${fields.primaryPhone}
|
|
|
|
from ${table}
|
|
|
|
where ${fields.primaryPhone}=${phone}
|
|
|
|
`);
|
|
|
|
|
2022-02-11 02:19:18 -05:00
|
|
|
export const hasUserWithIdentity = async (connectorId: string, userId: string) =>
|
|
|
|
pool.exists(
|
|
|
|
sql`
|
|
|
|
select ${fields.id}
|
|
|
|
from ${table}
|
|
|
|
where ${fields.identities}::json#>>'{${sql.identifier([connectorId])},userId}' = ${userId}
|
|
|
|
`
|
|
|
|
);
|
|
|
|
|
2022-01-17 23:44:08 -05:00
|
|
|
export const insertUser = buildInsertInto<CreateUser, User>(pool, Users, { returning: true });
|
2021-11-17 22:26:34 -05:00
|
|
|
|
2022-02-23 23:29:34 -05:00
|
|
|
const buildUserSearchConditionSql = (search: string) => {
|
|
|
|
const searchFields = [fields.primaryEmail, fields.primaryPhone, fields.username, fields.name];
|
|
|
|
const conditions = searchFields.map((filedName) => sql`${filedName} like ${'%' + search + '%'}`);
|
2022-02-16 02:55:08 -05:00
|
|
|
|
2022-02-23 23:29:34 -05:00
|
|
|
return sql`${sql.join(conditions, sql` or `)}`;
|
|
|
|
};
|
2022-02-16 02:55:08 -05:00
|
|
|
|
2022-02-23 23:29:34 -05:00
|
|
|
export const countUsers = async (search?: string) =>
|
|
|
|
pool.one<{ count: number }>(sql`
|
|
|
|
select count(*)
|
|
|
|
from ${table}
|
|
|
|
${conditionalSql(search, (search) => sql`where ${buildUserSearchConditionSql(search)}`)}
|
|
|
|
`);
|
|
|
|
|
|
|
|
export const findUsers = async (limit: number, offset: number, search?: string) =>
|
|
|
|
pool.many<User>(
|
|
|
|
sql`
|
|
|
|
select ${sql.join(Object.values(fields), sql`,`)}
|
|
|
|
from ${table}
|
|
|
|
${conditionalSql(search, (search) => sql`where ${buildUserSearchConditionSql(search)}`)}
|
|
|
|
limit ${limit}
|
|
|
|
offset ${offset}
|
|
|
|
`
|
|
|
|
);
|
2021-11-17 22:26:34 -05:00
|
|
|
|
2022-01-17 23:44:08 -05:00
|
|
|
const updateUser = buildUpdateWhere<CreateUser, User>(pool, Users, true);
|
2021-11-17 22:26:34 -05:00
|
|
|
|
2022-01-17 23:44:08 -05:00
|
|
|
export const updateUserById = async (id: string, set: Partial<OmitAutoSetFields<CreateUser>>) =>
|
2021-11-17 22:26:34 -05:00
|
|
|
updateUser({ set, where: { id } });
|
|
|
|
|
|
|
|
export const deleteUserById = async (id: string) => {
|
|
|
|
const { rowCount } = await pool.query(sql`
|
2021-12-20 01:20:23 -05:00
|
|
|
delete from ${table}
|
2022-02-28 01:30:27 -05:00
|
|
|
where ${fields.id}=${id}
|
2021-12-20 01:20:23 -05:00
|
|
|
`);
|
2022-01-27 06:26:34 -05:00
|
|
|
|
2021-11-17 22:26:34 -05:00
|
|
|
if (rowCount < 1) {
|
2022-02-13 22:50:47 -05:00
|
|
|
throw new DeletionError(Users.table, id);
|
2021-11-17 22:26:34 -05:00
|
|
|
}
|
|
|
|
};
|
2022-02-17 01:10:26 -05:00
|
|
|
|
|
|
|
export const clearUserCustomDataById = async (id: string) => {
|
|
|
|
const { rowCount } = await pool.query<User>(sql`
|
|
|
|
update ${table}
|
|
|
|
set ${fields.customData}='{}'::jsonb
|
2022-02-28 01:30:27 -05:00
|
|
|
where ${fields.id}=${id}
|
2022-02-17 01:10:26 -05:00
|
|
|
`);
|
|
|
|
|
|
|
|
if (rowCount < 1) {
|
2022-03-02 02:44:57 -05:00
|
|
|
throw new UpdateError(Users, { set: { customData: {} }, where: { id } });
|
2022-02-17 01:10:26 -05:00
|
|
|
}
|
|
|
|
};
|