Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into mcr-3988-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
haworku committed Apr 18, 2024
2 parents 7aa87f5 + 6f305d0 commit edf3656
Show file tree
Hide file tree
Showing 73 changed files with 4,433 additions and 2,178 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
BEGIN;
-- DropEnum
DROP TYPE "DocumentCategory";

-- CreateTable
CREATE TABLE "SubmissionPackageJoinTable" (
"submissionID" TEXT NOT NULL,
"contractRevisionID" TEXT NOT NULL,
"rateRevisionID" TEXT NOT NULL,
"ratePosition" INTEGER NOT NULL,

CONSTRAINT "SubmissionPackageJoinTable_pkey" PRIMARY KEY ("submissionID","contractRevisionID","rateRevisionID")
);

-- CreateTable
CREATE TABLE "_ContractRevisionTableToUpdateInfoTable" (
"A" TEXT NOT NULL,
"B" TEXT NOT NULL
);

-- CreateTable
CREATE TABLE "_RateRevisionTableToUpdateInfoTable" (
"A" TEXT NOT NULL,
"B" TEXT NOT NULL
);

-- CreateIndex
CREATE UNIQUE INDEX "_ContractRevisionTableToUpdateInfoTable_AB_unique" ON "_ContractRevisionTableToUpdateInfoTable"("A", "B");

-- CreateIndex
CREATE INDEX "_ContractRevisionTableToUpdateInfoTable_B_index" ON "_ContractRevisionTableToUpdateInfoTable"("B");

-- CreateIndex
CREATE UNIQUE INDEX "_RateRevisionTableToUpdateInfoTable_AB_unique" ON "_RateRevisionTableToUpdateInfoTable"("A", "B");

-- CreateIndex
CREATE INDEX "_RateRevisionTableToUpdateInfoTable_B_index" ON "_RateRevisionTableToUpdateInfoTable"("B");

-- AddForeignKey
ALTER TABLE "SubmissionPackageJoinTable" ADD CONSTRAINT "SubmissionPackageJoinTable_submissionID_fkey" FOREIGN KEY ("submissionID") REFERENCES "UpdateInfoTable"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "SubmissionPackageJoinTable" ADD CONSTRAINT "SubmissionPackageJoinTable_contractRevisionID_fkey" FOREIGN KEY ("contractRevisionID") REFERENCES "ContractRevisionTable"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "SubmissionPackageJoinTable" ADD CONSTRAINT "SubmissionPackageJoinTable_rateRevisionID_fkey" FOREIGN KEY ("rateRevisionID") REFERENCES "RateRevisionTable"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_ContractRevisionTableToUpdateInfoTable" ADD CONSTRAINT "_ContractRevisionTableToUpdateInfoTable_A_fkey" FOREIGN KEY ("A") REFERENCES "ContractRevisionTable"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_ContractRevisionTableToUpdateInfoTable" ADD CONSTRAINT "_ContractRevisionTableToUpdateInfoTable_B_fkey" FOREIGN KEY ("B") REFERENCES "UpdateInfoTable"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_RateRevisionTableToUpdateInfoTable" ADD CONSTRAINT "_RateRevisionTableToUpdateInfoTable_A_fkey" FOREIGN KEY ("A") REFERENCES "RateRevisionTable"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_RateRevisionTableToUpdateInfoTable" ADD CONSTRAINT "_RateRevisionTableToUpdateInfoTable_B_fkey" FOREIGN KEY ("B") REFERENCES "UpdateInfoTable"("id") ON DELETE CASCADE ON UPDATE CASCADE;

COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
BEGIN;
-- CreateTable
CREATE TABLE "DraftRateJoinTable" (
"contractID" TEXT NOT NULL,
"rateID" TEXT NOT NULL,
"ratePosition" INTEGER NOT NULL,

CONSTRAINT "DraftRateJoinTable_pkey" PRIMARY KEY ("contractID","rateID")
);

-- AddForeignKey
ALTER TABLE "DraftRateJoinTable" ADD CONSTRAINT "DraftRateJoinTable_contractID_fkey" FOREIGN KEY ("contractID") REFERENCES "ContractTable"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "DraftRateJoinTable" ADD CONSTRAINT "DraftRateJoinTable_rateID_fkey" FOREIGN KEY ("rateID") REFERENCES "RateTable"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
BEGIN;
-- DropForeignKey
ALTER TABLE "DraftRateJoinTable" DROP CONSTRAINT "DraftRateJoinTable_rateID_fkey";

-- AddForeignKey
ALTER TABLE "DraftRateJoinTable" ADD CONSTRAINT "DraftRateJoinTable_rateID_fkey" FOREIGN KEY ("rateID") REFERENCES "RateTable"("id") ON DELETE CASCADE ON UPDATE CASCADE;
COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
BEGIN;
-- This migration finds all draft contract and rate revisions and makes entries in
-- the DraftRateJoinTable, with their order defined by RANK
INSERT INTO "DraftRateJoinTable" ("contractID", "rateID", "ratePosition")
SELECT "ContractRevisionTable"."contractID", "RateRevisionTable"."rateID", RANK() OVER (
PARTITION BY "ContractRevisionTable".id
ORDER BY "RateRevisionTable"."createdAt" DESC
) from "ContractRevisionTable", "_ContractRevisionTableToRateTable", "RateRevisionTable" WHERE
"ContractRevisionTable".id = "_ContractRevisionTableToRateTable"."A" AND
"_ContractRevisionTableToRateTable"."B" = "RateRevisionTable"."rateID" AND
"RateRevisionTable"."submitInfoID" IS NULL AND
"ContractRevisionTable"."submitInfoID" IS NULL
ON CONFLICT DO NOTHING;
COMMIT;
48 changes: 46 additions & 2 deletions services/app-api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ model ContractTable {
questions Question[]
draftRates DraftRateJoinTable[]
revisions ContractRevisionTable[]
// This relationship is a scam. We never call it in our code but Prisma
// requires that there be an inverse to RateRevision.draftContracts which we do use
Expand All @@ -62,12 +64,25 @@ model RateTable {
state State @relation(fields: [stateCode], references: [stateCode])
stateNumber Int
draftContracts DraftRateJoinTable[]
revisions RateRevisionTable[]
// This relationship is a scam. We never call it in our code but Prisma
// requires that there be an inverse to ContractRevision.draftRates which we do use
draftContractRevisions ContractRevisionTable[]
}

// DraftRateJoinTable links a draft contract to a set of rates, draft or submitted.
model DraftRateJoinTable {
contractID String
contract ContractTable @relation(fields: [contractID], references: [id])
rateID String
rate RateTable @relation(fields: [rateID], references: [id], onDelete: Cascade)
ratePosition Int
@@id([contractID, rateID])
}

model ContractRevisionTable {
id String @id @default(uuid())
contractID String
Expand All @@ -81,6 +96,9 @@ model ContractRevisionTable {
submitInfoID String?
submitInfo UpdateInfoTable? @relation("submitContractInfo", fields: [submitInfoID], references: [id])
relatedSubmisions UpdateInfoTable[]
submissionPackages SubmissionPackageJoinTable[] @relation("contractRevision")
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
Expand Down Expand Up @@ -133,6 +151,9 @@ model RateRevisionTable {
submitInfoID String?
submitInfo UpdateInfoTable? @relation("submitRateInfo", fields: [submitInfoID], references: [id], onDelete: Cascade)
relatedSubmissions UpdateInfoTable[]
submissionPackages SubmissionPackageJoinTable[] @relation("rateRevision")
rateType RateType?
rateCapitationType RateCapitationType?
rateDocuments RateDocument[]
Expand Down Expand Up @@ -169,10 +190,33 @@ model UpdateInfoTable {
updatedByID String
updatedBy User @relation(fields: [updatedByID], references: [id])
updatedReason String
unlockedContracts ContractRevisionTable[] @relation("unlockContractInfo")
// the actually submitted contract/rates for this submission
submittedContracts ContractRevisionTable[] @relation("submitContractInfo")
unlockedRates RateRevisionTable[] @relation("unlockRateInfo")
submittedRates RateRevisionTable[] @relation("submitRateInfo")
// These relations are inverse 1:1 relations that are not really used.
unlockedContracts ContractRevisionTable[] @relation("unlockContractInfo")
unlockedRates RateRevisionTable[] @relation("unlockRateInfo")
submissionPackages SubmissionPackageJoinTable[] @relation("submission")
// these get ALL related contracts + revisions for given update.
relatedContracts ContractRevisionTable[]
relatedRates RateRevisionTable[]
}

// SubmissionPackageJoinTable records the set of related rate and contracts for a given submission.
model SubmissionPackageJoinTable {
submissionID String
submission UpdateInfoTable @relation("submission", fields: [submissionID], references: [id])
contractRevisionID String
contractRevision ContractRevisionTable @relation("contractRevision", fields: [contractRevisionID], references: [id])
rateRevisionID String
rateRevision RateRevisionTable @relation("rateRevision", fields: [rateRevisionID], references: [id])
// this number indicates the position of the rate in this contract
ratePosition Int
@@id([submissionID, contractRevisionID, rateRevisionID])
}

model ActuaryContact {
Expand Down
2 changes: 1 addition & 1 deletion services/app-api/src/dataMigrations/_sampleTest.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { sharedTestPrismaClient } from '../testHelpers/storeHelpers'
import { migrate } from './migrations/20231106200334_fix_empty_rates'
import { migrate } from './migrations/20231026123042_test_migrator_works'

/*
Demo of how to test a data migration - can run and test locally with this test.
Expand Down
7 changes: 0 additions & 7 deletions services/app-api/src/dataMigrations/dataMigrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { PrismaTransactionType } from '../postgres/prismaTypes'
import { migrate as migrate1 } from './migrations/20231026123042_test_migrator_works'
import { migrate as migrate2 } from './migrations/20231026124442_fix_rate_submittedat'
import { migrate as migrate3 } from './migrations/20231026124542_fix_erroneous_rates'
import { migrate as migrate4 } from './migrations/20231106200334_fix_empty_rates'

// MigrationType describes a single migration with a name and a callable function called migrateProto
interface DBMigrationType {
Expand Down Expand Up @@ -104,12 +103,6 @@ export async function migrate(
migrate: migrate3,
},
},
{
name: 'migrations/20231106200334_fix_empty_rates',
module: {
migrate: migrate4,
},
},
]
// for (const migrationFile of migrationFiles) {
// // const fullPath = './migrations/0001_test_migration'
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { contractRevisionWithRatesSchema } from './revisionTypes'
import { statusSchema } from './statusType'
import { pruneDuplicateEmails } from '../../emailer/formatters'
import { rateSchema } from './rateTypes'
import { contractPackageSubmissionSchema } from './packageSubmissions'

// Contract represents the contract specific information in a submission package
// All that data is contained in revisions, each revision represents the data in a single submission
Expand All @@ -20,6 +21,8 @@ const contractSchema = z.object({
draftRates: z.array(rateSchema).optional(),
// All revisions are submitted and in reverse chronological order
revisions: z.array(contractRevisionWithRatesSchema),

packageSubmissions: z.array(contractPackageSubmissionSchema),
})

const draftContractSchema = contractSchema.extend({
Expand Down
5 changes: 5 additions & 0 deletions services/app-api/src/domain-models/contractAndRates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,8 @@ export type {
RateRevisionWithContractsType,
ContractRevisionWithRatesType,
} from './revisionTypes'

export type {
ContractPackageSubmissionType,
ContractPackageSubmissionWithCauseType,
} from './packageSubmissions'
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { z } from 'zod'
import { contractRevisionSchema, rateRevisionSchema } from './revisionTypes'
import { updateInfoSchema } from './updateInfoType'

const contractPackageSubmissionSchema = z.object({
submitInfo: updateInfoSchema,
submittedRevisions: z.array(
z.union([contractRevisionSchema, rateRevisionSchema])
),
contractRevision: contractRevisionSchema,
rateRevisions: z.array(rateRevisionSchema),
})

const packgeSubmissionCause = z.union([
z.literal('CONTRACT_SUBMISSION'),
z.literal('RATE_SUBMISSION'),
z.literal('RATE_UNLINK'),
z.literal('RATE_LINK'),
])

const contractPackageSubmissionWithCauseSchema =
contractPackageSubmissionSchema.extend({
cause: packgeSubmissionCause,
})

type ContractPackageSubmissionType = z.infer<
typeof contractPackageSubmissionSchema
>

type ContractPackageSubmissionWithCauseType = z.infer<
typeof contractPackageSubmissionWithCauseSchema
>

export {
contractPackageSubmissionSchema,
contractPackageSubmissionWithCauseSchema,
}

export type {
ContractPackageSubmissionType,
ContractPackageSubmissionWithCauseType,
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const rateSchema = z.object({
updatedAt: z.date(),
status: statusSchema,
stateCode: z.string(),
parentContractID: z.string().uuid(),
stateNumber: z.number().min(1),
// If this rate is in a DRAFT or UNLOCKED status, there will be a draftRevision
draftRevision: rateRevisionWithContractsSchema.optional(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ const contractRevisionSchema = z.object({

const rateRevisionSchema = z.object({
id: z.string().uuid(),
rate: z.object({
id: z.string().uuid(),
stateCode: z.string(),
stateNumber: z.number().min(1),
createdAt: z.date(),
}),
rateID: z.string().uuid(),
submitInfo: updateInfoSchema.optional(),
unlockInfo: updateInfoSchema.optional(),
createdAt: z.date(),
Expand Down
2 changes: 2 additions & 0 deletions services/app-api/src/domain-models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export type {
RateRevisionWithContractsType,
RateFormDataType,
PackageStatusType,
ContractPackageSubmissionType,
ContractPackageSubmissionWithCauseType,
} from './contractAndRates'

export type {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ async function findAllContractsWithHistoryBySubmitInfo(
where: {
revisions: {
some: {
submitInfo: {
isNot: null,
submitInfoID: {
not: null,
},
},
},
Expand Down
Loading

0 comments on commit edf3656

Please sign in to comment.