Skip to content

Commit

Permalink
MCR-4887: add new application settings table (#3093)
Browse files Browse the repository at this point in the history
* Add New tables and migration

* Extend prisma to restrict new table methods

* Pass prisma options to extendedPrismaClient
  • Loading branch information
JasonLin0991 authored Jan 22, 2025
1 parent 82d51f4 commit 680d284
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
BEGIN;

-- CreateTable
CREATE TABLE "ApplicationSettings" (
"id" INTEGER NOT NULL DEFAULT 1,
"emailSettingsId" INTEGER DEFAULT 1,

CONSTRAINT "ApplicationSettings_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "EmailSettings" (
"id" INTEGER NOT NULL DEFAULT 1,
"emailSource" TEXT NOT NULL DEFAULT '[email protected]',
"devReviewTeamEmails" TEXT[] DEFAULT ARRAY['[email protected]']::TEXT[],
"cmsReviewHelpEmailAddress" TEXT[] DEFAULT ARRAY['MCOGDMCOActions <[email protected]>']::TEXT[],
"cmsRateHelpEmailAddress" TEXT[] DEFAULT ARRAY['MMCratesetting <[email protected]>']::TEXT[],
"oactEmails" TEXT[] DEFAULT ARRAY['OACT Dev1 <[email protected]>', 'OACT Dev2 <[email protected]>']::TEXT[],
"dmcpReviewEmails" TEXT[] DEFAULT ARRAY['DMCP Review Dev1 <[email protected]>', 'DMCP Review Dev2 <[email protected]>']::TEXT[],
"dmcpSubmissionEmails" TEXT[] DEFAULT ARRAY['DMCP Submission Dev1 <[email protected]>', 'DMCP Submission Dev2 <[email protected]>']::TEXT[],
"dmcoEmails" TEXT[] DEFAULT ARRAY['DMCO Dev1 <[email protected]>', 'DMCO Dev2 <[email protected]>']::TEXT[],
"helpDeskEmail" TEXT[] DEFAULT ARRAY['[email protected]']::TEXT[],
"applicationSettingsId" INTEGER DEFAULT 1,

CONSTRAINT "EmailSettings_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "ApplicationSettings_emailSettingsId_key" ON "ApplicationSettings"("emailSettingsId");

-- CreateIndex
CREATE UNIQUE INDEX "ApplicationSettings_id_key" ON "ApplicationSettings"("id");

-- CreateIndex
CREATE UNIQUE INDEX "EmailSettings_applicationSettingsId_key" ON "EmailSettings"("applicationSettingsId");

-- CreateIndex
CREATE UNIQUE INDEX "EmailSettings_id_key" ON "EmailSettings"("id");

-- AddForeignKey
ALTER TABLE "EmailSettings" ADD CONSTRAINT "EmailSettings_applicationSettingsId_fkey" FOREIGN KEY ("applicationSettingsId") REFERENCES "ApplicationSettings"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- Insert Initial Records
INSERT INTO "ApplicationSettings" ("id", "emailSettingsId")
VALUES (1, 1)
ON CONFLICT ("id") DO NOTHING;

INSERT INTO "EmailSettings" (
"id",
"emailSource",
"devReviewTeamEmails",
"cmsReviewHelpEmailAddress",
"cmsRateHelpEmailAddress",
"oactEmails",
"dmcpReviewEmails",
"dmcpSubmissionEmails",
"dmcoEmails",
"helpDeskEmail",
"applicationSettingsId"
) VALUES (
1,
'[email protected]',
ARRAY['[email protected]'],
ARRAY['MCOGDMCOActions <[email protected]>'],
ARRAY['MMCratesetting <[email protected]>'],
ARRAY['OACT Dev1 <[email protected]>', 'OACT Dev2 <[email protected]>'],
ARRAY['DMCP Review Dev1 <[email protected]>', 'DMCP Review Dev2 <[email protected]>'],
ARRAY['DMCP Submission Dev1 <[email protected]>', 'DMCP Submission Dev2 <[email protected]>'],
ARRAY['DMCO Dev1 <[email protected]>', 'DMCO Dev2 <[email protected]>'],
ARRAY['[email protected]'],
1
)
ON CONFLICT ("id") DO NOTHING;

COMMIT;
26 changes: 26 additions & 0 deletions services/app-api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,32 @@ model ProtoMigrationsTable {
migrationName String @id
}

model ApplicationSettings {
id Int @id @default(1)
emailSettings EmailSettings?
emailSettingsId Int? @unique @default(1)
@@unique([id])
}

model EmailSettings {
id Int @id @default(1)
emailSource String @default("[email protected]")
devReviewTeamEmails String[] @default(["[email protected]"])
cmsReviewHelpEmailAddress String[] @default(["MCOGDMCOActions <mc-review-qa+[email protected]>"])
cmsRateHelpEmailAddress String[] @default(["MMCratesetting <mc-review-qa+[email protected]>"])
oactEmails String[] @default(["OACT Dev1 <mc-review-qa+[email protected]>","OACT Dev2 <mc-review-qa+[email protected]>"])
dmcpReviewEmails String[] @default(["DMCP Review Dev1 <mc-review-qa+[email protected]>", "DMCP Review Dev2 <mc-review-qa+[email protected]>"])
dmcpSubmissionEmails String[] @default(["DMCP Submission Dev1 <mc-review-qa+[email protected]>", "DMCP Submission Dev2 <mc-review-qa+[email protected]>"])
dmcoEmails String[] @default(["DMCO Dev1 <mc-review-qa+[email protected]>", "DMCO Dev2 <mc-review-qa+[email protected]>"])
helpDeskEmail String[] @default(["[email protected]"])
applicationSettings ApplicationSettings? @relation(fields: [applicationSettingsId], references: [id])
applicationSettingsId Int? @unique @default(1)
@@unique([id])
}

model HealthPlanRevisionTable {
id String @id
createdAt DateTime
Expand Down
68 changes: 65 additions & 3 deletions services/app-api/src/postgres/prismaClient.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,76 @@
import { PrismaClient } from '@prisma/client'
import { type Prisma, PrismaClient } from '@prisma/client'

async function NewPrismaClient(connURL: string): Promise<PrismaClient | Error> {
const errorMessages = {
delete: 'Deletion of records is not allowed',
create: 'Creation of new records is not allowed',
}

/**
* Extends PrismaClient with custom behavior for specific models.
*
* Overrides delete and create operations on `applicationSettings` and `emailSettings`
* models to throw custom error messages.
*
* @param {Prisma.PrismaClientOptions} optionArgs - PrismaClient configuration options.
* @returns {PrismaClient} A new PrismaClient instance with extended behavior.
*/
function extendedPrismaClient(optionArgs: Prisma.PrismaClientOptions) {
return new PrismaClient(optionArgs).$extends({
query: {
applicationSettings: {
delete: () => {
throw new Error(
`ApplicationSettings: ${errorMessages.delete}`
)
},
deleteMany: () => {
throw new Error(
`ApplicationSettings: ${errorMessages.delete}`
)
},
create: () => {
throw new Error(
`ApplicationSettings: ${errorMessages.create}`
)
},
createMany: () => {
throw new Error(
`ApplicationSettings: ${errorMessages.create}`
)
},
},
emailSettings: {
delete: () => {
throw new Error(`EmailSettings: ${errorMessages.delete}`)
},
deleteMany: () => {
throw new Error(`EmailSettings: ${errorMessages.delete}`)
},
create: () => {
throw new Error(`EmailSettings: ${errorMessages.create}`)
},
createMany: () => {
throw new Error(`EmailSettings: ${errorMessages.create}`)
},
},
},
})
}

type ExtendedPrismaClient = ReturnType<typeof extendedPrismaClient>

async function NewPrismaClient(
connURL: string
): Promise<ExtendedPrismaClient | Error> {
try {
const prismaClient = new PrismaClient({
const prismaClient = extendedPrismaClient({
datasources: {
db: {
url: connURL,
},
},
})

return prismaClient
} catch (e: unknown) {
if (e instanceof Error) {
Expand Down

0 comments on commit 680d284

Please sign in to comment.