Skip to content

Commit

Permalink
feat: draft records (#1886)
Browse files Browse the repository at this point in the history
  • Loading branch information
casperiv0 authored Dec 31, 2023
1 parent f3f7172 commit 93e9a15
Show file tree
Hide file tree
Showing 13 changed files with 357 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- CreateEnum
CREATE TYPE "PublishStatus" AS ENUM ('DRAFT', 'PUBLISHED');

-- AlterTable
ALTER TABLE "Record" ADD COLUMN "publishStatus" "PublishStatus" NOT NULL DEFAULT 'DRAFT';
6 changes: 6 additions & 0 deletions apps/api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,7 @@ model Bolo {
// tickets, arrest reports, warrants, written warnings
model Record {
id String @id @default(uuid())
publishStatus PublishStatus @default(DRAFT)
caseNumber Int @default(autoincrement())
type RecordType
citizen Citizen? @relation(fields: [citizenId], references: [id], onDelete: Cascade)
Expand Down Expand Up @@ -1724,6 +1725,11 @@ model ActiveTone {
createdById String
}

enum PublishStatus {
DRAFT
PUBLISHED
}

enum ActiveToneType {
LEO
EMS_FD
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/controllers/leo/search/SearchController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export const vehicleSearchInclude = {
export const recordsInclude = (isRecordApprovalEnabled: boolean) => ({
where: isRecordApprovalEnabled ? { status: WhitelistStatus.ACCEPTED } : undefined,
include: {
citizen: true,
business: { select: { name: true, id: true } },
officer: { include: leoProperties },
seizedItems: true,
courtEntry: { include: { dates: true } },
Expand Down
42 changes: 42 additions & 0 deletions apps/api/src/controllers/record/records-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
type User,
type Business,
PaymentStatus,
type RecordType,
} from "@prisma/client";
import { validateSchema } from "lib/data/validate-schema";
import { combinedUnitProperties, leoProperties } from "utils/leo/includes";
Expand Down Expand Up @@ -68,6 +69,47 @@ export class RecordsController {
this.socket = socket;
}

@Get("/drafts")
@Description("Get draft records that a user created")
async getUserDraftRecords(
@Context("user") user: User,
@Context("cad") cad: cad & { features: Record<Feature, boolean> },
@QueryParams("type") type: string,
) {
const isEnabled = isFeatureEnabled({
feature: Feature.CITIZEN_RECORD_APPROVAL,
features: cad.features,
defaultReturn: false,
});

const draftRecords = await prisma.record.findMany({
// todo: visualize in the FE
take: 12,
where: {
publishStatus: "DRAFT",
officer: { userId: user.id },
type: type as RecordType,
},
include: recordsInclude(isEnabled).include,
});

return draftRecords as APITypes.GetCitizenByIdRecordsData;
}

@Delete("/drafts/:id")
async deleteDraftRecordById(@Context("user") user: User, @PathParams("id") id: string) {
const record = await prisma.record
.delete({
where: {
id,
officer: { userId: user.id },
},
})
.catch(() => null);

return !!record;
}

@Get("/active-warrants")
@Description("Get all active warrants (ACTIVE_WARRANTS must be enabled)")
@IsFeatureEnabled({ feature: Feature.ACTIVE_WARRANTS })
Expand Down
31 changes: 18 additions & 13 deletions apps/api/src/lib/leo/records/upsert-record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type CourtEntry,
type SeizedItem,
type Violation,
PublishStatus,
} from "@prisma/client";
import type { CREATE_TICKET_SCHEMA, CREATE_TICKET_SCHEMA_BUSINESS } from "@snailycad/schemas";
import { type PaymentStatus, type RecordType, WhitelistStatus } from "@snailycad/types";
Expand All @@ -27,19 +28,6 @@ interface UpsertRecordOptions {
}

export async function upsertRecord(options: UpsertRecordOptions) {
if (options.recordId) {
const record = await prisma.record.findUnique({
where: { id: options.recordId },
include: { violations: true, seizedItems: true },
});

if (!record) {
throw new NotFound("notFound");
}

await Promise.all([unlinkViolations(record.violations), unlinkSeizedItems(record.seizedItems)]);
}

let citizen;
let business;

Expand Down Expand Up @@ -104,6 +92,8 @@ export async function upsertRecord(options: UpsertRecordOptions) {
call911Id: options.data.call911Id || null,
incidentId: options.data.incidentId || null,
descriptionData: options.data.descriptionData || undefined,
publishStatus:
(options.data.publishStatus as PublishStatus | null) ?? PublishStatus.PUBLISHED,
},
update: {
notes: options.data.notes,
Expand All @@ -120,6 +110,8 @@ export async function upsertRecord(options: UpsertRecordOptions) {
vehicleSpeed: options.data.vehicleSpeed || null,
vehiclePaceType: options.data.vehiclePaceType || null,
speedLimit: options.data.speedLimit || null,
publishStatus:
(options.data.publishStatus as PublishStatus | null) ?? PublishStatus.PUBLISHED,
},
include: {
officer: { include: leoProperties },
Expand Down Expand Up @@ -192,6 +184,19 @@ export async function upsertRecord(options: UpsertRecordOptions) {
throw new ExtendedBadRequest(errors);
}

if (options.recordId) {
const record = await prisma.record.findUnique({
where: { id: options.recordId },
include: { violations: true, seizedItems: true },
});

if (!record) {
throw new NotFound("notFound");
}

await Promise.all([unlinkViolations(record.violations), unlinkSeizedItems(record.seizedItems)]);
}

const violations = await prisma.$transaction(
fullFilledValidatedViolations.map((item) => {
return prisma.violation.create({
Expand Down
5 changes: 4 additions & 1 deletion apps/client/locales/en/leo.json
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,10 @@
"counts": "Counts",
"stats": "Stats",
"deletedPenalCode": "Deleted Penal Code",
"exportCriminalRecord": "Export Criminal Record"
"exportCriminalRecord": "Export Criminal Record",
"savedAsDraft": "Successfully saved this record as a draft.",
"drafts": "Drafts",
"publishDraft": "Publish Draft"
},
"Bolos": {
"activeBolos": "Active Bolos",
Expand Down
Loading

0 comments on commit 93e9a15

Please sign in to comment.