cards.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. Cards = new Mongo.Collection('cards');
  2. // XXX To improve pub/sub performances a card document should include a
  3. // de-normalized number of comments so we don't have to publish the whole list
  4. // of comments just to display the number of them in the board view.
  5. Cards.attachSchema(new SimpleSchema({
  6. title: {
  7. type: String,
  8. },
  9. archived: {
  10. type: Boolean,
  11. autoValue() { // eslint-disable-line consistent-return
  12. if (this.isInsert && !this.isSet) {
  13. return false;
  14. }
  15. },
  16. },
  17. listId: {
  18. type: String,
  19. },
  20. // The system could work without this `boardId` information (we could deduce
  21. // the board identifier from the card), but it would make the system more
  22. // difficult to manage and less efficient.
  23. boardId: {
  24. type: String,
  25. },
  26. coverId: {
  27. type: String,
  28. optional: true,
  29. },
  30. createdAt: {
  31. type: Date,
  32. autoValue() { // eslint-disable-line consistent-return
  33. if (this.isInsert) {
  34. return new Date();
  35. } else {
  36. this.unset();
  37. }
  38. },
  39. },
  40. customFields: {
  41. type: [Object],
  42. optional: true,
  43. },
  44. 'customFields.$': {
  45. type: new SimpleSchema({
  46. _id: {
  47. type: String,
  48. },
  49. value: {
  50. type: Match.OneOf(String,Number,Boolean,Date),
  51. optional: true,
  52. },
  53. })
  54. },
  55. dateLastActivity: {
  56. type: Date,
  57. autoValue() {
  58. return new Date();
  59. },
  60. },
  61. description: {
  62. type: String,
  63. optional: true,
  64. },
  65. labelIds: {
  66. type: [String],
  67. optional: true,
  68. },
  69. members: {
  70. type: [String],
  71. optional: true,
  72. },
  73. startAt: {
  74. type: Date,
  75. optional: true,
  76. },
  77. dueAt: {
  78. type: Date,
  79. optional: true,
  80. },
  81. // XXX Should probably be called `authorId`. Is it even needed since we have
  82. // the `members` field?
  83. userId: {
  84. type: String,
  85. autoValue() { // eslint-disable-line consistent-return
  86. if (this.isInsert && !this.isSet) {
  87. return this.userId;
  88. }
  89. },
  90. },
  91. sort: {
  92. type: Number,
  93. decimal: true,
  94. },
  95. }));
  96. Cards.allow({
  97. insert(userId, doc) {
  98. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  99. },
  100. update(userId, doc) {
  101. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  102. },
  103. remove(userId, doc) {
  104. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  105. },
  106. fetch: ['boardId'],
  107. });
  108. Cards.helpers({
  109. list() {
  110. return Lists.findOne(this.listId);
  111. },
  112. board() {
  113. return Boards.findOne(this.boardId);
  114. },
  115. labels() {
  116. const boardLabels = this.board().labels;
  117. const cardLabels = _.filter(boardLabels, (label) => {
  118. return _.contains(this.labelIds, label._id);
  119. });
  120. return cardLabels;
  121. },
  122. hasLabel(labelId) {
  123. return _.contains(this.labelIds, labelId);
  124. },
  125. user() {
  126. return Users.findOne(this.userId);
  127. },
  128. isAssigned(memberId) {
  129. return _.contains(this.members, memberId);
  130. },
  131. activities() {
  132. return Activities.find({cardId: this._id}, {sort: {createdAt: -1}});
  133. },
  134. comments() {
  135. return CardComments.find({cardId: this._id}, {sort: {createdAt: -1}});
  136. },
  137. attachments() {
  138. return Attachments.find({cardId: this._id}, {sort: {uploadedAt: -1}});
  139. },
  140. cover() {
  141. const cover = Attachments.findOne(this.coverId);
  142. // if we return a cover before it is fully stored, we will get errors when we try to display it
  143. // todo XXX we could return a default "upload pending" image in the meantime?
  144. return cover && cover.url() && cover;
  145. },
  146. checklists() {
  147. return Checklists.find({cardId: this._id}, {sort: {createdAt: 1}});
  148. },
  149. checklistItemCount() {
  150. const checklists = this.checklists().fetch();
  151. return checklists.map((checklist) => {
  152. return checklist.itemCount();
  153. }).reduce((prev, next) => {
  154. return prev + next;
  155. }, 0);
  156. },
  157. checklistFinishedCount() {
  158. const checklists = this.checklists().fetch();
  159. return checklists.map((checklist) => {
  160. return checklist.finishedCount();
  161. }).reduce((prev, next) => {
  162. return prev + next;
  163. }, 0);
  164. },
  165. checklistFinished() {
  166. return this.hasChecklist() && this.checklistItemCount() === this.checklistFinishedCount();
  167. },
  168. hasChecklist() {
  169. return this.checklistItemCount() !== 0;
  170. },
  171. customFieldIndex(customFieldId) {
  172. return _.pluck(this.customFields, '_id').indexOf(customFieldId);
  173. },
  174. absoluteUrl() {
  175. const board = this.board();
  176. return FlowRouter.url('card', {
  177. boardId: board._id,
  178. slug: board.slug,
  179. cardId: this._id,
  180. });
  181. },
  182. });
  183. Cards.mutations({
  184. archive() {
  185. return {$set: {archived: true}};
  186. },
  187. restore() {
  188. return {$set: {archived: false}};
  189. },
  190. setTitle(title) {
  191. return {$set: {title}};
  192. },
  193. setDescription(description) {
  194. return {$set: {description}};
  195. },
  196. move(listId, sortIndex) {
  197. const mutatedFields = {listId};
  198. if (sortIndex) {
  199. mutatedFields.sort = sortIndex;
  200. }
  201. return {$set: mutatedFields};
  202. },
  203. addLabel(labelId) {
  204. return {$addToSet: {labelIds: labelId}};
  205. },
  206. removeLabel(labelId) {
  207. return {$pull: {labelIds: labelId}};
  208. },
  209. toggleLabel(labelId) {
  210. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  211. return this.removeLabel(labelId);
  212. } else {
  213. return this.addLabel(labelId);
  214. }
  215. },
  216. assignMember(memberId) {
  217. return {$addToSet: {members: memberId}};
  218. },
  219. unassignMember(memberId) {
  220. return {$pull: {members: memberId}};
  221. },
  222. toggleMember(memberId) {
  223. if (this.members && this.members.indexOf(memberId) > -1) {
  224. return this.unassignMember(memberId);
  225. } else {
  226. return this.assignMember(memberId);
  227. }
  228. },
  229. assignCustomField(customFieldId) {
  230. console.log("assignCustomField", customFieldId);
  231. return {$addToSet: {customFields: {_id: customFieldId, value: null}}};
  232. },
  233. unassignCustomField(customFieldId) {
  234. console.log("unassignCustomField", customFieldId);
  235. return {$pull: {customFields: {_id: customFieldId}}};
  236. },
  237. toggleCustomField(customFieldId) {
  238. if (this.customFields && this.customFieldIndex(customFieldId) > -1) {
  239. return this.unassignCustomField(customFieldId);
  240. } else {
  241. return this.assignCustomField(customFieldId);
  242. }
  243. },
  244. setCover(coverId) {
  245. return {$set: {coverId}};
  246. },
  247. unsetCover() {
  248. return {$unset: {coverId: ''}};
  249. },
  250. setStart(startAt) {
  251. return {$set: {startAt}};
  252. },
  253. unsetStart() {
  254. return {$unset: {startAt: ''}};
  255. },
  256. setDue(dueAt) {
  257. return {$set: {dueAt}};
  258. },
  259. unsetDue() {
  260. return {$unset: {dueAt: ''}};
  261. },
  262. });
  263. //FUNCTIONS FOR creation of Activities
  264. function cardMove(userId, doc, fieldNames, oldListId) {
  265. if (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) {
  266. Activities.insert({
  267. userId,
  268. oldListId,
  269. activityType: 'moveCard',
  270. listId: doc.listId,
  271. boardId: doc.boardId,
  272. cardId: doc._id,
  273. });
  274. }
  275. }
  276. function cardState(userId, doc, fieldNames) {
  277. if (_.contains(fieldNames, 'archived')) {
  278. if (doc.archived) {
  279. Activities.insert({
  280. userId,
  281. activityType: 'archivedCard',
  282. boardId: doc.boardId,
  283. listId: doc.listId,
  284. cardId: doc._id,
  285. });
  286. } else {
  287. Activities.insert({
  288. userId,
  289. activityType: 'restoredCard',
  290. boardId: doc.boardId,
  291. listId: doc.listId,
  292. cardId: doc._id,
  293. });
  294. }
  295. }
  296. }
  297. function cardMembers(userId, doc, fieldNames, modifier) {
  298. if (!_.contains(fieldNames, 'members'))
  299. return;
  300. let memberId;
  301. // Say hello to the new member
  302. if (modifier.$addToSet && modifier.$addToSet.members) {
  303. memberId = modifier.$addToSet.members;
  304. if (!_.contains(doc.members, memberId)) {
  305. Activities.insert({
  306. userId,
  307. memberId,
  308. activityType: 'joinMember',
  309. boardId: doc.boardId,
  310. cardId: doc._id,
  311. });
  312. }
  313. }
  314. // Say goodbye to the former member
  315. if (modifier.$pull && modifier.$pull.members) {
  316. memberId = modifier.$pull.members;
  317. // Check that the former member is member of the card
  318. if (_.contains(doc.members, memberId)) {
  319. Activities.insert({
  320. userId,
  321. memberId,
  322. activityType: 'unjoinMember',
  323. boardId: doc.boardId,
  324. cardId: doc._id,
  325. });
  326. }
  327. }
  328. }
  329. function cardCreation(userId, doc) {
  330. Activities.insert({
  331. userId,
  332. activityType: 'createCard',
  333. boardId: doc.boardId,
  334. listId: doc.listId,
  335. cardId: doc._id,
  336. });
  337. }
  338. function cardRemover(userId, doc) {
  339. Activities.remove({
  340. cardId: doc._id,
  341. });
  342. Checklists.remove({
  343. cardId: doc._id,
  344. });
  345. CardComments.remove({
  346. cardId: doc._id,
  347. });
  348. Attachments.remove({
  349. cardId: doc._id,
  350. });
  351. }
  352. if (Meteor.isServer) {
  353. // Cards are often fetched within a board, so we create an index to make these
  354. // queries more efficient.
  355. Meteor.startup(() => {
  356. Cards._collection._ensureIndex({boardId: 1, createdAt: -1});
  357. });
  358. Cards.after.insert((userId, doc) => {
  359. cardCreation(userId, doc);
  360. });
  361. // New activity for card (un)archivage
  362. Cards.after.update((userId, doc, fieldNames) => {
  363. cardState(userId, doc, fieldNames);
  364. });
  365. //New activity for card moves
  366. Cards.after.update(function (userId, doc, fieldNames) {
  367. const oldListId = this.previous.listId;
  368. cardMove(userId, doc, fieldNames, oldListId);
  369. });
  370. // Add a new activity if we add or remove a member to the card
  371. Cards.before.update((userId, doc, fieldNames, modifier) => {
  372. cardMembers(userId, doc, fieldNames, modifier);
  373. });
  374. // Remove all activities associated with a card if we remove the card
  375. // Remove also card_comments / checklists / attachments
  376. Cards.after.remove((userId, doc) => {
  377. cardRemover(userId, doc);
  378. });
  379. }
  380. //LISTS REST API
  381. if (Meteor.isServer) {
  382. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function (req, res, next) {
  383. const paramBoardId = req.params.boardId;
  384. const paramListId = req.params.listId;
  385. Authentication.checkBoardAccess(req.userId, paramBoardId);
  386. JsonRoutes.sendResult(res, {
  387. code: 200,
  388. data: Cards.find({boardId: paramBoardId, listId: paramListId, archived: false}).map(function (doc) {
  389. return {
  390. _id: doc._id,
  391. title: doc.title,
  392. description: doc.description,
  393. };
  394. }),
  395. });
  396. });
  397. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res, next) {
  398. const paramBoardId = req.params.boardId;
  399. const paramListId = req.params.listId;
  400. const paramCardId = req.params.cardId;
  401. Authentication.checkBoardAccess(req.userId, paramBoardId);
  402. JsonRoutes.sendResult(res, {
  403. code: 200,
  404. data: Cards.findOne({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false}),
  405. });
  406. });
  407. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function (req, res, next) {
  408. Authentication.checkUserId(req.userId);
  409. const paramBoardId = req.params.boardId;
  410. const paramListId = req.params.listId;
  411. const check = Users.findOne({_id: req.body.authorId});
  412. if (typeof check !== 'undefined') {
  413. const id = Cards.direct.insert({
  414. title: req.body.title,
  415. boardId: paramBoardId,
  416. listId: paramListId,
  417. description: req.body.description,
  418. userId: req.body.authorId,
  419. sort: 0,
  420. members: [req.body.authorId],
  421. });
  422. JsonRoutes.sendResult(res, {
  423. code: 200,
  424. data: {
  425. _id: id,
  426. },
  427. });
  428. const card = Cards.findOne({_id:id});
  429. cardCreation(req.body.authorId, card);
  430. } else {
  431. JsonRoutes.sendResult(res, {
  432. code: 401,
  433. });
  434. }
  435. });
  436. JsonRoutes.add('PUT', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res, next) {
  437. Authentication.checkUserId(req.userId);
  438. const paramBoardId = req.params.boardId;
  439. const paramCardId = req.params.cardId;
  440. const paramListId = req.params.listId;
  441. if (req.body.hasOwnProperty('title')) {
  442. const newTitle = req.body.title;
  443. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  444. {$set: {title: newTitle}});
  445. }
  446. if (req.body.hasOwnProperty('listId')) {
  447. const newParamListId = req.body.listId;
  448. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  449. {$set: {listId: newParamListId}});
  450. const card = Cards.findOne({_id: paramCardId} );
  451. cardMove(req.body.authorId, card, {fieldName: 'listId'}, paramListId);
  452. }
  453. if (req.body.hasOwnProperty('description')) {
  454. const newDescription = req.body.description;
  455. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  456. {$set: {description: newDescription}});
  457. }
  458. JsonRoutes.sendResult(res, {
  459. code: 200,
  460. data: {
  461. _id: paramCardId,
  462. },
  463. });
  464. });
  465. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res, next) {
  466. Authentication.checkUserId(req.userId);
  467. const paramBoardId = req.params.boardId;
  468. const paramListId = req.params.listId;
  469. const paramCardId = req.params.cardId;
  470. Cards.direct.remove({_id: paramCardId, listId: paramListId, boardId: paramBoardId});
  471. const card = Cards.find({_id: paramCardId} );
  472. cardRemover(req.body.authorId, card);
  473. JsonRoutes.sendResult(res, {
  474. code: 200,
  475. data: {
  476. _id: paramCardId,
  477. },
  478. });
  479. });
  480. }