cards.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. // customFields with definitions
  175. customFieldsWD() {
  176. // get all definitions
  177. const definitions = CustomFields.find({
  178. boardId: this.boardId,
  179. }).fetch();
  180. // match right definition to each field
  181. return this.customFields.map((customField) => {
  182. return {
  183. _id: customField._id,
  184. value: customField.value,
  185. definition: definitions.find((definition) => {
  186. return definition._id == customField._id;
  187. })
  188. }
  189. });
  190. },
  191. absoluteUrl() {
  192. const board = this.board();
  193. return FlowRouter.url('card', {
  194. boardId: board._id,
  195. slug: board.slug,
  196. cardId: this._id,
  197. });
  198. },
  199. });
  200. Cards.mutations({
  201. archive() {
  202. return {$set: {archived: true}};
  203. },
  204. restore() {
  205. return {$set: {archived: false}};
  206. },
  207. setTitle(title) {
  208. return {$set: {title}};
  209. },
  210. setDescription(description) {
  211. return {$set: {description}};
  212. },
  213. move(listId, sortIndex) {
  214. const mutatedFields = {listId};
  215. if (sortIndex) {
  216. mutatedFields.sort = sortIndex;
  217. }
  218. return {$set: mutatedFields};
  219. },
  220. addLabel(labelId) {
  221. return {$addToSet: {labelIds: labelId}};
  222. },
  223. removeLabel(labelId) {
  224. return {$pull: {labelIds: labelId}};
  225. },
  226. toggleLabel(labelId) {
  227. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  228. return this.removeLabel(labelId);
  229. } else {
  230. return this.addLabel(labelId);
  231. }
  232. },
  233. assignMember(memberId) {
  234. return {$addToSet: {members: memberId}};
  235. },
  236. unassignMember(memberId) {
  237. return {$pull: {members: memberId}};
  238. },
  239. toggleMember(memberId) {
  240. if (this.members && this.members.indexOf(memberId) > -1) {
  241. return this.unassignMember(memberId);
  242. } else {
  243. return this.assignMember(memberId);
  244. }
  245. },
  246. assignCustomField(customFieldId) {
  247. console.log("assignCustomField", customFieldId);
  248. return {$addToSet: {customFields: {_id: customFieldId, value: null}}};
  249. },
  250. unassignCustomField(customFieldId) {
  251. console.log("unassignCustomField", customFieldId);
  252. return {$pull: {customFields: {_id: customFieldId}}};
  253. },
  254. toggleCustomField(customFieldId) {
  255. if (this.customFields && this.customFieldIndex(customFieldId) > -1) {
  256. return this.unassignCustomField(customFieldId);
  257. } else {
  258. return this.assignCustomField(customFieldId);
  259. }
  260. },
  261. setCover(coverId) {
  262. return {$set: {coverId}};
  263. },
  264. unsetCover() {
  265. return {$unset: {coverId: ''}};
  266. },
  267. setStart(startAt) {
  268. return {$set: {startAt}};
  269. },
  270. unsetStart() {
  271. return {$unset: {startAt: ''}};
  272. },
  273. setDue(dueAt) {
  274. return {$set: {dueAt}};
  275. },
  276. unsetDue() {
  277. return {$unset: {dueAt: ''}};
  278. },
  279. });
  280. //FUNCTIONS FOR creation of Activities
  281. function cardMove(userId, doc, fieldNames, oldListId) {
  282. if (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) {
  283. Activities.insert({
  284. userId,
  285. oldListId,
  286. activityType: 'moveCard',
  287. listId: doc.listId,
  288. boardId: doc.boardId,
  289. cardId: doc._id,
  290. });
  291. }
  292. }
  293. function cardState(userId, doc, fieldNames) {
  294. if (_.contains(fieldNames, 'archived')) {
  295. if (doc.archived) {
  296. Activities.insert({
  297. userId,
  298. activityType: 'archivedCard',
  299. boardId: doc.boardId,
  300. listId: doc.listId,
  301. cardId: doc._id,
  302. });
  303. } else {
  304. Activities.insert({
  305. userId,
  306. activityType: 'restoredCard',
  307. boardId: doc.boardId,
  308. listId: doc.listId,
  309. cardId: doc._id,
  310. });
  311. }
  312. }
  313. }
  314. function cardMembers(userId, doc, fieldNames, modifier) {
  315. if (!_.contains(fieldNames, 'members'))
  316. return;
  317. let memberId;
  318. // Say hello to the new member
  319. if (modifier.$addToSet && modifier.$addToSet.members) {
  320. memberId = modifier.$addToSet.members;
  321. if (!_.contains(doc.members, memberId)) {
  322. Activities.insert({
  323. userId,
  324. memberId,
  325. activityType: 'joinMember',
  326. boardId: doc.boardId,
  327. cardId: doc._id,
  328. });
  329. }
  330. }
  331. // Say goodbye to the former member
  332. if (modifier.$pull && modifier.$pull.members) {
  333. memberId = modifier.$pull.members;
  334. // Check that the former member is member of the card
  335. if (_.contains(doc.members, memberId)) {
  336. Activities.insert({
  337. userId,
  338. memberId,
  339. activityType: 'unjoinMember',
  340. boardId: doc.boardId,
  341. cardId: doc._id,
  342. });
  343. }
  344. }
  345. }
  346. function cardCreation(userId, doc) {
  347. Activities.insert({
  348. userId,
  349. activityType: 'createCard',
  350. boardId: doc.boardId,
  351. listId: doc.listId,
  352. cardId: doc._id,
  353. });
  354. }
  355. function cardRemover(userId, doc) {
  356. Activities.remove({
  357. cardId: doc._id,
  358. });
  359. Checklists.remove({
  360. cardId: doc._id,
  361. });
  362. CardComments.remove({
  363. cardId: doc._id,
  364. });
  365. Attachments.remove({
  366. cardId: doc._id,
  367. });
  368. }
  369. if (Meteor.isServer) {
  370. // Cards are often fetched within a board, so we create an index to make these
  371. // queries more efficient.
  372. Meteor.startup(() => {
  373. Cards._collection._ensureIndex({boardId: 1, createdAt: -1});
  374. });
  375. Cards.after.insert((userId, doc) => {
  376. cardCreation(userId, doc);
  377. });
  378. // New activity for card (un)archivage
  379. Cards.after.update((userId, doc, fieldNames) => {
  380. cardState(userId, doc, fieldNames);
  381. });
  382. //New activity for card moves
  383. Cards.after.update(function (userId, doc, fieldNames) {
  384. const oldListId = this.previous.listId;
  385. cardMove(userId, doc, fieldNames, oldListId);
  386. });
  387. // Add a new activity if we add or remove a member to the card
  388. Cards.before.update((userId, doc, fieldNames, modifier) => {
  389. cardMembers(userId, doc, fieldNames, modifier);
  390. });
  391. // Remove all activities associated with a card if we remove the card
  392. // Remove also card_comments / checklists / attachments
  393. Cards.after.remove((userId, doc) => {
  394. cardRemover(userId, doc);
  395. });
  396. }
  397. //LISTS REST API
  398. if (Meteor.isServer) {
  399. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function (req, res, next) {
  400. const paramBoardId = req.params.boardId;
  401. const paramListId = req.params.listId;
  402. Authentication.checkBoardAccess(req.userId, paramBoardId);
  403. JsonRoutes.sendResult(res, {
  404. code: 200,
  405. data: Cards.find({boardId: paramBoardId, listId: paramListId, archived: false}).map(function (doc) {
  406. return {
  407. _id: doc._id,
  408. title: doc.title,
  409. description: doc.description,
  410. };
  411. }),
  412. });
  413. });
  414. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res, next) {
  415. const paramBoardId = req.params.boardId;
  416. const paramListId = req.params.listId;
  417. const paramCardId = req.params.cardId;
  418. Authentication.checkBoardAccess(req.userId, paramBoardId);
  419. JsonRoutes.sendResult(res, {
  420. code: 200,
  421. data: Cards.findOne({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false}),
  422. });
  423. });
  424. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function (req, res, next) {
  425. Authentication.checkUserId(req.userId);
  426. const paramBoardId = req.params.boardId;
  427. const paramListId = req.params.listId;
  428. const check = Users.findOne({_id: req.body.authorId});
  429. if (typeof check !== 'undefined') {
  430. const id = Cards.direct.insert({
  431. title: req.body.title,
  432. boardId: paramBoardId,
  433. listId: paramListId,
  434. description: req.body.description,
  435. userId: req.body.authorId,
  436. sort: 0,
  437. members: [req.body.authorId],
  438. });
  439. JsonRoutes.sendResult(res, {
  440. code: 200,
  441. data: {
  442. _id: id,
  443. },
  444. });
  445. const card = Cards.findOne({_id:id});
  446. cardCreation(req.body.authorId, card);
  447. } else {
  448. JsonRoutes.sendResult(res, {
  449. code: 401,
  450. });
  451. }
  452. });
  453. JsonRoutes.add('PUT', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res, next) {
  454. Authentication.checkUserId(req.userId);
  455. const paramBoardId = req.params.boardId;
  456. const paramCardId = req.params.cardId;
  457. const paramListId = req.params.listId;
  458. if (req.body.hasOwnProperty('title')) {
  459. const newTitle = req.body.title;
  460. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  461. {$set: {title: newTitle}});
  462. }
  463. if (req.body.hasOwnProperty('listId')) {
  464. const newParamListId = req.body.listId;
  465. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  466. {$set: {listId: newParamListId}});
  467. const card = Cards.findOne({_id: paramCardId} );
  468. cardMove(req.body.authorId, card, {fieldName: 'listId'}, paramListId);
  469. }
  470. if (req.body.hasOwnProperty('description')) {
  471. const newDescription = req.body.description;
  472. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  473. {$set: {description: newDescription}});
  474. }
  475. JsonRoutes.sendResult(res, {
  476. code: 200,
  477. data: {
  478. _id: paramCardId,
  479. },
  480. });
  481. });
  482. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res, next) {
  483. Authentication.checkUserId(req.userId);
  484. const paramBoardId = req.params.boardId;
  485. const paramListId = req.params.listId;
  486. const paramCardId = req.params.cardId;
  487. Cards.direct.remove({_id: paramCardId, listId: paramListId, boardId: paramBoardId});
  488. const card = Cards.find({_id: paramCardId} );
  489. cardRemover(req.body.authorId, card);
  490. JsonRoutes.sendResult(res, {
  491. code: 200,
  492. data: {
  493. _id: paramCardId,
  494. },
  495. });
  496. });
  497. }