checklistItems.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. listId: card.listId,
  93. swimlaneId: card.swimlaneId,
  94. });
  95. }
  96. function itemRemover(userId, doc) {
  97. Activities.remove({
  98. checklistItemId: doc._id,
  99. });
  100. }
  101. function publishCheckActivity(userId, doc){
  102. const card = Cards.findOne(doc.cardId);
  103. const boardId = card.boardId;
  104. let activityType;
  105. if(doc.isFinished){
  106. activityType = 'checkedItem';
  107. }else{
  108. activityType = 'uncheckedItem';
  109. }
  110. const act = {
  111. userId,
  112. activityType,
  113. cardId: doc.cardId,
  114. boardId,
  115. checklistId: doc.checklistId,
  116. checklistItemId: doc._id,
  117. checklistItemName:doc.title,
  118. listId: card.listId,
  119. swimlaneId: card.swimlaneId,
  120. };
  121. Activities.insert(act);
  122. }
  123. function publishChekListCompleted(userId, doc){
  124. const card = Cards.findOne(doc.cardId);
  125. const boardId = card.boardId;
  126. const checklistId = doc.checklistId;
  127. const checkList = Checklists.findOne({_id:checklistId});
  128. if(checkList.isFinished()){
  129. const act = {
  130. userId,
  131. activityType: 'completeChecklist',
  132. cardId: doc.cardId,
  133. boardId,
  134. checklistId: doc.checklistId,
  135. checklistName: checkList.title,
  136. listId: card.listId,
  137. swimlaneId: card.swimlaneId,
  138. };
  139. Activities.insert(act);
  140. }
  141. }
  142. function publishChekListUncompleted(userId, doc){
  143. const card = Cards.findOne(doc.cardId);
  144. const boardId = card.boardId;
  145. const checklistId = doc.checklistId;
  146. const checkList = Checklists.findOne({_id:checklistId});
  147. // BUGS in IFTTT Rules: https://github.com/wekan/wekan/issues/1972
  148. // Currently in checklist all are set as uncompleted/not checked,
  149. // IFTTT Rule does not move card to other list.
  150. // If following line is negated/changed to:
  151. // if(!checkList.isFinished()){
  152. // then unchecking of any checkbox will move card to other list,
  153. // even when all checkboxes are not yet unchecked.
  154. // What is correct code for only moving when all in list is unchecked?
  155. // TIPS: Finding files, ignoring some directories with grep -v:
  156. // cd wekan
  157. // find . | xargs grep 'count' -sl | grep -v .meteor | grep -v node_modules | grep -v .build
  158. // Maybe something related here?
  159. // wekan/client/components/rules/triggers/checklistTriggers.js
  160. if(checkList.isFinished()){
  161. const act = {
  162. userId,
  163. activityType: 'uncompleteChecklist',
  164. cardId: doc.cardId,
  165. boardId,
  166. checklistId: doc.checklistId,
  167. checklistName: checkList.title,
  168. listId: card.listId,
  169. swimlaneId: card.swimlaneId,
  170. };
  171. Activities.insert(act);
  172. }
  173. }
  174. // Activities
  175. if (Meteor.isServer) {
  176. Meteor.startup(() => {
  177. ChecklistItems._collection._ensureIndex({ checklistId: 1 });
  178. ChecklistItems._collection._ensureIndex({ cardId: 1 });
  179. });
  180. ChecklistItems.after.update((userId, doc, fieldNames) => {
  181. publishCheckActivity(userId, doc);
  182. publishChekListCompleted(userId, doc, fieldNames);
  183. });
  184. ChecklistItems.before.update((userId, doc, fieldNames) => {
  185. publishChekListUncompleted(userId, doc, fieldNames);
  186. });
  187. ChecklistItems.after.insert((userId, doc) => {
  188. itemCreation(userId, doc);
  189. });
  190. ChecklistItems.before.remove((userId, doc) => {
  191. itemRemover(userId, doc);
  192. const card = Cards.findOne(doc.cardId);
  193. const boardId = card.boardId;
  194. Activities.insert({
  195. userId,
  196. activityType: 'removedChecklistItem',
  197. cardId: doc.cardId,
  198. boardId,
  199. checklistId: doc.checklistId,
  200. checklistItemId: doc._id,
  201. checklistItemName:doc.title,
  202. listId: card.listId,
  203. swimlaneId: card.swimlaneId,
  204. });
  205. });
  206. }
  207. if (Meteor.isServer) {
  208. /**
  209. * @operation get_checklist_item
  210. * @tag Checklists
  211. * @summary Get a checklist item
  212. *
  213. * @param {string} boardId the board ID
  214. * @param {string} cardId the card ID
  215. * @param {string} checklistId the checklist ID
  216. * @param {string} itemId the ID of the item
  217. * @return_type ChecklistItems
  218. */
  219. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId/items/:itemId', function (req, res) {
  220. Authentication.checkUserId( req.userId);
  221. const paramItemId = req.params.itemId;
  222. const checklistItem = ChecklistItems.findOne({ _id: paramItemId });
  223. if (checklistItem) {
  224. JsonRoutes.sendResult(res, {
  225. code: 200,
  226. data: checklistItem,
  227. });
  228. } else {
  229. JsonRoutes.sendResult(res, {
  230. code: 500,
  231. });
  232. }
  233. });
  234. /**
  235. * @operation edit_checklist_item
  236. * @tag Checklists
  237. * @summary Edit a checklist item
  238. *
  239. * @param {string} boardId the board ID
  240. * @param {string} cardId the card ID
  241. * @param {string} checklistId the checklist ID
  242. * @param {string} itemId the ID of the item
  243. * @param {string} [isFinished] is the item checked?
  244. * @param {string} [title] the new text of the item
  245. * @return_type {_id: string}
  246. */
  247. JsonRoutes.add('PUT', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId/items/:itemId', function (req, res) {
  248. Authentication.checkUserId( req.userId);
  249. const paramItemId = req.params.itemId;
  250. if (req.body.hasOwnProperty('isFinished')) {
  251. ChecklistItems.direct.update({_id: paramItemId}, {$set: {isFinished: req.body.isFinished}});
  252. }
  253. if (req.body.hasOwnProperty('title')) {
  254. ChecklistItems.direct.update({_id: paramItemId}, {$set: {title: req.body.title}});
  255. }
  256. JsonRoutes.sendResult(res, {
  257. code: 200,
  258. data: {
  259. _id: paramItemId,
  260. },
  261. });
  262. });
  263. /**
  264. * @operation delete_checklist_item
  265. * @tag Checklists
  266. * @summary Delete a checklist item
  267. *
  268. * @description Note: this operation can't be reverted.
  269. *
  270. * @param {string} boardId the board ID
  271. * @param {string} cardId the card ID
  272. * @param {string} checklistId the checklist ID
  273. * @param {string} itemId the ID of the item to be removed
  274. * @return_type {_id: string}
  275. */
  276. JsonRoutes.add('DELETE', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId/items/:itemId', function (req, res) {
  277. Authentication.checkUserId( req.userId);
  278. const paramItemId = req.params.itemId;
  279. ChecklistItems.direct.remove({ _id: paramItemId });
  280. JsonRoutes.sendResult(res, {
  281. code: 200,
  282. data: {
  283. _id: paramItemId,
  284. },
  285. });
  286. });
  287. }