Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- providerSchema.pre('save', function(next) {
- if(this.level === 4){
- if(this.description.length >= 80 || this.certifications.length > 5){
- next(new Error('your current plan does not have this feature'));
- } else {
- next()
- }
- } else if(this.level === 3){
- if(this.description.length >= 50 || this.certifications.length > 3){
- next(new Error('your current plan does not have this feature'));
- } else {
- next()
- }
- } else if(this.level === 2){
- if(this.description.length >= 30 || this.certifications.length > 0 || this.teaser || this.social.length > 0){
- next(new Error('your current plan does not have this feature'));
- } else {
- next()
- }
- } else if(this.level === 1){
- if(this.description || this.certifications.length > 0 || this.teaser || this.social.length > 0 || this.locationLat || this.locationLong || this.workingHourEnd || this.workingHourStart){
- next(new Error('your current plan does not have this feature'));
- } else {
- next()
- }
- } else {
- if(this.description || this.certifications.length > 0 || this.teaser || this.social.length > 0 || this.locationLat || this.locationLong || this.workingHourEnd || this.workingHourStart){
- next(new Error('your current plan does not have this feature'));
- } else {
- next()
- }
- });
- providerSchema.pre('save', function(next) {
- if(((this.level === 4) && (this.description.length >= 80 || this.certifications.length > 5)) ||
- ((this.level === 3) && (this.description.length >= 50 || this.certifications.length > 3)) ||
- ((this.level === 2) && (this.description.length >= 30 || this.certifications.length > 0 || this.teaser || this.social.length > 0)) ||
- ((this.level === 1 || this.level === 0) && (this.description || this.certifications.length > 0 || this.teaser || this.social.length > 0 || this.locationLat || this.locationLong || this.workingHourEnd || this.workingHourStart))){
- next(new Error('your current plan does not have this feature'));
- } else {
- next()
- }
- });
- ```
- providerSchema.pre('save', function (next) {
- var valid = true;
- if (this.level === 4) {
- if (this.description.length >= 80 || this.certifications.length > 5) {
- valid = false;
- }
- } else if (this.level === 3) {
- if (this.description.length >= 50 || this.certifications.length > 3) {
- valid = false;
- }
- } else if (this.level === 2) {
- if (this.description.length >= 30 || this.certifications.length > 0 || this.teaser || this.social.length > 0) {
- valid = false;
- }
- } else if (this.level === 1) {
- if (this.description || this.certifications.length > 0 || this.teaser || this.social.length > 0 || this.locationLat || this.locationLong || this.workingHourEnd || this.workingHourStart) {
- valid = false;
- }
- } else {
- if (this.description || this.certifications.length > 0 || this.teaser || this.social.length > 0 || this.locationLat || this.locationLong || this.workingHourEnd || this.workingHourStart) {
- valid = false;
- }
- }
- if (valid) {
- next();
- } else {
- next(new Error('your current plan does not have this feature'));
- }
- });
- providerSchema.pre('save', function (next) {
- if (isValidFeatureRequest()) {
- next();
- } else {
- next(new Error('your current plan does not have this feature'));
- }
- function isValidFeatureRequest() {
- if (this.level === 4) {
- if (this.description.length >= 80 || this.certifications.length > 5) {
- return false;
- }
- } else if (this.level === 3) {
- if (this.description.length >= 50 || this.certifications.length > 3) {
- return false;
- }
- } else if (this.level === 2) {
- if (this.description.length >= 30 || this.certifications.length > 0 || this.teaser || this.social.length > 0) {
- return false;
- }
- } else if (this.level === 1) {
- if (this.description || this.certifications.length > 0 || this.teaser || this.social.length > 0 || this.locationLat || this.locationLong || this.workingHourEnd || this.workingHourStart) {
- return false;
- }
- } else {
- if (this.description || this.certifications.length > 0 || this.teaser || this.social.length > 0 || this.locationLat || this.locationLong || this.workingHourEnd || this.workingHourStart) {
- return false;
- }
- }
- return true;
- }
- });
- providerSchema.pre('save', function (next) {
- if (isValidFeatureRequest()) {
- next();
- } else {
- next(new Error('your current plan does not have this feature'));
- }
- function isValidFeatureRequest() {
- if (this.level === 4) {
- if (this.description.length >= 80 || this.certifications.length > 5) {
- return false;
- }
- } else if (this.level === 3) {
- if (this.description.length >= 50 || this.certifications.length > 3) {
- return false;
- }
- } else if (this.level === 2) {
- if (this.description.length >= 30 || this.certifications.length > 0 || this.teaser || this.social.length > 0) {
- return false;
- }
- } else {
- if (this.description || this.certifications.length > 0 || this.teaser || this.social.length > 0 || this.locationLat || this.locationLong || this.workingHourEnd || this.workingHourStart) {
- return false;
- }
- }
- return true;
- }
- });
- function | CC
- -------------------------------------------
- providerSchema | 2
- isValidFeatureRequest | 20
- providerSchema.pre('save', function (next) {
- if (isValidFeatureRequest()) {
- next();
- } else {
- next(new Error('your current plan does not have this feature'));
- }
- function isValidFeatureRequest() {
- if (this.level === 4) {
- if (descriptionOrCertificationsOffLimits(80, 5)) {
- return false;
- }
- } else if (this.level === 3) {
- if (descriptionOrCertificationsOffLimits(40, 3)) {
- return false;
- }
- } else if (this.level === 2) {
- if (descriptionOrCertificationsOffLimits(30, 0) || this.teaser || this.social.length > 0) {
- return false;
- }
- } else {
- if (this.description || this.certifications.length > 0 || this.teaser || this.social.length > 0 || this.locationLat || this.locationLong || this.workingHourEnd || this.workingHourStart) {
- return false;
- }
- }
- return true;
- }
- function descriptionOrCertificationsOffLimits(descriptionLimit, certificationsLimit) {
- return this.description.length >= descriptionLimit || this.certifications.length > certificationsLimit;
- }
- });
- function | CC
- -------------------------------------------
- providerSchema | 2
- isValidFeatureRequest | 17
- descriptionOrCertificationsOffLimits | 2
- providerSchema.pre('save', function (next) {
- if (isValidFeatureRequest()) {
- next();
- } else {
- next(new Error('your current plan does not have this feature'));
- }
- function isValidFeatureRequest() {
- if (this.level === 4) {
- if (descriptionOrCertificationsOffLimits(80, 5)) {
- return false;
- }
- } else if (this.level === 3) {
- if (descriptionOrCertificationsOffLimits(40, 3)) {
- return false;
- }
- } else if (this.level === 2) {
- if (descriptionOrCertificationsOffLimits(30, 0) || this.teaser || this.social.length > 0) {
- return false;
- }
- } else if (hasAny()) {
- return false;
- }
- return true;
- }
- function descriptionOrCertificationsOffLimits(descriptionLimit, certificationsLimit) {
- return this.description.length >= descriptionLimit || this.certifications.length > certificationsLimit;
- }
- function hasAny() {
- return this.description || this.certifications.length > 0 || this.teaser || this.social.length > 0 || this.locationLat || this.locationLong || this.workingHourEnd || this.workingHourStart;
- }
- });
- function | CC
- -------------------------------------------
- providerSchema | 2
- isValidFeatureRequest | 10
- descriptionOrCertificationsOffLimits | 2
- hasAny | 8
- function hasAny() {
- return this.description ||
- this.certifications.length > 0 ||
- this.teaser ||
- this.social.length > 0 ||
- this.locationLat ||
- this.locationLong ||
- this.workingHourEnd ||
- this.workingHourStart;
- }
- providerSchema.pre('save', function (next) {
- if (isValidFeatureRequest()) {
- next();
- } else {
- next(new Error('your current plan does not have this feature'));
- }
- function isValidFeatureRequest() {
- if (this.level === 4) {
- if (descriptionOrCertificationsOffLimits(80, 5)) {
- return false;
- }
- } else if (this.level === 3) {
- if (descriptionOrCertificationsOffLimits(40, 3)) {
- return false;
- }
- } else if (this.level === 2) {
- if (descriptionOrCertificationsOffLimits(30, 0) || hasTeaserOrSocial()) {
- return false;
- }
- } else if (hasAny()) {
- return false;
- }
- return true;
- }
- function descriptionOrCertificationsOffLimits(descriptionLimit, certificationsLimit) {
- return this.description.length >= descriptionLimit || this.certifications.length > certificationsLimit;
- }
- function hasTeaserOrSocial() {
- return this.teaser || this.social.length > 0;
- }
- function hasAny() {
- return this.description ||
- this.certifications.length > 0 ||
- this.teaser ||
- this.social.length > 0 ||
- this.locationLat ||
- this.locationLong ||
- this.workingHourEnd ||
- this.workingHourStart;
- }
- });
- function | CC
- -------------------------------------------
- providerSchema | 2
- isValidFeatureRequest | 9
- descriptionOrCertificationsOffLimits | 2
- hasTeaserOrSocial | 2
- hasAny | 8
- function isValidFeatureRequest() {
- if (this.level === 4 && descriptionOrCertificationsOffLimits(80, 5)) {
- return false;
- } else if (this.level === 3 && descriptionOrCertificationsOffLimits(40, 3)) {
- return false;
- } else if (this.level === 2 && descriptionOrCertificationsOffLimits(30, 0) || hasTeaserOrSocial()) {
- return false;
- } else if (hasAny()) {
- return false;
- }
- return true;
- }
- function isValidFeatureRequest() {
- if (isLevel4AndNotValid()) {
- return false;
- } else if (isLevel3AndNotValid()) {
- return false;
- } else if (isLevel2AndNotValid()) {
- return false;
- } else if (hasAny()) {
- return false;
- }
- return true;
- }
- function isLevel4AndNotValid() {
- return this.level === 4 && descriptionOrCertificationsOffLimits(80, 5);
- }
- function isLevel3AndNotValid() {
- return this.level === 3 && descriptionOrCertificationsOffLimits(40, 3);
- }
- function isLevel2AndNotValid() {
- this.level === 2 && (descriptionOrCertificationsOffLimits(30, 0) || hasTeaserOrSocial());
- }
- function | CC
- -------------------------------------------
- providerSchema | 2
- isValidFeatureRequest | 5
- isLevel4AndNotValid | 2
- isLevel3AndNotValid | 2
- isLevel2AndNotValid | 3
- descriptionOrCertificationsOffLimits | 2
- hasTeaserOrSocial | 2
- hasAny | 8
- function isValidFeatureRequest() {
- if (isLevel4AndNotValid()) {
- return false;
- }
- if (isLevel3AndNotValid()) {
- return false;
- }
- if (isLevel2AndNotValid()) {
- return false;
- }
- return !hasAny();
- }
- providerSchema.pre('save', function (next) {
- if (isValidFeatureRequest()) {
- next();
- } else {
- next(new Error('your current plan does not have this feature'));
- }
- function isValidFeatureRequest() {
- if (isLevel4AndNotValid()) {
- return false;
- }
- if (isLevel3AndNotValid()) {
- return false;
- }
- if (isLevel2AndNotValid()) {
- return false;
- }
- return !hasAny();
- }
- function isLevel4AndNotValid() {
- return this.level === 4 && descriptionOrCertificationsOffLimits(80, 5);
- }
- function isLevel3AndNotValid() {
- return this.level === 3 && descriptionOrCertificationsOffLimits(40, 3);
- }
- function isLevel2AndNotValid() {
- this.level === 2 && (descriptionOrCertificationsOffLimits(30, 0) || hasTeaserOrSocial());
- }
- function descriptionOrCertificationsOffLimits(descriptionLimit, certificationsLimit) {
- return this.description.length >= descriptionLimit || this.certifications.length > certificationsLimit;
- }
- function hasTeaserOrSocial() {
- return this.teaser || this.social.length > 0;
- }
- function hasAny() {
- return this.description ||
- this.certifications.length > 0 ||
- this.teaser ||
- this.social.length > 0 ||
- this.locationLat ||
- this.locationLong ||
- this.workingHourEnd ||
- this.workingHourStart;
- }
- });
- function | CC
- -------------------------------------------
- providerSchema | 2
- isValidFeatureRequest | 4
- isLevel4AndNotValid | 2
- isLevel3AndNotValid | 2
- isLevel2AndNotValid | 3
- descriptionOrCertificationsOffLimits | 2
- hasTeaserOrSocial | 2
- hasAny | 8
- function bar(foo) {
- if (foo === 2) { foo = 3 } // path 1
- else { foo = 4 } // path 2
- return foo;
- }
- function bar(foo) {
- if (foo === 2) { foo = 3 } // path 1
- // hidden else path 2
- return foo;
- }
- function bar(foo) {
- if (foo === 2) { foo = 3 } // path 1
- else if (foo === 4) { foo = 5 } // path 2
- else { foo = 4 } // path 3
- return foo;
- }
- function bar(foo) {
- if (foo === 2 || foo === 4) { foo = 3 } // path 1 and 2
- else { foo = 4 } // path 3
- return foo;
- }
- function poo(foo) {
- if (foo === 2 || foo === 4) { foo = 3 }
- else { foo = 4 }
- return foo;
- }
- function bar(foo) {
- if (foo === 2 || foo === 4) { foo = 3 }
- else { foo = poo(foo) }
- return foo;
- }
- providerSchema.pre('save', function(next) {
- if(((this.level === 4) && (this.description.length >= 80 || this.certifications.length > 5)) ||
- ((this.level === 3) && (this.description.length >= 50 || this.certifications.length > 3)) ||
- ((this.level === 2) && (this.description.length >= 30 || this.certifications.length > 0 || this.teaser || this.social.length > 0)) ||
- ((this.level === 1 || this.level === 0) && (this.description || this.certifications.length > 0 || this.teaser || this.social.length > 0 || this.locationLat || this.locationLong || this.workingHourEnd || this.workingHourStart))){
- next(new Error('your current plan does not have this feature'));
- } else {
- next()
- }
- });
- function foo(a) {
- if(a === 1) { a = 2 }
- else if (a === 2) { a = 3 }
- else if (a === 3) { a = 4 }
- else { a = undefined }
- return a;
- }
- function foo(a) {
- return ({"1": 2, "2": 3, "3": 4})[a];
- }
- providerSchema.pre('save', function(next) {
- const checkSocial = () => this.description || this.teaser || this.social.length > 0;
- const checkLocation = () => this.locationLat || this.locationLong || this.workingHourEnd || this.workingHourStart;
- const fail = () => false;
- const levels = {
- "4": {desc: 80, cert: 6, fail},
- "3": {desc: 50, cert: 4, fail},
- "2": {desc: 30, cert: 1, fail() { return checkSocial() } },
- "1": {desc: -1, cert: 1, fail() { return checkSocial() || checkLocation() } },
- "0": {desc: -1, cert: 1, fail() { return checkSocial() || checkLocation() } },
- };
- const checkPass= ({level, description, certifications}) => {
- if(levels[level]) {
- const check = levels[level];
- if(check.fail() && check.desc < description.length && check.cert < certifications.length) {
- return false;
- }
- }
- return true;
- }
- checkPass(this) ? next() : next(new Error("Your current plan does not have this feature."));
- });
- function validateData(data) {
- switch(data.level) {
- case 0:
- case 1:
- return data.description || data.certifications.length > 0 || data.teaser || data.social.length > 0 || data.locationLat || data.locationLong || data.workingHourEnd || data.workingHourStart
- case 2:
- return data.description.length >= 30 || data.certifications.length > 0 || data.teaser || data.social.length > 0);
- case 3:
- return (data.description.length >= 50 || data.certifications.length > 3));
- case 4:
- return (data.description.length >= 80 || data.certifications.length > 5);
- }
- }
- providerSchema.pre('save', function(next) {
- if(validateData(this)){
- next(new Error('your current plan does not have this feature'));
- } else {
- next()
- }
- });
- // For each data field we care about, at what level do various
- // conditions on that field become available?
- const LEVEL_REQUIREMENTS = [
- ({description}) => {
- if (description.length >= 80) return 5; // or maybe Infinity?
- if (description.length >= 50) return 4;
- if (description.length >= 30) return 3;
- if (description) return 2;
- return 0;
- },
- ({certifications}) => {
- if (certifications.length > 5) return 5;
- if (certifications.length > 3) return 4;
- if (certifications.length > 0) return 3;
- return 0;
- },
- ({teaser}) => teaser ? 3 : 0,
- ({social}) => social.length > 0 ? 3 : 0,
- ({locationLat}) => locationLat ? 2 : 0,
- ({locationLong}) => locationLong ? 2 : 0,
- ({workingHourEnd}) => workingHourEnd ? 2 : 0,
- ({workingHourStart}) => workingHourStart ? 2 : 0,
- ];
- function validate(data) {
- return LEVEL_REQUIREMENTS.every(levelRequirement => data.level >= levelRequirement(data));
- }
- ...
- providerSchema.pre('save', function(next) {
- if (validate(this)) {
- next();
- } else {
- next(new Error('your current plan does not have this feature'));
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement