-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
806 additions
and
34 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
packages/api/prisma/migrations/20220103114738_/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
-- CreateEnum | ||
CREATE TYPE "ExpungementRequestStatus" AS ENUM ('ACCEPTED', 'DENIED', 'PENDING'); | ||
|
||
-- AlterTable | ||
ALTER TABLE "Record" ADD COLUMN "expungementRequestId" TEXT; | ||
|
||
-- AlterTable | ||
ALTER TABLE "RecordLog" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP; | ||
|
||
-- AlterTable | ||
ALTER TABLE "Warrant" ADD COLUMN "expungementRequestId" TEXT; | ||
|
||
-- CreateTable | ||
CREATE TABLE "ExpungementRequest" ( | ||
"id" TEXT NOT NULL, | ||
"citizenId" TEXT NOT NULL, | ||
"userId" TEXT NOT NULL, | ||
"status" "ExpungementRequestStatus" NOT NULL DEFAULT E'PENDING', | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
||
CONSTRAINT "ExpungementRequest_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Record" ADD CONSTRAINT "Record_expungementRequestId_fkey" FOREIGN KEY ("expungementRequestId") REFERENCES "ExpungementRequest"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Warrant" ADD CONSTRAINT "Warrant_expungementRequestId_fkey" FOREIGN KEY ("expungementRequestId") REFERENCES "ExpungementRequest"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "ExpungementRequest" ADD CONSTRAINT "ExpungementRequest_citizenId_fkey" FOREIGN KEY ("citizenId") REFERENCES "Citizen"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "ExpungementRequest" ADD CONSTRAINT "ExpungementRequest_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { ExpungementRequestStatus } from "@prisma/client"; | ||
import { Controller } from "@tsed/di"; | ||
import { BadRequest, NotFound } from "@tsed/exceptions"; | ||
import { UseBeforeEach } from "@tsed/platform-middlewares"; | ||
import { BodyParams, PathParams } from "@tsed/platform-params"; | ||
import { Get, Put } from "@tsed/schema"; | ||
import { expungementRequestInclude } from "controllers/court/CourtController"; | ||
import { prisma } from "lib/prisma"; | ||
import { IsAuth } from "middlewares/index"; | ||
|
||
@UseBeforeEach(IsAuth) | ||
@Controller("/admin/manage/expungement-requests") | ||
export class ManageCourthouseController { | ||
@Get("/") | ||
async getRequests() { | ||
const requests = await prisma.expungementRequest.findMany({ | ||
include: expungementRequestInclude, | ||
}); | ||
|
||
return requests; | ||
} | ||
|
||
@Put("/:id") | ||
async updateExpungementRequest( | ||
@PathParams("id") id: string, | ||
@BodyParams("type") type: ExpungementRequestStatus, | ||
) { | ||
const isCorrect = Object.values(ExpungementRequestStatus).some((v) => v === type); | ||
|
||
if (!isCorrect) { | ||
throw new BadRequest("invalidType"); | ||
} | ||
|
||
const request = await prisma.expungementRequest.findUnique({ | ||
where: { id }, | ||
include: expungementRequestInclude, | ||
}); | ||
|
||
if (!request) { | ||
throw new NotFound("requestNotFound"); | ||
} | ||
|
||
if (type === ExpungementRequestStatus.ACCEPTED) { | ||
await Promise.all( | ||
request.warrants?.map(async (warrant) => { | ||
await prisma.warrant.delete({ | ||
where: { id: warrant.id }, | ||
}); | ||
}), | ||
); | ||
|
||
await Promise.all( | ||
request.records?.map(async (record) => { | ||
await prisma.record.delete({ | ||
where: { id: record.id }, | ||
}); | ||
}), | ||
); | ||
} | ||
|
||
const updated = await prisma.expungementRequest.update({ | ||
where: { id }, | ||
data: { | ||
status: type, | ||
}, | ||
}); | ||
|
||
return updated; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { User } from "@prisma/client"; | ||
import { BodyParams, Context, PathParams, UseBeforeEach } from "@tsed/common"; | ||
import { Controller } from "@tsed/di"; | ||
import { BadRequest, NotFound } from "@tsed/exceptions"; | ||
import { Get, JsonRequestBody, Post } from "@tsed/schema"; | ||
import { citizenInclude } from "controllers/citizen/CitizenController"; | ||
import { prisma } from "lib/prisma"; | ||
import { IsAuth } from "middlewares/IsAuth"; | ||
|
||
export const expungementRequestInclude = { | ||
citizen: true, | ||
warrants: true, | ||
records: { include: { violations: { include: { penalCode: true } } } }, | ||
}; | ||
|
||
@Controller("/expungement-requests") | ||
@UseBeforeEach(IsAuth) | ||
export class CourtController { | ||
@Get("/") | ||
async getRequestPerUser(@Context("user") user: User) { | ||
const requests = await prisma.expungementRequest.findMany({ | ||
where: { | ||
userId: user.id, | ||
}, | ||
include: expungementRequestInclude, | ||
}); | ||
|
||
return requests; | ||
} | ||
|
||
@Get("/:citizenId") | ||
async getCitizensRecords( | ||
@Context("user") user: User, | ||
@PathParams("citizenId") citizenId: string, | ||
) { | ||
const citizen = await prisma.citizen.findFirst({ | ||
where: { id: citizenId, userId: user.id }, | ||
include: { ...citizenInclude, warrants: true }, | ||
}); | ||
|
||
if (!citizen) { | ||
throw new NotFound("citizenNotFound"); | ||
} | ||
|
||
return citizen; | ||
} | ||
|
||
@Post("/:citizenId") | ||
async requestExpungement( | ||
@Context("user") user: User, | ||
@PathParams("citizenId") citizenId: string, | ||
@BodyParams() body: JsonRequestBody, | ||
) { | ||
const citizen = await prisma.citizen.findFirst({ | ||
where: { id: citizenId, userId: user.id }, | ||
}); | ||
|
||
if (!citizen) { | ||
throw new NotFound("citizenNotFound"); | ||
} | ||
|
||
const request = await prisma.expungementRequest.create({ | ||
data: { | ||
citizenId: citizen.id, | ||
userId: user.id, | ||
}, | ||
include: expungementRequestInclude, | ||
}); | ||
|
||
const warrants = body.get("warrants") as string[]; | ||
const arrestReports = body.get("arrestReports") as string[]; | ||
const tickets = body.get("tickets") as string[]; | ||
|
||
if (arrestReports.length <= 0 && tickets.length <= 0 && warrants.length <= 0) { | ||
throw new BadRequest("mustSpecifyMinOneArray"); | ||
} | ||
|
||
const updatedRecords = await Promise.all( | ||
[...arrestReports, ...tickets].map(async (id) => { | ||
const existing = await prisma.expungementRequest.findFirst({ | ||
where: { records: { some: { id } }, status: "PENDING" }, | ||
}); | ||
|
||
if (existing) { | ||
return error(new BadRequest("recordOrWarrantAlreadyLinked"), request.id); | ||
} | ||
|
||
return prisma.expungementRequest.update({ | ||
where: { id: request.id }, | ||
data: { | ||
records: { connect: { id } }, | ||
}, | ||
}); | ||
}), | ||
); | ||
|
||
const updatedWarrants = await Promise.all( | ||
warrants.map(async (id) => { | ||
const existing = await prisma.expungementRequest.findFirst({ | ||
where: { warrants: { some: { id } }, status: "PENDING" }, | ||
}); | ||
|
||
if (existing) { | ||
return error(new BadRequest("recordOrWarrantAlreadyLinked"), request.id); | ||
} | ||
|
||
return prisma.expungementRequest.update({ | ||
where: { id: request.id }, | ||
data: { | ||
warrants: { connect: { id } }, | ||
}, | ||
}); | ||
}), | ||
); | ||
|
||
return { ...request, warrants: updatedWarrants, records: updatedRecords }; | ||
} | ||
} | ||
|
||
async function error<T extends Error = Error>(error: T, id: string) { | ||
await prisma.expungementRequest.delete({ | ||
where: { id }, | ||
}); | ||
|
||
throw error; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.