Skip to content

Commit

Permalink
fix: only use experimental tables
Browse files Browse the repository at this point in the history
  • Loading branch information
casperiv0 committed Oct 21, 2023
1 parent 9559b98 commit 0189485
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { UPDATE_EMPLOYEE_SCHEMA } from "@snailycad/schemas";
import { EmployeeAsEnum } from "@snailycad/types";
import { ExtendedBadRequest } from "src/exceptions/extended-bad-request";
import { AuditLogActionType, createAuditLogEntry } from "@snailycad/audit-logger/server";
import { getPrismaModelOrderBy } from "~/utils/order-by";

const businessInclude = {
user: {
Expand All @@ -40,6 +41,7 @@ export class AdminManageBusinessesController {
@QueryParams("skip", Number) skip = 0,
@QueryParams("includeAll", Boolean) includeAll = false,
@QueryParams("query", String) query = "",
@QueryParams("sorting") sorting = "",
): Promise<APITypes.GetManageBusinessesData> {
const where = {} as Prisma.BusinessWhereInput;

Expand All @@ -51,13 +53,15 @@ export class AdminManageBusinessesController {
where.status = WhitelistStatus.PENDING;
}

const orderBy = getPrismaModelOrderBy(sorting);
const [totalCount, businesses] = await prisma.$transaction([
prisma.business.count({ where }),
prisma.business.findMany({
take: includeAll ? undefined : 35,
skip: includeAll ? undefined : skip,
where,
include: businessInclude,
orderBy,
}),
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { isFeatureEnabled } from "lib/upsert-cad";
import { leoProperties, unitProperties } from "utils/leo/includes";
import { recordsInclude } from "~/controllers/leo/search/SearchController";
import { ExtendedBadRequest } from "~/exceptions/extended-bad-request";
import { getPrismaModelOrderBy } from "~/utils/order-by";

@UseBeforeEach(IsAuth)
@Controller("/admin/manage/citizens")
Expand All @@ -37,6 +38,7 @@ export class AdminManageCitizensController {
@QueryParams("userId", String) userId?: string,
@QueryParams("includeOfficers", Boolean) includeOfficers = false,
@QueryParams("includeDeputies", Boolean) includeDeputies = false,
@QueryParams("sorting") sorting = "",
): Promise<APITypes.GetManageCitizensData> {
const [name, surname] = query.toString().toLowerCase().split(/ +/g);

Expand All @@ -58,10 +60,12 @@ export class AdminManageCitizensController {
}
: undefined;

const orderBy = getPrismaModelOrderBy(sorting);
const [totalCount, citizens] = await prisma.$transaction([
prisma.citizen.count({ where }),
prisma.citizen.findMany({
where,
orderBy,
include: {
gender: true,
ethnicity: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { isDiscordIdInUse } from "lib/discord/utils";
import { getTranslator } from "~/utils/get-translator";
import { sendRawWebhook, sendDiscordWebhook } from "~/lib/discord/webhooks";
import { type APIEmbed } from "discord-api-types/v10";
import { getPrismaModelOrderBy } from "~/utils/order-by";

const manageUsersSelect = (selectCitizens: boolean) =>
({
Expand Down Expand Up @@ -69,6 +70,7 @@ export class ManageUsersController {
@QueryParams("query", String) query = "",
@QueryParams("pendingOnly", Boolean) pendingOnly = false,
@QueryParams("includeAll", Boolean) includeAll = false,
@QueryParams("sorting") sorting = "",
): Promise<APITypes.GetManageUsersData> {
const where =
query || pendingOnly
Expand All @@ -87,9 +89,10 @@ export class ManageUsersController {
}
: undefined;

const orderBy = getPrismaModelOrderBy(sorting);
const [totalCount, pendingCount] = await prisma.$transaction([
prisma.user.count({ where }),
prisma.user.count({ where: { whitelistStatus: WhitelistStatus.PENDING } }),
prisma.user.count({ orderBy, where: { whitelistStatus: WhitelistStatus.PENDING } }),
]);

const shouldIncludeAll = includeAll;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function BlacklistedWordsTab() {
},
});
const [tempWord, wordState] = useTemporaryItem(asyncTable.items);
const tableState = useTableState(asyncTable);
const tableState = useTableState({ pagination: asyncTable.pagination });

function onRemoveWordClick(word: BlacklistedWord) {
wordState.setTempId(word.id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ export function AllCitizensTab({ citizens: initialData, totalCount, setCitizens
search,
initialData,
totalCount,
sortingSchema: {
name: "name",
surname: "surname",
dateOfBirth: "dateOfBirth",
gender: "gender.value",
ethnicity: "ethnicity.value",
hairColor: "hairColor",
eyeColor: "eyeColor",
weight: "weight",
height: "height",
user: "user.username",
},
fetchOptions: {
path: "/admin/manage/citizens",
onResponse: (json: GetManageCitizensData) => ({
Expand All @@ -48,7 +60,10 @@ export function AllCitizensTab({ citizens: initialData, totalCount, setCitizens
}),
},
});
const tableState = useTableState({ pagination: asyncTable.pagination });
const tableState = useTableState({
sorting: asyncTable.sorting,
pagination: asyncTable.pagination,
});

const [tempValue, valueState] = useTemporaryItem(asyncTable.items);
const { hasPermissions } = usePermission();
Expand Down Expand Up @@ -146,7 +161,8 @@ export function AllCitizensTab({ citizens: initialData, totalCount, setCitizens
tableState={tableState}
data={asyncTable.items.map((citizen) => ({
id: citizen.id,
name: `${citizen.name} ${citizen.surname}`,
name: citizen.name,
surname: citizen.surname,
dateOfBirth: (
<FullDate isDateOfBirth onlyDate>
{citizen.dateOfBirth}
Expand Down Expand Up @@ -193,7 +209,8 @@ export function AllCitizensTab({ citizens: initialData, totalCount, setCitizens
),
}))}
columns={[
{ header: tCitizen("fullName"), accessorKey: "name" },
{ header: tCitizen("name"), accessorKey: "name" },
{ header: tCitizen("surname"), accessorKey: "surname" },
{ header: tCitizen("dateOfBirth"), accessorKey: "dateOfBirth" },
{ header: tCitizen("gender"), accessorKey: "gender" },
{ header: tCitizen("ethnicity"), accessorKey: "ethnicity" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function CallsignsTab({ units }: Props) {
const common = useTranslations("Common");
const { generateCallsign } = useGenerateCallsign();
const modalState = useModal();
const tableState = useTableState(asyncTable);
const tableState = useTableState({ pagination: asyncTable.pagination });
const hasViewUsersPermissions = hasPermissions([Permissions.ViewUsers]);
const { department } = useValues();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,20 @@ export function AllUsersTab({ users, totalCount }: GetManageUsersData) {
search,
initialData: users,
totalCount,
sortingSchema: {
username: "username",
rank: "rank",
whitelistStatus: "whitelistStatus",
},
fetchOptions: {
path: "/admin/manage/users",
onResponse: (json: GetManageUsersData) => ({ totalCount: json.totalCount, data: json.users }),
},
});
const tableState = useTableState({ pagination: asyncTable.pagination });
const tableState = useTableState({
sorting: asyncTable.sorting,
pagination: asyncTable.pagination,
});

return (
<TabsContent aria-label={t("allUsers")} value="allUsers" className="mt-5">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function TowTaxiCallsTable({ initialData, type, noCallsText }: Props) {
},
},
});
const tableState = useTableState(asyncTable);
const tableState = useTableState({ pagination: asyncTable.pagination });
const [tempCall, callState] = useTemporaryItem<string, TowCall | TaxiCall>(asyncTable.items);

const modalState = useModal();
Expand Down
2 changes: 1 addition & 1 deletion apps/client/src/pages/admin/manage/businesses/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default function ManageBusinesses({ business, businessId }: Props) {
totalCount: business.totalCount,
initialData: business.employees,
});
const tableState = useTableState(asyncTable);
const tableState = useTableState({ pagination: asyncTable.pagination });
const [tempEmployee, employeeState] = useTemporaryItem(asyncTable.items);

const t = useTranslations();
Expand Down
12 changes: 11 additions & 1 deletion apps/client/src/pages/admin/manage/businesses/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ export default function ManageBusinesses({ businesses: data }: Props) {
search,
totalCount: data.totalCount,
initialData: data.businesses,
sortingSchema: {
name: "name",
user: "user.username",
whitelisted: "whitelisted",
whitelistStatus: "whitelistStatus",
owners: "employees._count",
},
fetchOptions: {
path: "/admin/manage/businesses",
onResponse: (json: GetManageBusinessesData) => ({
Expand All @@ -48,7 +55,10 @@ export default function ManageBusinesses({ businesses: data }: Props) {
}),
},
});
const tableState = useTableState(asyncTable);
const tableState = useTableState({
pagination: asyncTable.pagination,
sorting: asyncTable.sorting,
});
const [tempValue, valueState] = useTemporaryItem(asyncTable.items);

const TABS = [
Expand Down
2 changes: 1 addition & 1 deletion apps/client/src/pages/ems-fd/hospital-services.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default function EmsFdIncidents({ deadCitizens }: Props) {
initialData: deadCitizens.citizens,
totalCount: deadCitizens.totalCount,
});
const tableState = useTableState(asyncTable);
const tableState = useTableState({ pagination: asyncTable.pagination });
const [tempCitizen, tempCitizenState] = useTemporaryItem(asyncTable.items);

return (
Expand Down
2 changes: 1 addition & 1 deletion apps/client/src/pages/officer/impound-lot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default function ImpoundLot({ vehicles: data }: Props) {
initialData: data.vehicles,
totalCount: data.totalCount,
});
const tableState = useTableState(asyncTable);
const tableState = useTableState({ pagination: asyncTable.pagination });
const [tempVehicle, vehicleState] = useTemporaryItem(asyncTable.items);

function handleCheckoutClick(item: ImpoundedVehicle) {
Expand Down
2 changes: 1 addition & 1 deletion apps/client/src/pages/officer/my-record-reports.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default function MyOfficersLogs({ reports: data }: Props) {
totalCount: data.totalCount,
initialData: data.reports,
});
const tableState = useTableState(asyncTable);
const tableState = useTableState({ pagination: asyncTable.pagination });

return (
<Layout permissions={{ permissions: [Permissions.Leo] }} className="dark:text-white">
Expand Down
2 changes: 1 addition & 1 deletion apps/client/src/pages/pets/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function PetsPage(props: PetsPageProps) {
initialData: props.pets.pets,
totalCount: props.pets.totalCount,
});
const tableState = useTableState(asyncTable);
const tableState = useTableState({ pagination: asyncTable.pagination });

return (
<Layout className="dark:text-white">
Expand Down
2 changes: 1 addition & 1 deletion apps/client/src/pages/tow/logs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default function TowLogs(props: Props) {
},
},
});
const tableState = useTableState(asyncTable);
const tableState = useTableState({ pagination: asyncTable.pagination });

function assignedUnit(call: TowCall) {
return call.assignedUnit ? (
Expand Down

0 comments on commit 0189485

Please sign in to comment.