Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✏️ Feat/ #22 Application Update methods #80

Merged
merged 29 commits into from
Dec 4, 2024

Conversation

demariadaniel
Copy link
Contributor

@demariadaniel demariadaniel commented Nov 28, 2024

Feat: #22: Application Update Methods

Summary

  • Adds Service Method to update Application details, with related API route

Related Issues

Description of Changes

  • Adds Application Service editApplication method for updating application contents
  • Method sets application state to DRAFT
  • Adds API editApplication function which validates application state allows edits
  • Adds first Express route for Applications
  • Updates to test files, and drizzle config / dbml config related to PR discussion on:
    https://github.com/Pan-Canadian-Genome-Library/daco/pull/40/files

Special Instructions

Changes to Drizzle config require running pnpm run build to allow drizzle-kit operations
drizzle-kit now uses /dist for schema lookup

Readiness Checklist

  • Self Review
    • I have performed a self review of code
    • I have run the application locally and manually tested the feature
  • PR Format
    • The PR title is properly formatted to match the pattern: #{TicketNumber}: Description of Changes
    • Links are included to all relevant tickets
  • Labels Added
    • Label is added for each package/app that is modified (api, ui, data-model, etc.)
    • Label is added for the type of work done in this PR (feature, fix, chore, documentation)
  • Local Testing
    • Successfully built all packages locally
    • Successfully ran all test suites, all unit and integration tests pass
  • Updated Tests
    • Unit and integration tests have been added that describe the bug that was fixed or the features that were added
  • Documentation
    • All new environment variables added to .env.schema file and documented in the README
    • All changes to server HTTP endpoints have open-api documentation
    • All new functions exported from their module have TSDoc comment documentation

@@ -6,7 +6,7 @@
"scripts": {
"build": "rimraf ./dist && tsc",
"dev": "tsx watch --env-file=.env ./src/main.ts",
"test": "npx tsx --test --test-reporter=spec --experimental-test-coverage ./src/tests/**/*",
"test": "tsx --test --test-reporter=spec --experimental-test-coverage ./tests/**/*.test.ts",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Feedback from #40

@@ -20,7 +20,7 @@
import assert from 'node:assert';
import { after, describe, it } from 'node:test';

import { port } from '../server.js';
import { port } from '../../src/server.js';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

test folder moved out of src

@demariadaniel demariadaniel linked an issue Dec 2, 2024 that may be closed by this pull request
@demariadaniel demariadaniel self-assigned this Dec 2, 2024
@demariadaniel demariadaniel marked this pull request as ready for review December 2, 2024 17:25
@demariadaniel demariadaniel changed the title WIP: Feat/22 application update methods ✏️ Feat/ #22 Application Update methods Dec 2, 2024
Copy link
Contributor

@JamesTLopez JamesTLopez left a comment

Choose a reason for hiding this comment

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

Minor stuff and a suggestion, otherwise lgtm 🔥

Comment on lines 36 to 40
const applicationRecord = result.data;

// Validate Application state will allow updates
// Edits to Applications under review will revert state to 'DRAFT'
const { state } = applicationRecord;
Copy link
Contributor

Choose a reason for hiding this comment

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

Variable applicationRecord doesn't need to be defined here we can deconstruct directly out of result.data

Comment on lines 31 to 35
if (result.success) {
res.send(result.data);
} else {
res.status(500).send({ error: `Error updating application with id ${id}` });
res.status(500).send({ message: result.message, errors: result.errors });
}
Copy link
Contributor

@JamesTLopez JamesTLopez Dec 2, 2024

Choose a reason for hiding this comment

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

More of a personal suggestion, I'm a fan of this syntax I find it easier to read and easier to add more error checks in the future if needed:

if (!result.success) {
	res.status(500).send({ message: result.message, errors: result.errors });
         return;
}

res.send(result.data);

But the current solution works if you wish to proceed.

Copy link
Contributor Author

@demariadaniel demariadaniel Dec 2, 2024

Choose a reason for hiding this comment

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

Yes fair, the else could be removed. Generally we prefer putting the 'truthy' condition first but your recommendation is still applicable.

Also in the code snippet above I think you copied the Git diff, double res.send's would trigger an error.
Just to be sure I understand you right, you're saying else is not needed here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

edit: My editor seems to prefer keeping the else as a type guard to enforce result is failure

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yes my bad, there would be a return statement after the correct res. I have updated it to what my initial thoughts were.

@demariadaniel
Copy link
Contributor Author

Endpoint tested with Postman:
Screenshot 2024-12-02 at 2 43 31 PM


- Drizzle is configured to read the schema from the build directory (`/dist`) due to a known Drizzle Kit issue with ESM imports. Any Drizzle Kit operations (including migrations) require first building the application with `pnpm run build`.

- A schema dbml (database markup language) file can be generated using the script `pnpm run dbml`. This file is found at `./src/db/schema.dbml`.
Copy link

Choose a reason for hiding this comment

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

👍

@@ -6,5 +6,5 @@
"module": "NodeNext",
"moduleResolution": "NodeNext"
},
"include": ["src", "./drizzle.config.ts"]
"include": ["src", "./drizzle.config.ts", "tests"]
Copy link

Choose a reason for hiding this comment

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

is there a reason why we are including test folder in here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was an auto update from moving folders around, doesn't need to be included

const applicationRouter = express.Router();
const jsonParser = bodyParser.json();

applicationRouter.post('/application/edit/', jsonParser, async (req, res) => {
Copy link

Choose a reason for hiding this comment

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

seems this route hasn't been included in the Swagger documentation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call -- we just added swagger yesterday. Adding now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added /edit Swagger endpoint, tested and working

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Screenshot 2024-12-04 at 12 08 40 PM

if (result.success) {
res.send(result.data);
} else {
res.status(500).send({ message: result.message, errors: String(result.errors) });
Copy link

Choose a reason for hiding this comment

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

I see this will always return a 500 error code when condition is falsy. However, considering there is a TODO to implement auth and Zod validations, just curious is this also includes the case where id doesn't exist in the database? would that return 4xx or 5xx?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes -- if id is not found we're returning a 500
I can update to a 4xx

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added basic updates for 404 handling. We don't have a central error handler setup yet so I've added a TODO as well.

Copy link

@leoraba leoraba left a comment

Choose a reason for hiding this comment

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

Great updates in this PR. I’ve left a few comments but so far looks good to me.

@demariadaniel demariadaniel requested a review from leoraba December 4, 2024 17:25
Comment on lines +57 to +68
'400':
description: 'Error editing application - Application not found'
content:
application/json:
schema:
type: object
'200':
description: 'Application updated'
content:
application/json:
schema:
type: object
Copy link

Choose a reason for hiding this comment

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

responses schemas can also be defined here using the structures defined by the success and failure helpers

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will add in #58

Copy link

@leoraba leoraba left a comment

Choose a reason for hiding this comment

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

LGTM

@demariadaniel demariadaniel merged commit 391846c into main Dec 4, 2024
@demariadaniel demariadaniel deleted the feat/22-application-update-methods branch December 4, 2024 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add to Application Layer - User Updating Application
3 participants