checklistItems.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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.before.update((userId, doc, fieldNames, modifier, options) => {
  215. modifier.$set = modifier.$set || {};
  216. modifier.$set.modifiedAt = Date.now();
  217. });
  218. ChecklistItems.after.insert((userId, doc) => {
  219. itemCreation(userId, doc);
  220. });
  221. ChecklistItems.before.remove((userId, doc) => {
  222. itemRemover(userId, doc);
  223. const card = Cards.findOne(doc.cardId);
  224. const boardId = card.boardId;
  225. Activities.insert({
  226. userId,
  227. activityType: 'removedChecklistItem',
  228. cardId: doc.cardId,
  229. boardId,
  230. checklistId: doc.checklistId,
  231. checklistItemId: doc._id,
  232. checklistItemName: doc.title,
  233. listId: card.listId,
  234. swimlaneId: card.swimlaneId,
  235. });
  236. });
  237. }
  238. if (Meteor.isServer) {
  239. /**
  240. * @operation get_checklist_item
  241. * @tag Checklists
  242. * @summary Get a checklist item
  243. *
  244. * @param {string} boardId the board ID
  245. * @param {string} cardId the card ID
  246. * @param {string} checklistId the checklist ID
  247. * @param {string} itemId the ID of the item
  248. * @return_type ChecklistItems
  249. */
  250. JsonRoutes.add(
  251. 'GET',
  252. '/api/boards/:boardId/cards/:cardId/checklists/:checklistId/items/:itemId',
  253. function(req, res) {
  254. Authentication.checkUserId(req.userId);
  255. const paramItemId = req.params.itemId;
  256. const checklistItem = ChecklistItems.findOne({ _id: paramItemId });
  257. if (checklistItem) {
  258. JsonRoutes.sendResult(res, {
  259. code: 200,
  260. data: checklistItem,
  261. });
  262. } else {
  263. JsonRoutes.sendResult(res, {
  264. code: 500,
  265. });
  266. }
  267. }
  268. );
  269. /**
  270. * @operation edit_checklist_item
  271. * @tag Checklists
  272. * @summary Edit a checklist item
  273. *
  274. * @param {string} boardId the board ID
  275. * @param {string} cardId the card ID
  276. * @param {string} checklistId the checklist ID
  277. * @param {string} itemId the ID of the item
  278. * @param {string} [isFinished] is the item checked?
  279. * @param {string} [title] the new text of the item
  280. * @return_type {_id: string}
  281. */
  282. JsonRoutes.add(
  283. 'PUT',
  284. '/api/boards/:boardId/cards/:cardId/checklists/:checklistId/items/:itemId',
  285. function(req, res) {
  286. Authentication.checkUserId(req.userId);
  287. const paramItemId = req.params.itemId;
  288. if (req.body.hasOwnProperty('isFinished')) {
  289. ChecklistItems.direct.update(
  290. { _id: paramItemId },
  291. { $set: { isFinished: req.body.isFinished } }
  292. );
  293. }
  294. if (req.body.hasOwnProperty('title')) {
  295. ChecklistItems.direct.update(
  296. { _id: paramItemId },
  297. { $set: { title: req.body.title } }
  298. );
  299. }
  300. JsonRoutes.sendResult(res, {
  301. code: 200,
  302. data: {
  303. _id: paramItemId,
  304. },
  305. });
  306. }
  307. );
  308. /**
  309. * @operation delete_checklist_item
  310. * @tag Checklists
  311. * @summary Delete a checklist item
  312. *
  313. * @description Note: this operation can't be reverted.
  314. *
  315. * @param {string} boardId the board ID
  316. * @param {string} cardId the card ID
  317. * @param {string} checklistId the checklist ID
  318. * @param {string} itemId the ID of the item to be removed
  319. * @return_type {_id: string}
  320. */
  321. JsonRoutes.add(
  322. 'DELETE',
  323. '/api/boards/:boardId/cards/:cardId/checklists/:checklistId/items/:itemId',
  324. function(req, res) {
  325. Authentication.checkUserId(req.userId);
  326. const paramItemId = req.params.itemId;
  327. ChecklistItems.direct.remove({ _id: paramItemId });
  328. JsonRoutes.sendResult(res, {
  329. code: 200,
  330. data: {
  331. _id: paramItemId,
  332. },
  333. });
  334. }
  335. );
  336. }
  337. export default ChecklistItems;