206 lines
6.2 KiB
SQL
206 lines
6.2 KiB
SQL
-- CreateEnum
|
|
CREATE TYPE "MembershipRole" AS ENUM ('OWNER', 'ADMIN', 'USER');
|
|
|
|
-- CreateEnum
|
|
CREATE TYPE "GlobalRole" AS ENUM ('SUPERADMIN', 'CUSTOMER');
|
|
|
|
-- CreateEnum
|
|
CREATE TYPE "Role" AS ENUM ('USER', 'ADMIN');
|
|
|
|
-- CreateEnum
|
|
CREATE TYPE "TokenType" AS ENUM ('RESET_PASSWORD');
|
|
|
|
-- CreateEnum
|
|
CREATE TYPE "Direction" AS ENUM ('Inbound', 'Outbound');
|
|
|
|
-- CreateEnum
|
|
CREATE TYPE "MessageStatus" AS ENUM ('Queued', 'Sending', 'Sent', 'Failed', 'Delivered', 'Undelivered', 'Receiving', 'Received', 'Accepted', 'Scheduled', 'Read', 'PartiallyDelivered', 'Canceled', 'Error');
|
|
|
|
-- CreateEnum
|
|
CREATE TYPE "CallStatus" AS ENUM ('Queued', 'Ringing', 'InProgress', 'Completed', 'Busy', 'Failed', 'NoAnswer', 'Canceled');
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "Organization" (
|
|
"id" TEXT NOT NULL,
|
|
"createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"updatedAt" TIMESTAMPTZ NOT NULL,
|
|
"encryptionKey" TEXT NOT NULL,
|
|
"paddleCustomerId" TEXT,
|
|
"paddleSubscriptionId" TEXT,
|
|
"twilioAccountSid" TEXT,
|
|
"twilioAuthToken" TEXT,
|
|
"twimlAppSid" TEXT,
|
|
|
|
PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "Membership" (
|
|
"id" TEXT NOT NULL,
|
|
"role" "MembershipRole" NOT NULL,
|
|
"organizationId" TEXT NOT NULL,
|
|
"userId" TEXT,
|
|
"invitedName" TEXT,
|
|
"invitedEmail" TEXT,
|
|
|
|
PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "User" (
|
|
"id" TEXT NOT NULL,
|
|
"createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"updatedAt" TIMESTAMPTZ NOT NULL,
|
|
"name" TEXT,
|
|
"email" TEXT NOT NULL,
|
|
"hashedPassword" TEXT,
|
|
"role" "GlobalRole" NOT NULL DEFAULT E'CUSTOMER',
|
|
|
|
PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "Session" (
|
|
"id" TEXT NOT NULL,
|
|
"createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"updatedAt" TIMESTAMPTZ NOT NULL,
|
|
"expiresAt" TIMESTAMPTZ,
|
|
"handle" TEXT NOT NULL,
|
|
"hashedSessionToken" TEXT,
|
|
"antiCSRFToken" TEXT,
|
|
"publicData" TEXT,
|
|
"privateData" TEXT,
|
|
"userId" TEXT,
|
|
|
|
PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "Token" (
|
|
"id" TEXT NOT NULL,
|
|
"createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"updatedAt" TIMESTAMPTZ NOT NULL,
|
|
"hashedToken" TEXT NOT NULL,
|
|
"type" "TokenType" NOT NULL,
|
|
"expiresAt" TIMESTAMPTZ NOT NULL,
|
|
"sentTo" TEXT NOT NULL,
|
|
"userId" TEXT NOT NULL,
|
|
|
|
PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "Message" (
|
|
"id" TEXT NOT NULL,
|
|
"sentAt" TIMESTAMPTZ NOT NULL,
|
|
"content" TEXT NOT NULL,
|
|
"from" TEXT NOT NULL,
|
|
"to" TEXT NOT NULL,
|
|
"direction" "Direction" NOT NULL,
|
|
"status" "MessageStatus" NOT NULL,
|
|
"organizationId" TEXT NOT NULL,
|
|
"phoneNumberId" TEXT NOT NULL,
|
|
|
|
PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "PhoneCall" (
|
|
"id" TEXT NOT NULL,
|
|
"createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"from" TEXT NOT NULL,
|
|
"to" TEXT NOT NULL,
|
|
"status" "CallStatus" NOT NULL,
|
|
"direction" "Direction" NOT NULL,
|
|
"duration" TEXT NOT NULL,
|
|
"organizationId" TEXT NOT NULL,
|
|
"phoneNumberId" TEXT NOT NULL,
|
|
|
|
PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "PhoneNumber" (
|
|
"id" TEXT NOT NULL,
|
|
"createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"number" TEXT NOT NULL,
|
|
"organizationId" TEXT NOT NULL,
|
|
|
|
PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "NotificationSubscription" (
|
|
"id" TEXT NOT NULL,
|
|
"createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"updatedAt" TIMESTAMPTZ NOT NULL,
|
|
"endpoint" TEXT NOT NULL,
|
|
"expirationTime" INTEGER,
|
|
"keys_p256dh" TEXT NOT NULL,
|
|
"keys_auth" TEXT NOT NULL,
|
|
"organizationId" TEXT NOT NULL,
|
|
"phoneNumberId" TEXT NOT NULL,
|
|
|
|
PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "Organization.id_twilioAccountSid_unique" ON "Organization"("id", "twilioAccountSid");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "Membership.organizationId_invitedEmail_unique" ON "Membership"("organizationId", "invitedEmail");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "User.email_unique" ON "User"("email");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "Session.handle_unique" ON "Session"("handle");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "Token.hashedToken_type_unique" ON "Token"("hashedToken", "type");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "Message.organizationId_phoneNumberId_id_unique" ON "Message"("organizationId", "phoneNumberId", "id");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "PhoneCall.organizationId_phoneNumberId_id_unique" ON "PhoneCall"("organizationId", "phoneNumberId", "id");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "PhoneNumber.organizationId_id_unique" ON "PhoneNumber"("organizationId", "id");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "NotificationSubscription.endpoint_unique" ON "NotificationSubscription"("endpoint");
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "Membership" ADD FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "Membership" ADD FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "Session" ADD FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "Token" ADD FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "Message" ADD FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "Message" ADD FOREIGN KEY ("phoneNumberId") REFERENCES "PhoneNumber"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "PhoneCall" ADD FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "PhoneCall" ADD FOREIGN KEY ("phoneNumberId") REFERENCES "PhoneNumber"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "PhoneNumber" ADD FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "NotificationSubscription" ADD FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "NotificationSubscription" ADD FOREIGN KEY ("phoneNumberId") REFERENCES "PhoneNumber"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|