Skip to content

Commit

Permalink
fix: unit inactivity timeouts (#1876)
Browse files Browse the repository at this point in the history
  • Loading branch information
casperiv0 authored Nov 22, 2023
1 parent 93c1395 commit 1280408
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 109 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- AlterTable
ALTER TABLE "CombinedEmsFdUnit" DROP COLUMN "lastStatusChangeTimestamp",
ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;

-- AlterTable
ALTER TABLE "CombinedLeoUnit" DROP COLUMN "lastStatusChangeTimestamp",
ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;

-- AlterTable
ALTER TABLE "EmsFdDeputy" DROP COLUMN "lastStatusChangeTimestamp";

-- AlterTable
ALTER TABLE "MiscCadSettings" DROP COLUMN "inactivityTimeout";

-- AlterTable
ALTER TABLE "Officer" DROP COLUMN "lastStatusChangeTimestamp";
96 changes: 48 additions & 48 deletions apps/api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,6 @@ model Officer {
LeoIncidentInvolvedOfficers LeoIncident[] @relation("involvedOfficers")
combinedLeoUnit CombinedLeoUnit? @relation(fields: [combinedLeoUnitId], references: [id])
combinedLeoUnitId String?
lastStatusChangeTimestamp DateTime?
qualifications UnitQualification[]
IncidentInvolvedUnit IncidentInvolvedUnit[]
Note Note[]
Expand Down Expand Up @@ -1195,57 +1194,59 @@ model IncidentEvent {
}

model CombinedLeoUnit {
id String @id @default(uuid())
officers Officer[]
userDefinedCallsign String?
callsign String
callsign2 String?
department DepartmentValue? @relation(fields: [departmentId], references: [id])
departmentId String?
incremental Int?
radioChannelId String?
status StatusValue? @relation("combinedUnitStatusToValue", fields: [statusId], references: [id])
statusId String?
pairedUnitTemplate String?
activeCall Call911? @relation("CombinedLeoUnitActiveCall", fields: [activeCallId], references: [id])
activeCallId String?
activeIncident LeoIncident? @relation("combinedActiveIncident", fields: [activeIncidentId], references: [id])
activeIncidentId String?
lastStatusChangeTimestamp DateTime?
activeVehicle EmergencyVehicleValue? @relation("activeEmergencyVehicle_combined_unit", fields: [activeVehicleId], references: [id], onDelete: SetNull)
activeVehicleId String?
AssignedUnit AssignedUnit[]
IncidentInvolvedUnit IncidentInvolvedUnit[]
AssignedWarrantOfficer AssignedWarrantOfficer[]
EmsFdIncident EmsFdIncident? @relation(fields: [emsFdIncidentId], references: [id])
emsFdIncidentId String?
ChatCreator ChatCreator[]
id String @id @default(uuid())
officers Officer[]
userDefinedCallsign String?
callsign String
callsign2 String?
department DepartmentValue? @relation(fields: [departmentId], references: [id])
departmentId String?
incremental Int?
radioChannelId String?
status StatusValue? @relation("combinedUnitStatusToValue", fields: [statusId], references: [id])
statusId String?
pairedUnitTemplate String?
activeCall Call911? @relation("CombinedLeoUnitActiveCall", fields: [activeCallId], references: [id])
activeCallId String?
activeIncident LeoIncident? @relation("combinedActiveIncident", fields: [activeIncidentId], references: [id])
activeIncidentId String?
activeVehicle EmergencyVehicleValue? @relation("activeEmergencyVehicle_combined_unit", fields: [activeVehicleId], references: [id], onDelete: SetNull)
activeVehicleId String?
AssignedUnit AssignedUnit[]
IncidentInvolvedUnit IncidentInvolvedUnit[]
AssignedWarrantOfficer AssignedWarrantOfficer[]
EmsFdIncident EmsFdIncident? @relation(fields: [emsFdIncidentId], references: [id])
emsFdIncidentId String?
ChatCreator ChatCreator[]
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
@@index([statusId])
}

model CombinedEmsFdUnit {
id String @id @default(uuid())
deputies EmsFdDeputy[]
callsign String
userDefinedCallsign String?
callsign2 String?
department DepartmentValue? @relation(fields: [departmentId], references: [id])
departmentId String?
incremental Int?
radioChannelId String?
status StatusValue? @relation("combinedEmsFdUnitStatusToValue", fields: [statusId], references: [id])
statusId String?
pairedUnitTemplate String?
activeCall Call911? @relation("CombinedEmsFdUnitActiveCall", fields: [activeCallId], references: [id])
activeCallId String?
activeIncident LeoIncident? @relation("combinedEmsFdActiveIncident", fields: [activeIncidentId], references: [id])
activeIncidentId String?
lastStatusChangeTimestamp DateTime?
activeVehicle EmergencyVehicleValue? @relation("activeEmergencyVehicle_combined_emsfd_unit", fields: [activeVehicleId], references: [id], onDelete: SetNull)
activeVehicleId String?
EmsFdIncident EmsFdIncident? @relation(fields: [emsFdIncidentId], references: [id])
emsFdIncidentId String?
id String @id @default(uuid())
deputies EmsFdDeputy[]
callsign String
userDefinedCallsign String?
callsign2 String?
department DepartmentValue? @relation(fields: [departmentId], references: [id])
departmentId String?
incremental Int?
radioChannelId String?
status StatusValue? @relation("combinedEmsFdUnitStatusToValue", fields: [statusId], references: [id])
statusId String?
pairedUnitTemplate String?
activeCall Call911? @relation("CombinedEmsFdUnitActiveCall", fields: [activeCallId], references: [id])
activeCallId String?
activeIncident LeoIncident? @relation("combinedEmsFdActiveIncident", fields: [activeIncidentId], references: [id])
activeIncidentId String?
activeVehicle EmergencyVehicleValue? @relation("activeEmergencyVehicle_combined_emsfd_unit", fields: [activeVehicleId], references: [id], onDelete: SetNull)
activeVehicleId String?
EmsFdIncident EmsFdIncident? @relation(fields: [emsFdIncidentId], references: [id])
emsFdIncidentId String?
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
AssignedUnit AssignedUnit[]
IncidentInvolvedUnit IncidentInvolvedUnit[]
Expand Down Expand Up @@ -1596,7 +1597,6 @@ model EmsFdDeputy {
whitelistStatusId String?
logs OfficerLog[]
AssignedUnit AssignedUnit[]
lastStatusChangeTimestamp DateTime?
qualifications UnitQualification[]
IncidentInvolvedUnit IncidentInvolvedUnit[]
activeVehicle EmergencyVehicleValue? @relation("activeEmergencyVehicle_emsfd", fields: [activeVehicleId], references: [id], onDelete: SetNull)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ export class AdminManageUnitsController {
@QueryParams("days", Number) days = 2,
) {
const where = {
lastStatusChangeTimestamp: {
not: null,
updatedAt: {
lte: new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * days),
},
departmentId,
Expand All @@ -143,12 +142,12 @@ export class AdminManageUnitsController {
prisma.officer.findMany({
where,
include: _leoProperties,
orderBy: { lastStatusChangeTimestamp: "desc" },
orderBy: { updatedAt: "desc" },
}),
prisma.emsFdDeputy.findMany({
where,
include: unitProperties,
orderBy: { lastStatusChangeTimestamp: "desc" },
orderBy: { updatedAt: "desc" },
}),
]);

Expand Down Expand Up @@ -206,7 +205,7 @@ export class AdminManageUnitsController {
return prisma[prismaName].deleteMany({
where: {
id: unitId,
lastStatusChangeTimestamp: {
updatedAt: {
lte: new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * days),
},
},
Expand Down
6 changes: 3 additions & 3 deletions apps/api/src/controllers/citizen/CitizenController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const citizenInclude = Prisma.validator<Prisma.CitizenInclude>()({
suspendedLicenses: true,
licensePoints: true,
vehicles: {
orderBy: { createdAt: "desc" },
orderBy: { createdAt: Prisma.SortOrder.desc },
include: {
trimLevels: true,
flags: true,
Expand All @@ -60,15 +60,15 @@ export const citizenInclude = Prisma.validator<Prisma.CitizenInclude>()({
skip: 0,
},
weapons: {
orderBy: { createdAt: "desc" },
orderBy: { createdAt: Prisma.SortOrder.desc },
take: 12,
skip: 0,
include: {
model: { include: { value: true } },
registrationStatus: true,
},
},
medicalRecords: { include: { bloodGroup: true }, orderBy: { createdAt: "desc" } },
medicalRecords: { include: { bloodGroup: true }, orderBy: { createdAt: Prisma.SortOrder.desc } },
ethnicity: true,
gender: true,
weaponLicense: true,
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/controllers/dispatch/dispatch-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class DispatchController {
}),
include: combinedUnitProperties,
take: 25,
orderBy: { lastStatusChangeTimestamp: "desc" },
orderBy: { updatedAt: "desc" },
});

const combinedEmsFdDeputies = await prisma.combinedEmsFdUnit.findMany({
Expand All @@ -129,7 +129,7 @@ export class DispatchController {
}),
include: combinedEmsFdUnitProperties,
take: 25,
orderBy: { lastStatusChangeTimestamp: "desc" },
orderBy: { updatedAt: "desc" },
});

return [...officers, ...deputies, ...combinedOfficers, ...combinedEmsFdDeputies];
Expand Down
4 changes: 0 additions & 4 deletions apps/api/src/controllers/dispatch/status-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ export class StatusController {
activeVehicleId: activeEmergencyVehicleId,
statusId,
incremental,
lastStatusChangeTimestamp: new Date(),
},
include: leoProperties,
});
Expand All @@ -277,7 +276,6 @@ export class StatusController {
activeVehicleId: activeEmergencyVehicleId,
statusId,
incremental,
lastStatusChangeTimestamp: new Date(),
},
include: unitProperties,
});
Expand All @@ -288,7 +286,6 @@ export class StatusController {
userDefinedCallsign,
activeVehicleId: activeEmergencyVehicleId,
statusId,
lastStatusChangeTimestamp: new Date(),
},
include: combinedUnitProperties,
});
Expand All @@ -299,7 +296,6 @@ export class StatusController {
userDefinedCallsign,
activeVehicleId: activeEmergencyVehicleId,
statusId,
lastStatusChangeTimestamp: new Date(),
},
include: combinedEmsFdUnitProperties,
});
Expand Down
8 changes: 2 additions & 6 deletions apps/api/src/controllers/ems-fd/ems-fd-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,7 @@ export class EmsFdController {
@QueryParams("skip", Number) skip = 0,
@QueryParams("query", String) query?: string,
): Promise<APITypes.GetEmsFdActiveDeputies> {
const unitsInactivityFilter = getInactivityFilter(
cad,
"unitInactivityTimeout",
"lastStatusChangeTimestamp",
);
const unitsInactivityFilter = getInactivityFilter(cad, "unitInactivityTimeout");

const activeDispatcher = await prisma.activeDispatchers.findFirst({
where: { userId: user.id },
Expand All @@ -239,7 +235,7 @@ export class EmsFdController {
}),
prisma.combinedEmsFdUnit.findMany({
include: combinedEmsFdUnitProperties,
orderBy: { lastStatusChangeTimestamp: "desc" },
orderBy: { updatedAt: "desc" },
where: {
...unitsInactivityFilter?.filter,
departmentId: activeDispatcher?.departmentId || undefined,
Expand Down
18 changes: 11 additions & 7 deletions apps/api/src/controllers/leo/LeoController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ export class LeoController {
@QueryParams("skip", Number) skip = 0,
@QueryParams("query", String) query?: string,
): Promise<APITypes.GetActiveOfficersData> {
const unitsInactivityFilter = getInactivityFilter(
cad,
"unitInactivityTimeout",
"lastStatusChangeTimestamp",
);
const unitsInactivityFilter = getInactivityFilter(cad, "unitInactivityTimeout");

const activeDispatcher = await prisma.activeDispatchers.findFirst({
where: { userId: user.id },
Expand All @@ -86,14 +82,22 @@ export class LeoController {
}),
prisma.combinedLeoUnit.findMany({
include: combinedUnitProperties,
orderBy: { lastStatusChangeTimestamp: "desc" },
orderBy: { updatedAt: "desc" },
where: {
...unitsInactivityFilter?.filter,
status: { NOT: { shouldDo: ShouldDoType.SET_OFF_DUTY } },
...(unitsInactivityFilter?.filter ?? {}),

departmentId: activeDispatcher?.departmentId || undefined,
},
}),
]);

console.log({
fl: unitsInactivityFilter?.filter,
officers,
combinedUnits,
});

return [...combinedUnits, ...officers];
}

Expand Down
8 changes: 2 additions & 6 deletions apps/api/src/lib/get-active-ems-fd-deputy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ export async function getActiveDeputy(options: GetActiveDeputyOptions) {
});

const cad = await prisma.cad.findFirst({ include: { miscCadSettings: true } });
const unitsInactivityFilter = getInactivityFilter(
cad!,
"unitInactivityTimeout",
"lastStatusChangeTimestamp",
);
const unitsInactivityFilter = getInactivityFilter(cad!, "unitInactivityTimeout");

const filters: Prisma.Enumerable<Prisma.EmsFdDeputyWhereInput> = [
{ status: { shouldDo: ShouldDoType.SET_OFF_DUTY } },
Expand All @@ -76,7 +72,7 @@ export async function getActiveDeputy(options: GetActiveDeputyOptions) {

if (unitsInactivityFilter) {
filters.push({
lastStatusChangeTimestamp: { lte: unitsInactivityFilter.lastStatusChangeTimestamp },
updatedAt: { lte: unitsInactivityFilter.updatedAt },
});
}

Expand Down
8 changes: 2 additions & 6 deletions apps/api/src/lib/leo/activeOfficer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ export async function getActiveOfficer(options: GetActiveOfficerOptions) {
});

const cad = await prisma.cad.findFirst({ include: { miscCadSettings: true } });
const unitsInactivityFilter = getInactivityFilter(
cad!,
"unitInactivityTimeout",
"lastStatusChangeTimestamp",
);
const unitsInactivityFilter = getInactivityFilter(cad!, "unitInactivityTimeout");

const filters: Prisma.Enumerable<Prisma.OfficerWhereInput> = [
{ status: { shouldDo: "SET_OFF_DUTY" } },
Expand All @@ -76,7 +72,7 @@ export async function getActiveOfficer(options: GetActiveOfficerOptions) {

if (unitsInactivityFilter) {
filters.push({
lastStatusChangeTimestamp: { lte: unitsInactivityFilter.lastStatusChangeTimestamp },
updatedAt: { lte: unitsInactivityFilter.updatedAt },
});
}

Expand Down
11 changes: 4 additions & 7 deletions apps/api/src/lib/leo/setInactiveUnitsOffDuty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { prisma } from "lib/data/prisma";
import type { Socket } from "services/socket-service";
import { handleStartEndOfficerLog } from "./handleStartEndOfficerLog";

export async function setInactiveUnitsOffDuty(lastStatusChangeTimestamp: Date, socket: Socket) {
export async function setInactiveUnitsOffDuty(updatedAt: Date, socket: Socket) {
try {
const where = {
status: { shouldDo: { not: ShouldDoType.SET_OFF_DUTY } },
lastStatusChangeTimestamp: { lte: lastStatusChangeTimestamp },
updatedAt: { not: { gte: updatedAt } },
};

const [officers, deputies] = await prisma.$transaction([
Expand Down Expand Up @@ -68,14 +68,11 @@ export function filterInactiveUnits<Unit extends Officer | EmsFdDeputy | Combine
unit: Unit;
unitsInactivityFilter: any;
}) {
if (!unit.lastStatusChangeTimestamp || !unitsInactivityFilter?.lastStatusChangeTimestamp) {
if (!unit.updatedAt || !unitsInactivityFilter?.updatedAt) {
return unit;
}

if (
unit.lastStatusChangeTimestamp.getTime() <=
unitsInactivityFilter?.lastStatusChangeTimestamp.getTime()
) {
if (unit.updatedAt.getTime() <= unitsInactivityFilter?.updatedAt.getTime()) {
return {
...unit,
statusId: null,
Expand Down
Loading

0 comments on commit 1280408

Please sign in to comment.