Skip to content

Commit

Permalink
Add permit notes to db; migration, models, and types
Browse files Browse the repository at this point in the history
  • Loading branch information
qhanson55 committed Nov 15, 2024
1 parent 7fa264d commit 4162758
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 10 deletions.
54 changes: 54 additions & 0 deletions app/src/db/migrations/20241115000000_012-add-permit-notes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* eslint-disable max-len */
import stamps from '../stamps';

import { Knex } from 'knex';

export async function up(knex: Knex): Promise<void> {
return (
Promise.resolve()
// Create public schema tables
.then(() =>
knex.schema.createTable('permit_note', (table) => {
table.uuid('permit_note_id').primary(); // Primary key
table
.uuid('permit_id')
.notNullable()
.references('permit_id')
.inTable('public.permit')
.onUpdate('CASCADE')
.onDelete('CASCADE'); // Foreign key to 'permit'
table.text('note').notNullable().defaultTo('');
table.boolean('is_deleted').notNullable().defaultTo(false);
stamps(knex, table);
})
)

// Create public schema table triggers
.then(() =>
knex.schema.raw(`CREATE TRIGGER before_update_permit_note_trigger
BEFORE UPDATE ON public.permit_note
FOR EACH ROW
EXECUTE FUNCTION public.set_updated_at();`)
)

// Create audit triggers
.then(() =>
knex.schema.raw(`CREATE TRIGGER audit_permit_note_trigger
AFTER UPDATE OR DELETE ON public.permit_note
FOR EACH ROW
EXECUTE PROCEDURE audit.if_modified_func();`)
)
);
}

export async function down(knex: Knex): Promise<void> {
return (
Promise.resolve()
// Drop the audit triggers
.then(() => knex.schema.raw('DROP TRIGGER IF EXISTS audit_permit_note_trigger ON public.permit_note'))
// Drop public schema table triggers
.then(() => knex.schema.raw('DROP TRIGGER IF EXISTS before_update_permit_note_trigger ON public.permit_note'))
// Drop public schema tables
.then(() => knex.schema.dropTableIfExists('permit_note'))
);
}
29 changes: 29 additions & 0 deletions app/src/db/models/permit_note.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Prisma } from '@prisma/client';

import type { Stamps } from '../stamps';
import type { PermitNote } from '../../types';

// Define types
const _permitNote = Prisma.validator<Prisma.permit_noteDefaultArgs>()({});

type PrismaRelationPermitNote = Omit<Prisma.permit_noteGetPayload<typeof _permitNote>, keyof Stamps>;

export default {
toPrismaModel(input: PermitNote): PrismaRelationPermitNote {
return {
permit_note_id: input.permitNoteId,
permit_id: input.permitId,
note: input.note,
is_deleted: input.isDeleted
};
},

fromPrismaModel(input: PrismaRelationPermitNote): PermitNote {
return {
permitNoteId: input.permit_note_id,
permitId: input.permit_id,
note: input.note,
isDeleted: input.is_deleted
};
}
};
34 changes: 24 additions & 10 deletions app/src/db/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

generator client {
provider = "prisma-client-js"
previewFeatures = ["multiSchema", "views"]
Expand Down Expand Up @@ -105,23 +104,24 @@ model note {
}

model permit {
permit_id String @id @db.Uuid
permit_id String @id @db.Uuid
permit_type_id Int
activity_id String
issued_permit_id String?
tracking_id String?
auth_status String?
needed String?
status String?
submitted_date DateTime? @db.Timestamptz(6)
adjudication_date DateTime? @db.Timestamptz(6)
created_by String? @default("00000000-0000-0000-0000-000000000000")
created_at DateTime? @default(now()) @db.Timestamptz(6)
submitted_date DateTime? @db.Timestamptz(6)
adjudication_date DateTime? @db.Timestamptz(6)
created_by String? @default("00000000-0000-0000-0000-000000000000")
created_at DateTime? @default(now()) @db.Timestamptz(6)
updated_by String?
updated_at DateTime? @db.Timestamptz(6)
status_last_verified DateTime? @db.Timestamptz(6)
activity activity @relation(fields: [activity_id], references: [activity_id], onDelete: Cascade, map: "permit_activity_id_foreign")
permit_type permit_type @relation(fields: [permit_type_id], references: [permit_type_id], onDelete: Cascade, map: "permit_permit_type_id_foreign")
updated_at DateTime? @db.Timestamptz(6)
status_last_verified DateTime? @db.Timestamptz(6)
activity activity @relation(fields: [activity_id], references: [activity_id], onDelete: Cascade, map: "permit_activity_id_foreign")
permit_type permit_type @relation(fields: [permit_type_id], references: [permit_type_id], onDelete: Cascade, map: "permit_permit_type_id_foreign")
permit_note permit_note[]
@@unique([permit_id, permit_type_id, activity_id], map: "permit_permit_id_permit_type_id_activity_id_unique")
@@schema("public")
Expand Down Expand Up @@ -446,6 +446,20 @@ model subject_group {
@@schema("yars")
}

model permit_note {
permit_note_id String @id @db.Uuid
permit_id String @db.Uuid
note String @default("")
is_deleted Boolean @default(false)
created_by String? @default("00000000-0000-0000-0000-000000000000")
created_at DateTime? @default(now()) @db.Timestamptz(6)
updated_by String?
updated_at DateTime? @db.Timestamptz(6)
permit permit @relation(fields: [permit_id], references: [permit_id], onDelete: Cascade, map: "permit_note_permit_id_foreign")
@@schema("public")
}

view group_role_policy_vw {
row_number BigInt @unique
group_id Int?
Expand Down
8 changes: 8 additions & 0 deletions app/src/types/PermitNote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IStamps } from '../interfaces/IStamps';

export type PermitNote = {
permitNoteId: string; // Primary Key
permitId: string;
note: string;
isDeleted: boolean;
} & 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 @@ -20,6 +20,7 @@ export type { IdpAttributes } from './IdpAttributes';
export type { Middleware } from './Middleware';
export type { Note } from './Note';
export type { Permit } from './Permit';
export type { PermitNote } from './PermitNote';
export type { PermitType } from './PermitType';
export type { StatisticsFilters } from './StatisticsFilters';
export type { Submission } from './Submission';
Expand Down
8 changes: 8 additions & 0 deletions app/src/validators/permitNote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Joi from 'joi';

export const permitNoteSchema = Joi.object({
permitNoteId: Joi.string().max(255).required(),
permitId: Joi.string().max(255).required(),
note: Joi.string().max(255).required(),
isDeleted: Joi.boolean()
});

0 comments on commit 4162758

Please sign in to comment.