feat: add Prisma schema and PrismaService with integration tests (postgres on :5433)
This commit is contained in:
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
# Database
|
# Database
|
||||||
DATABASE_URL=postgresql://tower:tower_dev@localhost:5432/tower_dev
|
DATABASE_URL=postgresql://tower:tower_dev@localhost:5433/tower_dev
|
||||||
|
|
||||||
# Redis
|
# Redis
|
||||||
REDIS_URL=redis://localhost:6379
|
REDIS_URL=redis://localhost:6379
|
||||||
|
|||||||
@@ -0,0 +1,102 @@
|
|||||||
|
-- CreateEnum
|
||||||
|
CREATE TYPE "MessageStatus" AS ENUM ('PENDING', 'APPROVED', 'REJECTED', 'DISTRIBUTED', 'ARCHIVED');
|
||||||
|
|
||||||
|
-- CreateEnum
|
||||||
|
CREATE TYPE "ApprovalDecision" AS ENUM ('APPROVED', 'REJECTED');
|
||||||
|
|
||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE "Group" (
|
||||||
|
"id" TEXT NOT NULL,
|
||||||
|
"platform" TEXT NOT NULL,
|
||||||
|
"platformId" TEXT NOT NULL,
|
||||||
|
"name" TEXT NOT NULL,
|
||||||
|
"description" TEXT,
|
||||||
|
"isActive" BOOLEAN NOT NULL DEFAULT true,
|
||||||
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||||
|
|
||||||
|
CONSTRAINT "Group_pkey" PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
|
|
||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE "Message" (
|
||||||
|
"id" TEXT NOT NULL,
|
||||||
|
"platform" TEXT NOT NULL,
|
||||||
|
"platformMsgId" TEXT NOT NULL,
|
||||||
|
"sourceGroupId" TEXT NOT NULL,
|
||||||
|
"senderJid" TEXT NOT NULL,
|
||||||
|
"senderName" TEXT,
|
||||||
|
"content" TEXT NOT NULL,
|
||||||
|
"mediaUrl" TEXT,
|
||||||
|
"tags" TEXT[],
|
||||||
|
"status" "MessageStatus" NOT NULL DEFAULT 'PENDING',
|
||||||
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||||
|
|
||||||
|
CONSTRAINT "Message_pkey" PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
|
|
||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE "Approval" (
|
||||||
|
"id" TEXT NOT NULL,
|
||||||
|
"messageId" TEXT NOT NULL,
|
||||||
|
"adminId" TEXT NOT NULL,
|
||||||
|
"decision" "ApprovalDecision" NOT NULL,
|
||||||
|
"notes" TEXT,
|
||||||
|
"decidedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
|
||||||
|
CONSTRAINT "Approval_pkey" PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
|
|
||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE "SyncRoute" (
|
||||||
|
"id" TEXT NOT NULL,
|
||||||
|
"sourceGroupId" TEXT NOT NULL,
|
||||||
|
"targetGroupId" TEXT NOT NULL,
|
||||||
|
"isActive" BOOLEAN NOT NULL DEFAULT true,
|
||||||
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
|
||||||
|
CONSTRAINT "SyncRoute_pkey" PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
|
|
||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE "ConsentRecord" (
|
||||||
|
"id" TEXT NOT NULL,
|
||||||
|
"groupId" TEXT NOT NULL,
|
||||||
|
"memberJid" TEXT NOT NULL,
|
||||||
|
"consentType" TEXT NOT NULL,
|
||||||
|
"grantedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"revokedAt" TIMESTAMP(3),
|
||||||
|
|
||||||
|
CONSTRAINT "ConsentRecord_pkey" PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
|
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE UNIQUE INDEX "Group_platform_platformId_key" ON "Group"("platform", "platformId");
|
||||||
|
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE UNIQUE INDEX "Message_platform_platformMsgId_key" ON "Message"("platform", "platformMsgId");
|
||||||
|
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE UNIQUE INDEX "Approval_messageId_key" ON "Approval"("messageId");
|
||||||
|
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE UNIQUE INDEX "SyncRoute_sourceGroupId_targetGroupId_key" ON "SyncRoute"("sourceGroupId", "targetGroupId");
|
||||||
|
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE UNIQUE INDEX "ConsentRecord_groupId_memberJid_consentType_key" ON "ConsentRecord"("groupId", "memberJid", "consentType");
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "Message" ADD CONSTRAINT "Message_sourceGroupId_fkey" FOREIGN KEY ("sourceGroupId") REFERENCES "Group"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "Approval" ADD CONSTRAINT "Approval_messageId_fkey" FOREIGN KEY ("messageId") REFERENCES "Message"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "SyncRoute" ADD CONSTRAINT "SyncRoute_sourceGroupId_fkey" FOREIGN KEY ("sourceGroupId") REFERENCES "Group"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "SyncRoute" ADD CONSTRAINT "SyncRoute_targetGroupId_fkey" FOREIGN KEY ("targetGroupId") REFERENCES "Group"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "ConsentRecord" ADD CONSTRAINT "ConsentRecord_groupId_fkey" FOREIGN KEY ("groupId") REFERENCES "Group"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# Please do not edit this file manually
|
||||||
|
# It should be added in your version-control system (e.g., Git)
|
||||||
|
provider = "postgresql"
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
|
import { PrismaService } from './prisma.service';
|
||||||
|
|
||||||
|
describe('PrismaService', () => {
|
||||||
|
let prisma: PrismaService;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
|
providers: [PrismaService],
|
||||||
|
}).compile();
|
||||||
|
prisma = module.get<PrismaService>(PrismaService);
|
||||||
|
await prisma.onModuleInit();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await prisma.onModuleDestroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('connects to postgres', async () => {
|
||||||
|
const result = await prisma.$queryRaw<[{ ok: bigint }]>`SELECT 1 AS ok`;
|
||||||
|
expect(Number(result[0]!.ok)).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('creates and retrieves a Group, then cleans up', async () => {
|
||||||
|
const group = await prisma.group.create({
|
||||||
|
data: {
|
||||||
|
platform: 'whatsapp',
|
||||||
|
platformId: `test-group-${Date.now()}@g.us`,
|
||||||
|
name: 'Test Group',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(group.id).toBeDefined();
|
||||||
|
expect(group.platform).toBe('whatsapp');
|
||||||
|
expect(group.isActive).toBe(true);
|
||||||
|
|
||||||
|
await prisma.group.delete({ where: { id: group.id } });
|
||||||
|
});
|
||||||
|
});
|
||||||
+1
-1
@@ -6,7 +6,7 @@ services:
|
|||||||
POSTGRES_PASSWORD: tower_dev
|
POSTGRES_PASSWORD: tower_dev
|
||||||
POSTGRES_DB: tower_dev
|
POSTGRES_DB: tower_dev
|
||||||
ports:
|
ports:
|
||||||
- '5432:5432'
|
- '5433:5432'
|
||||||
volumes:
|
volumes:
|
||||||
- postgres_data:/var/lib/postgresql/data
|
- postgres_data:/var/lib/postgresql/data
|
||||||
healthcheck:
|
healthcheck:
|
||||||
|
|||||||
Generated
+5412
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user