checklistItems.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. });
  182. ChecklistItems.after.update((userId, doc, fieldNames) => {
  183. publishCheckActivity(userId, doc);
  184. publishChekListCompleted(userId, doc, fieldNames);
  185. });
  186. ChecklistItems.before.update((userId, doc, fieldNames) => {
  187. publishChekListUncompleted(userId, doc, fieldNames);
  188. });
  189. ChecklistItems.after.insert((userId, doc) => {
  190. itemCreation(userId, doc);
  191. });
  192. ChecklistItems.after.remove((userId, doc) => {
  193. itemRemover(userId, doc);
  194. });
  195. }
  196. if (Meteor.isServer) {
  197. /**
  198. * @operation get_checklist_item
  199. * @tag Checklists
  200. * @summary Get a checklist item
  201. *
  202. * @param {string} boardId the board ID
  203. * @param {string} cardId the card ID
  204. * @param {string} checklistId the checklist ID
  205. * @param {string} itemId the ID of the item
  206. * @return_type ChecklistItems
  207. */
  208. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId/items/:itemId', function (req, res) {
  209. Authentication.checkUserId( req.userId);
  210. const paramItemId = req.params.itemId;
  211. const checklistItem = ChecklistItems.findOne({ _id: paramItemId });
  212. if (checklistItem) {
  213. JsonRoutes.sendResult(res, {
  214. code: 200,
  215. data: checklistItem,
  216. });
  217. } else {
  218. JsonRoutes.sendResult(res, {
  219. code: 500,
  220. });
  221. }
  222. });
  223. /**
  224. * @operation edit_checklist_item
  225. * @tag Checklists
  226. * @summary Edit a checklist item
  227. *
  228. * @param {string} boardId the board ID
  229. * @param {string} cardId the card ID
  230. * @param {string} checklistId the checklist ID
  231. * @param {string} itemId the ID of the item
  232. * @param {string} [isFinished] is the item checked?
  233. * @param {string} [title] the new text of the item
  234. * @return_type {_id: string}
  235. */
  236. JsonRoutes.add('PUT', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId/items/:itemId', function (req, res) {
  237. Authentication.checkUserId( req.userId);
  238. const paramItemId = req.params.itemId;
  239. if (req.body.hasOwnProperty('isFinished')) {
  240. ChecklistItems.direct.update({_id: paramItemId}, {$set: {isFinished: req.body.isFinished}});
  241. }
  242. if (req.body.hasOwnProperty('title')) {
  243. ChecklistItems.direct.update({_id: paramItemId}, {$set: {title: req.body.title}});
  244. }
  245. JsonRoutes.sendResult(res, {
  246. code: 200,
  247. data: {
  248. _id: paramItemId,
  249. },
  250. });
  251. });
  252. /**
  253. * @operation delete_checklist_item
  254. * @tag Checklists
  255. * @summary Delete a checklist item
  256. *
  257. * @description Note: this operation can't be reverted.
  258. *
  259. * @param {string} boardId the board ID
  260. * @param {string} cardId the card ID
  261. * @param {string} checklistId the checklist ID
  262. * @param {string} itemId the ID of the item to be removed
  263. * @return_type {_id: string}
  264. */
  265. JsonRoutes.add('DELETE', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId/items/:itemId', function (req, res) {
  266. Authentication.checkUserId( req.userId);
  267. const paramItemId = req.params.itemId;
  268. ChecklistItems.direct.remove({ _id: paramItemId });
  269. JsonRoutes.sendResult(res, {
  270. code: 200,
  271. data: {
  272. _id: paramItemId,
  273. },
  274. });
  275. });
  276. }