import { Controller, Get, Param, Post, Inject, NotFoundException } from "@nestjs/common"; import { eq, desc } from "drizzle-orm"; import { schema, type Database } from "@courtmitra/db"; import { DB } from "../db/db.module.js"; @Controller() export class ComplianceController { constructor(@Inject(DB) private readonly db: Database) {} @Get("takedowns") async listTakedowns() { return this.db.select().from(schema.takedownRequests).orderBy(desc(schema.takedownRequests.createdAt)); } @Get("takedowns/:id") async getTakedown(@Param("id") id: string) { const [row] = await this.db .select() .from(schema.takedownRequests) .where(eq(schema.takedownRequests.id, id)) .limit(1); if (!row) throw new NotFoundException(`Takedown request ${id} not found`); return row; } @Post("takedowns/:id/acknowledge") async acknowledge(@Param("id") id: string) { const [row] = await this.db .select() .from(schema.takedownRequests) .where(eq(schema.takedownRequests.id, id)) .limit(1); if (!row) throw new NotFoundException(`Takedown request ${id} not found`); await this.db .update(schema.takedownRequests) .set({ status: "acknowledged", updatedAt: new Date() }) .where(eq(schema.takedownRequests.id, id)); return { success: true, status: "acknowledged" }; } @Post("takedowns/:id/comply") async comply(@Param("id") id: string) { const [row] = await this.db .select() .from(schema.takedownRequests) .where(eq(schema.takedownRequests.id, id)) .limit(1); if (!row) throw new NotFoundException(`Takedown request ${id} not found`); await this.db.transaction(async (tx) => { await tx .update(schema.takedownRequests) .set({ status: "complied", compliedAt: new Date(), updatedAt: new Date() }) .where(eq(schema.takedownRequests.id, id)); await tx .update(schema.actions) .set({ proofState: "receipt_verified", status: "succeeded", updatedAt: new Date() }) .where(eq(schema.actions.id, row.actionId)); await tx.insert(schema.missionEvents).values({ missionId: row.missionId, eventType: "action_executed", actorType: "responder", payload: { actionId: row.actionId, takedownRequestId: id, event: "intermediary_complied", }, }); }); return { success: true, status: "complied" }; } }