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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e3f3b78
Service Edit method
demariadaniel Nov 28, 2024
637f637
Move Test Files
demariadaniel Nov 28, 2024
501d6f0
Use .test naming convention
demariadaniel Nov 28, 2024
ed2be28
Move API test file
demariadaniel Nov 28, 2024
420a74f
Tests for Edit Method
demariadaniel Nov 28, 2024
c2ea32e
Remove npx
demariadaniel Nov 28, 2024
7b84efb
DBml / schema updates
demariadaniel Nov 29, 2024
1fb30cd
Use dist folder for config, add first readme update
demariadaniel Nov 29, 2024
a44b23c
Setup Router & API + Move logic out of service
demariadaniel Nov 29, 2024
42afe20
Working API Test w/ some reorg
demariadaniel Nov 29, 2024
eb27584
Edit for clarity
demariadaniel Dec 2, 2024
04f0dc6
Remove Demo endpoint
demariadaniel Dec 2, 2024
ffbe16f
Route + service improvements
demariadaniel Dec 2, 2024
ba949f7
Add todo
demariadaniel Dec 2, 2024
fc3e38a
Move service type, add error condition
demariadaniel Dec 2, 2024
5002a86
First updates with success/failure
demariadaniel Dec 2, 2024
728572e
Move tests
demariadaniel Dec 2, 2024
5d4915f
Use Success/Failure pattern with getById
demariadaniel Dec 2, 2024
060f027
Add TSDoc
demariadaniel Dec 2, 2024
2783fee
Add getDbInstance type
demariadaniel Dec 2, 2024
596affa
Throw err is no db instance
demariadaniel Dec 2, 2024
d3a8b30
Add body parser
demariadaniel Dec 2, 2024
b77bb36
Simplify variable declaration
demariadaniel Dec 2, 2024
c0079dc
Add Comments + Transaction
demariadaniel Dec 3, 2024
cfc1cb6
Merge branch 'main' of https://github.com/Pan-Canadian-Genome-Library…
demariadaniel Dec 3, 2024
f08b6a9
Print error message in response
demariadaniel Dec 3, 2024
fa1202e
Remove tests from tsconfig
demariadaniel Dec 4, 2024
fc7cce3
Add Edit endpoint to Swagger w/ 404
demariadaniel Dec 4, 2024
7ceb7ea
Add Error Todo
demariadaniel Dec 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ This app uses features from Node ^20.9, in combination with TSX for TypeScript s
### Database

- This application implements a Postgres database based on the following [data model](../../docs/model/README.md) and managed by [Drizzle ORM](https://orm.drizzle.team/docs/overview)

- 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.

👍

2 changes: 1 addition & 1 deletion apps/api/drizzle.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const connectionString = `postgres://${PG_USER}:${PG_PASSWORD}@${PG_HOST}

export default defineConfig({
out: './drizzle',
schema: './src/db/schemas/*',
schema: './dist/src/db/schemas/*',
dialect: 'postgresql',
dbCredentials: {
url: connectionString!,
Expand Down
217 changes: 0 additions & 217 deletions apps/api/drizzle/schema.ts

This file was deleted.

5 changes: 4 additions & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"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/**/*",
"dbml": "tsx ./src/db/dbml.ts",
"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

"start:prod": "node ./dist/src/main.js"
},
"devDependencies": {
Expand All @@ -24,6 +25,8 @@
},
"dependencies": {
"@pcgl-daco/data-model": "workspace:^",
"@types/body-parser": "^1.19.5",
"body-parser": "^1.20.3",
"cors": "^2.8.5",
"drizzle-orm": "^0.35.3",
"express": "^4.21.0",
Expand Down
59 changes: 59 additions & 0 deletions apps/api/src/api/application-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2024 The Ontario Institute for Cancer Research. All rights reserved
*
* This program and the accompanying materials are made available under the terms of
* the GNU Affero General Public License v3.0. You should have received a copy of the
* GNU Affero General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import { ApplicationStates } from '@pcgl-daco/data-model/src/types.js';
import { getDbInstance } from '../db/index.js';
import applicationService from '../service/application-service.js';
import { type ApplicationContentUpdates, type ApplicationService } from '../service/types.js';
import { failure } from '../utils/results.js';

/**
* Validates if a given Application state allows edits, then updates the record
* Updated records are returned in state 'DRAFT'
* @param id - Application ID
* @param update - Application Contents details to update
* @returns Success with Application data / Failure with Error
*/
export const editApplication = async ({ id, update }: { id: number; update: ApplicationContentUpdates }) => {
const database = getDbInstance();
const service: ApplicationService = applicationService(database);

const result = await service.getApplicationById({ id });

if (!result.success) {
return result;
}

const { state } = result.data;

// TODO: Replace w/ state machine https://github.com/Pan-Canadian-Genome-Library/daco/issues/58
const isEditState =
state === ApplicationStates.DRAFT ||
state === ApplicationStates.INSTITUTIONAL_REP_REVIEW ||
state === ApplicationStates.DAC_REVIEW;

if (isEditState) {
const result = await service.editApplication({ id, update });
return result;
} else {
const message = `Cannot update application with state ${state}`;
console.error(message);
return failure(message);
}
};
2 changes: 1 addition & 1 deletion apps/api/src/db/dbml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { pgGenerate } from 'drizzle-dbml-generator'; // Using Postgres for this example
import * as schema from '../../drizzle/schema.js';
import * as schema from './schemas/index.js';

const out = './src/db/schema.dbml';
const relational = true;
Expand Down
8 changes: 8 additions & 0 deletions apps/api/src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,17 @@ import { setStatus, Status } from '../app-health.js';

export type PostgresDb = ReturnType<typeof drizzle>;

let pgDatabase: PostgresDb;

export const getDbInstance = (): PostgresDb => {
if (!pgDatabase) throw new Error('Not connected to Postgres database');
return pgDatabase;
};

export const connectToDb = (connectionString: string): PostgresDb => {
try {
const db = drizzle(connectionString);
pgDatabase = db;

setStatus('db', { status: Status.OK });
return db;
Expand Down
21 changes: 19 additions & 2 deletions apps/api/src/db/schema.dbml
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ table applications {
state application_states [not null]
created_at timestamp [not null, default: `now()`]
approved_at timestamp
updated_at timestamp
expires_at timestamp
contents bigint
updated_at timestamp
}

table collaborators {
Expand All @@ -132,6 +132,7 @@ table files {
type file_types [not null]
submitter_user_id varchar(100) [not null]
submitted_at timestamp [not null]
content bytea [not null]
filename varchar(255)
}

Expand All @@ -150,4 +151,20 @@ table revision_requests {
project_notes text
requested_studies_approved boolean [not null]
requested_studies_notes text
}
}

ref: actions.application_id - applications.id

ref: actions.revisions_request_id > revision_requests.id

ref: agreements.application_id > application_contents.application_id

ref: applications.contents - application_contents.id

ref: application_contents.signed_pdf - files.id

ref: collaborators.application_id > application_contents.application_id

ref: files.application_id - applications.id

ref: revision_requests.application_id > application_contents.id
Loading