reports.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. 'use strict';
  2. const async = require('async');
  3. const db = require('../db');
  4. const hooks = require('./hooks');
  5. module.exports = {
  6. index: hooks.adminRequired((session, cb) => {
  7. db.models.reports.find({}).sort({ released: 'desc' }).exec((err, reports) => {
  8. if (err) console.error(err);
  9. else cb({ status: 'success', data: reports });
  10. });
  11. }),
  12. create: hooks.loginRequired((session, data, cb) => {
  13. async.waterfall([
  14. (next) => {
  15. db.models.report.find({ createdBy: data.createdBy, createdAt: data.createdAt }).exec((err, report) => {
  16. if (err) console.error(err);
  17. if (report) return cb({ status: 'failure', message: 'Report already exists' });
  18. else next();
  19. });
  20. },
  21. (next) => {
  22. let issues = [
  23. {
  24. name: 'Video',
  25. reasons: [
  26. 'Doesn\'t exist',
  27. 'It\'s private',
  28. 'It\'s not available in my country'
  29. ]
  30. },
  31. {
  32. name: 'Title',
  33. reasons: [
  34. 'Incorrect',
  35. 'Inappropriate'
  36. ]
  37. },
  38. {
  39. name: 'Duration',
  40. reasons: [
  41. 'Skips too soon',
  42. 'Skips too late',
  43. 'Starts too soon',
  44. 'Skips too late'
  45. ]
  46. },
  47. {
  48. name: 'Artists',
  49. reasons: [
  50. 'Incorrect',
  51. 'Inappropriate'
  52. ]
  53. },
  54. {
  55. name: 'Thumbnail',
  56. reasons: [
  57. 'Incorrect',
  58. 'Inappropriate',
  59. 'Doesn\'t exist'
  60. ]
  61. }
  62. ];
  63. for (let z = 0; z < data.issues.length; z++) {
  64. if (issues.filter(issue => { return issue.name == data.issues[z].name; }).length > 0) {
  65. for (let r = 0; r < issues.length; r++) {
  66. if (issues[r].reasons.every(reason => data.issues[z].reasons.indexOf(reason) < -1)) {
  67. return cb({ 'status': 'failure', 'message': 'Invalid data' });
  68. }
  69. }
  70. } else return cb({ 'status': 'failure', 'message': 'Invalid data' });
  71. }
  72. next();
  73. },
  74. (next) => {
  75. db.models.report.create(data, next);
  76. }
  77. ], err => {
  78. if (err) return cb({ 'status': 'failure', 'message': 'Something went wrong'});
  79. return cb({ 'status': 'success', 'message': 'Successfully created report' });
  80. });
  81. })
  82. };