cards.js 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996
  1. import {
  2. ALLOWED_COLORS,
  3. TYPE_CARD,
  4. TYPE_LINKED_BOARD,
  5. TYPE_LINKED_CARD,
  6. } from '../config/const';
  7. Cards = new Mongo.Collection('cards');
  8. // XXX To improve pub/sub performances a card document should include a
  9. // de-normalized number of comments so we don't have to publish the whole list
  10. // of comments just to display the number of them in the board view.
  11. Cards.attachSchema(
  12. new SimpleSchema({
  13. title: {
  14. /**
  15. * the title of the card
  16. */
  17. type: String,
  18. optional: true,
  19. defaultValue: '',
  20. },
  21. archived: {
  22. /**
  23. * is the card archived
  24. */
  25. type: Boolean,
  26. // eslint-disable-next-line consistent-return
  27. autoValue() {
  28. // eslint-disable-line consistent-return
  29. if (this.isInsert && !this.isSet) {
  30. return false;
  31. }
  32. },
  33. },
  34. archivedAt: {
  35. /**
  36. * latest archiving date
  37. */
  38. type: Date,
  39. optional: true,
  40. },
  41. parentId: {
  42. /**
  43. * ID of the parent card
  44. */
  45. type: String,
  46. optional: true,
  47. defaultValue: '',
  48. },
  49. listId: {
  50. /**
  51. * List ID where the card is
  52. */
  53. type: String,
  54. optional: true,
  55. defaultValue: '',
  56. },
  57. swimlaneId: {
  58. /**
  59. * Swimlane ID where the card is
  60. */
  61. type: String,
  62. },
  63. // The system could work without this `boardId` information (we could deduce
  64. // the board identifier from the card), but it would make the system more
  65. // difficult to manage and less efficient.
  66. boardId: {
  67. /**
  68. * Board ID of the card
  69. */
  70. type: String,
  71. optional: true,
  72. defaultValue: '',
  73. },
  74. coverId: {
  75. /**
  76. * Cover ID of the card
  77. */
  78. type: String,
  79. optional: true,
  80. defaultValue: '',
  81. },
  82. color: {
  83. type: String,
  84. optional: true,
  85. allowedValues: ALLOWED_COLORS,
  86. },
  87. createdAt: {
  88. /**
  89. * creation date
  90. */
  91. type: Date,
  92. // eslint-disable-next-line consistent-return
  93. autoValue() {
  94. if (this.isInsert) {
  95. return new Date();
  96. } else if (this.isUpsert) {
  97. return { $setOnInsert: new Date() };
  98. } else {
  99. this.unset();
  100. }
  101. },
  102. },
  103. modifiedAt: {
  104. type: Date,
  105. denyUpdate: false,
  106. // eslint-disable-next-line consistent-return
  107. autoValue() {
  108. if (this.isInsert || this.isUpsert || this.isUpdate) {
  109. return new Date();
  110. } else {
  111. this.unset();
  112. }
  113. },
  114. },
  115. customFields: {
  116. /**
  117. * list of custom fields
  118. */
  119. type: [Object],
  120. optional: true,
  121. defaultValue: [],
  122. },
  123. 'customFields.$': {
  124. type: new SimpleSchema({
  125. _id: {
  126. /**
  127. * the ID of the related custom field
  128. */
  129. type: String,
  130. optional: true,
  131. defaultValue: '',
  132. },
  133. value: {
  134. /**
  135. * value attached to the custom field
  136. */
  137. type: Match.OneOf(String, Number, Boolean, Date),
  138. optional: true,
  139. defaultValue: '',
  140. },
  141. }),
  142. },
  143. dateLastActivity: {
  144. /**
  145. * Date of last activity
  146. */
  147. type: Date,
  148. autoValue() {
  149. return new Date();
  150. },
  151. },
  152. description: {
  153. /**
  154. * description of the card
  155. */
  156. type: String,
  157. optional: true,
  158. defaultValue: '',
  159. },
  160. requestedBy: {
  161. /**
  162. * who requested the card (ID of the user)
  163. */
  164. type: String,
  165. optional: true,
  166. defaultValue: '',
  167. },
  168. assignedBy: {
  169. /**
  170. * who assigned the card (ID of the user)
  171. */
  172. type: String,
  173. optional: true,
  174. defaultValue: '',
  175. },
  176. labelIds: {
  177. /**
  178. * list of labels ID the card has
  179. */
  180. type: [String],
  181. optional: true,
  182. defaultValue: [],
  183. },
  184. members: {
  185. /**
  186. * list of members (user IDs)
  187. */
  188. type: [String],
  189. optional: true,
  190. defaultValue: [],
  191. },
  192. assignees: {
  193. /**
  194. * who is assignee of the card (user ID),
  195. * maximum one ID of assignee in array.
  196. */
  197. type: [String],
  198. optional: true,
  199. defaultValue: [],
  200. },
  201. receivedAt: {
  202. /**
  203. * Date the card was received
  204. */
  205. type: Date,
  206. optional: true,
  207. },
  208. startAt: {
  209. /**
  210. * Date the card was started to be worked on
  211. */
  212. type: Date,
  213. optional: true,
  214. },
  215. dueAt: {
  216. /**
  217. * Date the card is due
  218. */
  219. type: Date,
  220. optional: true,
  221. },
  222. endAt: {
  223. /**
  224. * Date the card ended
  225. */
  226. type: Date,
  227. optional: true,
  228. },
  229. spentTime: {
  230. /**
  231. * How much time has been spent on this
  232. */
  233. type: Number,
  234. decimal: true,
  235. optional: true,
  236. defaultValue: 0,
  237. },
  238. isOvertime: {
  239. /**
  240. * is the card over time?
  241. */
  242. type: Boolean,
  243. defaultValue: false,
  244. optional: true,
  245. },
  246. // XXX Should probably be called `authorId`. Is it even needed since we have
  247. // the `members` field?
  248. userId: {
  249. /**
  250. * user ID of the author of the card
  251. */
  252. type: String,
  253. // eslint-disable-next-line consistent-return
  254. autoValue() {
  255. // eslint-disable-line consistent-return
  256. if (this.isInsert && !this.isSet) {
  257. return this.userId;
  258. }
  259. },
  260. },
  261. sort: {
  262. /**
  263. * Sort value
  264. */
  265. type: Number,
  266. decimal: true,
  267. defaultValue: 0,
  268. },
  269. subtaskSort: {
  270. /**
  271. * subtask sort value
  272. */
  273. type: Number,
  274. decimal: true,
  275. defaultValue: -1,
  276. optional: true,
  277. },
  278. type: {
  279. /**
  280. * type of the card
  281. */
  282. type: String,
  283. defaultValue: TYPE_CARD,
  284. allowedValues: [TYPE_CARD, TYPE_LINKED_CARD, TYPE_LINKED_BOARD],
  285. },
  286. linkedId: {
  287. /**
  288. * ID of the linked card
  289. */
  290. type: String,
  291. optional: true,
  292. defaultValue: '',
  293. },
  294. vote: {
  295. /**
  296. * vote object, see below
  297. */
  298. type: Object,
  299. optional: true,
  300. },
  301. 'vote.question': {
  302. type: String,
  303. defaultValue: '',
  304. },
  305. 'vote.positive': {
  306. /**
  307. * list of members (user IDs)
  308. */
  309. type: [String],
  310. optional: true,
  311. defaultValue: [],
  312. },
  313. 'vote.negative': {
  314. /**
  315. * list of members (user IDs)
  316. */
  317. type: [String],
  318. optional: true,
  319. defaultValue: [],
  320. },
  321. 'vote.end': {
  322. type: Date,
  323. optional: true,
  324. defaultValue: null,
  325. },
  326. 'vote.public': {
  327. type: Boolean,
  328. defaultValue: false,
  329. },
  330. 'vote.allowNonBoardMembers': {
  331. type: Boolean,
  332. defaultValue: false,
  333. },
  334. }),
  335. );
  336. Cards.allow({
  337. insert(userId, doc) {
  338. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  339. },
  340. update(userId, doc, fields) {
  341. // Allow board members or logged in users if only vote get's changed
  342. return (
  343. allowIsBoardMember(userId, Boards.findOne(doc.boardId)) ||
  344. (_.isEqual(fields, ['vote', 'modifiedAt', 'dateLastActivity']) &&
  345. !!userId)
  346. );
  347. },
  348. remove(userId, doc) {
  349. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  350. },
  351. fetch: ['boardId'],
  352. });
  353. Cards.helpers({
  354. mapCustomFieldsToBoard(boardId) {
  355. // Map custom fields to new board
  356. return this.customFields.map(cf => {
  357. const oldCf = CustomFields.findOne(cf._id);
  358. const newCf = CustomFields.findOne({
  359. boardIds: boardId,
  360. name: oldCf.name,
  361. type: oldCf.type,
  362. });
  363. if (newCf) {
  364. cf._id = newCf._id;
  365. } else if (!_.contains(oldCf.boardIds, boardId)) {
  366. oldCf.addBoard(boardId);
  367. }
  368. return cf;
  369. });
  370. },
  371. copy(boardId, swimlaneId, listId) {
  372. const oldId = this._id;
  373. const oldCard = Cards.findOne(oldId);
  374. // we must only copy the labels and custom fields if the target board
  375. // differs from the source board
  376. if (this.boardId !== boardId) {
  377. const oldBoard = Boards.findOne(this.boardId);
  378. const oldBoardLabels = oldBoard.labels;
  379. // Get old label names
  380. const oldCardLabels = _.pluck(
  381. _.filter(oldBoardLabels, label => {
  382. return _.contains(this.labelIds, label._id);
  383. }),
  384. 'name',
  385. );
  386. const newBoard = Boards.findOne(boardId);
  387. const newBoardLabels = newBoard.labels;
  388. const newCardLabels = _.pluck(
  389. _.filter(newBoardLabels, label => {
  390. return _.contains(oldCardLabels, label.name);
  391. }),
  392. '_id',
  393. );
  394. // now set the new label ids
  395. delete this.labelIds;
  396. this.labelIds = newCardLabels;
  397. this.customFields = this.mapCustomFieldsToBoard(newBoard._id);
  398. }
  399. delete this._id;
  400. this.boardId = boardId;
  401. this.swimlaneId = swimlaneId;
  402. this.listId = listId;
  403. const _id = Cards.insert(this);
  404. // Copy attachments
  405. oldCard.attachments().forEach(att => {
  406. att.cardId = _id;
  407. delete att._id;
  408. return Attachments.insert(att);
  409. });
  410. // copy checklists
  411. Checklists.find({ cardId: oldId }).forEach(ch => {
  412. ch.copy(_id);
  413. });
  414. // copy subtasks
  415. Cards.find({ parentId: oldId }).forEach(subtask => {
  416. subtask.parentId = _id;
  417. subtask._id = null;
  418. Cards.insert(subtask);
  419. });
  420. // copy card comments
  421. CardComments.find({ cardId: oldId }).forEach(cmt => {
  422. cmt.copy(_id);
  423. });
  424. return _id;
  425. },
  426. link(boardId, swimlaneId, listId) {
  427. // TODO is there a better method to create a deepcopy?
  428. linkCard = JSON.parse(JSON.stringify(this));
  429. // TODO is this how it is meant to be?
  430. linkCard.linkedId = linkCard.linkedId || linkCard._id;
  431. linkCard.boardId = boardId;
  432. linkCard.swimlaneId = swimlaneId;
  433. linkCard.listId = listId;
  434. linkCard.type = 'cardType-linkedCard';
  435. delete linkCard._id;
  436. // TODO shall we copy the labels for a linked card?!
  437. delete linkCard.labelIds;
  438. return Cards.insert(linkCard);
  439. },
  440. list() {
  441. return Lists.findOne(this.listId);
  442. },
  443. swimlane() {
  444. return Swimlanes.findOne(this.swimlaneId);
  445. },
  446. board() {
  447. return Boards.findOne(this.boardId);
  448. },
  449. getList() {
  450. const list = this.list();
  451. if (!list) {
  452. return {
  453. _id: this.listId,
  454. title: 'Undefined List',
  455. archived: false,
  456. colorClass: '',
  457. };
  458. }
  459. return list;
  460. },
  461. getSwimlane() {
  462. const swimlane = this.swimlane();
  463. if (!swimlane) {
  464. return {
  465. _id: this.swimlaneId,
  466. title: 'Undefined Swimlane',
  467. archived: false,
  468. colorClass: '',
  469. };
  470. }
  471. return swimlane;
  472. },
  473. getBoard() {
  474. const board = this.board();
  475. if (!board) {
  476. return {
  477. _id: this.boardId,
  478. title: 'Undefined Board',
  479. archived: false,
  480. colorClass: '',
  481. };
  482. }
  483. return board;
  484. },
  485. labels() {
  486. const boardLabels = this.board().labels;
  487. const cardLabels = _.filter(boardLabels, label => {
  488. return _.contains(this.labelIds, label._id);
  489. });
  490. return cardLabels;
  491. },
  492. hasLabel(labelId) {
  493. return _.contains(this.labelIds, labelId);
  494. },
  495. user() {
  496. return Users.findOne(this.userId);
  497. },
  498. isAssigned(memberId) {
  499. return _.contains(this.getMembers(), memberId);
  500. },
  501. isAssignee(assigneeId) {
  502. return _.contains(this.getAssignees(), assigneeId);
  503. },
  504. activities() {
  505. if (this.isLinkedCard()) {
  506. return Activities.find(
  507. { cardId: this.linkedId },
  508. { sort: { createdAt: -1 } },
  509. );
  510. } else if (this.isLinkedBoard()) {
  511. return Activities.find(
  512. { boardId: this.linkedId },
  513. { sort: { createdAt: -1 } },
  514. );
  515. } else {
  516. return Activities.find({ cardId: this._id }, { sort: { createdAt: -1 } });
  517. }
  518. },
  519. comments() {
  520. if (this.isLinkedCard()) {
  521. return CardComments.find(
  522. { cardId: this.linkedId },
  523. { sort: { createdAt: -1 } },
  524. );
  525. } else {
  526. return CardComments.find(
  527. { cardId: this._id },
  528. { sort: { createdAt: -1 } },
  529. );
  530. }
  531. },
  532. attachments() {
  533. if (this.isLinkedCard()) {
  534. return Attachments.find(
  535. { cardId: this.linkedId },
  536. { sort: { uploadedAt: -1 } },
  537. );
  538. } else {
  539. return Attachments.find(
  540. { cardId: this._id },
  541. { sort: { uploadedAt: -1 } },
  542. );
  543. }
  544. },
  545. cover() {
  546. if (!this.coverId) return false;
  547. const cover = Attachments.findOne(this.coverId);
  548. // if we return a cover before it is fully stored, we will get errors when we try to display it
  549. // todo XXX we could return a default "upload pending" image in the meantime?
  550. return cover && cover.url() && cover;
  551. },
  552. checklists() {
  553. if (this.isLinkedCard()) {
  554. return Checklists.find({ cardId: this.linkedId }, { sort: { sort: 1 } });
  555. } else {
  556. return Checklists.find({ cardId: this._id }, { sort: { sort: 1 } });
  557. }
  558. },
  559. checklistItemCount() {
  560. const checklists = this.checklists().fetch();
  561. return checklists
  562. .map(checklist => {
  563. return checklist.itemCount();
  564. })
  565. .reduce((prev, next) => {
  566. return prev + next;
  567. }, 0);
  568. },
  569. checklistFinishedCount() {
  570. const checklists = this.checklists().fetch();
  571. return checklists
  572. .map(checklist => {
  573. return checklist.finishedCount();
  574. })
  575. .reduce((prev, next) => {
  576. return prev + next;
  577. }, 0);
  578. },
  579. checklistFinished() {
  580. return (
  581. this.hasChecklist() &&
  582. this.checklistItemCount() === this.checklistFinishedCount()
  583. );
  584. },
  585. hasChecklist() {
  586. return this.checklistItemCount() !== 0;
  587. },
  588. subtasks() {
  589. return Cards.find(
  590. {
  591. parentId: this._id,
  592. archived: false,
  593. },
  594. {
  595. sort: {
  596. sort: 1,
  597. },
  598. },
  599. );
  600. },
  601. allSubtasks() {
  602. return Cards.find(
  603. {
  604. parentId: this._id,
  605. archived: false,
  606. },
  607. {
  608. sort: {
  609. sort: 1,
  610. },
  611. },
  612. );
  613. },
  614. subtasksCount() {
  615. return Cards.find({
  616. parentId: this._id,
  617. archived: false,
  618. }).count();
  619. },
  620. subtasksFinishedCount() {
  621. return Cards.find({
  622. parentId: this._id,
  623. archived: true,
  624. }).count();
  625. },
  626. subtasksFinished() {
  627. const finishCount = this.subtasksFinishedCount();
  628. return finishCount > 0 && this.subtasksCount() === finishCount;
  629. },
  630. allowsSubtasks() {
  631. return this.subtasksCount() !== 0;
  632. },
  633. customFieldIndex(customFieldId) {
  634. return _.pluck(this.customFields, '_id').indexOf(customFieldId);
  635. },
  636. // customFields with definitions
  637. customFieldsWD() {
  638. // get all definitions
  639. const definitions = CustomFields.find({
  640. boardIds: { $in: [this.boardId] },
  641. }).fetch();
  642. if (!definitions) {
  643. return {};
  644. }
  645. // match right definition to each field
  646. if (!this.customFields) return [];
  647. const ret = this.customFields.map(customField => {
  648. const definition = definitions.find(definition => {
  649. return definition._id === customField._id;
  650. });
  651. if (!definition) {
  652. return {};
  653. }
  654. //search for "True Value" which is for DropDowns other then the Value (which is the id)
  655. let trueValue = customField.value;
  656. if (
  657. definition.settings.dropdownItems &&
  658. definition.settings.dropdownItems.length > 0
  659. ) {
  660. for (let i = 0; i < definition.settings.dropdownItems.length; i++) {
  661. if (definition.settings.dropdownItems[i]._id === customField.value) {
  662. trueValue = definition.settings.dropdownItems[i].name;
  663. }
  664. }
  665. }
  666. return {
  667. _id: customField._id,
  668. value: customField.value,
  669. trueValue,
  670. definition,
  671. };
  672. });
  673. // at linked cards custom fields definition is not found
  674. ret.sort(
  675. (a, b) =>
  676. a.definition !== undefined &&
  677. b.definition !== undefined &&
  678. a.definition.name !== undefined &&
  679. b.definition.name !== undefined &&
  680. a.definition.name.localeCompare(b.definition.name),
  681. );
  682. return ret;
  683. },
  684. colorClass() {
  685. if (this.color) return this.color;
  686. return '';
  687. },
  688. absoluteUrl() {
  689. const board = this.board();
  690. return FlowRouter.url('card', {
  691. boardId: board._id,
  692. slug: board.slug,
  693. cardId: this._id,
  694. });
  695. },
  696. originRelativeUrl() {
  697. const board = this.board();
  698. return FlowRouter.path('card', {
  699. boardId: board._id,
  700. slug: board.slug,
  701. cardId: this._id,
  702. });
  703. },
  704. canBeRestored() {
  705. const list = Lists.findOne({
  706. _id: this.listId,
  707. });
  708. if (
  709. !list.getWipLimit('soft') &&
  710. list.getWipLimit('enabled') &&
  711. list.getWipLimit('value') === list.cards().count()
  712. ) {
  713. return false;
  714. }
  715. return true;
  716. },
  717. parentCard() {
  718. if (this.parentId === '') {
  719. return null;
  720. }
  721. return Cards.findOne(this.parentId);
  722. },
  723. parentCardName() {
  724. let result = '';
  725. if (this.parentId !== '') {
  726. const card = Cards.findOne(this.parentId);
  727. if (card) {
  728. result = card.title;
  729. }
  730. }
  731. return result;
  732. },
  733. parentListId() {
  734. const result = [];
  735. let crtParentId = this.parentId;
  736. while (crtParentId !== '') {
  737. const crt = Cards.findOne(crtParentId);
  738. if (crt === null || crt === undefined) {
  739. // maybe it has been deleted
  740. break;
  741. }
  742. if (crtParentId in result) {
  743. // circular reference
  744. break;
  745. }
  746. result.unshift(crtParentId);
  747. crtParentId = crt.parentId;
  748. }
  749. return result;
  750. },
  751. parentList() {
  752. const resultId = [];
  753. const result = [];
  754. let crtParentId = this.parentId;
  755. while (crtParentId !== '') {
  756. const crt = Cards.findOne(crtParentId);
  757. if (crt === null || crt === undefined) {
  758. // maybe it has been deleted
  759. break;
  760. }
  761. if (crtParentId in resultId) {
  762. // circular reference
  763. break;
  764. }
  765. resultId.unshift(crtParentId);
  766. result.unshift(crt);
  767. crtParentId = crt.parentId;
  768. }
  769. return result;
  770. },
  771. parentString(sep) {
  772. return this.parentList()
  773. .map(function(elem) {
  774. return elem.title;
  775. })
  776. .join(sep);
  777. },
  778. isTopLevel() {
  779. return this.parentId === '';
  780. },
  781. isLinkedCard() {
  782. return this.type === 'cardType-linkedCard';
  783. },
  784. isLinkedBoard() {
  785. return this.type === 'cardType-linkedBoard';
  786. },
  787. isLinked() {
  788. return this.isLinkedCard() || this.isLinkedBoard();
  789. },
  790. setDescription(description) {
  791. if (this.isLinkedCard()) {
  792. return Cards.update({ _id: this.linkedId }, { $set: { description } });
  793. } else if (this.isLinkedBoard()) {
  794. return Boards.update({ _id: this.linkedId }, { $set: { description } });
  795. } else {
  796. return Cards.update({ _id: this._id }, { $set: { description } });
  797. }
  798. },
  799. getDescription() {
  800. if (this.isLinkedCard()) {
  801. const card = Cards.findOne({ _id: this.linkedId });
  802. if (card && card.description) return card.description;
  803. else return null;
  804. } else if (this.isLinkedBoard()) {
  805. const board = Boards.findOne({ _id: this.linkedId });
  806. if (board && board.description) return board.description;
  807. else return null;
  808. } else if (this.description) {
  809. return this.description;
  810. } else {
  811. return null;
  812. }
  813. },
  814. getMembers() {
  815. if (this.isLinkedCard()) {
  816. const card = Cards.findOne({ _id: this.linkedId });
  817. if (card === undefined) {
  818. return null;
  819. } else {
  820. return card.members;
  821. }
  822. } else if (this.isLinkedBoard()) {
  823. const board = Boards.findOne({ _id: this.linkedId });
  824. if (board === undefined) {
  825. return null;
  826. } else {
  827. return board.activeMembers().map(member => {
  828. return member.userId;
  829. });
  830. }
  831. } else {
  832. return this.members;
  833. }
  834. },
  835. getAssignees() {
  836. if (this.isLinkedCard()) {
  837. const card = Cards.findOne({ _id: this.linkedId });
  838. if (card === undefined) {
  839. return null;
  840. } else {
  841. return card.assignees;
  842. }
  843. } else if (this.isLinkedBoard()) {
  844. const board = Boards.findOne({ _id: this.linkedId });
  845. if (board === undefined) {
  846. return null;
  847. } else {
  848. return board.activeMembers().map(assignee => {
  849. return assignee.userId;
  850. });
  851. }
  852. } else {
  853. return this.assignees;
  854. }
  855. },
  856. assignMember(memberId) {
  857. if (this.isLinkedCard()) {
  858. return Cards.update(
  859. { _id: this.linkedId },
  860. { $addToSet: { members: memberId } },
  861. );
  862. } else if (this.isLinkedBoard()) {
  863. const board = Boards.findOne({ _id: this.linkedId });
  864. return board.addMember(memberId);
  865. } else {
  866. return Cards.update(
  867. { _id: this._id },
  868. { $addToSet: { members: memberId } },
  869. );
  870. }
  871. },
  872. assignAssignee(assigneeId) {
  873. if (this.isLinkedCard()) {
  874. return Cards.update(
  875. { _id: this.linkedId },
  876. { $addToSet: { assignees: assigneeId } },
  877. );
  878. } else if (this.isLinkedBoard()) {
  879. const board = Boards.findOne({ _id: this.linkedId });
  880. return board.addAssignee(assigneeId);
  881. } else {
  882. return Cards.update(
  883. { _id: this._id },
  884. { $addToSet: { assignees: assigneeId } },
  885. );
  886. }
  887. },
  888. unassignMember(memberId) {
  889. if (this.isLinkedCard()) {
  890. return Cards.update(
  891. { _id: this.linkedId },
  892. { $pull: { members: memberId } },
  893. );
  894. } else if (this.isLinkedBoard()) {
  895. const board = Boards.findOne({ _id: this.linkedId });
  896. return board.removeMember(memberId);
  897. } else {
  898. return Cards.update({ _id: this._id }, { $pull: { members: memberId } });
  899. }
  900. },
  901. unassignAssignee(assigneeId) {
  902. if (this.isLinkedCard()) {
  903. return Cards.update(
  904. { _id: this.linkedId },
  905. { $pull: { assignees: assigneeId } },
  906. );
  907. } else if (this.isLinkedBoard()) {
  908. const board = Boards.findOne({ _id: this.linkedId });
  909. return board.removeAssignee(assigneeId);
  910. } else {
  911. return Cards.update(
  912. { _id: this._id },
  913. { $pull: { assignees: assigneeId } },
  914. );
  915. }
  916. },
  917. toggleMember(memberId) {
  918. if (this.getMembers() && this.getMembers().indexOf(memberId) > -1) {
  919. return this.unassignMember(memberId);
  920. } else {
  921. return this.assignMember(memberId);
  922. }
  923. },
  924. toggleAssignee(assigneeId) {
  925. if (this.getAssignees() && this.getAssignees().indexOf(assigneeId) > -1) {
  926. return this.unassignAssignee(assigneeId);
  927. } else {
  928. return this.assignAssignee(assigneeId);
  929. }
  930. },
  931. getReceived() {
  932. if (this.isLinkedCard()) {
  933. const card = Cards.findOne({ _id: this.linkedId });
  934. if (card === undefined) {
  935. return null;
  936. } else {
  937. return card.receivedAt;
  938. }
  939. } else {
  940. return this.receivedAt;
  941. }
  942. },
  943. setReceived(receivedAt) {
  944. if (this.isLinkedCard()) {
  945. return Cards.update({ _id: this.linkedId }, { $set: { receivedAt } });
  946. } else {
  947. return Cards.update({ _id: this._id }, { $set: { receivedAt } });
  948. }
  949. },
  950. getStart() {
  951. if (this.isLinkedCard()) {
  952. const card = Cards.findOne({ _id: this.linkedId });
  953. if (card === undefined) {
  954. return null;
  955. } else {
  956. return card.startAt;
  957. }
  958. } else if (this.isLinkedBoard()) {
  959. const board = Boards.findOne({ _id: this.linkedId });
  960. if (board === undefined) {
  961. return null;
  962. } else {
  963. return board.startAt;
  964. }
  965. } else {
  966. return this.startAt;
  967. }
  968. },
  969. setStart(startAt) {
  970. if (this.isLinkedCard()) {
  971. return Cards.update({ _id: this.linkedId }, { $set: { startAt } });
  972. } else if (this.isLinkedBoard()) {
  973. return Boards.update({ _id: this.linkedId }, { $set: { startAt } });
  974. } else {
  975. return Cards.update({ _id: this._id }, { $set: { startAt } });
  976. }
  977. },
  978. getDue() {
  979. if (this.isLinkedCard()) {
  980. const card = Cards.findOne({ _id: this.linkedId });
  981. if (card === undefined) {
  982. return null;
  983. } else {
  984. return card.dueAt;
  985. }
  986. } else if (this.isLinkedBoard()) {
  987. const board = Boards.findOne({ _id: this.linkedId });
  988. if (board === undefined) {
  989. return null;
  990. } else {
  991. return board.dueAt;
  992. }
  993. } else {
  994. return this.dueAt;
  995. }
  996. },
  997. setDue(dueAt) {
  998. if (this.isLinkedCard()) {
  999. return Cards.update({ _id: this.linkedId }, { $set: { dueAt } });
  1000. } else if (this.isLinkedBoard()) {
  1001. return Boards.update({ _id: this.linkedId }, { $set: { dueAt } });
  1002. } else {
  1003. return Cards.update({ _id: this._id }, { $set: { dueAt } });
  1004. }
  1005. },
  1006. getEnd() {
  1007. if (this.isLinkedCard()) {
  1008. const card = Cards.findOne({ _id: this.linkedId });
  1009. if (card === undefined) {
  1010. return null;
  1011. } else {
  1012. return card.endAt;
  1013. }
  1014. } else if (this.isLinkedBoard()) {
  1015. const board = Boards.findOne({ _id: this.linkedId });
  1016. if (board === undefined) {
  1017. return null;
  1018. } else {
  1019. return board.endAt;
  1020. }
  1021. } else {
  1022. return this.endAt;
  1023. }
  1024. },
  1025. setEnd(endAt) {
  1026. if (this.isLinkedCard()) {
  1027. return Cards.update({ _id: this.linkedId }, { $set: { endAt } });
  1028. } else if (this.isLinkedBoard()) {
  1029. return Boards.update({ _id: this.linkedId }, { $set: { endAt } });
  1030. } else {
  1031. return Cards.update({ _id: this._id }, { $set: { endAt } });
  1032. }
  1033. },
  1034. getIsOvertime() {
  1035. if (this.isLinkedCard()) {
  1036. const card = Cards.findOne({ _id: this.linkedId });
  1037. if (card === undefined) {
  1038. return null;
  1039. } else {
  1040. return card.isOvertime;
  1041. }
  1042. } else if (this.isLinkedBoard()) {
  1043. const board = Boards.findOne({ _id: this.linkedId });
  1044. if (board === undefined) {
  1045. return null;
  1046. } else {
  1047. return board.isOvertime;
  1048. }
  1049. } else {
  1050. return this.isOvertime;
  1051. }
  1052. },
  1053. setIsOvertime(isOvertime) {
  1054. if (this.isLinkedCard()) {
  1055. return Cards.update({ _id: this.linkedId }, { $set: { isOvertime } });
  1056. } else if (this.isLinkedBoard()) {
  1057. return Boards.update({ _id: this.linkedId }, { $set: { isOvertime } });
  1058. } else {
  1059. return Cards.update({ _id: this._id }, { $set: { isOvertime } });
  1060. }
  1061. },
  1062. getSpentTime() {
  1063. if (this.isLinkedCard()) {
  1064. const card = Cards.findOne({ _id: this.linkedId });
  1065. if (card === undefined) {
  1066. return null;
  1067. } else {
  1068. return card.spentTime;
  1069. }
  1070. } else if (this.isLinkedBoard()) {
  1071. const board = Boards.findOne({ _id: this.linkedId });
  1072. if (board === undefined) {
  1073. return null;
  1074. } else {
  1075. return board.spentTime;
  1076. }
  1077. } else {
  1078. return this.spentTime;
  1079. }
  1080. },
  1081. setSpentTime(spentTime) {
  1082. if (this.isLinkedCard()) {
  1083. return Cards.update({ _id: this.linkedId }, { $set: { spentTime } });
  1084. } else if (this.isLinkedBoard()) {
  1085. return Boards.update({ _id: this.linkedId }, { $set: { spentTime } });
  1086. } else {
  1087. return Cards.update({ _id: this._id }, { $set: { spentTime } });
  1088. }
  1089. },
  1090. getVoteQuestion() {
  1091. if (this.isLinkedCard()) {
  1092. const card = Cards.findOne({ _id: this.linkedId });
  1093. if (card === undefined) {
  1094. return null;
  1095. } else if (card && card.vote) {
  1096. return card.vote.question;
  1097. } else {
  1098. return null;
  1099. }
  1100. } else if (this.isLinkedBoard()) {
  1101. const board = Boards.findOne({ _id: this.linkedId });
  1102. if (board === undefined) {
  1103. return null;
  1104. } else if (board && board.vote) {
  1105. return board.vote.question;
  1106. } else {
  1107. return null;
  1108. }
  1109. } else if (this.vote) {
  1110. return this.vote.question;
  1111. } else {
  1112. return null;
  1113. }
  1114. },
  1115. getVotePublic() {
  1116. if (this.isLinkedCard()) {
  1117. const card = Cards.findOne({ _id: this.linkedId });
  1118. if (card === undefined) {
  1119. return null;
  1120. } else if (card && card.vote) {
  1121. return card.vote.public;
  1122. } else {
  1123. return null;
  1124. }
  1125. } else if (this.isLinkedBoard()) {
  1126. const board = Boards.findOne({ _id: this.linkedId });
  1127. if (board === undefined) {
  1128. return null;
  1129. } else if (board && board.vote) {
  1130. return board.vote.public;
  1131. } else {
  1132. return null;
  1133. }
  1134. } else if (this.vote) {
  1135. return this.vote.public;
  1136. } else {
  1137. return null;
  1138. }
  1139. },
  1140. getVoteEnd() {
  1141. if (this.isLinkedCard()) {
  1142. const card = Cards.findOne({ _id: this.linkedId });
  1143. if (card === undefined) {
  1144. return null;
  1145. } else if (card && card.vote) {
  1146. return card.vote.end;
  1147. } else {
  1148. return null;
  1149. }
  1150. } else if (this.isLinkedBoard()) {
  1151. const board = Boards.findOne({ _id: this.linkedId });
  1152. if (board === undefined) {
  1153. return null;
  1154. } else if (board && board.vote) {
  1155. return board.vote.end;
  1156. } else {
  1157. return null;
  1158. }
  1159. } else if (this.vote) {
  1160. return this.vote.end;
  1161. } else {
  1162. return null;
  1163. }
  1164. },
  1165. expiredVote() {
  1166. let end = this.getVoteEnd();
  1167. if (end) {
  1168. end = moment(end);
  1169. return end.isBefore(new Date());
  1170. }
  1171. return false;
  1172. },
  1173. voteMemberPositive() {
  1174. if (this.vote && this.vote.positive)
  1175. return Users.find({ _id: { $in: this.vote.positive } });
  1176. return [];
  1177. },
  1178. voteMemberNegative() {
  1179. if (this.vote && this.vote.negative)
  1180. return Users.find({ _id: { $in: this.vote.negative } });
  1181. return [];
  1182. },
  1183. voteState() {
  1184. const userId = Meteor.userId();
  1185. let state;
  1186. if (this.vote) {
  1187. if (this.vote.positive) {
  1188. state = _.contains(this.vote.positive, userId);
  1189. if (state === true) return true;
  1190. }
  1191. if (this.vote.negative) {
  1192. state = _.contains(this.vote.negative, userId);
  1193. if (state === true) return false;
  1194. }
  1195. }
  1196. return null;
  1197. },
  1198. getId() {
  1199. if (this.isLinked()) {
  1200. return this.linkedId;
  1201. } else {
  1202. return this._id;
  1203. }
  1204. },
  1205. getTitle() {
  1206. if (this.isLinkedCard()) {
  1207. const card = Cards.findOne({ _id: this.linkedId });
  1208. if (card === undefined) {
  1209. return null;
  1210. } else {
  1211. return card.title;
  1212. }
  1213. } else if (this.isLinkedBoard()) {
  1214. const board = Boards.findOne({ _id: this.linkedId });
  1215. if (board === undefined) {
  1216. return null;
  1217. } else {
  1218. return board.title;
  1219. }
  1220. } else if (this.title === undefined) {
  1221. return null;
  1222. } else {
  1223. return this.title;
  1224. }
  1225. },
  1226. getBoardTitle() {
  1227. if (this.isLinkedCard()) {
  1228. const card = Cards.findOne({ _id: this.linkedId });
  1229. if (card === undefined) {
  1230. return null;
  1231. }
  1232. const board = Boards.findOne({ _id: card.boardId });
  1233. if (board === undefined) {
  1234. return null;
  1235. } else {
  1236. return board.title;
  1237. }
  1238. } else if (this.isLinkedBoard()) {
  1239. const board = Boards.findOne({ _id: this.linkedId });
  1240. if (board === undefined) {
  1241. return null;
  1242. } else {
  1243. return board.title;
  1244. }
  1245. } else {
  1246. const board = Boards.findOne({ _id: this.boardId });
  1247. if (board === undefined) {
  1248. return null;
  1249. } else {
  1250. return board.title;
  1251. }
  1252. }
  1253. },
  1254. setTitle(title) {
  1255. if (this.isLinkedCard()) {
  1256. return Cards.update({ _id: this.linkedId }, { $set: { title } });
  1257. } else if (this.isLinkedBoard()) {
  1258. return Boards.update({ _id: this.linkedId }, { $set: { title } });
  1259. } else {
  1260. return Cards.update({ _id: this._id }, { $set: { title } });
  1261. }
  1262. },
  1263. getArchived() {
  1264. if (this.isLinkedCard()) {
  1265. const card = Cards.findOne({ _id: this.linkedId });
  1266. if (card === undefined) {
  1267. return null;
  1268. } else {
  1269. return card.archived;
  1270. }
  1271. } else if (this.isLinkedBoard()) {
  1272. const board = Boards.findOne({ _id: this.linkedId });
  1273. if (board === undefined) {
  1274. return null;
  1275. } else {
  1276. return board.archived;
  1277. }
  1278. } else {
  1279. return this.archived;
  1280. }
  1281. },
  1282. setRequestedBy(requestedBy) {
  1283. if (this.isLinkedCard()) {
  1284. return Cards.update({ _id: this.linkedId }, { $set: { requestedBy } });
  1285. } else {
  1286. return Cards.update({ _id: this._id }, { $set: { requestedBy } });
  1287. }
  1288. },
  1289. getRequestedBy() {
  1290. if (this.isLinkedCard()) {
  1291. const card = Cards.findOne({ _id: this.linkedId });
  1292. if (card === undefined) {
  1293. return null;
  1294. } else {
  1295. return card.requestedBy;
  1296. }
  1297. } else {
  1298. return this.requestedBy;
  1299. }
  1300. },
  1301. setAssignedBy(assignedBy) {
  1302. if (this.isLinkedCard()) {
  1303. return Cards.update({ _id: this.linkedId }, { $set: { assignedBy } });
  1304. } else {
  1305. return Cards.update({ _id: this._id }, { $set: { assignedBy } });
  1306. }
  1307. },
  1308. getAssignedBy() {
  1309. if (this.isLinkedCard()) {
  1310. const card = Cards.findOne({ _id: this.linkedId });
  1311. if (card === undefined) {
  1312. return null;
  1313. } else {
  1314. return card.assignedBy;
  1315. }
  1316. } else {
  1317. return this.assignedBy;
  1318. }
  1319. },
  1320. isTemplateCard() {
  1321. return this.type === 'template-card';
  1322. },
  1323. votePublic() {
  1324. if (this.vote) return this.vote.public;
  1325. return null;
  1326. },
  1327. voteAllowNonBoardMembers() {
  1328. if (this.vote) return this.vote.allowNonBoardMembers;
  1329. return null;
  1330. },
  1331. voteCountNegative() {
  1332. if (this.vote && this.vote.negative) return this.vote.negative.length;
  1333. return null;
  1334. },
  1335. voteCountPositive() {
  1336. if (this.vote && this.vote.positive) return this.vote.positive.length;
  1337. return null;
  1338. },
  1339. voteCount() {
  1340. return this.voteCountPositive() + this.voteCountNegative();
  1341. },
  1342. });
  1343. Cards.mutations({
  1344. applyToChildren(funct) {
  1345. Cards.find({
  1346. parentId: this._id,
  1347. }).forEach(card => {
  1348. funct(card);
  1349. });
  1350. },
  1351. archive() {
  1352. this.applyToChildren(card => {
  1353. return card.archive();
  1354. });
  1355. return {
  1356. $set: {
  1357. archived: true,
  1358. archivedAt: new Date(),
  1359. },
  1360. };
  1361. },
  1362. restore() {
  1363. this.applyToChildren(card => {
  1364. return card.restore();
  1365. });
  1366. return {
  1367. $set: {
  1368. archived: false,
  1369. },
  1370. };
  1371. },
  1372. moveToEndOfList({ listId } = {}) {
  1373. let swimlaneId = this.swimlaneId;
  1374. const boardId = this.boardId;
  1375. let sortIndex = 0;
  1376. // This should never happen, but there was a bug that was fixed in commit
  1377. // ea0239538a68e225c867411a4f3e0d27c158383.
  1378. if (!swimlaneId) {
  1379. const board = Boards.findOne(boardId);
  1380. swimlaneId = board.getDefaultSwimline()._id;
  1381. }
  1382. // Move the minicard to the end of the target list
  1383. let parentElementDom = $(`#swimlane-${this.swimlaneId}`).get(0);
  1384. if (!parentElementDom) parentElementDom = $(':root');
  1385. const lastCardDom = $(parentElementDom)
  1386. .find(`#js-list-${listId} .js-minicard:last`)
  1387. .get(0);
  1388. if (lastCardDom) sortIndex = Utils.calculateIndex(lastCardDom, null).base;
  1389. return this.moveOptionalArgs({
  1390. boardId,
  1391. swimlaneId,
  1392. listId,
  1393. sort: sortIndex,
  1394. });
  1395. },
  1396. moveOptionalArgs({ boardId, swimlaneId, listId, sort } = {}) {
  1397. boardId = boardId || this.boardId;
  1398. swimlaneId = swimlaneId || this.swimlaneId;
  1399. // This should never happen, but there was a bug that was fixed in commit
  1400. // ea0239538a68e225c867411a4f3e0d27c158383.
  1401. if (!swimlaneId) {
  1402. const board = Boards.findOne(boardId);
  1403. swimlaneId = board.getDefaultSwimline()._id;
  1404. }
  1405. listId = listId || this.listId;
  1406. if (sort === undefined || sort === null) sort = this.sort;
  1407. return this.move(boardId, swimlaneId, listId, sort);
  1408. },
  1409. move(boardId, swimlaneId, listId, sort = null) {
  1410. const mutatedFields = {
  1411. boardId,
  1412. swimlaneId,
  1413. listId,
  1414. };
  1415. if (sort !== null) {
  1416. mutatedFields.sort = sort;
  1417. }
  1418. // we must only copy the labels and custom fields if the target board
  1419. // differs from the source board
  1420. if (this.boardId !== boardId) {
  1421. // Get label names
  1422. const oldBoard = Boards.findOne(this.boardId);
  1423. const oldBoardLabels = oldBoard.labels;
  1424. const oldCardLabels = _.pluck(
  1425. _.filter(oldBoardLabels, label => {
  1426. return _.contains(this.labelIds, label._id);
  1427. }),
  1428. 'name',
  1429. );
  1430. const newBoard = Boards.findOne(boardId);
  1431. const newBoardLabels = newBoard.labels;
  1432. const newCardLabelIds = _.pluck(
  1433. _.filter(newBoardLabels, label => {
  1434. return label.name && _.contains(oldCardLabels, label.name);
  1435. }),
  1436. '_id',
  1437. );
  1438. Object.assign(mutatedFields, {
  1439. labelIds: newCardLabelIds,
  1440. });
  1441. mutatedFields.customFields = this.mapCustomFieldsToBoard(newBoard._id);
  1442. }
  1443. Cards.update(this._id, {
  1444. $set: mutatedFields,
  1445. });
  1446. },
  1447. addLabel(labelId) {
  1448. return {
  1449. $addToSet: {
  1450. labelIds: labelId,
  1451. },
  1452. };
  1453. },
  1454. removeLabel(labelId) {
  1455. return {
  1456. $pull: {
  1457. labelIds: labelId,
  1458. },
  1459. };
  1460. },
  1461. toggleLabel(labelId) {
  1462. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  1463. return this.removeLabel(labelId);
  1464. } else {
  1465. return this.addLabel(labelId);
  1466. }
  1467. },
  1468. setColor(newColor) {
  1469. if (newColor === 'white') {
  1470. newColor = null;
  1471. }
  1472. return {
  1473. $set: {
  1474. color: newColor,
  1475. },
  1476. };
  1477. },
  1478. assignMember(memberId) {
  1479. return {
  1480. $addToSet: {
  1481. members: memberId,
  1482. },
  1483. };
  1484. },
  1485. assignAssignee(assigneeId) {
  1486. // If there is not any assignee, allow one assignee, not more.
  1487. /*
  1488. if (this.getAssignees().length === 0) {
  1489. return {
  1490. $addToSet: {
  1491. assignees: assigneeId,
  1492. },
  1493. };
  1494. */
  1495. // Allow more that one assignee:
  1496. // https://github.com/wekan/wekan/issues/3302
  1497. return {
  1498. $addToSet: {
  1499. assignees: assigneeId,
  1500. },
  1501. };
  1502. //} else {
  1503. // return false,
  1504. //}
  1505. },
  1506. unassignMember(memberId) {
  1507. return {
  1508. $pull: {
  1509. members: memberId,
  1510. },
  1511. };
  1512. },
  1513. unassignAssignee(assigneeId) {
  1514. return {
  1515. $pull: {
  1516. assignees: assigneeId,
  1517. },
  1518. };
  1519. },
  1520. toggleMember(memberId) {
  1521. if (this.members && this.members.indexOf(memberId) > -1) {
  1522. return this.unassignMember(memberId);
  1523. } else {
  1524. return this.assignMember(memberId);
  1525. }
  1526. },
  1527. toggleAssignee(assigneeId) {
  1528. if (this.assignees && this.assignees.indexOf(assigneeId) > -1) {
  1529. return this.unassignAssignee(assigneeId);
  1530. } else {
  1531. return this.assignAssignee(assigneeId);
  1532. }
  1533. },
  1534. assignCustomField(customFieldId) {
  1535. return {
  1536. $addToSet: {
  1537. customFields: {
  1538. _id: customFieldId,
  1539. value: null,
  1540. },
  1541. },
  1542. };
  1543. },
  1544. unassignCustomField(customFieldId) {
  1545. return {
  1546. $pull: {
  1547. customFields: {
  1548. _id: customFieldId,
  1549. },
  1550. },
  1551. };
  1552. },
  1553. toggleCustomField(customFieldId) {
  1554. if (this.customFields && this.customFieldIndex(customFieldId) > -1) {
  1555. return this.unassignCustomField(customFieldId);
  1556. } else {
  1557. return this.assignCustomField(customFieldId);
  1558. }
  1559. },
  1560. setCustomField(customFieldId, value) {
  1561. // todo
  1562. const index = this.customFieldIndex(customFieldId);
  1563. if (index > -1) {
  1564. const update = {
  1565. $set: {},
  1566. };
  1567. update.$set[`customFields.${index}.value`] = value;
  1568. return update;
  1569. }
  1570. // TODO
  1571. // Ignatz 18.05.2018: Return null to silence ESLint. No Idea if that is correct
  1572. return null;
  1573. },
  1574. setCover(coverId) {
  1575. return {
  1576. $set: {
  1577. coverId,
  1578. },
  1579. };
  1580. },
  1581. unsetCover() {
  1582. return {
  1583. $unset: {
  1584. coverId: '',
  1585. },
  1586. };
  1587. },
  1588. setReceived(receivedAt) {
  1589. return {
  1590. $set: {
  1591. receivedAt,
  1592. },
  1593. };
  1594. },
  1595. unsetReceived() {
  1596. return {
  1597. $unset: {
  1598. receivedAt: '',
  1599. },
  1600. };
  1601. },
  1602. setStart(startAt) {
  1603. return {
  1604. $set: {
  1605. startAt,
  1606. },
  1607. };
  1608. },
  1609. unsetStart() {
  1610. return {
  1611. $unset: {
  1612. startAt: '',
  1613. },
  1614. };
  1615. },
  1616. setDue(dueAt) {
  1617. return {
  1618. $set: {
  1619. dueAt,
  1620. },
  1621. };
  1622. },
  1623. unsetDue() {
  1624. return {
  1625. $unset: {
  1626. dueAt: '',
  1627. },
  1628. };
  1629. },
  1630. setEnd(endAt) {
  1631. return {
  1632. $set: {
  1633. endAt,
  1634. },
  1635. };
  1636. },
  1637. unsetEnd() {
  1638. return {
  1639. $unset: {
  1640. endAt: '',
  1641. },
  1642. };
  1643. },
  1644. setOvertime(isOvertime) {
  1645. return {
  1646. $set: {
  1647. isOvertime,
  1648. },
  1649. };
  1650. },
  1651. setSpentTime(spentTime) {
  1652. return {
  1653. $set: {
  1654. spentTime,
  1655. },
  1656. };
  1657. },
  1658. unsetSpentTime() {
  1659. return {
  1660. $unset: {
  1661. spentTime: '',
  1662. isOvertime: false,
  1663. },
  1664. };
  1665. },
  1666. setParentId(parentId) {
  1667. return {
  1668. $set: {
  1669. parentId,
  1670. },
  1671. };
  1672. },
  1673. setVoteQuestion(question, publicVote, allowNonBoardMembers) {
  1674. return {
  1675. $set: {
  1676. vote: {
  1677. question,
  1678. public: publicVote,
  1679. allowNonBoardMembers,
  1680. positive: [],
  1681. negative: [],
  1682. },
  1683. },
  1684. };
  1685. },
  1686. unsetVote() {
  1687. return {
  1688. $unset: {
  1689. vote: '',
  1690. },
  1691. };
  1692. },
  1693. setVoteEnd(end) {
  1694. return {
  1695. $set: { 'vote.end': end },
  1696. };
  1697. },
  1698. unsetVoteEnd() {
  1699. return {
  1700. $unset: { 'vote.end': '' },
  1701. };
  1702. },
  1703. setVote(userId, forIt) {
  1704. switch (forIt) {
  1705. case true:
  1706. // vote for it
  1707. return {
  1708. $pull: {
  1709. 'vote.negative': userId,
  1710. },
  1711. $addToSet: {
  1712. 'vote.positive': userId,
  1713. },
  1714. };
  1715. case false:
  1716. // vote against
  1717. return {
  1718. $pull: {
  1719. 'vote.positive': userId,
  1720. },
  1721. $addToSet: {
  1722. 'vote.negative': userId,
  1723. },
  1724. };
  1725. default:
  1726. // Remove votes
  1727. return {
  1728. $pull: {
  1729. 'vote.positive': userId,
  1730. 'vote.negative': userId,
  1731. },
  1732. };
  1733. }
  1734. },
  1735. });
  1736. //FUNCTIONS FOR creation of Activities
  1737. function updateActivities(doc, fieldNames, modifier) {
  1738. if (_.contains(fieldNames, 'labelIds') && _.contains(fieldNames, 'boardId')) {
  1739. Activities.find({
  1740. activityType: 'addedLabel',
  1741. cardId: doc._id,
  1742. }).forEach(a => {
  1743. const lidx = doc.labelIds.indexOf(a.labelId);
  1744. if (lidx !== -1 && modifier.$set.labelIds.length > lidx) {
  1745. Activities.update(a._id, {
  1746. $set: {
  1747. labelId: modifier.$set.labelIds[doc.labelIds.indexOf(a.labelId)],
  1748. boardId: modifier.$set.boardId,
  1749. },
  1750. });
  1751. } else {
  1752. Activities.remove(a._id);
  1753. }
  1754. });
  1755. } else if (_.contains(fieldNames, 'boardId')) {
  1756. Activities.remove({
  1757. activityType: 'addedLabel',
  1758. cardId: doc._id,
  1759. });
  1760. }
  1761. }
  1762. function cardMove(
  1763. userId,
  1764. doc,
  1765. fieldNames,
  1766. oldListId,
  1767. oldSwimlaneId,
  1768. oldBoardId,
  1769. ) {
  1770. if (_.contains(fieldNames, 'boardId') && doc.boardId !== oldBoardId) {
  1771. Activities.insert({
  1772. userId,
  1773. activityType: 'moveCardBoard',
  1774. boardName: Boards.findOne(doc.boardId).title,
  1775. boardId: doc.boardId,
  1776. oldBoardId,
  1777. oldBoardName: Boards.findOne(oldBoardId).title,
  1778. cardId: doc._id,
  1779. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1780. swimlaneId: doc.swimlaneId,
  1781. oldSwimlaneId,
  1782. });
  1783. } else if (
  1784. (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) ||
  1785. (_.contains(fieldNames, 'swimlaneId') && doc.swimlaneId !== oldSwimlaneId)
  1786. ) {
  1787. Activities.insert({
  1788. userId,
  1789. oldListId,
  1790. activityType: 'moveCard',
  1791. listName: Lists.findOne(doc.listId).title,
  1792. listId: doc.listId,
  1793. boardId: doc.boardId,
  1794. cardId: doc._id,
  1795. cardTitle: doc.title,
  1796. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1797. swimlaneId: doc.swimlaneId,
  1798. oldSwimlaneId,
  1799. });
  1800. }
  1801. }
  1802. function cardState(userId, doc, fieldNames) {
  1803. if (_.contains(fieldNames, 'archived')) {
  1804. if (doc.archived) {
  1805. Activities.insert({
  1806. userId,
  1807. activityType: 'archivedCard',
  1808. listName: Lists.findOne(doc.listId).title,
  1809. boardId: doc.boardId,
  1810. listId: doc.listId,
  1811. cardId: doc._id,
  1812. swimlaneId: doc.swimlaneId,
  1813. });
  1814. } else {
  1815. Activities.insert({
  1816. userId,
  1817. activityType: 'restoredCard',
  1818. boardId: doc.boardId,
  1819. listName: Lists.findOne(doc.listId).title,
  1820. listId: doc.listId,
  1821. cardId: doc._id,
  1822. swimlaneId: doc.swimlaneId,
  1823. });
  1824. }
  1825. }
  1826. }
  1827. function cardMembers(userId, doc, fieldNames, modifier) {
  1828. if (!_.contains(fieldNames, 'members')) return;
  1829. let memberId;
  1830. // Say hello to the new member
  1831. if (modifier.$addToSet && modifier.$addToSet.members) {
  1832. memberId = modifier.$addToSet.members;
  1833. const username = Users.findOne(memberId).username;
  1834. if (!_.contains(doc.members, memberId)) {
  1835. Activities.insert({
  1836. userId,
  1837. username,
  1838. activityType: 'joinMember',
  1839. boardId: doc.boardId,
  1840. cardId: doc._id,
  1841. memberId,
  1842. listId: doc.listId,
  1843. swimlaneId: doc.swimlaneId,
  1844. });
  1845. }
  1846. }
  1847. // Say goodbye to the former member
  1848. if (modifier.$pull && modifier.$pull.members) {
  1849. memberId = modifier.$pull.members;
  1850. const username = Users.findOne(memberId).username;
  1851. // Check that the former member is member of the card
  1852. if (_.contains(doc.members, memberId)) {
  1853. Activities.insert({
  1854. userId,
  1855. username,
  1856. activityType: 'unjoinMember',
  1857. boardId: doc.boardId,
  1858. cardId: doc._id,
  1859. memberId,
  1860. listId: doc.listId,
  1861. swimlaneId: doc.swimlaneId,
  1862. });
  1863. }
  1864. }
  1865. }
  1866. function cardAssignees(userId, doc, fieldNames, modifier) {
  1867. if (!_.contains(fieldNames, 'assignees')) return;
  1868. let assigneeId;
  1869. // Say hello to the new assignee
  1870. if (modifier.$addToSet && modifier.$addToSet.assignees) {
  1871. assigneeId = modifier.$addToSet.assignees;
  1872. const username = Users.findOne(assigneeId).username;
  1873. if (!_.contains(doc.assignees, assigneeId)) {
  1874. Activities.insert({
  1875. userId,
  1876. username,
  1877. activityType: 'joinAssignee',
  1878. boardId: doc.boardId,
  1879. cardId: doc._id,
  1880. assigneeId,
  1881. listId: doc.listId,
  1882. swimlaneId: doc.swimlaneId,
  1883. });
  1884. }
  1885. }
  1886. // Say goodbye to the former assignee
  1887. if (modifier.$pull && modifier.$pull.assignees) {
  1888. assigneeId = modifier.$pull.assignees;
  1889. const username = Users.findOne(assigneeId).username;
  1890. // Check that the former assignee is assignee of the card
  1891. if (_.contains(doc.assignees, assigneeId)) {
  1892. Activities.insert({
  1893. userId,
  1894. username,
  1895. activityType: 'unjoinAssignee',
  1896. boardId: doc.boardId,
  1897. cardId: doc._id,
  1898. assigneeId,
  1899. listId: doc.listId,
  1900. swimlaneId: doc.swimlaneId,
  1901. });
  1902. }
  1903. }
  1904. }
  1905. function cardLabels(userId, doc, fieldNames, modifier) {
  1906. if (!_.contains(fieldNames, 'labelIds')) return;
  1907. let labelId;
  1908. // Say hello to the new label
  1909. if (modifier.$addToSet && modifier.$addToSet.labelIds) {
  1910. labelId = modifier.$addToSet.labelIds;
  1911. if (!_.contains(doc.labelIds, labelId)) {
  1912. const act = {
  1913. userId,
  1914. labelId,
  1915. activityType: 'addedLabel',
  1916. boardId: doc.boardId,
  1917. cardId: doc._id,
  1918. listId: doc.listId,
  1919. swimlaneId: doc.swimlaneId,
  1920. };
  1921. Activities.insert(act);
  1922. }
  1923. }
  1924. // Say goodbye to the label
  1925. if (modifier.$pull && modifier.$pull.labelIds) {
  1926. labelId = modifier.$pull.labelIds;
  1927. // Check that the former member is member of the card
  1928. if (_.contains(doc.labelIds, labelId)) {
  1929. Activities.insert({
  1930. userId,
  1931. labelId,
  1932. activityType: 'removedLabel',
  1933. boardId: doc.boardId,
  1934. cardId: doc._id,
  1935. listId: doc.listId,
  1936. swimlaneId: doc.swimlaneId,
  1937. });
  1938. }
  1939. }
  1940. }
  1941. function cardCustomFields(userId, doc, fieldNames, modifier) {
  1942. if (!_.contains(fieldNames, 'customFields')) return;
  1943. // Say hello to the new customField value
  1944. if (modifier.$set) {
  1945. _.each(modifier.$set, (value, key) => {
  1946. if (key.startsWith('customFields')) {
  1947. const dotNotation = key.split('.');
  1948. // only individual changes are registered
  1949. if (dotNotation.length > 1) {
  1950. const customFieldId = doc.customFields[dotNotation[1]]._id;
  1951. const act = {
  1952. userId,
  1953. customFieldId,
  1954. value,
  1955. activityType: 'setCustomField',
  1956. boardId: doc.boardId,
  1957. cardId: doc._id,
  1958. listId: doc.listId,
  1959. swimlaneId: doc.swimlaneId,
  1960. };
  1961. Activities.insert(act);
  1962. }
  1963. }
  1964. });
  1965. }
  1966. // Say goodbye to the former customField value
  1967. if (modifier.$unset) {
  1968. _.each(modifier.$unset, (value, key) => {
  1969. if (key.startsWith('customFields')) {
  1970. const dotNotation = key.split('.');
  1971. // only individual changes are registered
  1972. if (dotNotation.length > 1) {
  1973. const customFieldId = doc.customFields[dotNotation[1]]._id;
  1974. const act = {
  1975. userId,
  1976. customFieldId,
  1977. activityType: 'unsetCustomField',
  1978. boardId: doc.boardId,
  1979. cardId: doc._id,
  1980. };
  1981. Activities.insert(act);
  1982. }
  1983. }
  1984. });
  1985. }
  1986. }
  1987. function cardCreation(userId, doc) {
  1988. Activities.insert({
  1989. userId,
  1990. activityType: 'createCard',
  1991. boardId: doc.boardId,
  1992. listName: Lists.findOne(doc.listId).title,
  1993. listId: doc.listId,
  1994. cardId: doc._id,
  1995. cardTitle: doc.title,
  1996. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1997. swimlaneId: doc.swimlaneId,
  1998. });
  1999. }
  2000. function cardRemover(userId, doc) {
  2001. ChecklistItems.remove({
  2002. cardId: doc._id,
  2003. });
  2004. Checklists.remove({
  2005. cardId: doc._id,
  2006. });
  2007. Cards.remove({
  2008. parentId: doc._id,
  2009. });
  2010. CardComments.remove({
  2011. cardId: doc._id,
  2012. });
  2013. Attachments.remove({
  2014. cardId: doc._id,
  2015. });
  2016. }
  2017. const findDueCards = days => {
  2018. const seekDue = ($from, $to, activityType) => {
  2019. Cards.find({
  2020. archived: false,
  2021. dueAt: { $gte: $from, $lt: $to },
  2022. }).forEach(card => {
  2023. const username = Users.findOne(card.userId).username;
  2024. const activity = {
  2025. userId: card.userId,
  2026. username,
  2027. activityType,
  2028. boardId: card.boardId,
  2029. cardId: card._id,
  2030. cardTitle: card.title,
  2031. listId: card.listId,
  2032. timeValue: card.dueAt,
  2033. swimlaneId: card.swimlaneId,
  2034. };
  2035. Activities.insert(activity);
  2036. });
  2037. };
  2038. const now = new Date(),
  2039. aday = 3600 * 24 * 1e3,
  2040. then = day => new Date(now.setHours(0, 0, 0, 0) + day * aday);
  2041. if (!days) return;
  2042. if (!days.map) days = [days];
  2043. days.map(day => {
  2044. let args = [];
  2045. if (day === 0) {
  2046. args = [then(0), then(1), 'duenow'];
  2047. } else if (day > 0) {
  2048. args = [then(1), then(day), 'almostdue'];
  2049. } else {
  2050. args = [then(day), now, 'pastdue'];
  2051. }
  2052. seekDue(...args);
  2053. });
  2054. };
  2055. const addCronJob = _.debounce(
  2056. Meteor.bindEnvironment(function findDueCardsDebounced() {
  2057. const envValue = process.env.NOTIFY_DUE_DAYS_BEFORE_AND_AFTER;
  2058. if (!envValue) {
  2059. return;
  2060. }
  2061. const notifydays = envValue
  2062. .split(',')
  2063. .map(value => {
  2064. const iValue = parseInt(value, 10);
  2065. if (!(iValue > 0 && iValue < 15)) {
  2066. // notifying due is disabled
  2067. return false;
  2068. } else {
  2069. return iValue;
  2070. }
  2071. })
  2072. .filter(Boolean);
  2073. const notifyitvl = process.env.NOTIFY_DUE_AT_HOUR_OF_DAY; //passed in the itvl has to be a number standing for the hour of current time
  2074. const defaultitvl = 8; // default every morning at 8am, if the passed env variable has parsing error use default
  2075. const itvl = parseInt(notifyitvl, 10) || defaultitvl;
  2076. const scheduler = (job => () => {
  2077. const now = new Date();
  2078. const hour = 3600 * 1e3;
  2079. if (now.getHours() === itvl) {
  2080. if (typeof job === 'function') {
  2081. job();
  2082. }
  2083. }
  2084. Meteor.setTimeout(scheduler, hour);
  2085. })(() => {
  2086. findDueCards(notifydays);
  2087. });
  2088. scheduler();
  2089. }),
  2090. 500,
  2091. );
  2092. if (Meteor.isServer) {
  2093. // Cards are often fetched within a board, so we create an index to make these
  2094. // queries more efficient.
  2095. Meteor.startup(() => {
  2096. Cards._collection._ensureIndex({ modifiedAt: -1 });
  2097. Cards._collection._ensureIndex({ boardId: 1, createdAt: -1 });
  2098. // https://github.com/wekan/wekan/issues/1863
  2099. // Swimlane added a new field in the cards collection of mongodb named parentId.
  2100. // When loading a board, mongodb is searching for every cards, the id of the parent (in the swinglanes collection).
  2101. // With a huge database, this result in a very slow app and high CPU on the mongodb side.
  2102. // To correct it, add Index to parentId:
  2103. Cards._collection._ensureIndex({ parentId: 1 });
  2104. // let notifydays = parseInt(process.env.NOTIFY_DUE_DAYS_BEFORE_AND_AFTER) || 2; // default as 2 days b4 and after
  2105. // let notifyitvl = parseInt(process.env.NOTIFY_DUE_AT_HOUR_OF_DAY) || 3600 * 24 * 1e3; // default interval as one day
  2106. // Meteor.call("findDueCards",notifydays,notifyitvl);
  2107. Meteor.defer(() => {
  2108. addCronJob();
  2109. });
  2110. });
  2111. Cards.after.insert((userId, doc) => {
  2112. cardCreation(userId, doc);
  2113. });
  2114. // New activity for card (un)archivage
  2115. Cards.after.update((userId, doc, fieldNames) => {
  2116. cardState(userId, doc, fieldNames);
  2117. });
  2118. //New activity for card moves
  2119. Cards.after.update(function(userId, doc, fieldNames) {
  2120. const oldListId = this.previous.listId;
  2121. const oldSwimlaneId = this.previous.swimlaneId;
  2122. const oldBoardId = this.previous.boardId;
  2123. cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId, oldBoardId);
  2124. });
  2125. // Add a new activity if we add or remove a member to the card
  2126. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2127. cardMembers(userId, doc, fieldNames, modifier);
  2128. updateActivities(doc, fieldNames, modifier);
  2129. });
  2130. // Add a new activity if we add or remove a assignee to the card
  2131. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2132. cardAssignees(userId, doc, fieldNames, modifier);
  2133. updateActivities(doc, fieldNames, modifier);
  2134. });
  2135. // Add a new activity if we add or remove a label to the card
  2136. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2137. cardLabels(userId, doc, fieldNames, modifier);
  2138. });
  2139. // Add a new activity if we edit a custom field
  2140. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2141. cardCustomFields(userId, doc, fieldNames, modifier);
  2142. });
  2143. // Add a new activity if modify time related field like dueAt startAt etc
  2144. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2145. const dla = 'dateLastActivity';
  2146. const fields = fieldNames.filter(name => name !== dla);
  2147. const timingaction = ['receivedAt', 'dueAt', 'startAt', 'endAt'];
  2148. const action = fields[0];
  2149. if (fields.length > 0 && _.contains(timingaction, action)) {
  2150. // add activities for user change these attributes
  2151. const value = modifier.$set[action];
  2152. const oldvalue = doc[action] || '';
  2153. const activityType = `a-${action}`;
  2154. const card = Cards.findOne(doc._id);
  2155. const list = card.list();
  2156. if (list) {
  2157. // change list modifiedAt, when user modified the key values in timingaction array, if it's endAt, put the modifiedAt of list back to one year ago for sorting purpose
  2158. const modifiedAt = new Date(
  2159. new Date(value).getTime() -
  2160. (action === 'endAt' ? 365 * 24 * 3600 * 1e3 : 0),
  2161. ); // set it as 1 year before
  2162. const boardId = list.boardId;
  2163. Lists.direct.update(
  2164. {
  2165. _id: list._id,
  2166. },
  2167. {
  2168. $set: {
  2169. modifiedAt,
  2170. boardId,
  2171. },
  2172. },
  2173. );
  2174. }
  2175. const username = Users.findOne(userId).username;
  2176. const activity = {
  2177. userId,
  2178. username,
  2179. activityType,
  2180. boardId: doc.boardId,
  2181. cardId: doc._id,
  2182. cardTitle: doc.title,
  2183. timeKey: action,
  2184. timeValue: value,
  2185. timeOldValue: oldvalue,
  2186. listId: card.listId,
  2187. swimlaneId: card.swimlaneId,
  2188. };
  2189. Activities.insert(activity);
  2190. }
  2191. });
  2192. // Remove all activities associated with a card if we remove the card
  2193. // Remove also card_comments / checklists / attachments
  2194. Cards.before.remove((userId, doc) => {
  2195. cardRemover(userId, doc);
  2196. });
  2197. }
  2198. //SWIMLANES REST API
  2199. if (Meteor.isServer) {
  2200. /**
  2201. * @operation get_swimlane_cards
  2202. * @summary get all cards attached to a swimlane
  2203. *
  2204. * @param {string} boardId the board ID
  2205. * @param {string} swimlaneId the swimlane ID
  2206. * @return_type [{_id: string,
  2207. * title: string,
  2208. * description: string,
  2209. * listId: string}]
  2210. */
  2211. JsonRoutes.add(
  2212. 'GET',
  2213. '/api/boards/:boardId/swimlanes/:swimlaneId/cards',
  2214. function(req, res) {
  2215. const paramBoardId = req.params.boardId;
  2216. const paramSwimlaneId = req.params.swimlaneId;
  2217. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2218. JsonRoutes.sendResult(res, {
  2219. code: 200,
  2220. data: Cards.find({
  2221. boardId: paramBoardId,
  2222. swimlaneId: paramSwimlaneId,
  2223. archived: false,
  2224. }).map(function(doc) {
  2225. return {
  2226. _id: doc._id,
  2227. title: doc.title,
  2228. description: doc.description,
  2229. listId: doc.listId,
  2230. receivedAt: doc.receivedAt,
  2231. startAt: doc.startAt,
  2232. dueAt: doc.dueAt,
  2233. endAt: doc.endAt,
  2234. assignees: doc.assignees,
  2235. };
  2236. }),
  2237. });
  2238. },
  2239. );
  2240. }
  2241. //LISTS REST API
  2242. if (Meteor.isServer) {
  2243. /**
  2244. * @operation get_all_cards
  2245. * @summary Get all Cards attached to a List
  2246. *
  2247. * @param {string} boardId the board ID
  2248. * @param {string} listId the list ID
  2249. * @return_type [{_id: string,
  2250. * title: string,
  2251. * description: string}]
  2252. */
  2253. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function(
  2254. req,
  2255. res,
  2256. ) {
  2257. const paramBoardId = req.params.boardId;
  2258. const paramListId = req.params.listId;
  2259. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2260. JsonRoutes.sendResult(res, {
  2261. code: 200,
  2262. data: Cards.find({
  2263. boardId: paramBoardId,
  2264. listId: paramListId,
  2265. archived: false,
  2266. }).map(function(doc) {
  2267. return {
  2268. _id: doc._id,
  2269. title: doc.title,
  2270. description: doc.description,
  2271. receivedAt: doc.receivedAt,
  2272. startAt: doc.startAt,
  2273. dueAt: doc.dueAt,
  2274. endAt: doc.endAt,
  2275. assignees: doc.assignees,
  2276. };
  2277. }),
  2278. });
  2279. });
  2280. /**
  2281. * @operation get_card
  2282. * @summary Get a Card
  2283. *
  2284. * @param {string} boardId the board ID
  2285. * @param {string} listId the list ID of the card
  2286. * @param {string} cardId the card ID
  2287. * @return_type Cards
  2288. */
  2289. JsonRoutes.add(
  2290. 'GET',
  2291. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  2292. function(req, res) {
  2293. const paramBoardId = req.params.boardId;
  2294. const paramListId = req.params.listId;
  2295. const paramCardId = req.params.cardId;
  2296. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2297. JsonRoutes.sendResult(res, {
  2298. code: 200,
  2299. data: Cards.findOne({
  2300. _id: paramCardId,
  2301. listId: paramListId,
  2302. boardId: paramBoardId,
  2303. archived: false,
  2304. }),
  2305. });
  2306. },
  2307. );
  2308. /**
  2309. * @operation new_card
  2310. * @summary Create a new Card
  2311. *
  2312. * @param {string} boardId the board ID of the new card
  2313. * @param {string} listId the list ID of the new card
  2314. * @param {string} authorID the user ID of the person owning the card
  2315. * @param {string} parentId the parent ID of the new card
  2316. * @param {string} title the title of the new card
  2317. * @param {string} description the description of the new card
  2318. * @param {string} swimlaneId the swimlane ID of the new card
  2319. * @param {string} [members] the member IDs list of the new card
  2320. * @param {string} [assignees] the array of maximum one ID of assignee of the new card
  2321. * @return_type {_id: string}
  2322. */
  2323. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function(
  2324. req,
  2325. res,
  2326. ) {
  2327. // Check user is logged in
  2328. Authentication.checkLoggedIn(req.userId);
  2329. const paramBoardId = req.params.boardId;
  2330. // Check user has permission to add card to the board
  2331. const board = Boards.findOne({
  2332. _id: paramBoardId,
  2333. });
  2334. const addPermission = allowIsBoardMemberCommentOnly(req.userId, board);
  2335. Authentication.checkAdminOrCondition(req.userId, addPermission);
  2336. const paramListId = req.params.listId;
  2337. const paramParentId = req.params.parentId;
  2338. const currentCards = Cards.find(
  2339. {
  2340. listId: paramListId,
  2341. archived: false,
  2342. },
  2343. { sort: ['sort'] },
  2344. );
  2345. const check = Users.findOne({
  2346. _id: req.body.authorId,
  2347. });
  2348. const members = req.body.members;
  2349. const assignees = req.body.assignees;
  2350. if (typeof check !== 'undefined') {
  2351. const id = Cards.direct.insert({
  2352. title: req.body.title,
  2353. boardId: paramBoardId,
  2354. listId: paramListId,
  2355. parentId: paramParentId,
  2356. description: req.body.description,
  2357. userId: req.body.authorId,
  2358. swimlaneId: req.body.swimlaneId,
  2359. sort: currentCards.count(),
  2360. members,
  2361. assignees,
  2362. });
  2363. JsonRoutes.sendResult(res, {
  2364. code: 200,
  2365. data: {
  2366. _id: id,
  2367. },
  2368. });
  2369. const card = Cards.findOne({
  2370. _id: id,
  2371. });
  2372. cardCreation(req.body.authorId, card);
  2373. } else {
  2374. JsonRoutes.sendResult(res, {
  2375. code: 401,
  2376. });
  2377. }
  2378. });
  2379. /*
  2380. * Note for the JSDoc:
  2381. * 'list' will be interpreted as the path parameter
  2382. * 'listID' will be interpreted as the body parameter
  2383. */
  2384. /**
  2385. * @operation edit_card
  2386. * @summary Edit Fields in a Card
  2387. *
  2388. * @description Edit a card
  2389. *
  2390. * The color has to be chosen between `white`, `green`, `yellow`, `orange`,
  2391. * `red`, `purple`, `blue`, `sky`, `lime`, `pink`, `black`, `silver`,
  2392. * `peachpuff`, `crimson`, `plum`, `darkgreen`, `slateblue`, `magenta`,
  2393. * `gold`, `navy`, `gray`, `saddlebrown`, `paleturquoise`, `mistyrose`,
  2394. * `indigo`:
  2395. *
  2396. * <img src="/card-colors.png" width="40%" alt="Wekan card colors" />
  2397. *
  2398. * Note: setting the color to white has the same effect than removing it.
  2399. *
  2400. * @param {string} boardId the board ID of the card
  2401. * @param {string} list the list ID of the card
  2402. * @param {string} cardId the ID of the card
  2403. * @param {string} [title] the new title of the card
  2404. * @param {string} [sort] the new sort value of the card
  2405. * @param {string} [listId] the new list ID of the card (move operation)
  2406. * @param {string} [description] the new description of the card
  2407. * @param {string} [authorId] change the owner of the card
  2408. * @param {string} [parentId] change the parent of the card
  2409. * @param {string} [labelIds] the new list of label IDs attached to the card
  2410. * @param {string} [swimlaneId] the new swimlane ID of the card
  2411. * @param {string} [members] the new list of member IDs attached to the card
  2412. * @param {string} [assignees] the array of maximum one ID of assignee attached to the card
  2413. * @param {string} [requestedBy] the new requestedBy field of the card
  2414. * @param {string} [assignedBy] the new assignedBy field of the card
  2415. * @param {string} [receivedAt] the new receivedAt field of the card
  2416. * @param {string} [assignBy] the new assignBy field of the card
  2417. * @param {string} [startAt] the new startAt field of the card
  2418. * @param {string} [dueAt] the new dueAt field of the card
  2419. * @param {string} [endAt] the new endAt field of the card
  2420. * @param {string} [spentTime] the new spentTime field of the card
  2421. * @param {boolean} [isOverTime] the new isOverTime field of the card
  2422. * @param {string} [customFields] the new customFields value of the card
  2423. * @param {string} [color] the new color of the card
  2424. * @param {Object} [vote] the vote object
  2425. * @param {string} vote.question the vote question
  2426. * @param {boolean} vote.public show who voted what
  2427. * @param {boolean} vote.allowNonBoardMembers allow all logged in users to vote?
  2428. * @return_type {_id: string}
  2429. */
  2430. JsonRoutes.add(
  2431. 'PUT',
  2432. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  2433. function(req, res) {
  2434. Authentication.checkUserId(req.userId);
  2435. const paramBoardId = req.params.boardId;
  2436. const paramCardId = req.params.cardId;
  2437. const paramListId = req.params.listId;
  2438. if (req.body.hasOwnProperty('title')) {
  2439. const newTitle = req.body.title;
  2440. Cards.direct.update(
  2441. {
  2442. _id: paramCardId,
  2443. listId: paramListId,
  2444. boardId: paramBoardId,
  2445. archived: false,
  2446. },
  2447. {
  2448. $set: {
  2449. title: newTitle,
  2450. },
  2451. },
  2452. );
  2453. }
  2454. if (req.body.hasOwnProperty('sort')) {
  2455. const newSort = req.body.sort;
  2456. Cards.direct.update(
  2457. {
  2458. _id: paramCardId,
  2459. listId: paramListId,
  2460. boardId: paramBoardId,
  2461. archived: false,
  2462. },
  2463. {
  2464. $set: {
  2465. sort: newSort,
  2466. },
  2467. },
  2468. );
  2469. }
  2470. if (req.body.hasOwnProperty('parentId')) {
  2471. const newParentId = req.body.parentId;
  2472. Cards.direct.update(
  2473. {
  2474. _id: paramCardId,
  2475. listId: paramListId,
  2476. boardId: paramBoardId,
  2477. archived: false,
  2478. },
  2479. {
  2480. $set: {
  2481. parentId: newParentId,
  2482. },
  2483. },
  2484. );
  2485. }
  2486. if (req.body.hasOwnProperty('description')) {
  2487. const newDescription = req.body.description;
  2488. Cards.direct.update(
  2489. {
  2490. _id: paramCardId,
  2491. listId: paramListId,
  2492. boardId: paramBoardId,
  2493. archived: false,
  2494. },
  2495. {
  2496. $set: {
  2497. description: newDescription,
  2498. },
  2499. },
  2500. );
  2501. }
  2502. if (req.body.hasOwnProperty('color')) {
  2503. const newColor = req.body.color;
  2504. Cards.direct.update(
  2505. {
  2506. _id: paramCardId,
  2507. listId: paramListId,
  2508. boardId: paramBoardId,
  2509. archived: false,
  2510. },
  2511. { $set: { color: newColor } },
  2512. );
  2513. }
  2514. if (req.body.hasOwnProperty('vote')) {
  2515. const newVote = req.body.vote;
  2516. newVote.positive = [];
  2517. newVote.negative = [];
  2518. if (!newVote.hasOwnProperty('public')) newVote.public = false;
  2519. if (!newVote.hasOwnProperty('allowNonBoardMembers'))
  2520. newVote.allowNonBoardMembers = false;
  2521. Cards.direct.update(
  2522. {
  2523. _id: paramCardId,
  2524. listId: paramListId,
  2525. boardId: paramBoardId,
  2526. archived: false,
  2527. },
  2528. { $set: { vote: newVote } },
  2529. );
  2530. }
  2531. if (req.body.hasOwnProperty('labelIds')) {
  2532. let newlabelIds = req.body.labelIds;
  2533. if (_.isString(newlabelIds)) {
  2534. if (newlabelIds === '') {
  2535. newlabelIds = null;
  2536. } else {
  2537. newlabelIds = [newlabelIds];
  2538. }
  2539. }
  2540. Cards.direct.update(
  2541. {
  2542. _id: paramCardId,
  2543. listId: paramListId,
  2544. boardId: paramBoardId,
  2545. archived: false,
  2546. },
  2547. {
  2548. $set: {
  2549. labelIds: newlabelIds,
  2550. },
  2551. },
  2552. );
  2553. }
  2554. if (req.body.hasOwnProperty('requestedBy')) {
  2555. const newrequestedBy = req.body.requestedBy;
  2556. Cards.direct.update(
  2557. {
  2558. _id: paramCardId,
  2559. listId: paramListId,
  2560. boardId: paramBoardId,
  2561. archived: false,
  2562. },
  2563. { $set: { requestedBy: newrequestedBy } },
  2564. );
  2565. }
  2566. if (req.body.hasOwnProperty('assignedBy')) {
  2567. const newassignedBy = req.body.assignedBy;
  2568. Cards.direct.update(
  2569. {
  2570. _id: paramCardId,
  2571. listId: paramListId,
  2572. boardId: paramBoardId,
  2573. archived: false,
  2574. },
  2575. { $set: { assignedBy: newassignedBy } },
  2576. );
  2577. }
  2578. if (req.body.hasOwnProperty('receivedAt')) {
  2579. const newreceivedAt = req.body.receivedAt;
  2580. Cards.direct.update(
  2581. {
  2582. _id: paramCardId,
  2583. listId: paramListId,
  2584. boardId: paramBoardId,
  2585. archived: false,
  2586. },
  2587. { $set: { receivedAt: newreceivedAt } },
  2588. );
  2589. }
  2590. if (req.body.hasOwnProperty('startAt')) {
  2591. const newstartAt = req.body.startAt;
  2592. Cards.direct.update(
  2593. {
  2594. _id: paramCardId,
  2595. listId: paramListId,
  2596. boardId: paramBoardId,
  2597. archived: false,
  2598. },
  2599. { $set: { startAt: newstartAt } },
  2600. );
  2601. }
  2602. if (req.body.hasOwnProperty('dueAt')) {
  2603. const newdueAt = req.body.dueAt;
  2604. Cards.direct.update(
  2605. {
  2606. _id: paramCardId,
  2607. listId: paramListId,
  2608. boardId: paramBoardId,
  2609. archived: false,
  2610. },
  2611. { $set: { dueAt: newdueAt } },
  2612. );
  2613. }
  2614. if (req.body.hasOwnProperty('endAt')) {
  2615. const newendAt = req.body.endAt;
  2616. Cards.direct.update(
  2617. {
  2618. _id: paramCardId,
  2619. listId: paramListId,
  2620. boardId: paramBoardId,
  2621. archived: false,
  2622. },
  2623. { $set: { endAt: newendAt } },
  2624. );
  2625. }
  2626. if (req.body.hasOwnProperty('spentTime')) {
  2627. const newspentTime = req.body.spentTime;
  2628. Cards.direct.update(
  2629. {
  2630. _id: paramCardId,
  2631. listId: paramListId,
  2632. boardId: paramBoardId,
  2633. archived: false,
  2634. },
  2635. { $set: { spentTime: newspentTime } },
  2636. );
  2637. }
  2638. if (req.body.hasOwnProperty('isOverTime')) {
  2639. const newisOverTime = req.body.isOverTime;
  2640. Cards.direct.update(
  2641. {
  2642. _id: paramCardId,
  2643. listId: paramListId,
  2644. boardId: paramBoardId,
  2645. archived: false,
  2646. },
  2647. { $set: { isOverTime: newisOverTime } },
  2648. );
  2649. }
  2650. if (req.body.hasOwnProperty('customFields')) {
  2651. const newcustomFields = req.body.customFields;
  2652. Cards.direct.update(
  2653. {
  2654. _id: paramCardId,
  2655. listId: paramListId,
  2656. boardId: paramBoardId,
  2657. archived: false,
  2658. },
  2659. { $set: { customFields: newcustomFields } },
  2660. );
  2661. }
  2662. if (req.body.hasOwnProperty('members')) {
  2663. let newmembers = req.body.members;
  2664. if (_.isString(newmembers)) {
  2665. if (newmembers === '') {
  2666. newmembers = null;
  2667. } else {
  2668. newmembers = [newmembers];
  2669. }
  2670. }
  2671. Cards.direct.update(
  2672. {
  2673. _id: paramCardId,
  2674. listId: paramListId,
  2675. boardId: paramBoardId,
  2676. archived: false,
  2677. },
  2678. { $set: { members: newmembers } },
  2679. );
  2680. }
  2681. if (req.body.hasOwnProperty('assignees')) {
  2682. let newassignees = req.body.assignees;
  2683. if (_.isString(newassignees)) {
  2684. if (newassignees === '') {
  2685. newassignees = null;
  2686. } else {
  2687. newassignees = [newassignees];
  2688. }
  2689. }
  2690. Cards.direct.update(
  2691. {
  2692. _id: paramCardId,
  2693. listId: paramListId,
  2694. boardId: paramBoardId,
  2695. archived: false,
  2696. },
  2697. { $set: { assignees: newassignees } },
  2698. );
  2699. }
  2700. if (req.body.hasOwnProperty('swimlaneId')) {
  2701. const newParamSwimlaneId = req.body.swimlaneId;
  2702. Cards.direct.update(
  2703. {
  2704. _id: paramCardId,
  2705. listId: paramListId,
  2706. boardId: paramBoardId,
  2707. archived: false,
  2708. },
  2709. { $set: { swimlaneId: newParamSwimlaneId } },
  2710. );
  2711. }
  2712. if (req.body.hasOwnProperty('listId')) {
  2713. const newParamListId = req.body.listId;
  2714. Cards.direct.update(
  2715. {
  2716. _id: paramCardId,
  2717. listId: paramListId,
  2718. boardId: paramBoardId,
  2719. archived: false,
  2720. },
  2721. {
  2722. $set: {
  2723. listId: newParamListId,
  2724. },
  2725. },
  2726. );
  2727. const card = Cards.findOne({
  2728. _id: paramCardId,
  2729. });
  2730. cardMove(
  2731. req.body.authorId,
  2732. card,
  2733. {
  2734. fieldName: 'listId',
  2735. },
  2736. paramListId,
  2737. );
  2738. }
  2739. JsonRoutes.sendResult(res, {
  2740. code: 200,
  2741. data: {
  2742. _id: paramCardId,
  2743. },
  2744. });
  2745. },
  2746. );
  2747. /**
  2748. * @operation delete_card
  2749. * @summary Delete a card from a board
  2750. *
  2751. * @description This operation **deletes** a card, and therefore the card
  2752. * is not put in the recycle bin.
  2753. *
  2754. * @param {string} boardId the board ID of the card
  2755. * @param {string} list the list ID of the card
  2756. * @param {string} cardId the ID of the card
  2757. * @return_type {_id: string}
  2758. */
  2759. JsonRoutes.add(
  2760. 'DELETE',
  2761. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  2762. function(req, res) {
  2763. Authentication.checkUserId(req.userId);
  2764. const paramBoardId = req.params.boardId;
  2765. const paramListId = req.params.listId;
  2766. const paramCardId = req.params.cardId;
  2767. const card = Cards.findOne({
  2768. _id: paramCardId,
  2769. });
  2770. Cards.direct.remove({
  2771. _id: paramCardId,
  2772. listId: paramListId,
  2773. boardId: paramBoardId,
  2774. });
  2775. cardRemover(req.body.authorId, card);
  2776. JsonRoutes.sendResult(res, {
  2777. code: 200,
  2778. data: {
  2779. _id: paramCardId,
  2780. },
  2781. });
  2782. },
  2783. );
  2784. /**
  2785. * @operation get_cards_by_custom_field
  2786. * @summary Get all Cards that matchs a value of a specific custom field
  2787. *
  2788. * @param {string} boardId the board ID
  2789. * @param {string} customFieldId the list ID
  2790. * @param {string} customFieldValue the value to look for
  2791. * @return_type [{_id: string,
  2792. * title: string,
  2793. * description: string,
  2794. * listId: string,
  2795. * swinlaneId: string}]
  2796. */
  2797. JsonRoutes.add(
  2798. 'GET',
  2799. '/api/boards/:boardId/cardsByCustomField/:customFieldId/:customFieldValue',
  2800. function(req, res) {
  2801. const paramBoardId = req.params.boardId;
  2802. const paramCustomFieldId = req.params.customFieldId;
  2803. const paramCustomFieldValue = req.params.customFieldValue;
  2804. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2805. JsonRoutes.sendResult(res, {
  2806. code: 200,
  2807. data: Cards.find({
  2808. boardId: paramBoardId,
  2809. customFields: {
  2810. $elemMatch: {
  2811. _id: paramCustomFieldId,
  2812. value: paramCustomFieldValue,
  2813. },
  2814. },
  2815. archived: false,
  2816. }).fetch(),
  2817. });
  2818. },
  2819. );
  2820. }
  2821. export default Cards;