checklistItems.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. ChecklistItems = new Mongo.Collection('checklistItems');
  2. /**
  3. * An item in a checklist
  4. */
  5. ChecklistItems.attachSchema(new SimpleSchema({
  6. title: {
  7. /**
  8. * the text of the item
  9. */
  10. type: String,
  11. },
  12. sort: {
  13. /**
  14. * the sorting field of the item
  15. */
  16. type: Number,
  17. decimal: true,
  18. },
  19. isFinished: {
  20. /**
  21. * Is the item checked?
  22. */
  23. type: Boolean,
  24. defaultValue: false,
  25. },
  26. checklistId: {
  27. /**
  28. * the checklist ID the item is attached to
  29. */
  30. type: String,
  31. },
  32. cardId: {
  33. /**
  34. * the card ID the item is attached to
  35. */
  36. type: String,
  37. },
  38. }));
  39. ChecklistItems.allow({
  40. insert(userId, doc) {
  41. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  42. },
  43. update(userId, doc) {
  44. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  45. },
  46. remove(userId, doc) {
  47. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  48. },
  49. fetch: ['userId', 'cardId'],
  50. });
  51. ChecklistItems.before.insert((userId, doc) => {
  52. if (!doc.userId) {
  53. doc.userId = userId;
  54. }
  55. });
  56. // Mutations
  57. ChecklistItems.mutations({
  58. setTitle(title) {
  59. return { $set: { title } };
  60. },
  61. check(){
  62. return { $set: { isFinished: true } };
  63. },
  64. uncheck(){
  65. return { $set: { isFinished: false } };
  66. },
  67. toggleItem() {
  68. return { $set: { isFinished: !this.isFinished } };
  69. },
  70. move(checklistId, sortIndex) {
  71. const cardId = Checklists.findOne(checklistId).cardId;
  72. const mutatedFields = {
  73. cardId,
  74. checklistId,
  75. sort: sortIndex,
  76. };
  77. return {$set: mutatedFields};
  78. },
  79. });
  80. // Activities helper
  81. function itemCreation(userId, doc) {
  82. const card = Cards.findOne(doc.cardId);
  83. const boardId = card.boardId;
  84. Activities.insert({
  85. userId,
  86. activityType: 'addChecklistItem',
  87. cardId: doc.cardId,
  88. boardId,
  89. checklistId: doc.checklistId,
  90. checklistItemId: doc._id,
  91. checklistItemName:doc.title,
  92. });
  93. }
  94. function itemRemover(userId, doc) {
  95. const card = Cards.findOne(doc.cardId);
  96. const boardId = card.boardId;
  97. Activities.insert({
  98. userId,
  99. activityType: 'removedChecklistItem',
  100. cardId: doc.cardId,
  101. boardId,
  102. checklistId: doc.checklistId,
  103. checklistItemId: doc._id,
  104. checklistItemName:doc.title,
  105. });
  106. Activities.remove({
  107. checklistItemId: doc._id,
  108. });
  109. }
  110. function publishCheckActivity(userId, doc){
  111. const card = Cards.findOne(doc.cardId);
  112. const boardId = card.boardId;
  113. let activityType;
  114. if(doc.isFinished){
  115. activityType = 'checkedItem';
  116. }else{
  117. activityType = 'uncheckedItem';
  118. }
  119. const act = {
  120. userId,
  121. activityType,
  122. cardId: doc.cardId,
  123. boardId,
  124. checklistId: doc.checklistId,
  125. checklistItemId: doc._id,
  126. checklistItemName:doc.title,
  127. };
  128. Activities.insert(act);
  129. }
  130. function publishChekListCompleted(userId, doc){
  131. const card = Cards.findOne(doc.cardId);
  132. const boardId = card.boardId;
  133. const checklistId = doc.checklistId;
  134. const checkList = Checklists.findOne({_id:checklistId});
  135. if(checkList.isFinished()){
  136. const act = {
  137. userId,
  138. activityType: 'completeChecklist',
  139. cardId: doc.cardId,
  140. boardId,
  141. checklistId: doc.checklistId,
  142. checklistName: checkList.title,
  143. };
  144. Activities.insert(act);
  145. }
  146. }
  147. function publishChekListUncompleted(userId, doc){
  148. const card = Cards.findOne(doc.cardId);
  149. const boardId = card.boardId;
  150. const checklistId = doc.checklistId;
  151. const checkList = Checklists.findOne({_id:checklistId});
  152. // BUGS in IFTTT Rules: https://github.com/wekan/wekan/issues/1972
  153. // Currently in checklist all are set as uncompleted/not checked,
  154. // IFTTT Rule does not move card to other list.
  155. // If following line is negated/changed to:
  156. // if(!checkList.isFinished()){
  157. // then unchecking of any checkbox will move card to other list,
  158. // even when all checkboxes are not yet unchecked.
  159. // What is correct code for only moving when all in list is unchecked?
  160. // TIPS: Finding files, ignoring some directories with grep -v:
  161. // cd wekan
  162. // find . | xargs grep 'count' -sl | grep -v .meteor | grep -v node_modules | grep -v .build
  163. // Maybe something related here?
  164. // wekan/client/components/rules/triggers/checklistTriggers.js
  165. if(checkList.isFinished()){
  166. const act = {
  167. userId,
  168. activityType: 'uncompleteChecklist',
  169. cardId: doc.cardId,
  170. boardId,
  171. checklistId: doc.checklistId,
  172. checklistName: checkList.title,
  173. };
  174. Activities.insert(act);
  175. }
  176. }
  177. // Activities
  178. if (Meteor.isServer) {
  179. Meteor.startup(() => {
  180. ChecklistItems._collection._ensureIndex({ checklistId: 1 });
  181. ChecklistItems._collection._ensureIndex({ cardId: 1 });
  182. });
  183. ChecklistItems.after.update((userId, doc, fieldNames) => {
  184. publishCheckActivity(userId, doc);
  185. publishChekListCompleted(userId, doc, fieldNames);
  186. });
  187. ChecklistItems.before.update((userId, doc, fieldNames) => {
  188. publishChekListUncompleted(userId, doc, fieldNames);
  189. });
  190. ChecklistItems.after.insert((userId, doc) => {
  191. itemCreation(userId, doc);
  192. });
  193. ChecklistItems.after.remove((userId, doc) => {
  194. itemRemover(userId, doc);
  195. });
  196. }
  197. if (Meteor.isServer) {
  198. /**
  199. * @operation get_checklist_item
  200. * @tag Checklists
  201. * @summary Get a checklist item
  202. *
  203. * @param {string} boardId the board ID
  204. * @param {string} cardId the card ID
  205. * @param {string} checklistId the checklist ID
  206. * @param {string} itemId the ID of the item
  207. * @return_type ChecklistItems
  208. */
  209. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId/items/:itemId', function (req, res) {
  210. Authentication.checkUserId( req.userId);
  211. const paramItemId = req.params.itemId;
  212. const checklistItem = ChecklistItems.findOne({ _id: paramItemId });
  213. if (checklistItem) {
  214. JsonRoutes.sendResult(res, {
  215. code: 200,
  216. data: checklistItem,
  217. });
  218. } else {
  219. JsonRoutes.sendResult(res, {
  220. code: 500,
  221. });
  222. }
  223. });
  224. /**
  225. * @operation edit_checklist_item
  226. * @tag Checklists
  227. * @summary Edit a checklist item
  228. *
  229. * @param {string} boardId the board ID
  230. * @param {string} cardId the card ID
  231. * @param {string} checklistId the checklist ID
  232. * @param {string} itemId the ID of the item
  233. * @param {string} [isFinished] is the item checked?
  234. * @param {string} [title] the new text of the item
  235. * @return_type {_id: string}
  236. */
  237. JsonRoutes.add('PUT', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId/items/:itemId', function (req, res) {
  238. Authentication.checkUserId( req.userId);
  239. const paramItemId = req.params.itemId;
  240. if (req.body.hasOwnProperty('isFinished')) {
  241. ChecklistItems.direct.update({_id: paramItemId}, {$set: {isFinished: req.body.isFinished}});
  242. }
  243. if (req.body.hasOwnProperty('title')) {
  244. ChecklistItems.direct.update({_id: paramItemId}, {$set: {title: req.body.title}});
  245. }
  246. JsonRoutes.sendResult(res, {
  247. code: 200,
  248. data: {
  249. _id: paramItemId,
  250. },
  251. });
  252. });
  253. /**
  254. * @operation delete_checklist_item
  255. * @tag Checklists
  256. * @summary Delete a checklist item
  257. *
  258. * @description Note: this operation can't be reverted.
  259. *
  260. * @param {string} boardId the board ID
  261. * @param {string} cardId the card ID
  262. * @param {string} checklistId the checklist ID
  263. * @param {string} itemId the ID of the item to be removed
  264. * @return_type {_id: string}
  265. */
  266. JsonRoutes.add('DELETE', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId/items/:itemId', function (req, res) {
  267. Authentication.checkUserId( req.userId);
  268. const paramItemId = req.params.itemId;
  269. ChecklistItems.direct.remove({ _id: paramItemId });
  270. JsonRoutes.sendResult(res, {
  271. code: 200,
  272. data: {
  273. _id: paramItemId,
  274. },
  275. });
  276. });
  277. }