checklistItems.js 7.8 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. Activities.remove({
  96. checklistItemId: doc._id,
  97. });
  98. }
  99. function publishCheckActivity(userId, doc){
  100. const card = Cards.findOne(doc.cardId);
  101. const boardId = card.boardId;
  102. let activityType;
  103. if(doc.isFinished){
  104. activityType = 'checkedItem';
  105. }else{
  106. activityType = 'uncheckedItem';
  107. }
  108. const act = {
  109. userId,
  110. activityType,
  111. cardId: doc.cardId,
  112. boardId,
  113. checklistId: doc.checklistId,
  114. checklistItemId: doc._id,
  115. checklistItemName:doc.title,
  116. };
  117. Activities.insert(act);
  118. }
  119. function publishChekListCompleted(userId, doc){
  120. const card = Cards.findOne(doc.cardId);
  121. const boardId = card.boardId;
  122. const checklistId = doc.checklistId;
  123. const checkList = Checklists.findOne({_id:checklistId});
  124. if(checkList.isFinished()){
  125. const act = {
  126. userId,
  127. activityType: 'completeChecklist',
  128. cardId: doc.cardId,
  129. boardId,
  130. checklistId: doc.checklistId,
  131. checklistName: checkList.title,
  132. };
  133. Activities.insert(act);
  134. }
  135. }
  136. function publishChekListUncompleted(userId, doc){
  137. const card = Cards.findOne(doc.cardId);
  138. const boardId = card.boardId;
  139. const checklistId = doc.checklistId;
  140. const checkList = Checklists.findOne({_id:checklistId});
  141. // BUGS in IFTTT Rules: https://github.com/wekan/wekan/issues/1972
  142. // Currently in checklist all are set as uncompleted/not checked,
  143. // IFTTT Rule does not move card to other list.
  144. // If following line is negated/changed to:
  145. // if(!checkList.isFinished()){
  146. // then unchecking of any checkbox will move card to other list,
  147. // even when all checkboxes are not yet unchecked.
  148. // What is correct code for only moving when all in list is unchecked?
  149. // TIPS: Finding files, ignoring some directories with grep -v:
  150. // cd wekan
  151. // find . | xargs grep 'count' -sl | grep -v .meteor | grep -v node_modules | grep -v .build
  152. // Maybe something related here?
  153. // wekan/client/components/rules/triggers/checklistTriggers.js
  154. if(checkList.isFinished()){
  155. const act = {
  156. userId,
  157. activityType: 'uncompleteChecklist',
  158. cardId: doc.cardId,
  159. boardId,
  160. checklistId: doc.checklistId,
  161. checklistName: checkList.title,
  162. };
  163. Activities.insert(act);
  164. }
  165. }
  166. // Activities
  167. if (Meteor.isServer) {
  168. Meteor.startup(() => {
  169. ChecklistItems._collection._ensureIndex({ checklistId: 1 });
  170. ChecklistItems._collection._ensureIndex({ cardId: 1 });
  171. });
  172. ChecklistItems.after.update((userId, doc, fieldNames) => {
  173. publishCheckActivity(userId, doc);
  174. publishChekListCompleted(userId, doc, fieldNames);
  175. });
  176. ChecklistItems.before.update((userId, doc, fieldNames) => {
  177. publishChekListUncompleted(userId, doc, fieldNames);
  178. });
  179. ChecklistItems.after.insert((userId, doc) => {
  180. itemCreation(userId, doc);
  181. });
  182. ChecklistItems.before.remove((userId, doc) => {
  183. itemRemover(userId, doc);
  184. const card = Cards.findOne(doc.cardId);
  185. const boardId = card.boardId;
  186. Activities.insert({
  187. userId,
  188. activityType: 'removedChecklistItem',
  189. cardId: doc.cardId,
  190. boardId,
  191. checklistId: doc.checklistId,
  192. checklistItemId: doc._id,
  193. checklistItemName:doc.title,
  194. });
  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. }