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/clin 2095 #35

Merged
merged 14 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
550 changes: 449 additions & 101 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@
"test": "jest --silent"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.312.0",
"@types/pg-format": "^1.0.2",
"@types/validator": "^13.7.1",
"@aws-sdk/client-s3": "^3.312.0",
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"express-xss-sanitizer": "^1.1.6",
"http-errors": "^1.7.2",
"http-status-codes": "^2.1.4",
"joi": "^17.9.2",
"keycloak-connect": "^15.0.2",
"node-pg-migrate": "^6.0.0",
"pg": "^8.7.1",
"sequelize": "^6.14.1",
"sequelize": "^6.28.2",
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

"uuidv4": "^6.2.13"
},
"devDependencies": {
Expand Down
7 changes: 7 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import cors from 'cors';
import express, { Express } from 'express';
import { sanitize, xss } from 'express-xss-sanitizer';
import { Keycloak } from 'keycloak-connect';

import { adminRoleName } from './config/env';
Expand All @@ -13,7 +14,13 @@ import { globalErrorHandler, globalErrorLogger } from './utils/errors';
export default (keycloak: Keycloak): Express => {
const app = express();

app.use((req, res, next) => {
req.body = sanitize(req.body);
next();
});

app.use(cors());
app.use(xss());
app.use(express.json({ limit: '50mb' }));

app.use(
Expand Down
25 changes: 24 additions & 1 deletion src/db/models/SavedFilter.ts
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add a custom validator for roles? making sure every role is alphanumerical only

Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,54 @@ SavedFilterModel.init(
type: DataTypes.STRING,
allowNull: false,
primaryKey: true,
validate: {
isUUID: 4,
},
},
keycloak_id: {
type: DataTypes.STRING,
allowNull: false,
validate: {
isUUID: 4,
Copy link
Contributor

Choose a reason for hiding this comment

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

Magic number.
const VERSION_4 = 4 ou const UUID_VERSION = 4 ou ...
ou regarder dans la lib sequilize si il existe une constante que tu pourrais importer au lieu de t'en créer une

},
},
title: {
Copy link
Contributor

Choose a reason for hiding this comment

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

sommes-nous sûr qu'il n'y a pas de titre funky avec des tirets "-" ou autres caractères du genre?

type: DataTypes.TEXT,
validate: {
isAlphanumeric: true,
},
},
title: DataTypes.TEXT,
tag: DataTypes.TEXT,
type: DataTypes.ENUM('query', 'filter'),
favorite: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
validate: {
isBoolean: true,
},
},
queries: {
type: DataTypes.ARRAY(DataTypes.JSONB),
allowNull: false,
defaultValue: [],
validate: {
isJSON: true,
},
},
creation_date: {
type: DataTypes.DATE,
defaultValue: new Date(),
validate: {
isDate: true,
},
},
updated_date: {
type: DataTypes.DATE,
defaultValue: new Date(),
validate: {
isDate: true,
},
},
},
{ sequelize: sequelizeConnection, modelName: 'saved_filters', timestamps: false },
Expand Down
108 changes: 97 additions & 11 deletions src/db/models/User.ts
Copy link
Contributor

Choose a reason for hiding this comment

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

Je pense qu'on devrait ajouter plus de validation de longeur, mais on pourra le faire dans un 2ième temps. Faut s'assurer que dans les BDs actuelles, on mette pas des contraintes qui ne soient pas compatibles avec le contenu de certains champs

Original file line number Diff line number Diff line change
Expand Up @@ -57,59 +57,145 @@ UserModel.init(
allowNull: false,
autoIncrement: true,
primaryKey: true,
unique: true,
validate: {
isInt: true,
},
},
keycloak_id: {
type: DataTypes.STRING,
allowNull: false,
validate: {
isUUID: 4,
},
},
deleted: {
type: DataTypes.BOOLEAN,
defaultValue: false,
validate: {
isBoolean: true,
},
},
first_name: {
type: DataTypes.CITEXT,
validate: {
len: [1, 20],
isAlpha: true,
},
},
last_name: {
type: DataTypes.CITEXT,
validate: {
len: [1, 20],
Copy link
Contributor

Choose a reason for hiding this comment

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

go with MAX=70

isAlpha: true,
},
},
era_commons_id: {
type: DataTypes.STRING,
validate: {
isAlpha: true,
},
},
nih_ned_id: {
type: DataTypes.STRING,
validate: {
isAlpha: true,
},
},
commercial_use_reason: {
type: DataTypes.STRING,
validate: {
isAlpha: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

alphanumeric

},
},
email: {
type: DataTypes.STRING,
validate: {
isEmail: true,
},
},
external_individual_fullname: {
type: DataTypes.TEXT,
validate: {
isAlpha: true,
},
},
external_individual_email: {
type: DataTypes.TEXT,
validate: {
isEmail: true,
},
},
first_name: DataTypes.CITEXT,
last_name: DataTypes.CITEXT,
era_commons_id: DataTypes.STRING,
nih_ned_id: DataTypes.STRING,
commercial_use_reason: DataTypes.STRING,
email: DataTypes.STRING,
external_individual_fullname: DataTypes.TEXT,
external_individual_email: DataTypes.TEXT,
roles: DataTypes.ARRAY(DataTypes.CITEXT),
affiliation: DataTypes.CITEXT,
public_email: DataTypes.TEXT,
linkedin: DataTypes.TEXT,
affiliation: {
type: DataTypes.CITEXT,
validate: {
isAlphanumeric: true,
},
},
public_email: {
type: DataTypes.TEXT,
validate: {
isEmail: true,
},
},
linkedin: {
type: DataTypes.TEXT,
validate: {
isUrl: true,
is: /^https?:\/\/(www\.)?linkedin\.com\/in\//i,
},
},
portal_usages: DataTypes.ARRAY(DataTypes.CITEXT),
research_domains: DataTypes.ARRAY(DataTypes.CITEXT),
research_area_description: DataTypes.TEXT,
profile_image_key: DataTypes.TEXT,
creation_date: {
type: DataTypes.DATE,
defaultValue: new Date(),
validate: {
isDate: true,
},
},
updated_date: {
type: DataTypes.DATE,
defaultValue: new Date(),
validate: {
isDate: true,
},
},
consent_date: DataTypes.DATE,
accepted_terms: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
validate: {
isBoolean: true,
},
},
understand_disclaimer: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
validate: {
isBoolean: true,
},
},
completed_registration: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
validate: {
isBoolean: true,
},
},
config: {
type: DataTypes.JSONB,
allowNull: false,
defaultValue: {},
validate: {
isJSON: true,
},
},
},
{ sequelize: sequelizeConnection, modelName: 'users', timestamps: false },
Expand Down
21 changes: 21 additions & 0 deletions src/db/models/UserSets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,53 @@ UserSetModel.init(
allowNull: false,
autoIncrement: true,
primaryKey: true,
validate: {
isUUID: 4,
},
},
keycloak_id: {
type: DataTypes.STRING,
allowNull: false,
validate: {
isUUID: 4,
Copy link
Contributor

Choose a reason for hiding this comment

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

même commentaire que plus haut

},
},
alias: {
type: DataTypes.STRING,
allowNull: false,
validate: {
isAlpha: true,
},
},
sharedpublicly: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
validate: {
isBoolean: true,
},
},
content: {
type: DataTypes.JSONB,
allowNull: false,
defaultValue: {},
validate: {
isJSON: true,
},
},
creation_date: {
type: DataTypes.DATE,
defaultValue: new Date(),
validate: {
isDate: true,
},
},
updated_date: {
type: DataTypes.DATE,
defaultValue: new Date(),
validate: {
isDate: true,
},
},
},
{ sequelize: sequelizeConnection, modelName: 'user_sets', timestamps: false },
Expand Down
10 changes: 9 additions & 1 deletion src/utils/errors.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { NextFunction, Request, Response } from 'express';
import { HttpError } from 'http-errors';
import { getReasonPhrase, StatusCodes } from 'http-status-codes';
import { BaseError, UniqueConstraintError } from 'sequelize';
import { BaseError, UniqueConstraintError, ValidationError } from 'sequelize';

export const globalErrorHandler = (err: unknown, _req: Request, res: Response, _next: NextFunction): void => {
if (err instanceof UniqueConstraintError) {
res.status(StatusCodes.UNPROCESSABLE_ENTITY).json({
error: 'A resource with the same id already exists.',
});
} else if (err instanceof ValidationError) {
const error = {
name: 'Invalid data',
errors: err.errors.map((error) => error.message.replace('%s', error.path)),
};
res.status(422).json({
Copy link
Contributor

Choose a reason for hiding this comment

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

  • magic number
  • tu peux le laisser sur un 422, mais pas sûr que ce soit nécéssairement le bon code...

error,
});
} else if (err instanceof HttpError) {
res.status(err.status).json({
error: err.message,
Expand Down