checklistItems.js 9.0 KB

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