Skip to content

Commit

Permalink
Refactor NoteModal to correctly use default props
Browse files Browse the repository at this point in the history
Minor other tweaks - SubmissionForm always editable
  • Loading branch information
kyle1morel committed Apr 19, 2024
1 parent 0d4658a commit 09cba9e
Show file tree
Hide file tree
Showing 22 changed files with 218 additions and 301 deletions.
2 changes: 1 addition & 1 deletion app/src/controllers/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const controller = {
req.body.mimeType,
req.body.length
);
res.status(200).json(response);
res.status(201).json(response);
} catch (e: unknown) {
next(e);
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/controllers/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const controller = {
...body,
createdBy: userId
});
res.status(200).json(response);
res.status(201).json(response);
} catch (e: unknown) {
next(e);
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/controllers/permit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const controller = {
try {
const userId = await userService.getCurrentUserId(getCurrentIdentity(req.currentUser, NIL), NIL);
const response = await permitService.createPermit({ ...(req.body as Permit), updatedBy: userId });
res.status(200).json(response);
res.status(201).json(response);
} catch (e: unknown) {
next(e);
}
Expand Down
44 changes: 22 additions & 22 deletions app/src/controllers/submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,6 @@ import type { NextFunction, Request, Response } from '../interfaces/IExpress';
import type { ChefsFormConfig, ChefsFormConfigData, Submission, ChefsSubmissionExport, Permit } from '../types';

const controller = {
createEmptySubmission: async (req: Request, res: Response, next: NextFunction) => {
let testSubmissionId;
let submissionQuery;

// Testing for activityId collisions, which are truncated UUIDs
// If a collision is detected, generate new UUID and test again
do {
testSubmissionId = uuidv4();
submissionQuery = await submissionService.getSubmission(testSubmissionId.substring(0, 8).toUpperCase());
} while (submissionQuery);

try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const submitter = (req.currentUser?.tokenPayload as any)?.idir_username;
const result = await submissionService.createEmptySubmission(testSubmissionId, submitter);

res.status(201).json({ activityId: result.activity_id });
} catch (e: unknown) {
next(e);
}
},

checkAndStoreNewSubmissions: async () => {
const cfg = config.get('server.chefs.forms') as ChefsFormConfig;

Expand Down Expand Up @@ -175,6 +153,28 @@ const controller = {
notStored.map((x) => x.permits?.map(async (y) => await permitService.createPermit(y)));
},

createEmptySubmission: async (req: Request, res: Response, next: NextFunction) => {
let testSubmissionId;
let submissionQuery;

// Testing for activityId collisions, which are truncated UUIDs
// If a collision is detected, generate new UUID and test again
do {
testSubmissionId = uuidv4();
submissionQuery = await submissionService.getSubmission(testSubmissionId.substring(0, 8).toUpperCase());
} while (submissionQuery);

try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const submitter = (req.currentUser?.tokenPayload as any)?.idir_username;
const result = await submissionService.createEmptySubmission(testSubmissionId, submitter);

res.status(201).json({ activityId: result.activity_id });
} catch (e: unknown) {
next(e);
}
},

getStatistics: async (
req: Request<never, { dateFrom: string; dateTo: string; monthYear: string; userId: string }>,
res: Response,
Expand Down
28 changes: 20 additions & 8 deletions app/src/services/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ const service = {
return note.fromPrismaModel(response);
},

/**
* @function deleteNote
* Soft deletes a note by marking is as deleted
* @param {string} noteId ID of the note to delete
* @returns {Promise<Note>} The result of running the update operation
*/
deleteNote: async (noteId: string) => {
const result = await prisma.note.update({
where: {
Expand Down Expand Up @@ -76,8 +82,15 @@ const service = {
return response.map((x) => note.fromPrismaModel(x));
},

/**
* @function updateNote
* Updates a note by marking the old note as deleted and creating a new one
* @param {Note} data New Note object
* @returns {Promise<Note>} The result of running the transaction
*/
updateNote: async (data: Note) => {
return await prisma.$transaction(async (trx) => {
// Mark old note as deleted
await trx.note.update({
where: {
note_id: data.noteId
Expand All @@ -87,16 +100,15 @@ const service = {
}
});

const newNote = {
...data,
noteId: uuidv4()
};

const newCreatedNote = await trx.note.create({
data: note.toPrismaModel(newNote)
// Create new note
const response = await trx.note.create({
data: note.toPrismaModel({
...data,
noteId: uuidv4()
})
});

return note.fromPrismaModel(newCreatedNote);
return note.fromPrismaModel(response);
});
}
};
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/file/DocumentCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import type { Document } from '@/types';
// Props
type Props = {
document: Document;
deleteButton?: boolean;
document: Document;
selectable?: boolean;
selected?: boolean;
};
Expand Down
9 changes: 4 additions & 5 deletions frontend/src/components/file/FileUpload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,21 @@ const fileInput: Ref<any> = ref(null);
// Actions
const toast = useToast();
const onFileUploadDragAndDrop = (event: FileUploadUploaderEvent) => {
onUpload(Array.isArray(event.files) ? event.files[0] : event.files);
};
const onFileUploadClick = () => {
fileInput.value.click();
};
const onFileUploadDragAndDrop = (event: FileUploadUploaderEvent) => {
onUpload(Array.isArray(event.files) ? event.files[0] : event.files);
};
const onUpload = async (file: File) => {
try {
const response = (await documentService.createDocument(file, props.activityId, getConfig.value.coms.bucketId))
?.data;
if (response) {
submissionStore.addDocument(response);
toast.success('Document uploaded');
}
} catch (e: any) {
Expand Down
44 changes: 8 additions & 36 deletions frontend/src/components/note/NoteCard.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { Button, Card, Divider, useToast } from '@/lib/primevue';
import NoteModal from '@/components/note/NoteModal.vue';
import { noteService, userService } from '@/services';
import { Button, Card, Divider } from '@/lib/primevue';
import { userService } from '@/services';
import { formatDate, formatDateShort } from '@/utils/formatters';
import type { Ref } from 'vue';
Expand All @@ -16,40 +16,11 @@ type Props = {
const props = withDefaults(defineProps<Props>(), {});
// Emits
const emit = defineEmits(['note:delete', 'note:edit']);
// State
const userName: Ref<string> = ref('');
const noteModalVisible: Ref<boolean> = ref(false);
const editNoteData: Ref<Note | undefined> = ref(undefined);
const userName: Ref<string> = ref('');
// Actions
const toast = useToast();
const editNote = (note: Note) => {
editNoteData.value = note;
noteModalVisible.value = true;
};
const onNoteSubmit = async (data: Note) => {
editNoteData.value = undefined;
try {
const newNote = (await noteService.updateNote(data)).data;
emit('note:edit', newNote, data.noteId);
toast.success('Note saved');
} catch (e: any) {
toast.error('Failed to save note', e.message);
} finally {
noteModalVisible.value = false;
}
};
const onNoteDelete = (noteId: string) => {
emit('note:delete', noteId);
};
onMounted(() => {
if (props.note.createdBy) {
userService.searchUsers({ userId: [props.note.createdBy] }).then((res) => {
Expand Down Expand Up @@ -77,7 +48,7 @@ onMounted(() => {
<Button
class="p-button-outlined"
aria-label="Edit"
@click="editNote(props.note)"
@click="noteModalVisible = true"
>
<font-awesome-icon
class="pr-2"
Expand Down Expand Up @@ -132,11 +103,12 @@ onMounted(() => {
</div>
</template>
</Card>

<NoteModal
v-if="props.note"
v-model:visible="noteModalVisible"
:note="editNoteData"
@note:delete="onNoteDelete"
@note:submit="onNoteSubmit"
:activity-id="props.note.activityId"
:note="props.note"
/>
</template>

Expand Down
Loading

0 comments on commit 09cba9e

Please sign in to comment.