Skip to content

Commit

Permalink
feat: add 'note' table to db, models, types for log notes feature, wip
Browse files Browse the repository at this point in the history
  • Loading branch information
wilwong89 committed Feb 13, 2024
1 parent ab8a381 commit 695e70a
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 0 deletions.
34 changes: 34 additions & 0 deletions app/src/db/migrations/20231212000000_init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export async function up(knex: Knex): Promise<void> {
table.boolean('aaiUpdated');
table.text('waitingOn');
table.timestamp('bringForwardDate', { useTz: true });
// TODO: REMOVE THIS WHEN DONE NOTE LOGS
table.text('notes');
table.text('intakeStatus');
table.text('applicationStatus');
Expand Down Expand Up @@ -192,6 +193,30 @@ export async function up(knex: Knex): Promise<void> {
for each row execute procedure public.set_updatedAt();`)
)

.then(() =>
knex.schema.createTable('note', (table) => {
table.uuid('note_id').primary();
table
.uuid('submission_id')
.notNullable()
.references('submissionId')
.inTable('submission')
.onUpdate('CASCADE')
.onDelete('CASCADE');
table.text('category_type').defaultTo('').notNullable();
table.text('note_type').defaultTo('').notNullable();
table.text('note').defaultTo('').notNullable();
stamps(knex, table);
table.unique(['note_id']);
})
)

.then(() =>
knex.schema.raw(`create trigger before_update_note_trigger
before update on public.note
for each row execute procedure public.set_updatedAt();`)
)

// Create public schema functions
.then(() =>
knex.schema.raw(`create or replace function public.get_activity_statistics(
Expand Down Expand Up @@ -352,6 +377,12 @@ export async function up(knex: Knex): Promise<void> {
FOR EACH ROW EXECUTE PROCEDURE audit.if_modified_func();`)
)

.then(() =>
knex.schema.raw(`CREATE TRIGGER audit_note_trigger
AFTER UPDATE OR DELETE ON note
FOR EACH ROW EXECUTE PROCEDURE audit.if_modified_func();`)
)

// Populate Baseline Data
.then(() => {
const users = ['system'];
Expand Down Expand Up @@ -620,6 +651,7 @@ export async function down(knex: Knex): Promise<void> {
.then(() => knex.schema.raw('DROP TRIGGER IF EXISTS audit_submission_trigger ON submission'))
.then(() => knex.schema.raw('DROP TRIGGER IF EXISTS audit_user_trigger ON "user"'))
.then(() => knex.schema.raw('DROP TRIGGER IF EXISTS audit_identity_provider_trigger ON identity_provider'))
.then(() => knex.schema.raw('DROP TRIGGER IF EXISTS audit_note_trigger ON note'))
// Drop audit schema and logged_actions table
.then(() => knex.schema.raw('DROP FUNCTION IF EXISTS audit.if_modified_func'))
.then(() => knex.schema.withSchema('audit').dropTableIfExists('logged_actions'))
Expand All @@ -642,6 +674,8 @@ export async function down(knex: Knex): Promise<void> {
knex.schema.raw('DROP TRIGGER IF EXISTS before_update_identity_provider_trigger ON identity_provider')
)
.then(() => knex.schema.dropTableIfExists('identity_provider'))
.then(() => knex.schema.raw('DROP TRIGGER IF EXISTS before_update_note_trigger ON note'))
.then(() => knex.schema.dropTableIfExists('note'))
// Drop public schema triggers
.then(() => knex.schema.raw('DROP FUNCTION IF EXISTS public.set_updatedAt'))
);
Expand Down
1 change: 1 addition & 0 deletions app/src/db/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { default as document } from './document';
export { default as identity_provider } from './identity_provider';
export { default as note } from './note';
export { default as permit } from './permit';
export { default as permit_type } from './permit_type';
export { default as submission } from './submission';
Expand Down
52 changes: 52 additions & 0 deletions app/src/db/models/note.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Prisma } from '@prisma/client';
import disconnectRelation from '../utils/disconnectRelation';

import type { IStamps } from '../../interfaces/IStamps';
import type { Note } from '../../types';

// Define types
const _note = Prisma.validator<Prisma.noteDefaultArgs>()({});
const _noteWithGraph = Prisma.validator<Prisma.noteDefaultArgs>()({});

type SubmissionRelation = {
submission:
| {
connect: {
submissionId: string;
};
}
| {
disconnect: boolean;
};
};

type PrismaRelationNote = Omit<Prisma.noteGetPayload<typeof _note>, 'submissionId' | keyof IStamps> &
SubmissionRelation;

type PrismaGraphNote = Prisma.noteGetPayload<typeof _noteWithGraph>;

export default {
toPrismaModel(input: Note): PrismaRelationNote {
// Note: submissionId conversion to submission_id will be required here
return {
note_id: input.note_id as string,
submission: input.submission_id ? { connect: { submissionId: input.submission_id } } : disconnectRelation,
category_type: input.category_type,
note: input.note,
note_type: input.note_type
};
},

fromPrismaModel(input: PrismaGraphNote | null): Note | null {
if (!input) return null;

return {
note_id: input.note_id,
submission_id: input.submissionId as string,
category_type: input.category_type || '',
note: input.note || '',
note_type: input.note_type || '',
createdAt: input.createdAt?.toISOString()
};
}
};
14 changes: 14 additions & 0 deletions app/src/db/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ model identity_provider {
user user[]
}

model note {
note_id String @id @unique(map: "note_note_id_unique") @db.Uuid
submissionId String @db.Uuid
category_type String?
note_type String?
note String?
createdBy String? @default("00000000-0000-0000-0000-000000000000")
createdAt DateTime? @default(now()) @db.Timestamptz(6)
updatedBy String?
updatedAt DateTime? @db.Timestamptz(6)
submission submission @relation(fields: [submissionId], references: [submissionId], onDelete: Cascade, map: "note_submissionid_foreign")
}

model permit {
permitId String @id @db.Uuid
permitTypeId Int
Expand Down Expand Up @@ -132,6 +145,7 @@ model submission {
updatedBy String?
updatedAt DateTime? @db.Timestamptz(6)
document document[]
note note[]
permit permit[]
user user? @relation(fields: [assignedToUserId], references: [userId], onDelete: Cascade, map: "submission_assignedtouserid_foreign")
}
Expand Down
9 changes: 9 additions & 0 deletions app/src/types/Note.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { IStamps } from '../interfaces/IStamps';

export type Note = {
note_id: string; // Primary Key
submission_id: string;
category_type: string;
note: string;
note_type: string;
} & Partial<IStamps>;
1 change: 1 addition & 0 deletions app/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type { ChefsSubmissionFormExport } from './ChefsSubmissionFormExport';
export type { CurrentUser } from './CurrentUser';
export type { Document } from './Document';
export type { IdentityProvider } from './IdentityProvider';
export type { Note } from './Note';
export type { Permit } from './Permit';
export type { PermitType } from './PermitType';
export type { SubmissionSearchParameters } from './SubmissionSearchParameters';
Expand Down
9 changes: 9 additions & 0 deletions frontend/src/types/Note.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { IStamps } from '@/interfaces';

export type Note = {
note_id: string; // Primary Key
submission_id: number;
note_type: string;
category_type?: string;
note?: string;
} & Partial<IStamps>;
1 change: 1 addition & 0 deletions frontend/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export type { Permit } from './Permit';
export type { PermitType } from './PermitType';
export type { User } from './User';
export type { UserSearchParameters } from './UserSearchParameters';
export type { Note } from './Note';

0 comments on commit 695e70a

Please sign in to comment.