126 lines
4.8 KiB
TypeScript
126 lines
4.8 KiB
TypeScript
import { Injectable, OnModuleInit, OnModuleDestroy } from "@nestjs/common";
|
|
import { PrismaClient } from "@prisma/client";
|
|
|
|
import { Logger } from "@nestjs/common";
|
|
|
|
@Injectable()
|
|
export class PrismaService
|
|
extends PrismaClient
|
|
implements OnModuleInit, OnModuleDestroy
|
|
{
|
|
private readonly logger = new Logger(PrismaService.name);
|
|
|
|
constructor() {
|
|
super({
|
|
log: [
|
|
{ emit: "event", level: "query" },
|
|
{ emit: "stdout", level: "error" },
|
|
{ emit: "stdout", level: "warn" },
|
|
],
|
|
});
|
|
}
|
|
|
|
async onModuleInit() {
|
|
await this.$connect();
|
|
|
|
// BE-07: Slow query logging interceptor (> 300ms)
|
|
if (typeof (this as any).$on === "function") {
|
|
(this as any).$on("query", (e: any) => {
|
|
if (e.duration >= 300) {
|
|
this.logger.warn(
|
|
`[SlowQueryAlert] Duration: ${e.duration}ms | Target: ${e.target} | Query: ${e.query.substring(0, 300)}...`,
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
await this.ensureTablesExist();
|
|
}
|
|
|
|
private async ensureTablesExist() {
|
|
const ddlStatements = [
|
|
// 1. Tickets Table
|
|
`CREATE TABLE IF NOT EXISTS "tickets" (
|
|
"id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
"ticket_number" VARCHAR(50) NOT NULL UNIQUE,
|
|
"user_id" UUID NOT NULL REFERENCES "users"("id") ON DELETE CASCADE,
|
|
"pet_id" UUID REFERENCES "pets"("id") ON DELETE SET NULL,
|
|
"subject" VARCHAR(255) NOT NULL,
|
|
"category" VARCHAR(50) NOT NULL DEFAULT 'VET_CONSULTATION',
|
|
"priority" VARCHAR(20) NOT NULL DEFAULT 'MEDIUM',
|
|
"status" VARCHAR(30) NOT NULL DEFAULT 'OPEN',
|
|
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
)`,
|
|
`CREATE INDEX IF NOT EXISTS "tickets_user_id_idx" ON "tickets"("user_id")`,
|
|
`CREATE INDEX IF NOT EXISTS "tickets_status_idx" ON "tickets"("status")`,
|
|
`CREATE INDEX IF NOT EXISTS "tickets_category_idx" ON "tickets"("category")`,
|
|
`CREATE INDEX IF NOT EXISTS "tickets_created_at_idx" ON "tickets"("created_at")`,
|
|
|
|
// 2. Ticket Messages Table
|
|
`CREATE TABLE IF NOT EXISTS "ticket_messages" (
|
|
"id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
"ticket_id" UUID NOT NULL REFERENCES "tickets"("id") ON DELETE CASCADE,
|
|
"sender_id" UUID,
|
|
"sender_role" VARCHAR(30) NOT NULL DEFAULT 'USER',
|
|
"sender_name" VARCHAR(100) NOT NULL,
|
|
"message" TEXT NOT NULL,
|
|
"attachment" TEXT,
|
|
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
)`,
|
|
`CREATE INDEX IF NOT EXISTS "ticket_messages_ticket_id_idx" ON "ticket_messages"("ticket_id")`,
|
|
`CREATE INDEX IF NOT EXISTS "ticket_messages_created_at_idx" ON "ticket_messages"("created_at")`,
|
|
|
|
// 3. SMS Logs Table
|
|
`CREATE TABLE IF NOT EXISTS "sms_logs" (
|
|
"id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
"receptor" VARCHAR(20) NOT NULL,
|
|
"type" VARCHAR(50) NOT NULL DEFAULT 'GENERIC',
|
|
"pattern_id" INTEGER,
|
|
"args" TEXT[] DEFAULT ARRAY[]::TEXT[],
|
|
"message_text" TEXT,
|
|
"status" VARCHAR(20) NOT NULL DEFAULT 'SUCCESS',
|
|
"rec_id" VARCHAR(50),
|
|
"error_message" TEXT,
|
|
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
)`,
|
|
`CREATE INDEX IF NOT EXISTS "sms_logs_receptor_idx" ON "sms_logs"("receptor")`,
|
|
`CREATE INDEX IF NOT EXISTS "sms_logs_status_idx" ON "sms_logs"("status")`,
|
|
`CREATE INDEX IF NOT EXISTS "sms_logs_created_at_idx" ON "sms_logs"("created_at")`,
|
|
|
|
// 4. Product Reviews Table
|
|
`CREATE TABLE IF NOT EXISTS "product_reviews" (
|
|
"id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
"product_id" UUID NOT NULL REFERENCES "products"("id") ON DELETE CASCADE,
|
|
"user_id" UUID REFERENCES "users"("id") ON DELETE SET NULL,
|
|
"user_name" VARCHAR(100) NOT NULL,
|
|
"user_phone" VARCHAR(20),
|
|
"rating" INTEGER NOT NULL DEFAULT 5,
|
|
"comment" TEXT NOT NULL,
|
|
"status" VARCHAR(20) NOT NULL DEFAULT 'PENDING',
|
|
"admin_reply" TEXT,
|
|
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
)`,
|
|
`CREATE INDEX IF NOT EXISTS "product_reviews_product_id_idx" ON "product_reviews"("product_id")`,
|
|
`CREATE INDEX IF NOT EXISTS "product_reviews_status_idx" ON "product_reviews"("status")`,
|
|
`CREATE INDEX IF NOT EXISTS "product_reviews_created_at_idx" ON "product_reviews"("created_at")`,
|
|
];
|
|
|
|
for (const sql of ddlStatements) {
|
|
try {
|
|
await this.$executeRawUnsafe(sql);
|
|
} catch (err: any) {
|
|
console.warn(
|
|
`[PrismaService] DDL execution note for (${sql.substring(0, 40)}...):`,
|
|
err?.message || err,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
async onModuleDestroy() {
|
|
await this.$disconnect();
|
|
}
|
|
}
|