diff --git a/package-lock.json b/package-lock.json index a025f74..377e71c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -264,11 +264,11 @@ "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" }, "bcrypt": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/bcrypt/-/bcrypt-4.0.1.tgz", - "integrity": "sha512-hSIZHkUxIDS5zA2o00Kf2O5RfVbQ888n54xQoF/eIaquU4uaLxK8vhhBdktd0B3n2MjkcAWzv4mnhogykBKOUQ==", + "version": "3.0.8", + "resolved": "https://registry.npmjs.org/bcrypt/-/bcrypt-3.0.8.tgz", + "integrity": "sha512-jKV6RvLhI36TQnPDvUFqBEnGX9c8dRRygKxCZu7E+MgLfKZbmmXL8a7/SFFOyHoPNX9nV81cKRC5tbQfvEQtpw==", "requires": { - "node-addon-api": "^2.0.0", + "nan": "2.14.0", "node-pre-gyp": "0.14.0" } }, @@ -2044,6 +2044,11 @@ "integrity": "sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==", "dev": true }, + "nan": { + "version": "2.14.0", + "resolved": "https://registry.npmjs.org/nan/-/nan-2.14.0.tgz", + "integrity": "sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg==" + }, "natural-compare": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz", @@ -2086,11 +2091,6 @@ "integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==", "dev": true }, - "node-addon-api": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-2.0.0.tgz", - "integrity": "sha512-ASCL5U13as7HhOExbT6OlWJJUV/lLzL2voOSP1UVehpRD8FbSrSDjfScK/KwAvVTI5AS6r4VwbOMlIqtvRidnA==" - }, "node-pre-gyp": { "version": "0.14.0", "resolved": "https://registry.npmjs.org/node-pre-gyp/-/node-pre-gyp-0.14.0.tgz", diff --git a/package.json b/package.json index d818cae..cbac758 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "license": "MIT", "dependencies": { "@hapi/joi": "^17.1.0", - "bcrypt": "^4.0.1", + "bcrypt": "^3.0.8", "body-parser": "^1.19.0", "dotenv": "^8.2.0", "express": "^4.17.1", diff --git a/src/models/schema/attribute.js b/src/models/schema/attribute.js index 8f285d2..6b37d3e 100644 --- a/src/models/schema/attribute.js +++ b/src/models/schema/attribute.js @@ -10,7 +10,16 @@ module.exports = (sequelize, DataTypes) => { primaryKey: true, }, // This must be provided if editable is false - defaultValue: { type: DataTypes.TEXT }, + defaultValue: { + type: DataTypes.TEXT, + validation: { + defaultValueValidation(editable) { + if (editable === false && this.defaultValue !== null) throw new Error('defaultValue validation Error'); + }, + }, + + + }, editable: { type: DataTypes.BOOLEAN, defaultValue: false, diff --git a/src/models/schema/item.js b/src/models/schema/item.js index 0d0b3f3..8053f35 100644 --- a/src/models/schema/item.js +++ b/src/models/schema/item.js @@ -19,6 +19,11 @@ module.exports = (sequelize, DataTypes) => { labId: { type: DataTypes.UUID, references: { model: 'Lab', key: 'id' }, + Validation: { + labIdValidation(isDraft) { + if (isDraft === false && this.labId !== null) throw new Error('LabId validation failed'); + }, + }, }, itemSetId: { type: DataTypes.UUID, diff --git a/src/models/schema/request.js b/src/models/schema/request.js index ad24ac2..57ef16d 100644 --- a/src/models/schema/request.js +++ b/src/models/schema/request.js @@ -20,10 +20,40 @@ module.exports = (sequelize, DataTypes) => { references: { model: 'Supervisor', key: 'id' }, }, reason: { type: DataTypes.TEXT }, - declineReason: { type: DataTypes.TEXT }, + declineReason: { + type: DataTypes.TEXT, + Validation: { + declineReasonValidation(status) { + const nNullList = ['DECLINED', 'INVALIDATED']; + const nullList = ['REQUESTED', 'ACCEPTED']; + + nNullList.forEach((item) => { + if (status.type === item && this.declineReason === null) throw new Error('Decline Reason Validation Error!'); + }); + + nullList.forEach((item) => { + if (status.type === item && this.returnedDate !== null) throw new Error('Decline Reason Validation Error!'); + }); + }, + }, + }, supervisorToken: { type: DataTypes.UUID, unique: 'supervisor_token', + Validation: { + supervisorTokenValidation(status) { + const nNullList = ['REQUESTED', 'ACCEPTED']; + const nullList = ['DECLINED', 'INVALIDATED']; + + nNullList.forEach((item) => { + if (status.type === item && this.supervisorToken === null) throw new Error('Supervisor Token Validation Error!'); + }); + + nullList.forEach((item) => { + if (status.type === item && this.supervisorToken !== null) throw new Error('Supervisor Token Validation Error!'); + }); + }, + }, }, status: { type: DataTypes.ENUM('REQUESTED', 'ACCEPTED', 'DECLINED', 'INVALIDATED'), diff --git a/src/models/schema/request_item.js b/src/models/schema/request_item.js index 2dc0188..7dc10e7 100644 --- a/src/models/schema/request_item.js +++ b/src/models/schema/request_item.js @@ -10,9 +10,54 @@ module.exports = (sequelize, DataTypes) => { primaryKey: true, references: { model: 'Request', key: 'id' }, }, - returnedDate: { type: DataTypes.DATE }, - dueDate: { type: DataTypes.DATE }, - borrowedDate: { type: DataTypes.DATE }, + returnedDate: { + type: DataTypes.DATE, + Validation: { + returnedDateValidation(status) { + const nNullList = ['PENDING', 'BORROWED', 'REJECTED']; + const nullList = ['RETURNED', 'EXPIRED']; + nNullList.forEach((item) => { + if (status.type === item && this.returnedDate !== null) throw new Error('Returned Date Validation Error!'); + }); + + nullList.forEach((item) => { + if (status.type === item && this.returnedDate === null) throw new Error('Returned Date Validation Error!'); + }); + }, + }, + }, + dueDate: { + type: DataTypes.DATE, + Validation: { + dueDateValidation(status) { + const nNullList = ['PENDING', 'REJECTED']; + const nullList = ['RETURNED', 'BORROWED', 'EXPIRED']; + nNullList.forEach((item) => { + if (status.type === item && this.dueDate !== null) throw new Error('Due Date Validation Error!'); + }); + + nullList.forEach((item) => { + if (status.type === item && this.dueDate === null) throw new Error('Due Date Validation Error!'); + }); + }, + }, + }, + borrowedDate: { + type: DataTypes.DATE, + Validation: { + borrowedDateValidation(status) { + const nNullList = ['PENDING', 'REJECTED']; + const nullList = ['RETURNED', 'BORROWED', 'EXPIRED']; + nNullList.forEach((item) => { + if (status.type === item && this.borrowedDate !== null) throw new Error('Borrowed Date Validation Error!'); + }); + + nullList.forEach((item) => { + if (status.type === item && this.borrowedDate === null) throw new Error('Borrowed Date Validation Error!'); + }); + }, + }, + }, status: { type: DataTypes.ENUM('PENDING', 'BORROWED', 'RETURNED', 'REJECTED', 'EXPIRED'), defaultValue: 'PENDING',