Skip to content

Prisma schema mapping - #352

Merged
qhanson55 merged 40 commits into
masterfrom
chore/prisma-mapping
Sep 3, 2025
Merged

Prisma schema mapping#352
qhanson55 merged 40 commits into
masterfrom
chore/prisma-mapping

Conversation

@kyle1morel

@kyle1morel kyle1morel commented Aug 20, 2025

Copy link
Copy Markdown
Collaborator

Description

This PR fundamentally changes the way the database is interacted with. Prisma now maps the database snake_case naming to camelCase naming in the schema.prisma file. This can be done automatically using a new package, prisma-case-format. This package is automatically set up as part of the prisma:migrate command. prisma:case can be run independently to do a case update and resync.

The old manually created DB application types and prisma dao/dto layers are now gone. Models now exist in the models.ts file using a strict structure. The const base is a validated Prisma object for that table. The exported Base type is the auto generated Prisma structure. This type should be used for all prisma.create() calls. The exported type below the Base type is the type generally used through the application, extending Base to include any relational information.

const permitBase = Prisma.validator<Prisma.permitDefaultArgs>()({});
export type PermitBase = Prisma.permitGetPayload<typeof permitBase>;
export type Permit = PermitBase & {
  permitNote?: PermitNote[];
  permitTracking?: PermitTracking[];
  permitType?: PermitType;
};

Non-DB types should be added to stuff.d.ts. Yes, stuff. That's what it is.

The controller layers now export each function independently instead of wrapping them all as a single default export. This allows the router layers to easily add the controller function as part of the chain. Each controller function now creates a single Prisma transaction using the transactionWrapper, and each service call is to use this transaction to ensure data integrity and proper data rollbacks in the event of an error.

The service layers now export each function independently instead of wrapping them all as a single default export. Each service function now takes a PrismaTransactionClient as its first parameter. This is to be the transaction object created in the controller. The Prisma client should never interact with the database, only the PrismaTransactionClient should be used.

Some default unit testing data for the back end has been added to app\tests\unit\data\index.ts. There was way too many duplicated testing types spread throughout the files making it a nightmare to update tests should a DB schema change happen. Use these directly or destructure them and make any necessary data changes locally in a test. DO NOT change the existing types unless a schema change is required. If you need something wildly different than the existing ones then add new ones.

And probably a whole lot more. There's 277 file changes. Sorry.

https://apps.nrs.gov.bc.ca/int/jira/browse/PADS-589

Types of changes

Bug fix (non-breaking change which fixes an issue)
New feature (non-breaking change which adds functionality)

Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the CONTRIBUTING doc
  • I have checked that unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

@kyle1morel
kyle1morel requested a review from a team as a code owner August 20, 2025 19:51
firstName: req.query.firstName,
lastName: req.query.lastName,
contactApplicantRelationship: req.query.contactApplicantRelationship,
phoneNumber: req.query.phoneNumber,

Check warning

Code scanning / CodeQL

Sensitive data read from GET request Medium

Route handler
for GET requests uses query parameter as sensitive data.
Comment thread app/src/controllers/document.ts Fixed
Comment thread app/src/services/coms.ts Dismissed
@kyle1morel
kyle1morel force-pushed the chore/prisma-mapping branch from 36d9607 to 13dd675 Compare August 20, 2025 20:40
@github-actions

github-actions Bot commented Aug 20, 2025

Copy link
Copy Markdown

Coverage Report (Application)

Totals Coverage
Statements: 62.78% ( 2624 / 4180 )
Methods: 53% ( 406 / 766 )
Lines: 73.06% ( 1698 / 2324 )
Branches: 47.71% ( 520 / 1090 )

@github-actions

github-actions Bot commented Aug 20, 2025

Copy link
Copy Markdown

Coverage Report (Frontend)

Totals Coverage
Statements: 40.97% ( 4027 / 9830 )
Methods: 29.23% ( 494 / 1690 )
Lines: 49.84% ( 2540 / 5096 )
Branches: 32.62% ( 993 / 3044 )

@kyle1morel
kyle1morel force-pushed the chore/prisma-mapping branch from 13dd675 to bc895d2 Compare August 20, 2025 20:51
@kyle1morel kyle1morel changed the title Prisma data mapping Prisma schema mapping Aug 20, 2025
@qhanson55
qhanson55 requested a review from Copilot August 20, 2025 21:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fundamentally changes the database interaction layer by implementing Prisma schema mapping from snake_case to camelCase naming conventions. It replaces manually created DB types with auto-generated Prisma models, removes the old DAO/DTO layers, and restructures controllers and services to use individual function exports with transaction wrappers for better data integrity.

Key changes include:

  • Migration from snake_case to camelCase database field naming using prisma-case-format
  • Replacement of manual DB types with auto-generated Prisma models in models.ts
  • Restructuring of controllers and services to export individual functions instead of default exports
  • Implementation of transaction wrappers for all database operations

Reviewed Changes

Copilot reviewed 174 out of 277 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
app/tests/unit/controllers/*.spec.ts Updated test files to use new controller function imports and transaction mocks
app/tests/mocks/prismaMock.ts Enhanced mock setup to support transaction wrapper and extended client types
app/src/validators/*.ts Updated field names from snake_case to camelCase and restructured validation schemas
app/src/utils/regexp.ts Added common regex patterns for validation
app/src/types/models.ts New auto-generated Prisma model types replacing manual DB types
app/src/types/index.ts Simplified to export only models and utility types
app/src/services/*.ts Converted to individual function exports with transaction client parameters
Files not reviewed (1)
  • app/package-lock.json: Language not supported

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread app/src/validators/stamps.ts
Comment thread app/src/validators/stamps.ts Outdated
Comment thread app/src/validators/housingProject.ts
Comment thread app/src/types/models.ts
Comment thread app/src/services/yars.ts Outdated
Comment thread app/src/services/user.ts
Comment thread app/app.ts Outdated
Comment thread app/src/controllers/accessRequest.ts
Comment thread app/src/controllers/accessRequest.ts Outdated
Comment thread app/src/controllers/document.ts Fixed
@Subin1Doo

Copy link
Copy Markdown

Here's what I found so far (Housing -NAV view)

Projects submissions table

  • Financially supported column not sorting
  • First and last name of proponent not showing in data table
  • Affected by Natural Disaster data value is showing as 'True' or 'False' instead of Yes or No

Project submission information tab

  • Affected by natural disaster field is acting weird.. The field gets emptied out whenever I change or update any other random field and save the info tab. The data seems to be there still, I can leave the project without any issue. But, coming back to the same project, error validation for this field gets triggered when I try to save with additional updates.
  • unable to test ATS enquiry push.. is this only functional in prod? Getting status code 500

Enquiry

  • Tried creating a manual enquiry submission, getting this error: "Error: Failed to save enquiry undefined is not an object (evaluating 'N.contactId')"

@sanjaytkbabu

sanjaytkbabu commented Aug 22, 2025

Copy link
Copy Markdown
Contributor
  1. TypeError: Cannot read properties of undefined (reading '0') when searching for a related project when creating an enquiry
  2. Got an internal server error 500 once when I created a housing submission as a proponent, couldn't reproduce. "detail": "\nInvalidprisma.permit_tracking.create()invocation:\n\n{\n data: {\n trackingId: \"1212\",\n shownToProponent: true,\n+ permit: {\n+ create: permitCreateWithoutPermitTrackingInput | permitUncheckedCreateWithoutPermitTrackingInput,\n+ connectOrCreate: permitCreateOrConnectWithoutPermitTrackingInput,\n+ connect: permitWhereUniqueInput\n+ }\n }\n}\n\nArgumentpermitis missing.",

@Subin1Doo

Copy link
Copy Markdown

Housing NAV view

Enquiry submission

  • Related project field broken. Typing something triggers this error ' undefined is not an object (evaluating 'u[0].contacts[0]').
  • Able to select a related project, but the basic information fields aren't getting reflected properly.

Contacts page

  • Irrelevant submissions are being listed under all contacts. Seems like all project and enquiry submissions are being listed under all contacts, regardless of who submitted it.

@kyle1morel
kyle1morel force-pushed the chore/prisma-mapping branch from 4cbd26c to 42c0d20 Compare August 25, 2025 20:32
Comment thread app/src/controllers/electrificationProject.ts Outdated
Comment thread app/src/controllers/electrificationProject.ts Outdated
Comment thread app/src/controllers/electrificationProject.ts Outdated
Comment thread app/src/controllers/enquiry.ts Outdated
Comment thread app/src/controllers/enquiry.ts Outdated
Comment thread app/src/controllers/enquiry.ts Outdated
@naixin-zhangbc

naixin-zhangbc commented Aug 26, 2025

Copy link
Copy Markdown

Housing Proponent intake form

Couldn't submit the intake form. Received the following error message upon submission

  • The first try showed "Failed to save intake. AxiosError: Timeout of 10000ms exceeded"
  • The second try showed "Failed to save intake. AxiosError: Request failed with status code 500"

After hitting these errors, the app wouldn't respond when trying to navigate to other pages. After refreshing the app worked again.

Electrification Proponent intake form
Couldn't submit the intake form either. Error code 422.

tsDoc, function names, Array to []
@kyle1morel
kyle1morel force-pushed the chore/prisma-mapping branch from 5fdd632 to efb4752 Compare August 27, 2025 20:18
@slhurley

slhurley commented Aug 27, 2025

Copy link
Copy Markdown
Contributor

For starters (need these fixed before going much further)

  • Intake form - as soon as you say you are a Business, the validation fails (ignore for now exists in Test)
  • Intake form - the Relationship to Project, Preferred contact method and Phone number are not saved to your Contact info which of course means you can't edit the Info tab. Should be automatic if they provided it on intake.
  • I used the polygon to set the Location on the form, it didn't save any location info or auto-generate any PIDs.
  • Directly after intake submission and before the Nav gets a chance to update anything (ie no Navigator assigned), on the Proponent project page it shows the Navigator as being "Test2 Person" instead of the unassigned message.
  • If in the Contact Profile page the only way out is to click Home which takes you to the menu rather than to your first page and makes you reselect Housing/Electric.
  • Can't submit an Enquiry to ATS....Client works but not Enquiry.
  • 422 error if trying to submit on the Electrification side AND the Contact info wasn't already pre-populated. (related to second bullet point above likely)
  • On the Info tab of an Enquiry, click on the link where it says "This activity is related to Activity...." you will get the "are you sure you want to Leave this page message"?

@Subin1Doo

Subin1Doo commented Aug 27, 2025

Copy link
Copy Markdown

Electrification specific - NAV view

  • For manual enquiry submissions, error toast for missing basic info fields not working. If the NAVs try to save after skipping the basic info section, it should trigger an error toast.
  • Are these fields supposed to be mandatory..? If they are, they're missing error validation - [do they have an EPA? / How many megawatts / Company / BC Env assessment needed / project category] - NOT MANDATORY in Test

@Subin1Doo

Subin1Doo commented Aug 27, 2025

Copy link
Copy Markdown

More NAV view errors across both initiatives

Enquiry submissions

  • For completed enquiries, deactivate add notes button. - THIS IS OK TO LEAVE
  • Confirmation modal for re-opening a completed enquiry showing code language.

@slhurley

slhurley commented Aug 28, 2025

Copy link
Copy Markdown
Contributor
  • I filled in the Location PIDs field on the intake form but it was not saved. (Fixed)
  • If the intake form fills in permits they already have, on the Nav side you can't add the Tracking ID name without getting a 422. (This was fixed before with PADS-625 in Jalapeno sprint)
  • Related Enquiries field does not populate on the projects info page even if it has related enquiries in the tab

@kyle1morel
kyle1morel force-pushed the chore/prisma-mapping branch 4 times, most recently from 4b8779b to 17a3711 Compare August 28, 2025 22:27
Contact info saving on intakes. Auto PID generation on intake. Correct assignee name in prop project view. Permit tracking create stamps. Project financially supported. Enquiry errors & reopen localization. Fill related enquiries field
@kyle1morel
kyle1morel force-pushed the chore/prisma-mapping branch from 17a3711 to b171374 Compare August 28, 2025 22:54
Comment thread app/src/routes/v1/noteHistory.ts Outdated
Comment thread app/src/routes/v1/permit.ts Outdated
Comment thread app/src/routes/v1/reporting.ts
Comment thread app/src/routes/v1/sourceSystemKind.ts Outdated
Comment thread app/src/db/prisma/schema.prisma
Comment thread app/src/db/prisma/schema.prisma
Comment thread app/src/services/activity.ts Outdated
Comment thread app/src/services/activityContact.ts Outdated
Comment thread app/src/services/contact.ts
Comment thread app/src/services/electrificationProject.ts Outdated
Comment thread app/src/services/email.ts Outdated
Comment thread app/src/services/housingProject.ts Outdated
Comment thread app/src/services/yars.ts
Comment thread app/src/services/permit.ts Outdated
Comment thread app/src/types/models.ts Outdated
Comment thread app/src/types/models.ts
Comment thread app/src/types/stuff.d.ts Outdated
Use `filesize` in create body instead of `length`
@sonarqubecloud

sonarqubecloud Bot commented Sep 3, 2025

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed for 'nr-permitconnect-navigator-service app'

Failed conditions
63.8% Coverage on New Code (required ≥ 80%)
C Security Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@sonarqubecloud

sonarqubecloud Bot commented Sep 3, 2025

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed for 'nr-permitconnect-navigator-service frontend'

Failed conditions
39.7% Coverage on New Code (required ≥ 80%)
5.5% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

Comment thread frontend/src/components/authorization/AuthorizationForm.vue
@qhanson55
qhanson55 merged commit 79240b3 into master Sep 3, 2025
18 of 21 checks passed
@qhanson55
qhanson55 deleted the chore/prisma-mapping branch September 8, 2025 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants