| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 | ChecklistItems = new Mongo.Collection('checklistItems');/** * An item in a checklist */ChecklistItems.attachSchema(  new SimpleSchema({    title: {      /**       * the text of the item       */      type: String,    },    sort: {      /**       * the sorting field of the item       */      type: Number,      decimal: true,    },    isFinished: {      /**       * Is the item checked?       */      type: Boolean,      defaultValue: false,    },    checklistId: {      /**       * the checklist ID the item is attached to       */      type: String,    },    cardId: {      /**       * the card ID the item is attached to       */      type: String,    },    createdAt: {      type: Date,      optional: true,      // eslint-disable-next-line consistent-return      autoValue() {        if (this.isInsert) {          return new Date();        } else if (this.isUpsert) {          return { $setOnInsert: new Date() };        } else {          this.unset();        }      },    },    modifiedAt: {      type: Date,      denyUpdate: false,      // eslint-disable-next-line consistent-return      autoValue() {        if (this.isInsert || this.isUpsert || this.isUpdate) {          return new Date();        } else {          this.unset();        }      },    },  }),);ChecklistItems.allow({  insert(userId, doc) {    return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));  },  update(userId, doc) {    return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));  },  remove(userId, doc) {    return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));  },  fetch: ['userId', 'cardId'],});ChecklistItems.before.insert((userId, doc) => {  if (!doc.userId) {    doc.userId = userId;  }});// MutationsChecklistItems.mutations({  setTitle(title) {    return { $set: { title } };  },  check() {    return { $set: { isFinished: true } };  },  uncheck() {    return { $set: { isFinished: false } };  },  toggleItem() {    return { $set: { isFinished: !this.isFinished } };  },  move(checklistId, sortIndex) {    const cardId = Checklists.findOne(checklistId).cardId;    const mutatedFields = {      cardId,      checklistId,      sort: sortIndex,    };    return { $set: mutatedFields };  },});// Activities helperfunction itemCreation(userId, doc) {  const card = Cards.findOne(doc.cardId);  const boardId = card.boardId;  Activities.insert({    userId,    activityType: 'addChecklistItem',    cardId: doc.cardId,    boardId,    checklistId: doc.checklistId,    checklistItemId: doc._id,    checklistItemName: doc.title,    listId: card.listId,    swimlaneId: card.swimlaneId,  });}function itemRemover(userId, doc) {  Activities.remove({    checklistItemId: doc._id,  });}function publishCheckActivity(userId, doc) {  const card = Cards.findOne(doc.cardId);  const boardId = card.boardId;  let activityType;  if (doc.isFinished) {    activityType = 'checkedItem';  } else {    activityType = 'uncheckedItem';  }  const act = {    userId,    activityType,    cardId: doc.cardId,    boardId,    checklistId: doc.checklistId,    checklistItemId: doc._id,    checklistItemName: doc.title,    listId: card.listId,    swimlaneId: card.swimlaneId,  };  Activities.insert(act);}function publishChekListCompleted(userId, doc) {  const card = Cards.findOne(doc.cardId);  const boardId = card.boardId;  const checklistId = doc.checklistId;  const checkList = Checklists.findOne({ _id: checklistId });  if (checkList.isFinished()) {    const act = {      userId,      activityType: 'completeChecklist',      cardId: doc.cardId,      boardId,      checklistId: doc.checklistId,      checklistName: checkList.title,      listId: card.listId,      swimlaneId: card.swimlaneId,    };    Activities.insert(act);  }}function publishChekListUncompleted(userId, doc) {  const card = Cards.findOne(doc.cardId);  const boardId = card.boardId;  const checklistId = doc.checklistId;  const checkList = Checklists.findOne({ _id: checklistId });  // BUGS in IFTTT Rules: https://github.com/wekan/wekan/issues/1972  //       Currently in checklist all are set as uncompleted/not checked,  //       IFTTT Rule does not move card to other list.  //       If following line is negated/changed to:  //         if(!checkList.isFinished()){  //       then unchecking of any checkbox will move card to other list,  //       even when all checkboxes are not yet unchecked.  //       What is correct code for only moving when all in list is unchecked?  // TIPS: Finding  files, ignoring some directories with grep -v:  //         cd wekan  //         find . | xargs grep 'count' -sl | grep -v .meteor | grep -v node_modules | grep -v .build  //       Maybe something related here?  //         wekan/client/components/rules/triggers/checklistTriggers.js  if (checkList.isFinished()) {    const act = {      userId,      activityType: 'uncompleteChecklist',      cardId: doc.cardId,      boardId,      checklistId: doc.checklistId,      checklistName: checkList.title,      listId: card.listId,      swimlaneId: card.swimlaneId,    };    Activities.insert(act);  }}// Activitiesif (Meteor.isServer) {  Meteor.startup(() => {    ChecklistItems._collection._ensureIndex({ modifiedAt: -1 });    ChecklistItems._collection._ensureIndex({ checklistId: 1 });    ChecklistItems._collection._ensureIndex({ cardId: 1 });  });  ChecklistItems.after.update((userId, doc, fieldNames) => {    publishCheckActivity(userId, doc);    publishChekListCompleted(userId, doc, fieldNames);  });  ChecklistItems.before.update((userId, doc, fieldNames) => {    publishChekListUncompleted(userId, doc, fieldNames);  });  ChecklistItems.after.insert((userId, doc) => {    itemCreation(userId, doc);  });  ChecklistItems.before.remove((userId, doc) => {    itemRemover(userId, doc);    const card = Cards.findOne(doc.cardId);    const boardId = card.boardId;    Activities.insert({      userId,      activityType: 'removedChecklistItem',      cardId: doc.cardId,      boardId,      checklistId: doc.checklistId,      checklistItemId: doc._id,      checklistItemName: doc.title,      listId: card.listId,      swimlaneId: card.swimlaneId,    });  });}if (Meteor.isServer) {  /**   * @operation get_checklist_item   * @tag Checklists   * @summary Get a checklist item   *   * @param {string} boardId the board ID   * @param {string} cardId the card ID   * @param {string} checklistId the checklist ID   * @param {string} itemId the ID of the item   * @return_type ChecklistItems   */  JsonRoutes.add(    'GET',    '/api/boards/:boardId/cards/:cardId/checklists/:checklistId/items/:itemId',    function(req, res) {      Authentication.checkUserId(req.userId);      const paramItemId = req.params.itemId;      const checklistItem = ChecklistItems.findOne({ _id: paramItemId });      if (checklistItem) {        JsonRoutes.sendResult(res, {          code: 200,          data: checklistItem,        });      } else {        JsonRoutes.sendResult(res, {          code: 500,        });      }    },  );  /**   * @operation edit_checklist_item   * @tag Checklists   * @summary Edit a checklist item   *   * @param {string} boardId the board ID   * @param {string} cardId the card ID   * @param {string} checklistId the checklist ID   * @param {string} itemId the ID of the item   * @param {string} [isFinished] is the item checked?   * @param {string} [title] the new text of the item   * @return_type {_id: string}   */  JsonRoutes.add(    'PUT',    '/api/boards/:boardId/cards/:cardId/checklists/:checklistId/items/:itemId',    function(req, res) {      Authentication.checkUserId(req.userId);      const paramItemId = req.params.itemId;      function isTrue(data) {        try {          return data.toLowerCase() === 'true';        } catch (error) {          return data;        }      }      if (req.body.hasOwnProperty('isFinished')) {        ChecklistItems.direct.update(          { _id: paramItemId },          { $set: { isFinished: isTrue(req.body.isFinished) } },        );      }      if (req.body.hasOwnProperty('title')) {        ChecklistItems.direct.update(          { _id: paramItemId },          { $set: { title: req.body.title } },        );      }      JsonRoutes.sendResult(res, {        code: 200,        data: {          _id: paramItemId,        },      });    },  );  /**   * @operation delete_checklist_item   * @tag Checklists   * @summary Delete a checklist item   *   * @description Note: this operation can't be reverted.   *   * @param {string} boardId the board ID   * @param {string} cardId the card ID   * @param {string} checklistId the checklist ID   * @param {string} itemId the ID of the item to be removed   * @return_type {_id: string}   */  JsonRoutes.add(    'DELETE',    '/api/boards/:boardId/cards/:cardId/checklists/:checklistId/items/:itemId',    function(req, res) {      Authentication.checkUserId(req.userId);      const paramItemId = req.params.itemId;      ChecklistItems.direct.remove({ _id: paramItemId });      JsonRoutes.sendResult(res, {        code: 200,        data: {          _id: paramItemId,        },      });    },  );}export default ChecklistItems;
 |