From 4ccea8d3e56bb90b12799608298fc31ef16740e9 Mon Sep 17 00:00:00 2001 From: Wilson Wong Date: Wed, 14 Feb 2024 17:53:42 -0800 Subject: [PATCH] Fix UI/UX consistency, adjusted db model and backend logic, clean up formatting --- app/src/controllers/note.ts | 13 ++++-- app/src/db/migrations/20231212000000_init.ts | 4 +- app/src/db/models/note.ts | 12 ++++-- app/src/db/prisma/schema.prisma | 8 ++-- app/src/services/note.ts | 15 +++---- app/src/types/Note.ts | 5 +-- frontend/src/components/note/NoteCard.vue | 28 +++++++------ frontend/src/components/note/NoteModal.vue | 42 +++++++++++-------- frontend/src/components/permit/PermitCard.vue | 2 +- frontend/src/types/Note.ts | 6 --- frontend/src/views/SubmissionView.vue | 8 ++-- 11 files changed, 78 insertions(+), 65 deletions(-) diff --git a/app/src/controllers/note.ts b/app/src/controllers/note.ts index b0313f68..b1387eeb 100644 --- a/app/src/controllers/note.ts +++ b/app/src/controllers/note.ts @@ -1,12 +1,19 @@ -import { noteService } from '../services'; +import { NIL } from 'uuid'; + +import { noteService, userService } from '../services'; +import { getCurrentIdentity } from '../components/utils'; import type { NextFunction, Request, Response } from '../interfaces/IExpress'; -import { CurrentUser } from '../types'; const controller = { createNote: async (req: Request, res: Response, next: NextFunction) => { try { - const response = await noteService.createNote(req.body, req.currentUser as CurrentUser); + const userId = await userService.getCurrentUserId(getCurrentIdentity(req.currentUser, NIL), NIL); + + // TODO: define body type in request + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const body = req.body as any; + const response = await noteService.createNote({ ...body, createdBy: userId }); res.status(200).send(response); } catch (e: unknown) { next(e); diff --git a/app/src/db/migrations/20231212000000_init.ts b/app/src/db/migrations/20231212000000_init.ts index 82188b92..ac1a84a8 100644 --- a/app/src/db/migrations/20231212000000_init.ts +++ b/app/src/db/migrations/20231212000000_init.ts @@ -205,9 +205,7 @@ export async function up(knex: Knex): Promise { table.text('note').defaultTo('').notNullable(); table.text('note_type').defaultTo('').notNullable(); table.text('title').defaultTo('').notNullable(); - table.text('createdAt'); - table.text('createdBy'); - table.unique(['note_id']); + stamps(knex, table); }) ) diff --git a/app/src/db/models/note.ts b/app/src/db/models/note.ts index 8aade961..b4d3a2ca 100644 --- a/app/src/db/models/note.ts +++ b/app/src/db/models/note.ts @@ -30,8 +30,10 @@ export default { note_type: input.noteType, submission: { connect: { submissionId: input.submissionId } }, title: input.title, - createdAt: input.createdAt ?? new Date().toISOString(), - createdBy: input.createdBy + createdAt: input.createdAt ? new Date(input.createdAt) : null, + createdBy: input.createdBy as string, + updatedAt: input.updatedAt ? new Date(input.updatedAt) : null, + updatedBy: input.updatedBy as string }; }, @@ -45,8 +47,10 @@ export default { submission: submission.fromPrismaModel(input.submission) as ChefsSubmissionForm, submissionId: input.submission_id as string, title: input.title || '', - createdAt: input.createdAt, - createdBy: input.createdBy + createdAt: input.createdAt?.toISOString() ?? null, + createdBy: input.createdBy, + updatedAt: input.updatedAt?.toISOString() ?? null, + updatedBy: input.updatedBy }; } }; diff --git a/app/src/db/prisma/schema.prisma b/app/src/db/prisma/schema.prisma index a27117f3..87b4f182 100644 --- a/app/src/db/prisma/schema.prisma +++ b/app/src/db/prisma/schema.prisma @@ -45,13 +45,15 @@ model identity_provider { } model note { - note_id String @id @unique(map: "note_note_id_unique") @db.Uuid + note_id String @id @db.Uuid submission_id String @db.Uuid note String @default("") note_type String @default("") title String @default("") - createdAt String? - createdBy 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: [submission_id], references: [submissionId], onDelete: Cascade, map: "note_submission_id_foreign") } diff --git a/app/src/services/note.ts b/app/src/services/note.ts index 4e9667fd..c65be0e5 100644 --- a/app/src/services/note.ts +++ b/app/src/services/note.ts @@ -1,22 +1,20 @@ import prisma from '../db/dataConnection'; import { note } from '../db/models'; import { v4 as uuidv4 } from 'uuid'; -import { addDashesToUuid } from '../components/utils'; -import type { CurrentUser, Note } from '../types'; -import { JwtPayload } from 'jsonwebtoken'; +import type { Note } from '../types'; const service = { /** * @function createNote - * Creates a Permit - * @param note Note Object + * Creates a note + * @param data Note Object + * @param identityId string * @returns {Promise} The result of running the findUnique operation */ - createNote: async (data: Note, currentUser: CurrentUser) => { + createNote: async (data: Note) => { const newNote = { ...data, - createdBy: addDashesToUuid((currentUser.tokenPayload as JwtPayload)?.idir_user_guid), noteId: uuidv4() }; const create = await prisma.note.create({ @@ -44,6 +42,9 @@ const service = { include: { user: true } } }, + orderBy: { + createdAt: 'desc' + }, where: { submission_id: submissionId } diff --git a/app/src/types/Note.ts b/app/src/types/Note.ts index 823ab994..156dfb3f 100644 --- a/app/src/types/Note.ts +++ b/app/src/types/Note.ts @@ -1,3 +1,4 @@ +import { IStamps } from '../interfaces/IStamps'; import type { ChefsSubmissionForm } from './ChefsSubmissionForm'; export type Note = { @@ -7,6 +8,4 @@ export type Note = { noteType: string; submission: ChefsSubmissionForm; title: string; - createdAt: string | null; - createdBy: string | null; -}; +} & Partial; diff --git a/frontend/src/components/note/NoteCard.vue b/frontend/src/components/note/NoteCard.vue index 23088705..2f7afc12 100644 --- a/frontend/src/components/note/NoteCard.vue +++ b/frontend/src/components/note/NoteCard.vue @@ -3,7 +3,7 @@ import { onMounted, ref } from 'vue'; import { Card, Divider } from '@/lib/primevue'; import { userService } from '@/services'; -import { formatDate } from '@/utils/formatters'; +import { formatDateShort } from '@/utils/formatters'; import type { Ref } from 'vue'; import type { Note } from '@/types'; @@ -17,13 +17,13 @@ type Props = { const props = withDefaults(defineProps(), {}); // State -const cardData: Ref = ref(props.note); const userName: Ref = ref(''); -onMounted(async () => { +onMounted(() => { if (props.note.createdBy) { - const res = (await userService.searchUsers({ identityId: [props.note.createdBy] })).data; - userName.value = res.length ? res.pop().fullName : ''; + userService.searchUsers({ userId: [props.note.createdBy] }).then((res) => { + userName.value = res?.data.length ? res?.data[0].fullName : ''; + }); } }); @@ -31,7 +31,7 @@ onMounted(async () => { - diff --git a/frontend/src/components/note/NoteModal.vue b/frontend/src/components/note/NoteModal.vue index a58617b1..6887fdcf 100644 --- a/frontend/src/components/note/NoteModal.vue +++ b/frontend/src/components/note/NoteModal.vue @@ -1,14 +1,12 @@