checklistItems.js 11 KB

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