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

convert to typescript #95

Merged
merged 16 commits into from
Mar 24, 2022
Merged

convert to typescript #95

merged 16 commits into from
Mar 24, 2022

Conversation

j-chad
Copy link
Contributor

@j-chad j-chad commented Mar 23, 2022

Description

Converts the backend codebase to typescript

Fixes/resolves #90

Type of change

  • Improvement (non-breaking change which improves existing functionality)

Checklist:

Leave blank if not applicable

I have completed these steps when making this pull request:

  • I have checked that the PR is from a forked repository
  • I have assigned my name to the issue
  • I have moved the issue to the In Progress column
  • I have labelled the PR appropriately
  • I have assigned people responsible to the PR

Before opening the PR for review:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have documented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have moved the linked issue to the Review in Progress column

@j-chad j-chad added the enhancement New feature or request label Mar 23, 2022
@j-chad j-chad self-assigned this Mar 23, 2022
* @param databaseName the name of the database being connected to
* @param isTestDatabase conditional for if the database in use is for testing
*/
export function connect(databaseName: string, isTestDatabase: boolean = false): Promise<typeof database> {
Copy link

Choose a reason for hiding this comment

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

It looks like these function parameters are never used

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah I overwrote some of the code so that it runs locally. Will change this before opening

Comment on lines 31 to 35
if (isTestDatabase) {
return database.connection.readyState;
} else {
return database.connection.readyState;
}
Copy link

Choose a reason for hiding this comment

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

This if statement is redundant if we do the same thing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah I'm not sure what the thought was there 😆

Copy link
Collaborator

@fishmonger45 fishmonger45 left a comment

Choose a reason for hiding this comment

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

looks fine though the tests fail all fail on me. but i probably haven't set something up properly. i'll report back.
well done on keeping the conversion unopinionated!

"@types/convict": "^6.1.1",
"@types/express": "^4.17.13",
"@types/mocha": "^9.1.0",
"@types/mongoose": "^5.11.97",
Copy link
Collaborator

@fishmonger45 fishmonger45 Mar 24, 2022

Choose a reason for hiding this comment

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

mongoose has it's own types afaik, so don't need this?

Comment on lines +33 to +35
dbServerRoutes(app);
forumServerRoutes(app);
userServerRoutes(app);
Copy link
Collaborator

Choose a reason for hiding this comment

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

is dbServerRoutes etc usually put into a reexports file so that you can do the imports on a single line? eg: as a file routes/routes.module.ts

User.searchUserByAuthToken(authToken, function (result) {
if (result.status !== 200) {
return res.sendStatus(result.status).message(result.message);
console.log(result)
Copy link
Collaborator

Choose a reason for hiding this comment

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

extra log here. noting just in case you missed; probably on purpose because authtoken stuff

Copy link
Contributor Author

@j-chad j-chad Mar 24, 2022

Choose a reason for hiding this comment

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

yeah this was in there before. I will remove it

Comment on lines +6 to 9
export function isValidDocumentID(id) {
const regExpDocumentID = new RegExp('^[a-fA-F0-9]{24}$');
return id && typeof id === 'string' && id.length === 24 && regExpDocumentID.test(id);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

goodness... we can probably kill some of this validate stuff w/ types when we refactor

Copy link
Contributor Author

Choose a reason for hiding this comment

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

absolutely

})
.catch((err) => {
// Forum post is already in the database with unique attributes, return duplicate conflict error
if (err.code === 11000) {
Copy link
Collaborator

@fishmonger45 fishmonger45 Mar 24, 2022

Choose a reason for hiding this comment

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

this code seems like magic to me; what is this error code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's a mongodb error code. I think we should fix this in another ticket later

* @param params object containing forum post attributes
* @param done function callback, returns status code, and message if error, or JSON if successful
*/
export function insertPost (params, done) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

we should run prettier over the project

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll add this as part of #93

});
.then((res) => {
if (res.hashedPassword.match(hashPassword(plaintextPassword))) {
return done({status: 200, res})
Copy link
Collaborator

Choose a reason for hiding this comment

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

should replace these magics with the enums if avail.

server.ts Outdated

// Database name - use environment variable DATABASE_NAME or enter here
const databaseName = process.env.DATABASE_NAME;
const databaseName = process.env.DATABASE_NAME as string;
Copy link
Collaborator

@fishmonger45 fishmonger45 Mar 24, 2022

Choose a reason for hiding this comment

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

doesn't seem to be used & don't envvars always cast as string? (is the explicit cast required?)

@j-chad
Copy link
Contributor Author

j-chad commented Mar 24, 2022

the tests are very very broken it's not just you

@j-chad j-chad marked this pull request as ready for review March 24, 2022 03:20
@j-chad j-chad requested a review from a team as a code owner March 24, 2022 03:20
@j-chad j-chad requested review from kaylward and removed request for a team March 24, 2022 03:20
updates['comments'] = updates.comments ? result.comments.concat(updates.comments) : result.comments

// If the update does not just involve up votes, down votes, or comments, and actual edits
if (Object.keys(updates).length > 3) {
Copy link

Choose a reason for hiding this comment

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

Can we make this magic number a constant

Copy link
Contributor Author

@j-chad j-chad Mar 24, 2022

Choose a reason for hiding this comment

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

Let's not focus on all the unrelated issues right now, there are more than enough of them to fix later. If we try fix them now we cant parallelise efficiently

*/
describe("Database connection test", function() {
it("should connect to and disconnect from the database with correct statuses", async function() {
const testDatabaseName = process.env.DATABASE_TEST_NAME;
Copy link

Choose a reason for hiding this comment

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

This variable looks like it is never used

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah as per our meeting we will be ignoring tests until they can be reviewed and fixed

Copy link

Choose a reason for hiding this comment

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

gotcha

Copy link
Contributor Author

Choose a reason for hiding this comment

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

they're very broken and dependant on a prod database. I have started some initial work on fixing them but we want to get this in first

Copy link

Choose a reason for hiding this comment

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

yikes. thats good and makes sense though. thanks for the heads up

Copy link

@mped822 mped822 left a comment

Choose a reason for hiding this comment

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

lgtm

@j-chad j-chad merged commit 7cf1a6c into SE701-T5:main Mar 24, 2022
@j-chad j-chad deleted the 90-typescript branch March 24, 2022 04:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Convert to Typescript
3 participants