checklistItems.js 9.4 KB

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