Skip to content

Commit

Permalink
🔨 chore!: only load required data (#1323)
Browse files Browse the repository at this point in the history
* 🔨 chore: only load required data

* 🚀 chore: improve manage units loading

* 🔨 chore: improve department whitelisting

* remove unused cache

* 🔨 chore: minor ssr fixes
  • Loading branch information
casperiv0 authored Dec 15, 2022
1 parent 21179d7 commit acf6e3c
Show file tree
Hide file tree
Showing 18 changed files with 404 additions and 270 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Prisma, Rank, RecordType, WhitelistStatus } from "@prisma/client";
import { UsePermissions, Permissions } from "middlewares/UsePermissions";

import type * as APITypes from "@snailycad/types/api";
import { AcceptDeclineType, ACCEPT_DECLINE_TYPES } from "../AdminManageUnitsController";
import { AcceptDeclineType, ACCEPT_DECLINE_TYPES } from "../manage-units-controller";
import { BadRequest, NotFound } from "@tsed/exceptions";

const recordsInclude = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Feature, Rank, WhitelistStatus, cad, CadFeature, MiscCadSettings } from "@prisma/client";
import { UPDATE_UNIT_SCHEMA, UPDATE_UNIT_CALLSIGN_SCHEMA } from "@snailycad/schemas";
import { PathParams, BodyParams, Context } from "@tsed/common";
import { PathParams, BodyParams, Context, QueryParams } from "@tsed/common";
import { Controller } from "@tsed/di";
import { BadRequest, NotFound } from "@tsed/exceptions";
import { UseBeforeEach } from "@tsed/platform-middlewares";
Expand Down Expand Up @@ -51,17 +51,39 @@ export class AdminManageUnitsController {
Permissions.ManageAwardsAndQualifications,
],
})
async getUnits(): Promise<APITypes.GetManageUnitsData> {
const units = await Promise.all([
(
await prisma.officer.findMany({ include: leoProperties })
).map((v) => ({ ...v, type: "OFFICER" as const })),
(
await prisma.emsFdDeputy.findMany({ include: unitProperties })
).map((v) => ({ ...v, type: "DEPUTY" as const })),
async getUnits(
@QueryParams("skip", Number) skip = 0,
@QueryParams("includeAll", Boolean) includeAll = false,
@QueryParams("query", String) query = "",
@QueryParams("pendingOnly", Boolean) pendingOnly = false,
): Promise<APITypes.GetManageUnitsData> {
const [officerCount, _officers] = await prisma.$transaction([
prisma.officer.count({ where: this.createWhere({ query, pendingOnly }, "OFFICER") }),
prisma.officer.findMany({
take: includeAll ? undefined : 35,
skip: includeAll ? undefined : skip,
include: leoProperties,
where: this.createWhere({ query, pendingOnly }, "OFFICER"),
}),
]);

const [emsFdDeputiesCount, _emsFdDeputies] = await prisma.$transaction([
prisma.emsFdDeputy.count({ where: this.createWhere({ query, pendingOnly }, "DEPUTY") }),
prisma.emsFdDeputy.findMany({
take: includeAll ? undefined : 35,
skip: includeAll ? undefined : skip,
include: unitProperties,
where: this.createWhere({ query, pendingOnly }, "DEPUTY"),
}),
]);

return units.flat(1);
const officers = _officers.map((o) => ({ ...o, type: "OFFICER" as const }));
const emsFdDeputies = _emsFdDeputies.map((o) => ({ ...o, type: "DEPUTY" as const }));

return {
units: [...officers, ...emsFdDeputies],
totalCount: officerCount + emsFdDeputiesCount,
};
}

@Get("/:id")
Expand Down Expand Up @@ -385,20 +407,20 @@ export class AdminManageUnitsController {
return { ...updated, deleted: true };
}
case "SET_DEPARTMENT_NULL": {
// @ts-expect-error function has the same properties
const updated = await prisma[prismaName].update({
where: { id: unit.id },
data: { departmentId: null },
include: unitType === "leo" ? leoProperties : unitProperties,
});

if (unit.whitelistStatusId) {
await prisma.leoWhitelistStatus.update({
where: { id: unit.whitelistStatusId },
data: { status: WhitelistStatus.DECLINED },
});
}

// @ts-expect-error function has the same properties
const updated = await prisma[prismaName].update({
where: { id: unit.id },
data: { departmentId: null },
include: unitType === "leo" ? leoProperties : unitProperties,
});

return updated;
}
case "SET_DEPARTMENT_DEFAULT": {
Expand Down Expand Up @@ -579,4 +601,51 @@ export class AdminManageUnitsController {

return true;
}

private createWhere(
{ query, pendingOnly }: { query: string; pendingOnly: boolean },
type: "OFFICER" | "DEPUTY" = "OFFICER",
) {
const [name, surname] = query.toString().toLowerCase().split(/ +/g);

if (!query) {
return pendingOnly
? {
whitelistStatus: { status: WhitelistStatus.PENDING },
}
: {};
}

const where: any = {
...(pendingOnly ? { whitelistStatus: { status: WhitelistStatus.PENDING } } : {}),
OR: [
{ callsign: query },
{ callsign2: query },
{ department: { value: { value: { contains: query, mode: "insensitive" } } } },
{ status: { value: { value: { contains: query, mode: "insensitive" } } } },
{
citizen: {
OR: [
{
name: { contains: name, mode: "insensitive" },
surname: { contains: surname, mode: "insensitive" },
},
{
name: { contains: name, mode: "insensitive" },
surname: { contains: surname, mode: "insensitive" },
},
],
},
},
],
};

if (type === "OFFICER") {
where.OR.push({
divisions: { some: { value: { value: { contains: query, mode: "insensitive" } } } },
});
}

return where;
}
}
10 changes: 9 additions & 1 deletion apps/api/src/controllers/citizen/CitizenController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,15 @@ export class CitizenController {
prisma.citizen.findMany({
where,
orderBy: { updatedAt: "desc" },
include: { user: { select: userProperties } },
select: {
name: true,
surname: true,
imageId: true,
id: true,
userId: true,
socialSecurityNumber: true,
user: { select: userProperties },
},
skip,
take: 35,
}),
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/controllers/leo/DmvController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ContentType, Description, Get, Post } from "@tsed/schema";
import {
AcceptDeclineType,
ACCEPT_DECLINE_TYPES,
} from "controllers/admin/manage/AdminManageUnitsController";
} from "controllers/admin/manage/manage-units-controller";
import { prisma } from "lib/prisma";
import { IsAuth } from "middlewares/IsAuth";
import { UsePermissions, Permissions } from "middlewares/UsePermissions";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ContentType, Description, Get, Post } from "@tsed/schema";
import {
AcceptDeclineType,
ACCEPT_DECLINE_TYPES,
} from "controllers/admin/manage/AdminManageUnitsController";
} from "controllers/admin/manage/manage-units-controller";
import { prisma } from "lib/prisma";
import { IsAuth } from "middlewares/IsAuth";
import { UsePermissions, Permissions } from "middlewares/UsePermissions";
Expand Down

This file was deleted.

Loading

0 comments on commit acf6e3c

Please sign in to comment.