cards.js 73 KB

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