cards.js 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910
  1. Cards = new Mongo.Collection('cards');
  2. // XXX To improve pub/sub performances a card document should include a
  3. // de-normalized number of comments so we don't have to publish the whole list
  4. // of comments just to display the number of them in the board view.
  5. Cards.attachSchema(new SimpleSchema({
  6. title: {
  7. /**
  8. * the title of the card
  9. */
  10. type: String,
  11. optional: true,
  12. defaultValue: '',
  13. },
  14. archived: {
  15. /**
  16. * is the card archived
  17. */
  18. type: Boolean,
  19. autoValue() { // eslint-disable-line consistent-return
  20. if (this.isInsert && !this.isSet) {
  21. return false;
  22. }
  23. },
  24. },
  25. parentId: {
  26. /**
  27. * ID of the parent card
  28. */
  29. type: String,
  30. optional: true,
  31. defaultValue: '',
  32. },
  33. listId: {
  34. /**
  35. * List ID where the card is
  36. */
  37. type: String,
  38. optional: true,
  39. defaultValue: '',
  40. },
  41. swimlaneId: {
  42. /**
  43. * Swimlane ID where the card is
  44. */
  45. type: String,
  46. },
  47. // The system could work without this `boardId` information (we could deduce
  48. // the board identifier from the card), but it would make the system more
  49. // difficult to manage and less efficient.
  50. boardId: {
  51. /**
  52. * Board ID of the card
  53. */
  54. type: String,
  55. optional: true,
  56. defaultValue: '',
  57. },
  58. coverId: {
  59. /**
  60. * Cover ID of the card
  61. */
  62. type: String,
  63. optional: true,
  64. defaultValue: '',
  65. },
  66. color: {
  67. type: String,
  68. optional: true,
  69. allowedValues: [
  70. 'white', 'green', 'yellow', 'orange', 'red', 'purple',
  71. 'blue', 'sky', 'lime', 'pink', 'black',
  72. 'silver', 'peachpuff', 'crimson', 'plum', 'darkgreen',
  73. 'slateblue', 'magenta', 'gold', 'navy', 'gray',
  74. 'saddlebrown', 'paleturquoise', 'mistyrose', 'indigo',
  75. ],
  76. },
  77. createdAt: {
  78. /**
  79. * creation date
  80. */
  81. type: Date,
  82. autoValue() { // eslint-disable-line consistent-return
  83. if (this.isInsert) {
  84. return new Date();
  85. } else {
  86. this.unset();
  87. }
  88. },
  89. },
  90. customFields: {
  91. /**
  92. * list of custom fields
  93. */
  94. type: [Object],
  95. optional: true,
  96. defaultValue: [],
  97. },
  98. 'customFields.$': {
  99. type: new SimpleSchema({
  100. _id: {
  101. /**
  102. * the ID of the related custom field
  103. */
  104. type: String,
  105. optional: true,
  106. defaultValue: '',
  107. },
  108. value: {
  109. /**
  110. * value attached to the custom field
  111. */
  112. type: Match.OneOf(String, Number, Boolean, Date),
  113. optional: true,
  114. defaultValue: '',
  115. },
  116. }),
  117. },
  118. dateLastActivity: {
  119. /**
  120. * Date of last activity
  121. */
  122. type: Date,
  123. autoValue() {
  124. return new Date();
  125. },
  126. },
  127. description: {
  128. /**
  129. * description of the card
  130. */
  131. type: String,
  132. optional: true,
  133. defaultValue: '',
  134. },
  135. requestedBy: {
  136. /**
  137. * who requested the card (ID of the user)
  138. */
  139. type: String,
  140. optional: true,
  141. defaultValue: '',
  142. },
  143. assignedBy: {
  144. /**
  145. * who assigned the card (ID of the user)
  146. */
  147. type: String,
  148. optional: true,
  149. defaultValue: '',
  150. },
  151. labelIds: {
  152. /**
  153. * list of labels ID the card has
  154. */
  155. type: [String],
  156. optional: true,
  157. defaultValue: [],
  158. },
  159. members: {
  160. /**
  161. * list of members (user IDs)
  162. */
  163. type: [String],
  164. optional: true,
  165. defaultValue: [],
  166. },
  167. receivedAt: {
  168. /**
  169. * Date the card was received
  170. */
  171. type: Date,
  172. optional: true,
  173. },
  174. startAt: {
  175. /**
  176. * Date the card was started to be worked on
  177. */
  178. type: Date,
  179. optional: true,
  180. },
  181. dueAt: {
  182. /**
  183. * Date the card is due
  184. */
  185. type: Date,
  186. optional: true,
  187. },
  188. endAt: {
  189. /**
  190. * Date the card ended
  191. */
  192. type: Date,
  193. optional: true,
  194. },
  195. spentTime: {
  196. /**
  197. * How much time has been spent on this
  198. */
  199. type: Number,
  200. decimal: true,
  201. optional: true,
  202. defaultValue: 0,
  203. },
  204. isOvertime: {
  205. /**
  206. * is the card over time?
  207. */
  208. type: Boolean,
  209. defaultValue: false,
  210. optional: true,
  211. },
  212. // XXX Should probably be called `authorId`. Is it even needed since we have
  213. // the `members` field?
  214. userId: {
  215. /**
  216. * user ID of the author of the card
  217. */
  218. type: String,
  219. autoValue() { // eslint-disable-line consistent-return
  220. if (this.isInsert && !this.isSet) {
  221. return this.userId;
  222. }
  223. },
  224. },
  225. sort: {
  226. /**
  227. * Sort value
  228. */
  229. type: Number,
  230. decimal: true,
  231. defaultValue: '',
  232. },
  233. subtaskSort: {
  234. /**
  235. * subtask sort value
  236. */
  237. type: Number,
  238. decimal: true,
  239. defaultValue: -1,
  240. optional: true,
  241. },
  242. type: {
  243. /**
  244. * type of the card
  245. */
  246. type: String,
  247. defaultValue: 'cardType-card',
  248. },
  249. linkedId: {
  250. /**
  251. * ID of the linked card
  252. */
  253. type: String,
  254. optional: true,
  255. defaultValue: '',
  256. },
  257. }));
  258. Cards.allow({
  259. insert(userId, doc) {
  260. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  261. },
  262. update(userId, doc) {
  263. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  264. },
  265. remove(userId, doc) {
  266. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  267. },
  268. fetch: ['boardId'],
  269. });
  270. Cards.helpers({
  271. copy(boardId, swimlaneId, listId) {
  272. const oldBoard = Boards.findOne(this.boardId);
  273. const oldBoardLabels = oldBoard.labels;
  274. // Get old label names
  275. const oldCardLabels = _.pluck(_.filter(oldBoardLabels, (label) => {
  276. return _.contains(this.labelIds, label._id);
  277. }), 'name');
  278. const newBoard = Boards.findOne(boardId);
  279. const newBoardLabels = newBoard.labels;
  280. const newCardLabels = _.pluck(_.filter(newBoardLabels, (label) => {
  281. return _.contains(oldCardLabels, label.name);
  282. }), '_id');
  283. const oldId = this._id;
  284. const oldCard = Cards.findOne(oldId);
  285. delete this._id;
  286. delete this.labelIds;
  287. this.labelIds= newCardLabels;
  288. this.boardId = boardId;
  289. this.swimlaneId = swimlaneId;
  290. this.listId = listId;
  291. const _id = Cards.insert(this);
  292. // Copy attachments
  293. oldCard.attachments().forEach((att) => {
  294. att.cardId = _id;
  295. delete att._id;
  296. return Attachments.insert(att);
  297. });
  298. // copy checklists
  299. Checklists.find({cardId: oldId}).forEach((ch) => {
  300. // REMOVE verify copy with arguments
  301. ch.copy(_id);
  302. });
  303. // copy subtasks
  304. Cards.find({parentId: oldId}).forEach((subtask) => {
  305. subtask.parentId = _id;
  306. subtask._id = null;
  307. // REMOVE verify copy with arguments instead of insert?
  308. Cards.insert(subtask);
  309. });
  310. // copy card comments
  311. CardComments.find({cardId: oldId}).forEach((cmt) => {
  312. // REMOVE verify copy with arguments
  313. cmt.copy(_id);
  314. });
  315. return _id;
  316. },
  317. list() {
  318. return Lists.findOne(this.listId);
  319. },
  320. board() {
  321. return Boards.findOne(this.boardId);
  322. },
  323. labels() {
  324. const boardLabels = this.board().labels;
  325. const cardLabels = _.filter(boardLabels, (label) => {
  326. return _.contains(this.labelIds, label._id);
  327. });
  328. return cardLabels;
  329. },
  330. hasLabel(labelId) {
  331. return _.contains(this.labelIds, labelId);
  332. },
  333. user() {
  334. return Users.findOne(this.userId);
  335. },
  336. isAssigned(memberId) {
  337. return _.contains(this.getMembers(), memberId);
  338. },
  339. activities() {
  340. if (this.isLinkedCard()) {
  341. return Activities.find({cardId: this.linkedId}, {sort: {createdAt: -1}});
  342. } else if (this.isLinkedBoard()) {
  343. return Activities.find({boardId: this.linkedId}, {sort: {createdAt: -1}});
  344. } else {
  345. return Activities.find({cardId: this._id}, {sort: {createdAt: -1}});
  346. }
  347. },
  348. comments() {
  349. if (this.isLinkedCard()) {
  350. return CardComments.find({cardId: this.linkedId}, {sort: {createdAt: -1}});
  351. } else {
  352. return CardComments.find({cardId: this._id}, {sort: {createdAt: -1}});
  353. }
  354. },
  355. attachments() {
  356. if (this.isLinkedCard()) {
  357. return Attachments.find({cardId: this.linkedId}, {sort: {uploadedAt: -1}});
  358. } else {
  359. return Attachments.find({cardId: this._id}, {sort: {uploadedAt: -1}});
  360. }
  361. },
  362. cover() {
  363. const cover = Attachments.findOne(this.coverId);
  364. // if we return a cover before it is fully stored, we will get errors when we try to display it
  365. // todo XXX we could return a default "upload pending" image in the meantime?
  366. return cover && cover.url() && cover;
  367. },
  368. checklists() {
  369. if (this.isLinkedCard()) {
  370. return Checklists.find({cardId: this.linkedId}, {sort: { sort: 1 } });
  371. } else {
  372. return Checklists.find({cardId: this._id}, {sort: { sort: 1 } });
  373. }
  374. },
  375. checklistItemCount() {
  376. const checklists = this.checklists().fetch();
  377. return checklists.map((checklist) => {
  378. return checklist.itemCount();
  379. }).reduce((prev, next) => {
  380. return prev + next;
  381. }, 0);
  382. },
  383. checklistFinishedCount() {
  384. const checklists = this.checklists().fetch();
  385. return checklists.map((checklist) => {
  386. return checklist.finishedCount();
  387. }).reduce((prev, next) => {
  388. return prev + next;
  389. }, 0);
  390. },
  391. checklistFinished() {
  392. return this.hasChecklist() && this.checklistItemCount() === this.checklistFinishedCount();
  393. },
  394. hasChecklist() {
  395. return this.checklistItemCount() !== 0;
  396. },
  397. subtasks() {
  398. return Cards.find({
  399. parentId: this._id,
  400. archived: false,
  401. }, {
  402. sort: {
  403. sort: 1,
  404. },
  405. });
  406. },
  407. allSubtasks() {
  408. return Cards.find({
  409. parentId: this._id,
  410. archived: false,
  411. }, {
  412. sort: {
  413. sort: 1,
  414. },
  415. });
  416. },
  417. subtasksCount() {
  418. return Cards.find({
  419. parentId: this._id,
  420. archived: false,
  421. }).count();
  422. },
  423. subtasksFinishedCount() {
  424. return Cards.find({
  425. parentId: this._id,
  426. archived: true,
  427. }).count();
  428. },
  429. subtasksFinished() {
  430. const finishCount = this.subtasksFinishedCount();
  431. return finishCount > 0 && this.subtasksCount() === finishCount;
  432. },
  433. allowsSubtasks() {
  434. return this.subtasksCount() !== 0;
  435. },
  436. customFieldIndex(customFieldId) {
  437. return _.pluck(this.customFields, '_id').indexOf(customFieldId);
  438. },
  439. // customFields with definitions
  440. customFieldsWD() {
  441. // get all definitions
  442. const definitions = CustomFields.find({
  443. boardId: this.boardId,
  444. }).fetch();
  445. // match right definition to each field
  446. if (!this.customFields) return [];
  447. return this.customFields.map((customField) => {
  448. const definition = definitions.find((definition) => {
  449. return definition._id === customField._id;
  450. });
  451. //search for "True Value" which is for DropDowns other then the Value (which is the id)
  452. let trueValue = customField.value;
  453. if (definition.settings.dropdownItems && definition.settings.dropdownItems.length > 0) {
  454. for (let i = 0; i < definition.settings.dropdownItems.length; i++) {
  455. if (definition.settings.dropdownItems[i]._id === customField.value) {
  456. trueValue = definition.settings.dropdownItems[i].name;
  457. }
  458. }
  459. }
  460. return {
  461. _id: customField._id,
  462. value: customField.value,
  463. trueValue,
  464. definition,
  465. };
  466. });
  467. },
  468. colorClass() {
  469. if (this.color)
  470. return this.color;
  471. return '';
  472. },
  473. absoluteUrl() {
  474. const board = this.board();
  475. return FlowRouter.url('card', {
  476. boardId: board._id,
  477. slug: board.slug,
  478. cardId: this._id,
  479. });
  480. },
  481. canBeRestored() {
  482. const list = Lists.findOne({
  483. _id: this.listId,
  484. });
  485. if (!list.getWipLimit('soft') && list.getWipLimit('enabled') && list.getWipLimit('value') === list.cards().count()) {
  486. return false;
  487. }
  488. return true;
  489. },
  490. parentCard() {
  491. if (this.parentId === '') {
  492. return null;
  493. }
  494. return Cards.findOne(this.parentId);
  495. },
  496. parentCardName() {
  497. let result = '';
  498. if (this.parentId !== '') {
  499. const card = Cards.findOne(this.parentId);
  500. if (card) {
  501. result = card.title;
  502. }
  503. }
  504. return result;
  505. },
  506. parentListId() {
  507. const result = [];
  508. let crtParentId = this.parentId;
  509. while (crtParentId !== '') {
  510. const crt = Cards.findOne(crtParentId);
  511. if ((crt === null) || (crt === undefined)) {
  512. // maybe it has been deleted
  513. break;
  514. }
  515. if (crtParentId in result) {
  516. // circular reference
  517. break;
  518. }
  519. result.unshift(crtParentId);
  520. crtParentId = crt.parentId;
  521. }
  522. return result;
  523. },
  524. parentList() {
  525. const resultId = [];
  526. const result = [];
  527. let crtParentId = this.parentId;
  528. while (crtParentId !== '') {
  529. const crt = Cards.findOne(crtParentId);
  530. if ((crt === null) || (crt === undefined)) {
  531. // maybe it has been deleted
  532. break;
  533. }
  534. if (crtParentId in resultId) {
  535. // circular reference
  536. break;
  537. }
  538. resultId.unshift(crtParentId);
  539. result.unshift(crt);
  540. crtParentId = crt.parentId;
  541. }
  542. return result;
  543. },
  544. parentString(sep) {
  545. return this.parentList().map(function(elem) {
  546. return elem.title;
  547. }).join(sep);
  548. },
  549. isTopLevel() {
  550. return this.parentId === '';
  551. },
  552. isLinkedCard() {
  553. return this.type === 'cardType-linkedCard';
  554. },
  555. isLinkedBoard() {
  556. return this.type === 'cardType-linkedBoard';
  557. },
  558. isLinked() {
  559. return this.isLinkedCard() || this.isLinkedBoard();
  560. },
  561. setDescription(description) {
  562. if (this.isLinkedCard()) {
  563. return Cards.update({_id: this.linkedId}, {$set: {description}});
  564. } else if (this.isLinkedBoard()) {
  565. return Boards.update({_id: this.linkedId}, {$set: {description}});
  566. } else {
  567. return Cards.update(
  568. {_id: this._id},
  569. {$set: {description}}
  570. );
  571. }
  572. },
  573. getDescription() {
  574. if (this.isLinkedCard()) {
  575. const card = Cards.findOne({_id: this.linkedId});
  576. if (card && card.description)
  577. return card.description;
  578. else
  579. return null;
  580. } else if (this.isLinkedBoard()) {
  581. const board = Boards.findOne({_id: this.linkedId});
  582. if (board && board.description)
  583. return board.description;
  584. else
  585. return null;
  586. } else if (this.description) {
  587. return this.description;
  588. } else {
  589. return null;
  590. }
  591. },
  592. getMembers() {
  593. if (this.isLinkedCard()) {
  594. const card = Cards.findOne({_id: this.linkedId});
  595. return card.members;
  596. } else if (this.isLinkedBoard()) {
  597. const board = Boards.findOne({_id: this.linkedId});
  598. return board.activeMembers().map((member) => {
  599. return member.userId;
  600. });
  601. } else {
  602. return this.members;
  603. }
  604. },
  605. assignMember(memberId) {
  606. if (this.isLinkedCard()) {
  607. return Cards.update(
  608. { _id: this.linkedId },
  609. { $addToSet: { members: memberId }}
  610. );
  611. } else if (this.isLinkedBoard()) {
  612. const board = Boards.findOne({_id: this.linkedId});
  613. return board.addMember(memberId);
  614. } else {
  615. return Cards.update(
  616. { _id: this._id },
  617. { $addToSet: { members: memberId}}
  618. );
  619. }
  620. },
  621. unassignMember(memberId) {
  622. if (this.isLinkedCard()) {
  623. return Cards.update(
  624. { _id: this.linkedId },
  625. { $pull: { members: memberId }}
  626. );
  627. } else if (this.isLinkedBoard()) {
  628. const board = Boards.findOne({_id: this.linkedId});
  629. return board.removeMember(memberId);
  630. } else {
  631. return Cards.update(
  632. { _id: this._id },
  633. { $pull: { members: memberId}}
  634. );
  635. }
  636. },
  637. toggleMember(memberId) {
  638. if (this.getMembers() && this.getMembers().indexOf(memberId) > -1) {
  639. return this.unassignMember(memberId);
  640. } else {
  641. return this.assignMember(memberId);
  642. }
  643. },
  644. getReceived() {
  645. if (this.isLinkedCard()) {
  646. const card = Cards.findOne({_id: this.linkedId});
  647. return card.receivedAt;
  648. } else {
  649. return this.receivedAt;
  650. }
  651. },
  652. setReceived(receivedAt) {
  653. if (this.isLinkedCard()) {
  654. return Cards.update(
  655. {_id: this.linkedId},
  656. {$set: {receivedAt}}
  657. );
  658. } else {
  659. return Cards.update(
  660. {_id: this._id},
  661. {$set: {receivedAt}}
  662. );
  663. }
  664. },
  665. getStart() {
  666. if (this.isLinkedCard()) {
  667. const card = Cards.findOne({_id: this.linkedId});
  668. return card.startAt;
  669. } else if (this.isLinkedBoard()) {
  670. const board = Boards.findOne({_id: this.linkedId});
  671. return board.startAt;
  672. } else {
  673. return this.startAt;
  674. }
  675. },
  676. setStart(startAt) {
  677. if (this.isLinkedCard()) {
  678. return Cards.update(
  679. { _id: this.linkedId },
  680. {$set: {startAt}}
  681. );
  682. } else if (this.isLinkedBoard()) {
  683. return Boards.update(
  684. {_id: this.linkedId},
  685. {$set: {startAt}}
  686. );
  687. } else {
  688. return Cards.update(
  689. {_id: this._id},
  690. {$set: {startAt}}
  691. );
  692. }
  693. },
  694. getDue() {
  695. if (this.isLinkedCard()) {
  696. const card = Cards.findOne({_id: this.linkedId});
  697. return card.dueAt;
  698. } else if (this.isLinkedBoard()) {
  699. const board = Boards.findOne({_id: this.linkedId});
  700. return board.dueAt;
  701. } else {
  702. return this.dueAt;
  703. }
  704. },
  705. setDue(dueAt) {
  706. if (this.isLinkedCard()) {
  707. return Cards.update(
  708. { _id: this.linkedId },
  709. {$set: {dueAt}}
  710. );
  711. } else if (this.isLinkedBoard()) {
  712. return Boards.update(
  713. {_id: this.linkedId},
  714. {$set: {dueAt}}
  715. );
  716. } else {
  717. return Cards.update(
  718. {_id: this._id},
  719. {$set: {dueAt}}
  720. );
  721. }
  722. },
  723. getEnd() {
  724. if (this.isLinkedCard()) {
  725. const card = Cards.findOne({_id: this.linkedId});
  726. return card.endAt;
  727. } else if (this.isLinkedBoard()) {
  728. const board = Boards.findOne({_id: this.linkedId});
  729. return board.endAt;
  730. } else {
  731. return this.endAt;
  732. }
  733. },
  734. setEnd(endAt) {
  735. if (this.isLinkedCard()) {
  736. return Cards.update(
  737. { _id: this.linkedId },
  738. {$set: {endAt}}
  739. );
  740. } else if (this.isLinkedBoard()) {
  741. return Boards.update(
  742. {_id: this.linkedId},
  743. {$set: {endAt}}
  744. );
  745. } else {
  746. return Cards.update(
  747. {_id: this._id},
  748. {$set: {endAt}}
  749. );
  750. }
  751. },
  752. getIsOvertime() {
  753. if (this.isLinkedCard()) {
  754. const card = Cards.findOne({ _id: this.linkedId });
  755. return card.isOvertime;
  756. } else if (this.isLinkedBoard()) {
  757. const board = Boards.findOne({ _id: this.linkedId});
  758. return board.isOvertime;
  759. } else {
  760. return this.isOvertime;
  761. }
  762. },
  763. setIsOvertime(isOvertime) {
  764. if (this.isLinkedCard()) {
  765. return Cards.update(
  766. { _id: this.linkedId },
  767. {$set: {isOvertime}}
  768. );
  769. } else if (this.isLinkedBoard()) {
  770. return Boards.update(
  771. {_id: this.linkedId},
  772. {$set: {isOvertime}}
  773. );
  774. } else {
  775. return Cards.update(
  776. {_id: this._id},
  777. {$set: {isOvertime}}
  778. );
  779. }
  780. },
  781. getSpentTime() {
  782. if (this.isLinkedCard()) {
  783. const card = Cards.findOne({ _id: this.linkedId });
  784. return card.spentTime;
  785. } else if (this.isLinkedBoard()) {
  786. const board = Boards.findOne({ _id: this.linkedId});
  787. return board.spentTime;
  788. } else {
  789. return this.spentTime;
  790. }
  791. },
  792. setSpentTime(spentTime) {
  793. if (this.isLinkedCard()) {
  794. return Cards.update(
  795. { _id: this.linkedId },
  796. {$set: {spentTime}}
  797. );
  798. } else if (this.isLinkedBoard()) {
  799. return Boards.update(
  800. {_id: this.linkedId},
  801. {$set: {spentTime}}
  802. );
  803. } else {
  804. return Cards.update(
  805. {_id: this._id},
  806. {$set: {spentTime}}
  807. );
  808. }
  809. },
  810. getId() {
  811. if (this.isLinked()) {
  812. return this.linkedId;
  813. } else {
  814. return this._id;
  815. }
  816. },
  817. getTitle() {
  818. if (this.isLinkedCard()) {
  819. const card = Cards.findOne({ _id: this.linkedId });
  820. return card.title;
  821. } else if (this.isLinkedBoard()) {
  822. const board = Boards.findOne({ _id: this.linkedId});
  823. return board.title;
  824. } else {
  825. return this.title;
  826. }
  827. },
  828. getBoardTitle() {
  829. if (this.isLinkedCard()) {
  830. const card = Cards.findOne({ _id: this.linkedId });
  831. const board = Boards.findOne({ _id: card.boardId });
  832. return board.title;
  833. } else if (this.isLinkedBoard()) {
  834. const board = Boards.findOne({ _id: this.linkedId});
  835. return board.title;
  836. } else {
  837. const board = Boards.findOne({ _id: this.boardId });
  838. return board.title;
  839. }
  840. },
  841. setTitle(title) {
  842. if (this.isLinkedCard()) {
  843. return Cards.update(
  844. { _id: this.linkedId },
  845. {$set: {title}}
  846. );
  847. } else if (this.isLinkedBoard()) {
  848. return Boards.update(
  849. {_id: this.linkedId},
  850. {$set: {title}}
  851. );
  852. } else {
  853. return Cards.update(
  854. {_id: this._id},
  855. {$set: {title}}
  856. );
  857. }
  858. },
  859. getArchived() {
  860. if (this.isLinkedCard()) {
  861. const card = Cards.findOne({ _id: this.linkedId });
  862. return card.archived;
  863. } else if (this.isLinkedBoard()) {
  864. const board = Boards.findOne({ _id: this.linkedId});
  865. return board.archived;
  866. } else {
  867. return this.archived;
  868. }
  869. },
  870. setRequestedBy(requestedBy) {
  871. if (this.isLinkedCard()) {
  872. return Cards.update(
  873. { _id: this.linkedId },
  874. {$set: {requestedBy}}
  875. );
  876. } else {
  877. return Cards.update(
  878. {_id: this._id},
  879. {$set: {requestedBy}}
  880. );
  881. }
  882. },
  883. getRequestedBy() {
  884. if (this.isLinkedCard()) {
  885. const card = Cards.findOne({ _id: this.linkedId });
  886. return card.requestedBy;
  887. } else {
  888. return this.requestedBy;
  889. }
  890. },
  891. setAssignedBy(assignedBy) {
  892. if (this.isLinkedCard()) {
  893. return Cards.update(
  894. { _id: this.linkedId },
  895. {$set: {assignedBy}}
  896. );
  897. } else {
  898. return Cards.update(
  899. {_id: this._id},
  900. {$set: {assignedBy}}
  901. );
  902. }
  903. },
  904. getAssignedBy() {
  905. if (this.isLinkedCard()) {
  906. const card = Cards.findOne({ _id: this.linkedId });
  907. return card.assignedBy;
  908. } else {
  909. return this.assignedBy;
  910. }
  911. },
  912. isTemplateCard() {
  913. return this.type === 'template-card';
  914. },
  915. });
  916. Cards.mutations({
  917. applyToChildren(funct) {
  918. Cards.find({
  919. parentId: this._id,
  920. }).forEach((card) => {
  921. funct(card);
  922. });
  923. },
  924. archive() {
  925. this.applyToChildren((card) => {
  926. return card.archive();
  927. });
  928. return {
  929. $set: {
  930. archived: true,
  931. },
  932. };
  933. },
  934. restore() {
  935. this.applyToChildren((card) => {
  936. return card.restore();
  937. });
  938. return {
  939. $set: {
  940. archived: false,
  941. },
  942. };
  943. },
  944. setTitle(title) {
  945. return {
  946. $set: {
  947. title,
  948. },
  949. };
  950. },
  951. setDescription(description) {
  952. return {
  953. $set: {
  954. description,
  955. },
  956. };
  957. },
  958. setRequestedBy(requestedBy) {
  959. return {
  960. $set: {
  961. requestedBy,
  962. },
  963. };
  964. },
  965. setAssignedBy(assignedBy) {
  966. return {
  967. $set: {
  968. assignedBy,
  969. },
  970. };
  971. },
  972. move(swimlaneId, listId, sortIndex) {
  973. const list = Lists.findOne(listId);
  974. const mutatedFields = {
  975. swimlaneId,
  976. listId,
  977. boardId: list.boardId,
  978. sort: sortIndex,
  979. };
  980. return {
  981. $set: mutatedFields,
  982. };
  983. },
  984. addLabel(labelId) {
  985. return {
  986. $addToSet: {
  987. labelIds: labelId,
  988. },
  989. };
  990. },
  991. removeLabel(labelId) {
  992. return {
  993. $pull: {
  994. labelIds: labelId,
  995. },
  996. };
  997. },
  998. toggleLabel(labelId) {
  999. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  1000. return this.removeLabel(labelId);
  1001. } else {
  1002. return this.addLabel(labelId);
  1003. }
  1004. },
  1005. setColor(newColor) {
  1006. if (newColor === 'white') {
  1007. newColor = null;
  1008. }
  1009. return {
  1010. $set: {
  1011. color: newColor,
  1012. },
  1013. };
  1014. },
  1015. assignMember(memberId) {
  1016. return {
  1017. $addToSet: {
  1018. members: memberId,
  1019. },
  1020. };
  1021. },
  1022. unassignMember(memberId) {
  1023. return {
  1024. $pull: {
  1025. members: memberId,
  1026. },
  1027. };
  1028. },
  1029. toggleMember(memberId) {
  1030. if (this.members && this.members.indexOf(memberId) > -1) {
  1031. return this.unassignMember(memberId);
  1032. } else {
  1033. return this.assignMember(memberId);
  1034. }
  1035. },
  1036. assignCustomField(customFieldId) {
  1037. return {
  1038. $addToSet: {
  1039. customFields: {
  1040. _id: customFieldId,
  1041. value: null,
  1042. },
  1043. },
  1044. };
  1045. },
  1046. unassignCustomField(customFieldId) {
  1047. return {
  1048. $pull: {
  1049. customFields: {
  1050. _id: customFieldId,
  1051. },
  1052. },
  1053. };
  1054. },
  1055. toggleCustomField(customFieldId) {
  1056. if (this.customFields && this.customFieldIndex(customFieldId) > -1) {
  1057. return this.unassignCustomField(customFieldId);
  1058. } else {
  1059. return this.assignCustomField(customFieldId);
  1060. }
  1061. },
  1062. setCustomField(customFieldId, value) {
  1063. // todo
  1064. const index = this.customFieldIndex(customFieldId);
  1065. if (index > -1) {
  1066. const update = {
  1067. $set: {},
  1068. };
  1069. update.$set[`customFields.${index}.value`] = value;
  1070. return update;
  1071. }
  1072. // TODO
  1073. // Ignatz 18.05.2018: Return null to silence ESLint. No Idea if that is correct
  1074. return null;
  1075. },
  1076. setCover(coverId) {
  1077. return {
  1078. $set: {
  1079. coverId,
  1080. },
  1081. };
  1082. },
  1083. unsetCover() {
  1084. return {
  1085. $unset: {
  1086. coverId: '',
  1087. },
  1088. };
  1089. },
  1090. setReceived(receivedAt) {
  1091. return {
  1092. $set: {
  1093. receivedAt,
  1094. },
  1095. };
  1096. },
  1097. unsetReceived() {
  1098. return {
  1099. $unset: {
  1100. receivedAt: '',
  1101. },
  1102. };
  1103. },
  1104. setStart(startAt) {
  1105. return {
  1106. $set: {
  1107. startAt,
  1108. },
  1109. };
  1110. },
  1111. unsetStart() {
  1112. return {
  1113. $unset: {
  1114. startAt: '',
  1115. },
  1116. };
  1117. },
  1118. setDue(dueAt) {
  1119. return {
  1120. $set: {
  1121. dueAt,
  1122. },
  1123. };
  1124. },
  1125. unsetDue() {
  1126. return {
  1127. $unset: {
  1128. dueAt: '',
  1129. },
  1130. };
  1131. },
  1132. setEnd(endAt) {
  1133. return {
  1134. $set: {
  1135. endAt,
  1136. },
  1137. };
  1138. },
  1139. unsetEnd() {
  1140. return {
  1141. $unset: {
  1142. endAt: '',
  1143. },
  1144. };
  1145. },
  1146. setOvertime(isOvertime) {
  1147. return {
  1148. $set: {
  1149. isOvertime,
  1150. },
  1151. };
  1152. },
  1153. setSpentTime(spentTime) {
  1154. return {
  1155. $set: {
  1156. spentTime,
  1157. },
  1158. };
  1159. },
  1160. unsetSpentTime() {
  1161. return {
  1162. $unset: {
  1163. spentTime: '',
  1164. isOvertime: false,
  1165. },
  1166. };
  1167. },
  1168. setParentId(parentId) {
  1169. return {
  1170. $set: {
  1171. parentId,
  1172. },
  1173. };
  1174. },
  1175. });
  1176. //FUNCTIONS FOR creation of Activities
  1177. function cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId) {
  1178. if ((_.contains(fieldNames, 'listId') && doc.listId !== oldListId) ||
  1179. (_.contains(fieldNames, 'swimlaneId') && doc.swimlaneId !== oldSwimlaneId)){
  1180. Activities.insert({
  1181. userId,
  1182. oldListId,
  1183. activityType: 'moveCard',
  1184. listName: Lists.findOne(doc.listId).title,
  1185. listId: doc.listId,
  1186. boardId: doc.boardId,
  1187. cardId: doc._id,
  1188. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1189. swimlaneId: doc.swimlaneId,
  1190. oldSwimlaneId,
  1191. });
  1192. }
  1193. }
  1194. function cardState(userId, doc, fieldNames) {
  1195. if (_.contains(fieldNames, 'archived')) {
  1196. if (doc.archived) {
  1197. Activities.insert({
  1198. userId,
  1199. activityType: 'archivedCard',
  1200. listName: Lists.findOne(doc.listId).title,
  1201. boardId: doc.boardId,
  1202. listId: doc.listId,
  1203. cardId: doc._id,
  1204. });
  1205. } else {
  1206. Activities.insert({
  1207. userId,
  1208. activityType: 'restoredCard',
  1209. boardId: doc.boardId,
  1210. listName: Lists.findOne(doc.listId).title,
  1211. listId: doc.listId,
  1212. cardId: doc._id,
  1213. });
  1214. }
  1215. }
  1216. }
  1217. function cardMembers(userId, doc, fieldNames, modifier) {
  1218. if (!_.contains(fieldNames, 'members'))
  1219. return;
  1220. let memberId;
  1221. // Say hello to the new member
  1222. if (modifier.$addToSet && modifier.$addToSet.members) {
  1223. memberId = modifier.$addToSet.members;
  1224. const username = Users.findOne(memberId).username;
  1225. if (!_.contains(doc.members, memberId)) {
  1226. Activities.insert({
  1227. userId,
  1228. username,
  1229. activityType: 'joinMember',
  1230. boardId: doc.boardId,
  1231. cardId: doc._id,
  1232. });
  1233. }
  1234. }
  1235. // Say goodbye to the former member
  1236. if (modifier.$pull && modifier.$pull.members) {
  1237. memberId = modifier.$pull.members;
  1238. const username = Users.findOne(memberId).username;
  1239. // Check that the former member is member of the card
  1240. if (_.contains(doc.members, memberId)) {
  1241. Activities.insert({
  1242. userId,
  1243. username,
  1244. activityType: 'unjoinMember',
  1245. boardId: doc.boardId,
  1246. cardId: doc._id,
  1247. });
  1248. }
  1249. }
  1250. }
  1251. function cardLabels(userId, doc, fieldNames, modifier) {
  1252. if (!_.contains(fieldNames, 'labelIds'))
  1253. return;
  1254. let labelId;
  1255. // Say hello to the new label
  1256. if (modifier.$addToSet && modifier.$addToSet.labelIds) {
  1257. labelId = modifier.$addToSet.labelIds;
  1258. if (!_.contains(doc.labelIds, labelId)) {
  1259. const act = {
  1260. userId,
  1261. labelId,
  1262. activityType: 'addedLabel',
  1263. boardId: doc.boardId,
  1264. cardId: doc._id,
  1265. };
  1266. Activities.insert(act);
  1267. }
  1268. }
  1269. // Say goodbye to the label
  1270. if (modifier.$pull && modifier.$pull.labelIds) {
  1271. labelId = modifier.$pull.labelIds;
  1272. // Check that the former member is member of the card
  1273. if (_.contains(doc.labelIds, labelId)) {
  1274. Activities.insert({
  1275. userId,
  1276. labelId,
  1277. activityType: 'removedLabel',
  1278. boardId: doc.boardId,
  1279. cardId: doc._id,
  1280. });
  1281. }
  1282. }
  1283. }
  1284. function cardCustomFields(userId, doc, fieldNames, modifier) {
  1285. if (!_.contains(fieldNames, 'customFields'))
  1286. return;
  1287. // Say hello to the new customField value
  1288. if (modifier.$set) {
  1289. _.each(modifier.$set, (value, key) => {
  1290. if (key.startsWith('customFields')) {
  1291. const dotNotation = key.split('.');
  1292. // only individual changes are registered
  1293. if (dotNotation.length > 1) {
  1294. const customFieldId = doc.customFields[dot_notation[1]]._id;
  1295. const act = {
  1296. userId,
  1297. customFieldId,
  1298. value,
  1299. activityType: 'setCustomField',
  1300. boardId: doc.boardId,
  1301. cardId: doc._id,
  1302. };
  1303. Activities.insert(act);
  1304. }
  1305. }
  1306. });
  1307. }
  1308. // Say goodbye to the former customField value
  1309. if (modifier.$unset) {
  1310. _.each(modifier.$unset, (value, key) => {
  1311. if (key.startsWith('customFields')) {
  1312. const dotNotation = key.split('.');
  1313. // only individual changes are registered
  1314. if (dotNotation.length > 1) {
  1315. const customFieldId = doc.customFields[dot_notation[1]]._id;
  1316. const act = {
  1317. userId,
  1318. customFieldId,
  1319. activityType: 'unsetCustomField',
  1320. boardId: doc.boardId,
  1321. cardId: doc._id,
  1322. };
  1323. Activities.insert(act);
  1324. }
  1325. }
  1326. });
  1327. }
  1328. }
  1329. function cardCreation(userId, doc) {
  1330. Activities.insert({
  1331. userId,
  1332. activityType: 'createCard',
  1333. boardId: doc.boardId,
  1334. listName: Lists.findOne(doc.listId).title,
  1335. listId: doc.listId,
  1336. cardId: doc._id,
  1337. cardTitle:doc.title,
  1338. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1339. swimlaneId: doc.swimlaneId,
  1340. });
  1341. }
  1342. function cardRemover(userId, doc) {
  1343. Activities.remove({
  1344. cardId: doc._id,
  1345. });
  1346. Checklists.remove({
  1347. cardId: doc._id,
  1348. });
  1349. Cards.remove({
  1350. parentId: doc._id,
  1351. });
  1352. CardComments.remove({
  1353. cardId: doc._id,
  1354. });
  1355. Attachments.remove({
  1356. cardId: doc._id,
  1357. });
  1358. }
  1359. if (Meteor.isServer) {
  1360. // Cards are often fetched within a board, so we create an index to make these
  1361. // queries more efficient.
  1362. Meteor.startup(() => {
  1363. Cards._collection._ensureIndex({boardId: 1, createdAt: -1});
  1364. // https://github.com/wekan/wekan/issues/1863
  1365. // Swimlane added a new field in the cards collection of mongodb named parentId.
  1366. // When loading a board, mongodb is searching for every cards, the id of the parent (in the swinglanes collection).
  1367. // With a huge database, this result in a very slow app and high CPU on the mongodb side.
  1368. // To correct it, add Index to parentId:
  1369. Cards._collection._ensureIndex({parentId: 1});
  1370. });
  1371. Cards.after.insert((userId, doc) => {
  1372. cardCreation(userId, doc);
  1373. });
  1374. // New activity for card (un)archivage
  1375. Cards.after.update((userId, doc, fieldNames) => {
  1376. cardState(userId, doc, fieldNames);
  1377. });
  1378. //New activity for card moves
  1379. Cards.after.update(function(userId, doc, fieldNames) {
  1380. const oldListId = this.previous.listId;
  1381. const oldSwimlaneId = this.previous.swimlaneId;
  1382. cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId);
  1383. });
  1384. // Add a new activity if we add or remove a member to the card
  1385. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1386. cardMembers(userId, doc, fieldNames, modifier);
  1387. });
  1388. // Add a new activity if we add or remove a label to the card
  1389. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1390. cardLabels(userId, doc, fieldNames, modifier);
  1391. });
  1392. // Add a new activity if we edit a custom field
  1393. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1394. cardCustomFields(userId, doc, fieldNames, modifier);
  1395. });
  1396. // Remove all activities associated with a card if we remove the card
  1397. // Remove also card_comments / checklists / attachments
  1398. Cards.after.remove((userId, doc) => {
  1399. cardRemover(userId, doc);
  1400. });
  1401. }
  1402. //SWIMLANES REST API
  1403. if (Meteor.isServer) {
  1404. /**
  1405. * @operation get_swimlane_cards
  1406. * @summary get all cards attached to a swimlane
  1407. *
  1408. * @param {string} boardId the board ID
  1409. * @param {string} swimlaneId the swimlane ID
  1410. * @return_type [{_id: string,
  1411. * title: string,
  1412. * description: string,
  1413. * listId: string}]
  1414. */
  1415. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes/:swimlaneId/cards', function(req, res) {
  1416. const paramBoardId = req.params.boardId;
  1417. const paramSwimlaneId = req.params.swimlaneId;
  1418. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1419. JsonRoutes.sendResult(res, {
  1420. code: 200,
  1421. data: Cards.find({
  1422. boardId: paramBoardId,
  1423. swimlaneId: paramSwimlaneId,
  1424. archived: false,
  1425. }).map(function(doc) {
  1426. return {
  1427. _id: doc._id,
  1428. title: doc.title,
  1429. description: doc.description,
  1430. listId: doc.listId,
  1431. };
  1432. }),
  1433. });
  1434. });
  1435. }
  1436. //LISTS REST API
  1437. if (Meteor.isServer) {
  1438. /**
  1439. * @operation get_all_cards
  1440. * @summary Get all Cards attached to a List
  1441. *
  1442. * @param {string} boardId the board ID
  1443. * @param {string} listId the list ID
  1444. * @return_type [{_id: string,
  1445. * title: string,
  1446. * description: string}]
  1447. */
  1448. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function(req, res) {
  1449. const paramBoardId = req.params.boardId;
  1450. const paramListId = req.params.listId;
  1451. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1452. JsonRoutes.sendResult(res, {
  1453. code: 200,
  1454. data: Cards.find({
  1455. boardId: paramBoardId,
  1456. listId: paramListId,
  1457. archived: false,
  1458. }).map(function(doc) {
  1459. return {
  1460. _id: doc._id,
  1461. title: doc.title,
  1462. description: doc.description,
  1463. };
  1464. }),
  1465. });
  1466. });
  1467. /**
  1468. * @operation get_card
  1469. * @summary Get a Card
  1470. *
  1471. * @param {string} boardId the board ID
  1472. * @param {string} listId the list ID of the card
  1473. * @param {string} cardId the card ID
  1474. * @return_type Cards
  1475. */
  1476. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards/:cardId', function(req, res) {
  1477. const paramBoardId = req.params.boardId;
  1478. const paramListId = req.params.listId;
  1479. const paramCardId = req.params.cardId;
  1480. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1481. JsonRoutes.sendResult(res, {
  1482. code: 200,
  1483. data: Cards.findOne({
  1484. _id: paramCardId,
  1485. listId: paramListId,
  1486. boardId: paramBoardId,
  1487. archived: false,
  1488. }),
  1489. });
  1490. });
  1491. /**
  1492. * @operation new_card
  1493. * @summary Create a new Card
  1494. *
  1495. * @param {string} boardId the board ID of the new card
  1496. * @param {string} listId the list ID of the new card
  1497. * @param {string} authorID the user ID of the person owning the card
  1498. * @param {string} title the title of the new card
  1499. * @param {string} description the description of the new card
  1500. * @param {string} swimlaneId the swimlane ID of the new card
  1501. * @param {string} [members] the member IDs list of the new card
  1502. * @return_type {_id: string}
  1503. */
  1504. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function(req, res) {
  1505. Authentication.checkUserId(req.userId);
  1506. const paramBoardId = req.params.boardId;
  1507. const paramListId = req.params.listId;
  1508. const currentCards = Cards.find({
  1509. listId: paramListId,
  1510. archived: false,
  1511. }, { sort: ['sort'] });
  1512. const check = Users.findOne({
  1513. _id: req.body.authorId,
  1514. });
  1515. const members = req.body.members || [req.body.authorId];
  1516. if (typeof check !== 'undefined') {
  1517. const id = Cards.direct.insert({
  1518. title: req.body.title,
  1519. boardId: paramBoardId,
  1520. listId: paramListId,
  1521. description: req.body.description,
  1522. userId: req.body.authorId,
  1523. swimlaneId: req.body.swimlaneId,
  1524. sort: currentCards.count(),
  1525. members,
  1526. });
  1527. JsonRoutes.sendResult(res, {
  1528. code: 200,
  1529. data: {
  1530. _id: id,
  1531. },
  1532. });
  1533. const card = Cards.findOne({
  1534. _id: id,
  1535. });
  1536. cardCreation(req.body.authorId, card);
  1537. } else {
  1538. JsonRoutes.sendResult(res, {
  1539. code: 401,
  1540. });
  1541. }
  1542. });
  1543. /*
  1544. * Note for the JSDoc:
  1545. * 'list' will be interpreted as the path parameter
  1546. * 'listID' will be interpreted as the body parameter
  1547. */
  1548. /**
  1549. * @operation edit_card
  1550. * @summary Edit Fields in a Card
  1551. *
  1552. * @description Edit a card
  1553. *
  1554. * The color has to be chosen between `white`, `green`, `yellow`, `orange`,
  1555. * `red`, `purple`, `blue`, `sky`, `lime`, `pink`, `black`, `silver`,
  1556. * `peachpuff`, `crimson`, `plum`, `darkgreen`, `slateblue`, `magenta`,
  1557. * `gold`, `navy`, `gray`, `saddlebrown`, `paleturquoise`, `mistyrose`,
  1558. * `indigo`:
  1559. *
  1560. * <img src="/card-colors.png" width="40%" alt="Wekan card colors" />
  1561. *
  1562. * Note: setting the color to white has the same effect than removing it.
  1563. *
  1564. * @param {string} boardId the board ID of the card
  1565. * @param {string} list the list ID of the card
  1566. * @param {string} cardId the ID of the card
  1567. * @param {string} [title] the new title of the card
  1568. * @param {string} [listId] the new list ID of the card (move operation)
  1569. * @param {string} [description] the new description of the card
  1570. * @param {string} [authorId] change the owner of the card
  1571. * @param {string} [labelIds] the new list of label IDs attached to the card
  1572. * @param {string} [swimlaneId] the new swimlane ID of the card
  1573. * @param {string} [members] the new list of member IDs attached to the card
  1574. * @param {string} [requestedBy] the new requestedBy field of the card
  1575. * @param {string} [assignedBy] the new assignedBy field of the card
  1576. * @param {string} [receivedAt] the new receivedAt field of the card
  1577. * @param {string} [assignBy] the new assignBy field of the card
  1578. * @param {string} [startAt] the new startAt field of the card
  1579. * @param {string} [dueAt] the new dueAt field of the card
  1580. * @param {string} [endAt] the new endAt field of the card
  1581. * @param {string} [spentTime] the new spentTime field of the card
  1582. * @param {boolean} [isOverTime] the new isOverTime field of the card
  1583. * @param {string} [customFields] the new customFields value of the card
  1584. * @param {string} [color] the new color of the card
  1585. * @return_type {_id: string}
  1586. */
  1587. JsonRoutes.add('PUT', '/api/boards/:boardId/lists/:listId/cards/:cardId', function(req, res) {
  1588. Authentication.checkUserId(req.userId);
  1589. const paramBoardId = req.params.boardId;
  1590. const paramCardId = req.params.cardId;
  1591. const paramListId = req.params.listId;
  1592. if (req.body.hasOwnProperty('title')) {
  1593. const newTitle = req.body.title;
  1594. Cards.direct.update({
  1595. _id: paramCardId,
  1596. listId: paramListId,
  1597. boardId: paramBoardId,
  1598. archived: false,
  1599. }, {
  1600. $set: {
  1601. title: newTitle,
  1602. },
  1603. });
  1604. }
  1605. if (req.body.hasOwnProperty('listId')) {
  1606. const newParamListId = req.body.listId;
  1607. Cards.direct.update({
  1608. _id: paramCardId,
  1609. listId: paramListId,
  1610. boardId: paramBoardId,
  1611. archived: false,
  1612. }, {
  1613. $set: {
  1614. listId: newParamListId,
  1615. },
  1616. });
  1617. const card = Cards.findOne({
  1618. _id: paramCardId,
  1619. });
  1620. cardMove(req.body.authorId, card, {
  1621. fieldName: 'listId',
  1622. }, paramListId);
  1623. }
  1624. if (req.body.hasOwnProperty('description')) {
  1625. const newDescription = req.body.description;
  1626. Cards.direct.update({
  1627. _id: paramCardId,
  1628. listId: paramListId,
  1629. boardId: paramBoardId,
  1630. archived: false,
  1631. }, {
  1632. $set: {
  1633. description: newDescription,
  1634. },
  1635. });
  1636. }
  1637. if (req.body.hasOwnProperty('color')) {
  1638. const newColor = req.body.color;
  1639. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1640. {$set: {color: newColor}});
  1641. }
  1642. if (req.body.hasOwnProperty('labelIds')) {
  1643. let newlabelIds = req.body.labelIds;
  1644. if (_.isString(newlabelIds)) {
  1645. if (newlabelIds === '') {
  1646. newlabelIds = null;
  1647. }
  1648. else {
  1649. newlabelIds = [newlabelIds];
  1650. }
  1651. }
  1652. Cards.direct.update({
  1653. _id: paramCardId,
  1654. listId: paramListId,
  1655. boardId: paramBoardId,
  1656. archived: false,
  1657. }, {
  1658. $set: {
  1659. labelIds: newlabelIds,
  1660. },
  1661. });
  1662. }
  1663. if (req.body.hasOwnProperty('requestedBy')) {
  1664. const newrequestedBy = req.body.requestedBy;
  1665. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1666. {$set: {requestedBy: newrequestedBy}});
  1667. }
  1668. if (req.body.hasOwnProperty('assignedBy')) {
  1669. const newassignedBy = req.body.assignedBy;
  1670. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1671. {$set: {assignedBy: newassignedBy}});
  1672. }
  1673. if (req.body.hasOwnProperty('receivedAt')) {
  1674. const newreceivedAt = req.body.receivedAt;
  1675. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1676. {$set: {receivedAt: newreceivedAt}});
  1677. }
  1678. if (req.body.hasOwnProperty('startAt')) {
  1679. const newstartAt = req.body.startAt;
  1680. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1681. {$set: {startAt: newstartAt}});
  1682. }
  1683. if (req.body.hasOwnProperty('dueAt')) {
  1684. const newdueAt = req.body.dueAt;
  1685. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1686. {$set: {dueAt: newdueAt}});
  1687. }
  1688. if (req.body.hasOwnProperty('endAt')) {
  1689. const newendAt = req.body.endAt;
  1690. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1691. {$set: {endAt: newendAt}});
  1692. }
  1693. if (req.body.hasOwnProperty('spentTime')) {
  1694. const newspentTime = req.body.spentTime;
  1695. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1696. {$set: {spentTime: newspentTime}});
  1697. }
  1698. if (req.body.hasOwnProperty('isOverTime')) {
  1699. const newisOverTime = req.body.isOverTime;
  1700. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1701. {$set: {isOverTime: newisOverTime}});
  1702. }
  1703. if (req.body.hasOwnProperty('customFields')) {
  1704. const newcustomFields = req.body.customFields;
  1705. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1706. {$set: {customFields: newcustomFields}});
  1707. }
  1708. if (req.body.hasOwnProperty('members')) {
  1709. let newmembers = req.body.members;
  1710. if (_.isString(newmembers)) {
  1711. if (newmembers === '') {
  1712. newmembers = null;
  1713. }
  1714. else {
  1715. newmembers = [newmembers];
  1716. }
  1717. }
  1718. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1719. {$set: {members: newmembers}});
  1720. }
  1721. if (req.body.hasOwnProperty('swimlaneId')) {
  1722. const newParamSwimlaneId = req.body.swimlaneId;
  1723. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1724. {$set: {swimlaneId: newParamSwimlaneId}});
  1725. }
  1726. JsonRoutes.sendResult(res, {
  1727. code: 200,
  1728. data: {
  1729. _id: paramCardId,
  1730. },
  1731. });
  1732. });
  1733. /**
  1734. * @operation delete_card
  1735. * @summary Delete a card from a board
  1736. *
  1737. * @description This operation **deletes** a card, and therefore the card
  1738. * is not put in the recycle bin.
  1739. *
  1740. * @param {string} boardId the board ID of the card
  1741. * @param {string} list the list ID of the card
  1742. * @param {string} cardId the ID of the card
  1743. * @return_type {_id: string}
  1744. */
  1745. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId/cards/:cardId', function(req, res) {
  1746. Authentication.checkUserId(req.userId);
  1747. const paramBoardId = req.params.boardId;
  1748. const paramListId = req.params.listId;
  1749. const paramCardId = req.params.cardId;
  1750. Cards.direct.remove({
  1751. _id: paramCardId,
  1752. listId: paramListId,
  1753. boardId: paramBoardId,
  1754. });
  1755. const card = Cards.find({
  1756. _id: paramCardId,
  1757. });
  1758. cardRemover(req.body.authorId, card);
  1759. JsonRoutes.sendResult(res, {
  1760. code: 200,
  1761. data: {
  1762. _id: paramCardId,
  1763. },
  1764. });
  1765. });
  1766. }