Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.28 KB | None | 0 0
  1. providerSchema.pre('save', function(next) {
  2.  
  3. if(this.level === 4){
  4. if(this.description.length >= 80 || this.certifications.length > 5){
  5. next(new Error('your current plan does not have this feature'));
  6. } else {
  7. next()
  8. }
  9. } else if(this.level === 3){
  10. if(this.description.length >= 50 || this.certifications.length > 3){
  11. next(new Error('your current plan does not have this feature'));
  12. } else {
  13. next()
  14. }
  15. } else if(this.level === 2){
  16. if(this.description.length >= 30 || this.certifications.length > 0 || this.teaser || this.social.length > 0){
  17. next(new Error('your current plan does not have this feature'));
  18. } else {
  19. next()
  20. }
  21. } else if(this.level === 1){
  22. if(this.description || this.certifications.length > 0 || this.teaser || this.social.length > 0 || this.locationLat || this.locationLong || this.workingHourEnd || this.workingHourStart){
  23. next(new Error('your current plan does not have this feature'));
  24. } else {
  25. next()
  26. }
  27. } else {
  28. if(this.description || this.certifications.length > 0 || this.teaser || this.social.length > 0 || this.locationLat || this.locationLong || this.workingHourEnd || this.workingHourStart){
  29. next(new Error('your current plan does not have this feature'));
  30. } else {
  31. next()
  32. }
  33. });
  34.  
  35. providerSchema.pre('save', function(next) {
  36.  
  37. if(((this.level === 4) && (this.description.length >= 80 || this.certifications.length > 5)) ||
  38. ((this.level === 3) && (this.description.length >= 50 || this.certifications.length > 3)) ||
  39. ((this.level === 2) && (this.description.length >= 30 || this.certifications.length > 0 || this.teaser || this.social.length > 0)) ||
  40. ((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))){
  41. next(new Error('your current plan does not have this feature'));
  42. } else {
  43. next()
  44. }
  45. });
  46. ```
  47.  
  48. providerSchema.pre('save', function (next) {
  49.  
  50. var valid = true;
  51. if (this.level === 4) {
  52. if (this.description.length >= 80 || this.certifications.length > 5) {
  53. valid = false;
  54. }
  55. } else if (this.level === 3) {
  56. if (this.description.length >= 50 || this.certifications.length > 3) {
  57. valid = false;
  58. }
  59. } else if (this.level === 2) {
  60. if (this.description.length >= 30 || this.certifications.length > 0 || this.teaser || this.social.length > 0) {
  61. valid = false;
  62. }
  63. } else if (this.level === 1) {
  64. if (this.description || this.certifications.length > 0 || this.teaser || this.social.length > 0 || this.locationLat || this.locationLong || this.workingHourEnd || this.workingHourStart) {
  65. valid = false;
  66. }
  67. } else {
  68. if (this.description || this.certifications.length > 0 || this.teaser || this.social.length > 0 || this.locationLat || this.locationLong || this.workingHourEnd || this.workingHourStart) {
  69. valid = false;
  70. }
  71. }
  72.  
  73. if (valid) {
  74. next();
  75. } else {
  76. next(new Error('your current plan does not have this feature'));
  77. }
  78. });
  79.  
  80. providerSchema.pre('save', function (next) {
  81. if (isValidFeatureRequest()) {
  82. next();
  83. } else {
  84. next(new Error('your current plan does not have this feature'));
  85. }
  86.  
  87. function isValidFeatureRequest() {
  88. if (this.level === 4) {
  89. if (this.description.length >= 80 || this.certifications.length > 5) {
  90. return false;
  91. }
  92. } else if (this.level === 3) {
  93. if (this.description.length >= 50 || this.certifications.length > 3) {
  94. return false;
  95. }
  96. } else if (this.level === 2) {
  97. if (this.description.length >= 30 || this.certifications.length > 0 || this.teaser || this.social.length > 0) {
  98. return false;
  99. }
  100. } else if (this.level === 1) {
  101. if (this.description || this.certifications.length > 0 || this.teaser || this.social.length > 0 || this.locationLat || this.locationLong || this.workingHourEnd || this.workingHourStart) {
  102. return false;
  103. }
  104. } else {
  105. if (this.description || this.certifications.length > 0 || this.teaser || this.social.length > 0 || this.locationLat || this.locationLong || this.workingHourEnd || this.workingHourStart) {
  106. return false;
  107. }
  108. }
  109. return true;
  110. }
  111. });
  112.  
  113. providerSchema.pre('save', function (next) {
  114. if (isValidFeatureRequest()) {
  115. next();
  116. } else {
  117. next(new Error('your current plan does not have this feature'));
  118. }
  119.  
  120. function isValidFeatureRequest() {
  121. if (this.level === 4) {
  122. if (this.description.length >= 80 || this.certifications.length > 5) {
  123. return false;
  124. }
  125. } else if (this.level === 3) {
  126. if (this.description.length >= 50 || this.certifications.length > 3) {
  127. return false;
  128. }
  129. } else if (this.level === 2) {
  130. if (this.description.length >= 30 || this.certifications.length > 0 || this.teaser || this.social.length > 0) {
  131. return false;
  132. }
  133. } else {
  134. if (this.description || this.certifications.length > 0 || this.teaser || this.social.length > 0 || this.locationLat || this.locationLong || this.workingHourEnd || this.workingHourStart) {
  135. return false;
  136. }
  137. }
  138. return true;
  139. }
  140. });
  141.  
  142. function | CC
  143. -------------------------------------------
  144. providerSchema | 2
  145. isValidFeatureRequest | 20
  146.  
  147. providerSchema.pre('save', function (next) {
  148. if (isValidFeatureRequest()) {
  149. next();
  150. } else {
  151. next(new Error('your current plan does not have this feature'));
  152. }
  153.  
  154. function isValidFeatureRequest() {
  155. if (this.level === 4) {
  156. if (descriptionOrCertificationsOffLimits(80, 5)) {
  157. return false;
  158. }
  159. } else if (this.level === 3) {
  160. if (descriptionOrCertificationsOffLimits(40, 3)) {
  161. return false;
  162. }
  163. } else if (this.level === 2) {
  164. if (descriptionOrCertificationsOffLimits(30, 0) || this.teaser || this.social.length > 0) {
  165. return false;
  166. }
  167. } else {
  168. if (this.description || this.certifications.length > 0 || this.teaser || this.social.length > 0 || this.locationLat || this.locationLong || this.workingHourEnd || this.workingHourStart) {
  169. return false;
  170. }
  171. }
  172. return true;
  173. }
  174.  
  175. function descriptionOrCertificationsOffLimits(descriptionLimit, certificationsLimit) {
  176. return this.description.length >= descriptionLimit || this.certifications.length > certificationsLimit;
  177. }
  178. });
  179.  
  180. function | CC
  181. -------------------------------------------
  182. providerSchema | 2
  183. isValidFeatureRequest | 17
  184. descriptionOrCertificationsOffLimits | 2
  185.  
  186. providerSchema.pre('save', function (next) {
  187. if (isValidFeatureRequest()) {
  188. next();
  189. } else {
  190. next(new Error('your current plan does not have this feature'));
  191. }
  192.  
  193. function isValidFeatureRequest() {
  194. if (this.level === 4) {
  195. if (descriptionOrCertificationsOffLimits(80, 5)) {
  196. return false;
  197. }
  198. } else if (this.level === 3) {
  199. if (descriptionOrCertificationsOffLimits(40, 3)) {
  200. return false;
  201. }
  202. } else if (this.level === 2) {
  203. if (descriptionOrCertificationsOffLimits(30, 0) || this.teaser || this.social.length > 0) {
  204. return false;
  205. }
  206. } else if (hasAny()) {
  207. return false;
  208. }
  209. return true;
  210. }
  211.  
  212. function descriptionOrCertificationsOffLimits(descriptionLimit, certificationsLimit) {
  213. return this.description.length >= descriptionLimit || this.certifications.length > certificationsLimit;
  214. }
  215.  
  216. function hasAny() {
  217. return this.description || this.certifications.length > 0 || this.teaser || this.social.length > 0 || this.locationLat || this.locationLong || this.workingHourEnd || this.workingHourStart;
  218. }
  219. });
  220.  
  221. function | CC
  222. -------------------------------------------
  223. providerSchema | 2
  224. isValidFeatureRequest | 10
  225. descriptionOrCertificationsOffLimits | 2
  226. hasAny | 8
  227.  
  228. function hasAny() {
  229. return this.description ||
  230. this.certifications.length > 0 ||
  231. this.teaser ||
  232. this.social.length > 0 ||
  233. this.locationLat ||
  234. this.locationLong ||
  235. this.workingHourEnd ||
  236. this.workingHourStart;
  237. }
  238.  
  239. providerSchema.pre('save', function (next) {
  240. if (isValidFeatureRequest()) {
  241. next();
  242. } else {
  243. next(new Error('your current plan does not have this feature'));
  244. }
  245.  
  246. function isValidFeatureRequest() {
  247. if (this.level === 4) {
  248. if (descriptionOrCertificationsOffLimits(80, 5)) {
  249. return false;
  250. }
  251. } else if (this.level === 3) {
  252. if (descriptionOrCertificationsOffLimits(40, 3)) {
  253. return false;
  254. }
  255. } else if (this.level === 2) {
  256. if (descriptionOrCertificationsOffLimits(30, 0) || hasTeaserOrSocial()) {
  257. return false;
  258. }
  259. } else if (hasAny()) {
  260. return false;
  261. }
  262. return true;
  263. }
  264.  
  265. function descriptionOrCertificationsOffLimits(descriptionLimit, certificationsLimit) {
  266. return this.description.length >= descriptionLimit || this.certifications.length > certificationsLimit;
  267. }
  268.  
  269. function hasTeaserOrSocial() {
  270. return this.teaser || this.social.length > 0;
  271. }
  272.  
  273. function hasAny() {
  274. return this.description ||
  275. this.certifications.length > 0 ||
  276. this.teaser ||
  277. this.social.length > 0 ||
  278. this.locationLat ||
  279. this.locationLong ||
  280. this.workingHourEnd ||
  281. this.workingHourStart;
  282. }
  283. });
  284.  
  285. function | CC
  286. -------------------------------------------
  287. providerSchema | 2
  288. isValidFeatureRequest | 9
  289. descriptionOrCertificationsOffLimits | 2
  290. hasTeaserOrSocial | 2
  291. hasAny | 8
  292.  
  293. function isValidFeatureRequest() {
  294. if (this.level === 4 && descriptionOrCertificationsOffLimits(80, 5)) {
  295. return false;
  296. } else if (this.level === 3 && descriptionOrCertificationsOffLimits(40, 3)) {
  297. return false;
  298. } else if (this.level === 2 && descriptionOrCertificationsOffLimits(30, 0) || hasTeaserOrSocial()) {
  299. return false;
  300. } else if (hasAny()) {
  301. return false;
  302. }
  303. return true;
  304. }
  305.  
  306. function isValidFeatureRequest() {
  307. if (isLevel4AndNotValid()) {
  308. return false;
  309. } else if (isLevel3AndNotValid()) {
  310. return false;
  311. } else if (isLevel2AndNotValid()) {
  312. return false;
  313. } else if (hasAny()) {
  314. return false;
  315. }
  316. return true;
  317. }
  318.  
  319. function isLevel4AndNotValid() {
  320. return this.level === 4 && descriptionOrCertificationsOffLimits(80, 5);
  321. }
  322.  
  323. function isLevel3AndNotValid() {
  324. return this.level === 3 && descriptionOrCertificationsOffLimits(40, 3);
  325. }
  326.  
  327. function isLevel2AndNotValid() {
  328. this.level === 2 && (descriptionOrCertificationsOffLimits(30, 0) || hasTeaserOrSocial());
  329. }
  330.  
  331. function | CC
  332. -------------------------------------------
  333. providerSchema | 2
  334. isValidFeatureRequest | 5
  335. isLevel4AndNotValid | 2
  336. isLevel3AndNotValid | 2
  337. isLevel2AndNotValid | 3
  338. descriptionOrCertificationsOffLimits | 2
  339. hasTeaserOrSocial | 2
  340. hasAny | 8
  341.  
  342. function isValidFeatureRequest() {
  343. if (isLevel4AndNotValid()) {
  344. return false;
  345. }
  346.  
  347. if (isLevel3AndNotValid()) {
  348. return false;
  349. }
  350.  
  351. if (isLevel2AndNotValid()) {
  352. return false;
  353. }
  354.  
  355. return !hasAny();
  356. }
  357.  
  358. providerSchema.pre('save', function (next) {
  359. if (isValidFeatureRequest()) {
  360. next();
  361. } else {
  362. next(new Error('your current plan does not have this feature'));
  363. }
  364.  
  365. function isValidFeatureRequest() {
  366. if (isLevel4AndNotValid()) {
  367. return false;
  368. }
  369.  
  370. if (isLevel3AndNotValid()) {
  371. return false;
  372. }
  373.  
  374. if (isLevel2AndNotValid()) {
  375. return false;
  376. }
  377.  
  378. return !hasAny();
  379. }
  380.  
  381. function isLevel4AndNotValid() {
  382. return this.level === 4 && descriptionOrCertificationsOffLimits(80, 5);
  383. }
  384.  
  385. function isLevel3AndNotValid() {
  386. return this.level === 3 && descriptionOrCertificationsOffLimits(40, 3);
  387. }
  388.  
  389. function isLevel2AndNotValid() {
  390. this.level === 2 && (descriptionOrCertificationsOffLimits(30, 0) || hasTeaserOrSocial());
  391. }
  392.  
  393. function descriptionOrCertificationsOffLimits(descriptionLimit, certificationsLimit) {
  394. return this.description.length >= descriptionLimit || this.certifications.length > certificationsLimit;
  395. }
  396.  
  397. function hasTeaserOrSocial() {
  398. return this.teaser || this.social.length > 0;
  399. }
  400.  
  401. function hasAny() {
  402. return this.description ||
  403. this.certifications.length > 0 ||
  404. this.teaser ||
  405. this.social.length > 0 ||
  406. this.locationLat ||
  407. this.locationLong ||
  408. this.workingHourEnd ||
  409. this.workingHourStart;
  410. }
  411. });
  412.  
  413. function | CC
  414. -------------------------------------------
  415. providerSchema | 2
  416. isValidFeatureRequest | 4
  417. isLevel4AndNotValid | 2
  418. isLevel3AndNotValid | 2
  419. isLevel2AndNotValid | 3
  420. descriptionOrCertificationsOffLimits | 2
  421. hasTeaserOrSocial | 2
  422. hasAny | 8
  423.  
  424. function bar(foo) {
  425. if (foo === 2) { foo = 3 } // path 1
  426. else { foo = 4 } // path 2
  427. return foo;
  428. }
  429.  
  430.  
  431. function bar(foo) {
  432. if (foo === 2) { foo = 3 } // path 1
  433. // hidden else path 2
  434. return foo;
  435. }
  436.  
  437. function bar(foo) {
  438. if (foo === 2) { foo = 3 } // path 1
  439. else if (foo === 4) { foo = 5 } // path 2
  440. else { foo = 4 } // path 3
  441. return foo;
  442. }
  443.  
  444. function bar(foo) {
  445. if (foo === 2 || foo === 4) { foo = 3 } // path 1 and 2
  446. else { foo = 4 } // path 3
  447. return foo;
  448. }
  449.  
  450. function poo(foo) {
  451. if (foo === 2 || foo === 4) { foo = 3 }
  452. else { foo = 4 }
  453. return foo;
  454. }
  455. function bar(foo) {
  456. if (foo === 2 || foo === 4) { foo = 3 }
  457. else { foo = poo(foo) }
  458. return foo;
  459. }
  460.  
  461. providerSchema.pre('save', function(next) {
  462.  
  463. if(((this.level === 4) && (this.description.length >= 80 || this.certifications.length > 5)) ||
  464. ((this.level === 3) && (this.description.length >= 50 || this.certifications.length > 3)) ||
  465. ((this.level === 2) && (this.description.length >= 30 || this.certifications.length > 0 || this.teaser || this.social.length > 0)) ||
  466. ((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))){
  467. next(new Error('your current plan does not have this feature'));
  468. } else {
  469. next()
  470. }
  471. });
  472.  
  473. function foo(a) {
  474. if(a === 1) { a = 2 }
  475. else if (a === 2) { a = 3 }
  476. else if (a === 3) { a = 4 }
  477. else { a = undefined }
  478. return a;
  479. }
  480.  
  481. function foo(a) {
  482. return ({"1": 2, "2": 3, "3": 4})[a];
  483. }
  484.  
  485. providerSchema.pre('save', function(next) {
  486. const checkSocial = () => this.description || this.teaser || this.social.length > 0;
  487. const checkLocation = () => this.locationLat || this.locationLong || this.workingHourEnd || this.workingHourStart;
  488. const fail = () => false;
  489. const levels = {
  490. "4": {desc: 80, cert: 6, fail},
  491. "3": {desc: 50, cert: 4, fail},
  492. "2": {desc: 30, cert: 1, fail() { return checkSocial() } },
  493. "1": {desc: -1, cert: 1, fail() { return checkSocial() || checkLocation() } },
  494. "0": {desc: -1, cert: 1, fail() { return checkSocial() || checkLocation() } },
  495. };
  496.  
  497. const checkPass= ({level, description, certifications}) => {
  498. if(levels[level]) {
  499. const check = levels[level];
  500. if(check.fail() && check.desc < description.length && check.cert < certifications.length) {
  501. return false;
  502. }
  503. }
  504. return true;
  505. }
  506. checkPass(this) ? next() : next(new Error("Your current plan does not have this feature."));
  507.  
  508. });
  509.  
  510. function validateData(data) {
  511.  
  512. switch(data.level) {
  513. case 0:
  514. case 1:
  515. return data.description || data.certifications.length > 0 || data.teaser || data.social.length > 0 || data.locationLat || data.locationLong || data.workingHourEnd || data.workingHourStart
  516. case 2:
  517. return data.description.length >= 30 || data.certifications.length > 0 || data.teaser || data.social.length > 0);
  518. case 3:
  519. return (data.description.length >= 50 || data.certifications.length > 3));
  520. case 4:
  521. return (data.description.length >= 80 || data.certifications.length > 5);
  522. }
  523. }
  524.  
  525. providerSchema.pre('save', function(next) {
  526.  
  527. if(validateData(this)){
  528. next(new Error('your current plan does not have this feature'));
  529. } else {
  530. next()
  531. }
  532. });
  533.  
  534. // For each data field we care about, at what level do various
  535. // conditions on that field become available?
  536. const LEVEL_REQUIREMENTS = [
  537. ({description}) => {
  538. if (description.length >= 80) return 5; // or maybe Infinity?
  539. if (description.length >= 50) return 4;
  540. if (description.length >= 30) return 3;
  541. if (description) return 2;
  542. return 0;
  543. },
  544. ({certifications}) => {
  545. if (certifications.length > 5) return 5;
  546. if (certifications.length > 3) return 4;
  547. if (certifications.length > 0) return 3;
  548. return 0;
  549. },
  550. ({teaser}) => teaser ? 3 : 0,
  551. ({social}) => social.length > 0 ? 3 : 0,
  552. ({locationLat}) => locationLat ? 2 : 0,
  553. ({locationLong}) => locationLong ? 2 : 0,
  554. ({workingHourEnd}) => workingHourEnd ? 2 : 0,
  555. ({workingHourStart}) => workingHourStart ? 2 : 0,
  556. ];
  557.  
  558. function validate(data) {
  559. return LEVEL_REQUIREMENTS.every(levelRequirement => data.level >= levelRequirement(data));
  560. }
  561.  
  562. ...
  563.  
  564. providerSchema.pre('save', function(next) {
  565. if (validate(this)) {
  566. next();
  567. } else {
  568. next(new Error('your current plan does not have this feature'));
  569. }
  570. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement