Skip to content

Commit

Permalink
Update admin/website search. Update param names.
Browse files Browse the repository at this point in the history
  • Loading branch information
briancao committed Apr 29, 2024
1 parent 0fc8828 commit 4e2bfd7
Show file tree
Hide file tree
Showing 17 changed files with 90 additions and 77 deletions.
6 changes: 3 additions & 3 deletions src/components/hooks/queries/useFilterQuery.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { UseQueryOptions } from '@tanstack/react-query';
import { useState } from 'react';
import { useApi } from './useApi';
import { FilterResult, SearchFilter, FilterQueryResult } from 'lib/types';
import { PageResult, PageParams, FilterQueryResult } from 'lib/types';

export function useFilterQuery<T = any>({
queryKey,
queryFn,
...options
}: Omit<UseQueryOptions, 'queryFn'> & { queryFn: (params?: object) => any }): FilterQueryResult<T> {
const [params, setParams] = useState<T | SearchFilter>({
const [params, setParams] = useState<T | PageParams>({
query: '',
page: 1,
});
Expand All @@ -21,7 +21,7 @@ export function useFilterQuery<T = any>({
});

return {
result: data as FilterResult<any>,
result: data as PageResult<any>,
query,
params,
setParams,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { MYSQL, POSTGRESQL, getDatabaseType } from 'lib/db';
import { SESSION_COLUMNS, OPERATORS, DEFAULT_PAGE_SIZE } from './constants';
import { fetchWebsite } from './load';
import { maxDate } from './date';
import { QueryFilters, QueryOptions, SearchFilter } from './types';
import { QueryFilters, QueryOptions, PageParams } from './types';
import { filtersToArray } from './params';

const MYSQL_DATE_FORMATS = {
Expand Down Expand Up @@ -191,7 +191,7 @@ async function rawQuery(sql: string, data: object): Promise<any> {
return prisma.rawQuery(query, params);
}

async function pagedQuery<T>(model: string, criteria: T, filters: SearchFilter) {
async function pagedQuery<T>(model: string, criteria: T, filters: PageParams) {
const { page = 1, pageSize, orderBy, sortDescending = false } = filters || {};
const size = +pageSize || DEFAULT_PAGE_SIZE;

Expand Down
34 changes: 5 additions & 29 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,15 @@ export type DynamicDataType = ObjectValues<typeof DATA_TYPE>;
export type KafkaTopic = ObjectValues<typeof KAFKA_TOPIC>;
export type ReportType = ObjectValues<typeof REPORT_TYPES>;

export interface WebsiteSearchFilter extends SearchFilter {
userId?: string;
teamId?: string;
includeTeams?: boolean;
onlyTeams?: boolean;
}

export interface UserSearchFilter extends SearchFilter {
teamId?: string;
}

export interface TeamSearchFilter extends SearchFilter {
userId?: string;
}

export interface TeamUserSearchFilter extends SearchFilter {
teamId?: string;
}

export interface ReportSearchFilter extends SearchFilter {
userId?: string;
websiteId?: string;
}

export interface SearchFilter {
export interface PageParams {
query?: string;
page?: number;
pageSize?: number;
orderBy?: string;
sortDescending?: boolean;
}

export interface FilterResult<T> {
export interface PageResult<T> {
data: T;
count: number;
page: number;
Expand All @@ -66,10 +42,10 @@ export interface FilterResult<T> {
}

export interface FilterQueryResult<T> {
result: FilterResult<T>;
result: PageResult<T>;
query: any;
params: SearchFilter;
setParams: Dispatch<SetStateAction<T | SearchFilter>>;
params: PageParams;
setParams: Dispatch<SetStateAction<T | PageParams>>;
}

export interface DynamicData {
Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/admin/users.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { canViewUsers } from 'lib/auth';
import { useAuth, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, Role, SearchFilter, User } from 'lib/types';
import { NextApiRequestQueryBody, Role, PageParams, User } from 'lib/types';
import { pageInfo } from 'lib/schema';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getUsers } from 'queries';
import * as yup from 'yup';

export interface UsersRequestQuery extends SearchFilter {}
export interface UsersRequestQuery extends PageParams {}
export interface UsersRequestBody {
userId: string;
username: string;
Expand Down
43 changes: 40 additions & 3 deletions src/pages/api/admin/websites.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { canViewAllWebsites } from 'lib/auth';
import { ROLES } from 'lib/constants';
import { useAuth, useCors, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
import { pageInfo } from 'lib/schema';
import { NextApiRequestQueryBody, PageParams } from 'lib/types';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getWebsites } from 'queries';
import * as yup from 'yup';
import { pageInfo } from 'lib/schema';

export interface WebsitesRequestQuery extends SearchFilter {}
export interface WebsitesRequestQuery extends PageParams {
userId?: string;
includeTeams?: boolean;
}

export interface WebsitesRequestBody {
name: string;
Expand Down Expand Up @@ -39,15 +43,48 @@ export default async (
return unauthorized(res);
}

const { userId, includeOwnedTeams } = req.query;

const websites = await getWebsites(
{
where: {
OR: [
...(userId && [{ userId }]),
...(userId &&
includeOwnedTeams && [
{
team: {
deletedAt: null,
teamUser: {
some: {
role: ROLES.teamOwner,
userId,
},
},
},
},
]),
],
},
include: {
user: {
select: {
username: true,
id: true,
},
},
team: {
where: {
deletedAt: null,
},
include: {
teamUser: {
where: {
role: ROLES.teamOwner,
},
},
},
},
},
},
req.query,
Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/teams/[teamId]/users/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { canAddUserToTeam, canViewTeam } from 'lib/auth';
import { useAuth, useValidate } from 'lib/middleware';
import { pageInfo } from 'lib/schema';
import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
import { NextApiRequestQueryBody, PageParams } from 'lib/types';
import { NextApiResponse } from 'next';
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
import { createTeamUser, getTeamUser, getTeamUsers } from 'queries';
import * as yup from 'yup';

export interface TeamUserRequestQuery extends SearchFilter {
export interface TeamUserRequestQuery extends PageParams {
teamId: string;
}

Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/teams/[teamId]/websites/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as yup from 'yup';
import { canViewTeam } from 'lib/auth';
import { useAuth, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
import { NextApiRequestQueryBody, PageParams } from 'lib/types';
import { pageInfo } from 'lib/schema';
import { NextApiResponse } from 'next';
import { ok, unauthorized } from 'next-basics';
import { getTeamWebsites } from 'queries';

export interface TeamWebsiteRequestQuery extends SearchFilter {
export interface TeamWebsiteRequestQuery extends PageParams {
teamId: string;
}

Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/teams/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { Team } from '@prisma/client';
import { canCreateTeam } from 'lib/auth';
import { uuid } from 'lib/crypto';
import { useAuth, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
import { NextApiRequestQueryBody, PageParams } from 'lib/types';
import { pageInfo } from 'lib/schema';
import { NextApiResponse } from 'next';
import { getRandomChars, methodNotAllowed, ok, unauthorized } from 'next-basics';
import { createTeam } from 'queries';

export interface TeamsRequestQuery extends SearchFilter {}
export interface TeamsRequestQuery extends PageParams {}
export interface TeamsRequestBody {
name: string;
}
Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/users/[userId]/teams.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as yup from 'yup';
import { useAuth, useCors, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
import { NextApiRequestQueryBody, PageParams } from 'lib/types';
import { pageInfo } from 'lib/schema';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getUserTeams } from 'queries';

export interface UserTeamsRequestQuery extends SearchFilter {
export interface UserTeamsRequestQuery extends PageParams {
userId: string;
}

Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { canCreateUser } from 'lib/auth';
import { ROLES } from 'lib/constants';
import { uuid } from 'lib/crypto';
import { useAuth, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, Role, SearchFilter, User } from 'lib/types';
import { NextApiRequestQueryBody, Role, PageParams, User } from 'lib/types';
import { pageInfo } from 'lib/schema';
import { NextApiResponse } from 'next';
import { badRequest, hashPassword, methodNotAllowed, ok, unauthorized } from 'next-basics';
import { createUser, getUserByUsername } from 'queries';
import * as yup from 'yup';

export interface UsersRequestQuery extends SearchFilter {}
export interface UsersRequestQuery extends PageParams {}
export interface UsersRequestBody {
username: string;
password: string;
Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/websites/[websiteId]/reports.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as yup from 'yup';
import { canViewWebsite } from 'lib/auth';
import { useAuth, useCors, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
import { NextApiRequestQueryBody, PageParams } from 'lib/types';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getWebsiteReports } from 'queries';
import { pageInfo } from 'lib/schema';

export interface ReportsRequestQuery extends SearchFilter {
export interface ReportsRequestQuery extends PageParams {
websiteId: string;
}

Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/websites/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { canCreateTeamWebsite, canCreateWebsite } from 'lib/auth';
import { uuid } from 'lib/crypto';
import { useAuth, useCors, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
import { NextApiRequestQueryBody, PageParams } from 'lib/types';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { createWebsite } from 'queries';
import userWebsitesRoute from 'pages/api/users/[userId]/websites';
import * as yup from 'yup';
import { pageInfo } from 'lib/schema';

export interface WebsitesRequestQuery extends SearchFilter {}
export interface WebsitesRequestQuery extends PageParams {}

export interface WebsitesRequestBody {
name: string;
Expand Down
14 changes: 7 additions & 7 deletions src/queries/admin/report.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Prisma, Report } from '@prisma/client';
import prisma from 'lib/prisma';
import { FilterResult, ReportSearchFilter } from 'lib/types';
import { PageResult, PageParams } from 'lib/types';
import ReportFindManyArgs = Prisma.ReportFindManyArgs;

async function findReport(criteria: Prisma.ReportFindUniqueArgs): Promise<Report> {
Expand All @@ -17,8 +17,8 @@ export async function getReport(reportId: string): Promise<Report> {

export async function getReports(
criteria: ReportFindManyArgs,
filters: ReportSearchFilter = {},
): Promise<FilterResult<Report[]>> {
filters: PageParams = {},
): Promise<PageResult<Report[]>> {
const { query } = filters;

const where: Prisma.ReportWhereInput = {
Expand Down Expand Up @@ -50,8 +50,8 @@ export async function getReports(

export async function getUserReports(
userId: string,
filters?: ReportSearchFilter,
): Promise<FilterResult<Report[]>> {
filters?: PageParams,
): Promise<PageResult<Report[]>> {
return getReports(
{
where: {
Expand All @@ -72,8 +72,8 @@ export async function getUserReports(

export async function getWebsiteReports(
websiteId: string,
filters: ReportSearchFilter = {},
): Promise<FilterResult<Report[]>> {
filters: PageParams = {},
): Promise<PageResult<Report[]>> {
return getReports(
{
where: {
Expand Down
8 changes: 4 additions & 4 deletions src/queries/admin/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Prisma, Team } from '@prisma/client';
import { ROLES } from 'lib/constants';
import { uuid } from 'lib/crypto';
import prisma from 'lib/prisma';
import { FilterResult, TeamSearchFilter } from 'lib/types';
import { PageResult, PageParams } from 'lib/types';
import TeamFindManyArgs = Prisma.TeamFindManyArgs;

export async function findTeam(criteria: Prisma.TeamFindUniqueArgs): Promise<Team> {
Expand All @@ -22,8 +22,8 @@ export async function getTeam(teamId: string, options: { includeMembers?: boolea

export async function getTeams(
criteria: TeamFindManyArgs,
filters: TeamSearchFilter = {},
): Promise<FilterResult<Team[]>> {
filters: PageParams = {},
): Promise<PageResult<Team[]>> {
const { getSearchParameters } = prisma;
const { query } = filters;

Expand All @@ -42,7 +42,7 @@ export async function getTeams(
);
}

export async function getUserTeams(userId: string, filters: TeamSearchFilter = {}) {
export async function getUserTeams(userId: string, filters: PageParams = {}) {
return getTeams(
{
where: {
Expand Down
6 changes: 3 additions & 3 deletions src/queries/admin/teamUser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Prisma, TeamUser } from '@prisma/client';
import { uuid } from 'lib/crypto';
import prisma from 'lib/prisma';
import { FilterResult, TeamUserSearchFilter } from 'lib/types';
import { PageResult, PageParams } from 'lib/types';
import TeamUserFindManyArgs = Prisma.TeamUserFindManyArgs;

export async function findTeamUser(criteria: Prisma.TeamUserFindUniqueArgs): Promise<TeamUser> {
Expand All @@ -19,8 +19,8 @@ export async function getTeamUser(teamId: string, userId: string): Promise<TeamU

export async function getTeamUsers(
criteria: TeamUserFindManyArgs,
filters?: TeamUserSearchFilter,
): Promise<FilterResult<TeamUser[]>> {
filters?: PageParams,
): Promise<PageResult<TeamUser[]>> {
const { query } = filters;

const where: Prisma.TeamUserWhereInput = {
Expand Down
Loading

0 comments on commit 4e2bfd7

Please sign in to comment.