cards.js 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011
  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. originRelativeUrl() {
  716. const board = this.board();
  717. return FlowRouter.path('card', {
  718. boardId: board._id,
  719. slug: board.slug,
  720. cardId: this._id,
  721. });
  722. },
  723. canBeRestored() {
  724. const list = Lists.findOne({
  725. _id: this.listId,
  726. });
  727. if (
  728. !list.getWipLimit('soft') &&
  729. list.getWipLimit('enabled') &&
  730. list.getWipLimit('value') === list.cards().count()
  731. ) {
  732. return false;
  733. }
  734. return true;
  735. },
  736. parentCard() {
  737. if (this.parentId === '') {
  738. return null;
  739. }
  740. return Cards.findOne(this.parentId);
  741. },
  742. parentCardName() {
  743. let result = '';
  744. if (this.parentId !== '') {
  745. const card = Cards.findOne(this.parentId);
  746. if (card) {
  747. result = card.title;
  748. }
  749. }
  750. return result;
  751. },
  752. parentListId() {
  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 result) {
  762. // circular reference
  763. break;
  764. }
  765. result.unshift(crtParentId);
  766. crtParentId = crt.parentId;
  767. }
  768. return result;
  769. },
  770. parentList() {
  771. const resultId = [];
  772. const result = [];
  773. let crtParentId = this.parentId;
  774. while (crtParentId !== '') {
  775. const crt = Cards.findOne(crtParentId);
  776. if (crt === null || crt === undefined) {
  777. // maybe it has been deleted
  778. break;
  779. }
  780. if (crtParentId in resultId) {
  781. // circular reference
  782. break;
  783. }
  784. resultId.unshift(crtParentId);
  785. result.unshift(crt);
  786. crtParentId = crt.parentId;
  787. }
  788. return result;
  789. },
  790. parentString(sep) {
  791. return this.parentList()
  792. .map(function(elem) {
  793. return elem.title;
  794. })
  795. .join(sep);
  796. },
  797. isTopLevel() {
  798. return this.parentId === '';
  799. },
  800. isLinkedCard() {
  801. return this.type === 'cardType-linkedCard';
  802. },
  803. isLinkedBoard() {
  804. return this.type === 'cardType-linkedBoard';
  805. },
  806. isLinked() {
  807. return this.isLinkedCard() || this.isLinkedBoard();
  808. },
  809. setDescription(description) {
  810. if (this.isLinkedCard()) {
  811. return Cards.update({ _id: this.linkedId }, { $set: { description } });
  812. } else if (this.isLinkedBoard()) {
  813. return Boards.update({ _id: this.linkedId }, { $set: { description } });
  814. } else {
  815. return Cards.update({ _id: this._id }, { $set: { description } });
  816. }
  817. },
  818. getDescription() {
  819. if (this.isLinkedCard()) {
  820. const card = Cards.findOne({ _id: this.linkedId });
  821. if (card && card.description) return card.description;
  822. else return null;
  823. } else if (this.isLinkedBoard()) {
  824. const board = Boards.findOne({ _id: this.linkedId });
  825. if (board && board.description) return board.description;
  826. else return null;
  827. } else if (this.description) {
  828. return this.description;
  829. } else {
  830. return null;
  831. }
  832. },
  833. getMembers() {
  834. if (this.isLinkedCard()) {
  835. const card = Cards.findOne({ _id: this.linkedId });
  836. if (card === undefined) {
  837. return null;
  838. } else {
  839. return card.members;
  840. }
  841. } else if (this.isLinkedBoard()) {
  842. const board = Boards.findOne({ _id: this.linkedId });
  843. if (board === undefined) {
  844. return null;
  845. } else {
  846. return board.activeMembers().map(member => {
  847. return member.userId;
  848. });
  849. }
  850. } else {
  851. return this.members;
  852. }
  853. },
  854. getAssignees() {
  855. if (this.isLinkedCard()) {
  856. const card = Cards.findOne({ _id: this.linkedId });
  857. if (card === undefined) {
  858. return null;
  859. } else {
  860. return card.assignees;
  861. }
  862. } else if (this.isLinkedBoard()) {
  863. const board = Boards.findOne({ _id: this.linkedId });
  864. if (board === undefined) {
  865. return null;
  866. } else {
  867. return board.activeMembers().map(assignee => {
  868. return assignee.userId;
  869. });
  870. }
  871. } else {
  872. return this.assignees;
  873. }
  874. },
  875. assignMember(memberId) {
  876. if (this.isLinkedCard()) {
  877. return Cards.update(
  878. { _id: this.linkedId },
  879. { $addToSet: { members: memberId } },
  880. );
  881. } else if (this.isLinkedBoard()) {
  882. const board = Boards.findOne({ _id: this.linkedId });
  883. return board.addMember(memberId);
  884. } else {
  885. return Cards.update(
  886. { _id: this._id },
  887. { $addToSet: { members: memberId } },
  888. );
  889. }
  890. },
  891. assignAssignee(assigneeId) {
  892. if (this.isLinkedCard()) {
  893. return Cards.update(
  894. { _id: this.linkedId },
  895. { $addToSet: { assignees: assigneeId } },
  896. );
  897. } else if (this.isLinkedBoard()) {
  898. const board = Boards.findOne({ _id: this.linkedId });
  899. return board.addAssignee(assigneeId);
  900. } else {
  901. return Cards.update(
  902. { _id: this._id },
  903. { $addToSet: { assignees: assigneeId } },
  904. );
  905. }
  906. },
  907. unassignMember(memberId) {
  908. if (this.isLinkedCard()) {
  909. return Cards.update(
  910. { _id: this.linkedId },
  911. { $pull: { members: memberId } },
  912. );
  913. } else if (this.isLinkedBoard()) {
  914. const board = Boards.findOne({ _id: this.linkedId });
  915. return board.removeMember(memberId);
  916. } else {
  917. return Cards.update({ _id: this._id }, { $pull: { members: memberId } });
  918. }
  919. },
  920. unassignAssignee(assigneeId) {
  921. if (this.isLinkedCard()) {
  922. return Cards.update(
  923. { _id: this.linkedId },
  924. { $pull: { assignees: assigneeId } },
  925. );
  926. } else if (this.isLinkedBoard()) {
  927. const board = Boards.findOne({ _id: this.linkedId });
  928. return board.removeAssignee(assigneeId);
  929. } else {
  930. return Cards.update(
  931. { _id: this._id },
  932. { $pull: { assignees: assigneeId } },
  933. );
  934. }
  935. },
  936. toggleMember(memberId) {
  937. if (this.getMembers() && this.getMembers().indexOf(memberId) > -1) {
  938. return this.unassignMember(memberId);
  939. } else {
  940. return this.assignMember(memberId);
  941. }
  942. },
  943. toggleAssignee(assigneeId) {
  944. if (this.getAssignees() && this.getAssignees().indexOf(assigneeId) > -1) {
  945. return this.unassignAssignee(assigneeId);
  946. } else {
  947. return this.assignAssignee(assigneeId);
  948. }
  949. },
  950. getReceived() {
  951. if (this.isLinkedCard()) {
  952. const card = Cards.findOne({ _id: this.linkedId });
  953. if (card === undefined) {
  954. return null;
  955. } else {
  956. return card.receivedAt;
  957. }
  958. } else {
  959. return this.receivedAt;
  960. }
  961. },
  962. setReceived(receivedAt) {
  963. if (this.isLinkedCard()) {
  964. return Cards.update({ _id: this.linkedId }, { $set: { receivedAt } });
  965. } else {
  966. return Cards.update({ _id: this._id }, { $set: { receivedAt } });
  967. }
  968. },
  969. getStart() {
  970. if (this.isLinkedCard()) {
  971. const card = Cards.findOne({ _id: this.linkedId });
  972. if (card === undefined) {
  973. return null;
  974. } else {
  975. return card.startAt;
  976. }
  977. } else if (this.isLinkedBoard()) {
  978. const board = Boards.findOne({ _id: this.linkedId });
  979. if (board === undefined) {
  980. return null;
  981. } else {
  982. return board.startAt;
  983. }
  984. } else {
  985. return this.startAt;
  986. }
  987. },
  988. setStart(startAt) {
  989. if (this.isLinkedCard()) {
  990. return Cards.update({ _id: this.linkedId }, { $set: { startAt } });
  991. } else if (this.isLinkedBoard()) {
  992. return Boards.update({ _id: this.linkedId }, { $set: { startAt } });
  993. } else {
  994. return Cards.update({ _id: this._id }, { $set: { startAt } });
  995. }
  996. },
  997. getDue() {
  998. if (this.isLinkedCard()) {
  999. const card = Cards.findOne({ _id: this.linkedId });
  1000. if (card === undefined) {
  1001. return null;
  1002. } else {
  1003. return card.dueAt;
  1004. }
  1005. } else if (this.isLinkedBoard()) {
  1006. const board = Boards.findOne({ _id: this.linkedId });
  1007. if (board === undefined) {
  1008. return null;
  1009. } else {
  1010. return board.dueAt;
  1011. }
  1012. } else {
  1013. return this.dueAt;
  1014. }
  1015. },
  1016. setDue(dueAt) {
  1017. if (this.isLinkedCard()) {
  1018. return Cards.update({ _id: this.linkedId }, { $set: { dueAt } });
  1019. } else if (this.isLinkedBoard()) {
  1020. return Boards.update({ _id: this.linkedId }, { $set: { dueAt } });
  1021. } else {
  1022. return Cards.update({ _id: this._id }, { $set: { dueAt } });
  1023. }
  1024. },
  1025. getEnd() {
  1026. if (this.isLinkedCard()) {
  1027. const card = Cards.findOne({ _id: this.linkedId });
  1028. if (card === undefined) {
  1029. return null;
  1030. } else {
  1031. return card.endAt;
  1032. }
  1033. } else if (this.isLinkedBoard()) {
  1034. const board = Boards.findOne({ _id: this.linkedId });
  1035. if (board === undefined) {
  1036. return null;
  1037. } else {
  1038. return board.endAt;
  1039. }
  1040. } else {
  1041. return this.endAt;
  1042. }
  1043. },
  1044. setEnd(endAt) {
  1045. if (this.isLinkedCard()) {
  1046. return Cards.update({ _id: this.linkedId }, { $set: { endAt } });
  1047. } else if (this.isLinkedBoard()) {
  1048. return Boards.update({ _id: this.linkedId }, { $set: { endAt } });
  1049. } else {
  1050. return Cards.update({ _id: this._id }, { $set: { endAt } });
  1051. }
  1052. },
  1053. getIsOvertime() {
  1054. if (this.isLinkedCard()) {
  1055. const card = Cards.findOne({ _id: this.linkedId });
  1056. if (card === undefined) {
  1057. return null;
  1058. } else {
  1059. return card.isOvertime;
  1060. }
  1061. } else if (this.isLinkedBoard()) {
  1062. const board = Boards.findOne({ _id: this.linkedId });
  1063. if (board === undefined) {
  1064. return null;
  1065. } else {
  1066. return board.isOvertime;
  1067. }
  1068. } else {
  1069. return this.isOvertime;
  1070. }
  1071. },
  1072. setIsOvertime(isOvertime) {
  1073. if (this.isLinkedCard()) {
  1074. return Cards.update({ _id: this.linkedId }, { $set: { isOvertime } });
  1075. } else if (this.isLinkedBoard()) {
  1076. return Boards.update({ _id: this.linkedId }, { $set: { isOvertime } });
  1077. } else {
  1078. return Cards.update({ _id: this._id }, { $set: { isOvertime } });
  1079. }
  1080. },
  1081. getSpentTime() {
  1082. if (this.isLinkedCard()) {
  1083. const card = Cards.findOne({ _id: this.linkedId });
  1084. if (card === undefined) {
  1085. return null;
  1086. } else {
  1087. return card.spentTime;
  1088. }
  1089. } else if (this.isLinkedBoard()) {
  1090. const board = Boards.findOne({ _id: this.linkedId });
  1091. if (board === undefined) {
  1092. return null;
  1093. } else {
  1094. return board.spentTime;
  1095. }
  1096. } else {
  1097. return this.spentTime;
  1098. }
  1099. },
  1100. setSpentTime(spentTime) {
  1101. if (this.isLinkedCard()) {
  1102. return Cards.update({ _id: this.linkedId }, { $set: { spentTime } });
  1103. } else if (this.isLinkedBoard()) {
  1104. return Boards.update({ _id: this.linkedId }, { $set: { spentTime } });
  1105. } else {
  1106. return Cards.update({ _id: this._id }, { $set: { spentTime } });
  1107. }
  1108. },
  1109. getVoteQuestion() {
  1110. if (this.isLinkedCard()) {
  1111. const card = Cards.findOne({ _id: this.linkedId });
  1112. if (card === undefined) {
  1113. return null;
  1114. } else if (card && card.vote) {
  1115. return card.vote.question;
  1116. } else {
  1117. return null;
  1118. }
  1119. } else if (this.isLinkedBoard()) {
  1120. const board = Boards.findOne({ _id: this.linkedId });
  1121. if (board === undefined) {
  1122. return null;
  1123. } else if (board && board.vote) {
  1124. return board.vote.question;
  1125. } else {
  1126. return null;
  1127. }
  1128. } else if (this.vote) {
  1129. return this.vote.question;
  1130. } else {
  1131. return null;
  1132. }
  1133. },
  1134. getVotePublic() {
  1135. if (this.isLinkedCard()) {
  1136. const card = Cards.findOne({ _id: this.linkedId });
  1137. if (card === undefined) {
  1138. return null;
  1139. } else if (card && card.vote) {
  1140. return card.vote.public;
  1141. } else {
  1142. return null;
  1143. }
  1144. } else if (this.isLinkedBoard()) {
  1145. const board = Boards.findOne({ _id: this.linkedId });
  1146. if (board === undefined) {
  1147. return null;
  1148. } else if (board && board.vote) {
  1149. return board.vote.public;
  1150. } else {
  1151. return null;
  1152. }
  1153. } else if (this.vote) {
  1154. return this.vote.public;
  1155. } else {
  1156. return null;
  1157. }
  1158. },
  1159. getVoteEnd() {
  1160. if (this.isLinkedCard()) {
  1161. const card = Cards.findOne({ _id: this.linkedId });
  1162. if (card === undefined) {
  1163. return null;
  1164. } else if (card && card.vote) {
  1165. return card.vote.end;
  1166. } else {
  1167. return null;
  1168. }
  1169. } else if (this.isLinkedBoard()) {
  1170. const board = Boards.findOne({ _id: this.linkedId });
  1171. if (board === undefined) {
  1172. return null;
  1173. } else if (board && board.vote) {
  1174. return board.vote.end;
  1175. } else {
  1176. return null;
  1177. }
  1178. } else if (this.vote) {
  1179. return this.vote.end;
  1180. } else {
  1181. return null;
  1182. }
  1183. },
  1184. expiredVote() {
  1185. let end = this.getVoteEnd();
  1186. if (end) {
  1187. end = moment(end);
  1188. return end.isBefore(new Date());
  1189. }
  1190. return false;
  1191. },
  1192. voteMemberPositive() {
  1193. if (this.vote && this.vote.positive)
  1194. return Users.find({ _id: { $in: this.vote.positive } });
  1195. return [];
  1196. },
  1197. voteMemberNegative() {
  1198. if (this.vote && this.vote.negative)
  1199. return Users.find({ _id: { $in: this.vote.negative } });
  1200. return [];
  1201. },
  1202. voteState() {
  1203. const userId = Meteor.userId();
  1204. let state;
  1205. if (this.vote) {
  1206. if (this.vote.positive) {
  1207. state = _.contains(this.vote.positive, userId);
  1208. if (state === true) return true;
  1209. }
  1210. if (this.vote.negative) {
  1211. state = _.contains(this.vote.negative, userId);
  1212. if (state === true) return false;
  1213. }
  1214. }
  1215. return null;
  1216. },
  1217. getId() {
  1218. if (this.isLinked()) {
  1219. return this.linkedId;
  1220. } else {
  1221. return this._id;
  1222. }
  1223. },
  1224. getTitle() {
  1225. if (this.isLinkedCard()) {
  1226. const card = Cards.findOne({ _id: this.linkedId });
  1227. if (card === undefined) {
  1228. return null;
  1229. } else {
  1230. return card.title;
  1231. }
  1232. } else if (this.isLinkedBoard()) {
  1233. const board = Boards.findOne({ _id: this.linkedId });
  1234. if (board === undefined) {
  1235. return null;
  1236. } else {
  1237. return board.title;
  1238. }
  1239. } else if (this.title === undefined) {
  1240. return null;
  1241. } else {
  1242. return this.title;
  1243. }
  1244. },
  1245. getBoardTitle() {
  1246. if (this.isLinkedCard()) {
  1247. const card = Cards.findOne({ _id: this.linkedId });
  1248. if (card === undefined) {
  1249. return null;
  1250. }
  1251. const board = Boards.findOne({ _id: card.boardId });
  1252. if (board === undefined) {
  1253. return null;
  1254. } else {
  1255. return board.title;
  1256. }
  1257. } else if (this.isLinkedBoard()) {
  1258. const board = Boards.findOne({ _id: this.linkedId });
  1259. if (board === undefined) {
  1260. return null;
  1261. } else {
  1262. return board.title;
  1263. }
  1264. } else {
  1265. const board = Boards.findOne({ _id: this.boardId });
  1266. if (board === undefined) {
  1267. return null;
  1268. } else {
  1269. return board.title;
  1270. }
  1271. }
  1272. },
  1273. setTitle(title) {
  1274. if (this.isLinkedCard()) {
  1275. return Cards.update({ _id: this.linkedId }, { $set: { title } });
  1276. } else if (this.isLinkedBoard()) {
  1277. return Boards.update({ _id: this.linkedId }, { $set: { title } });
  1278. } else {
  1279. return Cards.update({ _id: this._id }, { $set: { title } });
  1280. }
  1281. },
  1282. getArchived() {
  1283. if (this.isLinkedCard()) {
  1284. const card = Cards.findOne({ _id: this.linkedId });
  1285. if (card === undefined) {
  1286. return null;
  1287. } else {
  1288. return card.archived;
  1289. }
  1290. } else if (this.isLinkedBoard()) {
  1291. const board = Boards.findOne({ _id: this.linkedId });
  1292. if (board === undefined) {
  1293. return null;
  1294. } else {
  1295. return board.archived;
  1296. }
  1297. } else {
  1298. return this.archived;
  1299. }
  1300. },
  1301. setRequestedBy(requestedBy) {
  1302. if (this.isLinkedCard()) {
  1303. return Cards.update({ _id: this.linkedId }, { $set: { requestedBy } });
  1304. } else {
  1305. return Cards.update({ _id: this._id }, { $set: { requestedBy } });
  1306. }
  1307. },
  1308. getRequestedBy() {
  1309. if (this.isLinkedCard()) {
  1310. const card = Cards.findOne({ _id: this.linkedId });
  1311. if (card === undefined) {
  1312. return null;
  1313. } else {
  1314. return card.requestedBy;
  1315. }
  1316. } else {
  1317. return this.requestedBy;
  1318. }
  1319. },
  1320. setAssignedBy(assignedBy) {
  1321. if (this.isLinkedCard()) {
  1322. return Cards.update({ _id: this.linkedId }, { $set: { assignedBy } });
  1323. } else {
  1324. return Cards.update({ _id: this._id }, { $set: { assignedBy } });
  1325. }
  1326. },
  1327. getAssignedBy() {
  1328. if (this.isLinkedCard()) {
  1329. const card = Cards.findOne({ _id: this.linkedId });
  1330. if (card === undefined) {
  1331. return null;
  1332. } else {
  1333. return card.assignedBy;
  1334. }
  1335. } else {
  1336. return this.assignedBy;
  1337. }
  1338. },
  1339. isTemplateCard() {
  1340. return this.type === 'template-card';
  1341. },
  1342. votePublic() {
  1343. if (this.vote) return this.vote.public;
  1344. return null;
  1345. },
  1346. voteAllowNonBoardMembers() {
  1347. if (this.vote) return this.vote.allowNonBoardMembers;
  1348. return null;
  1349. },
  1350. voteCountNegative() {
  1351. if (this.vote && this.vote.negative) return this.vote.negative.length;
  1352. return null;
  1353. },
  1354. voteCountPositive() {
  1355. if (this.vote && this.vote.positive) return this.vote.positive.length;
  1356. return null;
  1357. },
  1358. voteCount() {
  1359. return this.voteCountPositive() + this.voteCountNegative();
  1360. },
  1361. });
  1362. Cards.mutations({
  1363. applyToChildren(funct) {
  1364. Cards.find({
  1365. parentId: this._id,
  1366. }).forEach(card => {
  1367. funct(card);
  1368. });
  1369. },
  1370. archive() {
  1371. this.applyToChildren(card => {
  1372. return card.archive();
  1373. });
  1374. return {
  1375. $set: {
  1376. archived: true,
  1377. archivedAt: new Date(),
  1378. },
  1379. };
  1380. },
  1381. restore() {
  1382. this.applyToChildren(card => {
  1383. return card.restore();
  1384. });
  1385. return {
  1386. $set: {
  1387. archived: false,
  1388. },
  1389. };
  1390. },
  1391. moveToEndOfList({ listId } = {}) {
  1392. let swimlaneId = this.swimlaneId;
  1393. const boardId = this.boardId;
  1394. let sortIndex = 0;
  1395. // This should never happen, but there was a bug that was fixed in commit
  1396. // ea0239538a68e225c867411a4f3e0d27c158383.
  1397. if (!swimlaneId) {
  1398. const board = Boards.findOne(boardId);
  1399. swimlaneId = board.getDefaultSwimline()._id;
  1400. }
  1401. // Move the minicard to the end of the target list
  1402. let parentElementDom = $(`#swimlane-${this.swimlaneId}`).get(0);
  1403. if (!parentElementDom) parentElementDom = $(':root');
  1404. const lastCardDom = $(parentElementDom)
  1405. .find(`#js-list-${listId} .js-minicard:last`)
  1406. .get(0);
  1407. if (lastCardDom) sortIndex = Utils.calculateIndex(lastCardDom, null).base;
  1408. return this.moveOptionalArgs({
  1409. boardId,
  1410. swimlaneId,
  1411. listId,
  1412. sort: sortIndex,
  1413. });
  1414. },
  1415. moveOptionalArgs({ boardId, swimlaneId, listId, sort } = {}) {
  1416. boardId = boardId || this.boardId;
  1417. swimlaneId = swimlaneId || this.swimlaneId;
  1418. // This should never happen, but there was a bug that was fixed in commit
  1419. // ea0239538a68e225c867411a4f3e0d27c158383.
  1420. if (!swimlaneId) {
  1421. const board = Boards.findOne(boardId);
  1422. swimlaneId = board.getDefaultSwimline()._id;
  1423. }
  1424. listId = listId || this.listId;
  1425. if (sort === undefined || sort === null) sort = this.sort;
  1426. return this.move(boardId, swimlaneId, listId, sort);
  1427. },
  1428. move(boardId, swimlaneId, listId, sort) {
  1429. const mutatedFields = {
  1430. boardId,
  1431. swimlaneId,
  1432. listId,
  1433. sort,
  1434. };
  1435. // we must only copy the labels and custom fields if the target board
  1436. // differs from the source board
  1437. if (this.boardId !== boardId) {
  1438. // Get label names
  1439. const oldBoard = Boards.findOne(this.boardId);
  1440. const oldBoardLabels = oldBoard.labels;
  1441. const oldCardLabels = _.pluck(
  1442. _.filter(oldBoardLabels, label => {
  1443. return _.contains(this.labelIds, label._id);
  1444. }),
  1445. 'name',
  1446. );
  1447. const newBoard = Boards.findOne(boardId);
  1448. const newBoardLabels = newBoard.labels;
  1449. const newCardLabelIds = _.pluck(
  1450. _.filter(newBoardLabels, label => {
  1451. return label.name && _.contains(oldCardLabels, label.name);
  1452. }),
  1453. '_id',
  1454. );
  1455. Object.assign(mutatedFields, {
  1456. labelIds: newCardLabelIds,
  1457. });
  1458. mutatedFields.customFields = this.mapCustomFieldsToBoard(newBoard._id);
  1459. }
  1460. Cards.update(this._id, {
  1461. $set: mutatedFields,
  1462. });
  1463. },
  1464. addLabel(labelId) {
  1465. return {
  1466. $addToSet: {
  1467. labelIds: labelId,
  1468. },
  1469. };
  1470. },
  1471. removeLabel(labelId) {
  1472. return {
  1473. $pull: {
  1474. labelIds: labelId,
  1475. },
  1476. };
  1477. },
  1478. toggleLabel(labelId) {
  1479. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  1480. return this.removeLabel(labelId);
  1481. } else {
  1482. return this.addLabel(labelId);
  1483. }
  1484. },
  1485. setColor(newColor) {
  1486. if (newColor === 'white') {
  1487. newColor = null;
  1488. }
  1489. return {
  1490. $set: {
  1491. color: newColor,
  1492. },
  1493. };
  1494. },
  1495. assignMember(memberId) {
  1496. return {
  1497. $addToSet: {
  1498. members: memberId,
  1499. },
  1500. };
  1501. },
  1502. assignAssignee(assigneeId) {
  1503. // If there is not any assignee, allow one assignee, not more.
  1504. /*
  1505. if (this.getAssignees().length === 0) {
  1506. return {
  1507. $addToSet: {
  1508. assignees: assigneeId,
  1509. },
  1510. };
  1511. */
  1512. // Allow more that one assignee:
  1513. // https://github.com/wekan/wekan/issues/3302
  1514. return {
  1515. $addToSet: {
  1516. assignees: assigneeId,
  1517. },
  1518. };
  1519. //} else {
  1520. // return false,
  1521. //}
  1522. },
  1523. unassignMember(memberId) {
  1524. return {
  1525. $pull: {
  1526. members: memberId,
  1527. },
  1528. };
  1529. },
  1530. unassignAssignee(assigneeId) {
  1531. return {
  1532. $pull: {
  1533. assignees: assigneeId,
  1534. },
  1535. };
  1536. },
  1537. toggleMember(memberId) {
  1538. if (this.members && this.members.indexOf(memberId) > -1) {
  1539. return this.unassignMember(memberId);
  1540. } else {
  1541. return this.assignMember(memberId);
  1542. }
  1543. },
  1544. toggleAssignee(assigneeId) {
  1545. if (this.assignees && this.assignees.indexOf(assigneeId) > -1) {
  1546. return this.unassignAssignee(assigneeId);
  1547. } else {
  1548. return this.assignAssignee(assigneeId);
  1549. }
  1550. },
  1551. assignCustomField(customFieldId) {
  1552. return {
  1553. $addToSet: {
  1554. customFields: {
  1555. _id: customFieldId,
  1556. value: null,
  1557. },
  1558. },
  1559. };
  1560. },
  1561. unassignCustomField(customFieldId) {
  1562. return {
  1563. $pull: {
  1564. customFields: {
  1565. _id: customFieldId,
  1566. },
  1567. },
  1568. };
  1569. },
  1570. toggleCustomField(customFieldId) {
  1571. if (this.customFields && this.customFieldIndex(customFieldId) > -1) {
  1572. return this.unassignCustomField(customFieldId);
  1573. } else {
  1574. return this.assignCustomField(customFieldId);
  1575. }
  1576. },
  1577. setCustomField(customFieldId, value) {
  1578. // todo
  1579. const index = this.customFieldIndex(customFieldId);
  1580. if (index > -1) {
  1581. const update = {
  1582. $set: {},
  1583. };
  1584. update.$set[`customFields.${index}.value`] = value;
  1585. return update;
  1586. }
  1587. // TODO
  1588. // Ignatz 18.05.2018: Return null to silence ESLint. No Idea if that is correct
  1589. return null;
  1590. },
  1591. setCover(coverId) {
  1592. return {
  1593. $set: {
  1594. coverId,
  1595. },
  1596. };
  1597. },
  1598. unsetCover() {
  1599. return {
  1600. $unset: {
  1601. coverId: '',
  1602. },
  1603. };
  1604. },
  1605. setReceived(receivedAt) {
  1606. return {
  1607. $set: {
  1608. receivedAt,
  1609. },
  1610. };
  1611. },
  1612. unsetReceived() {
  1613. return {
  1614. $unset: {
  1615. receivedAt: '',
  1616. },
  1617. };
  1618. },
  1619. setStart(startAt) {
  1620. return {
  1621. $set: {
  1622. startAt,
  1623. },
  1624. };
  1625. },
  1626. unsetStart() {
  1627. return {
  1628. $unset: {
  1629. startAt: '',
  1630. },
  1631. };
  1632. },
  1633. setDue(dueAt) {
  1634. return {
  1635. $set: {
  1636. dueAt,
  1637. },
  1638. };
  1639. },
  1640. unsetDue() {
  1641. return {
  1642. $unset: {
  1643. dueAt: '',
  1644. },
  1645. };
  1646. },
  1647. setEnd(endAt) {
  1648. return {
  1649. $set: {
  1650. endAt,
  1651. },
  1652. };
  1653. },
  1654. unsetEnd() {
  1655. return {
  1656. $unset: {
  1657. endAt: '',
  1658. },
  1659. };
  1660. },
  1661. setOvertime(isOvertime) {
  1662. return {
  1663. $set: {
  1664. isOvertime,
  1665. },
  1666. };
  1667. },
  1668. setSpentTime(spentTime) {
  1669. return {
  1670. $set: {
  1671. spentTime,
  1672. },
  1673. };
  1674. },
  1675. unsetSpentTime() {
  1676. return {
  1677. $unset: {
  1678. spentTime: '',
  1679. isOvertime: false,
  1680. },
  1681. };
  1682. },
  1683. setParentId(parentId) {
  1684. return {
  1685. $set: {
  1686. parentId,
  1687. },
  1688. };
  1689. },
  1690. setVoteQuestion(question, publicVote, allowNonBoardMembers) {
  1691. return {
  1692. $set: {
  1693. vote: {
  1694. question,
  1695. public: publicVote,
  1696. allowNonBoardMembers,
  1697. positive: [],
  1698. negative: [],
  1699. },
  1700. },
  1701. };
  1702. },
  1703. unsetVote() {
  1704. return {
  1705. $unset: {
  1706. vote: '',
  1707. },
  1708. };
  1709. },
  1710. setVoteEnd(end) {
  1711. return {
  1712. $set: { 'vote.end': end },
  1713. };
  1714. },
  1715. unsetVoteEnd() {
  1716. return {
  1717. $unset: { 'vote.end': '' },
  1718. };
  1719. },
  1720. setVote(userId, forIt) {
  1721. switch (forIt) {
  1722. case true:
  1723. // vote for it
  1724. return {
  1725. $pull: {
  1726. 'vote.negative': userId,
  1727. },
  1728. $addToSet: {
  1729. 'vote.positive': userId,
  1730. },
  1731. };
  1732. case false:
  1733. // vote against
  1734. return {
  1735. $pull: {
  1736. 'vote.positive': userId,
  1737. },
  1738. $addToSet: {
  1739. 'vote.negative': userId,
  1740. },
  1741. };
  1742. default:
  1743. // Remove votes
  1744. return {
  1745. $pull: {
  1746. 'vote.positive': userId,
  1747. 'vote.negative': userId,
  1748. },
  1749. };
  1750. }
  1751. },
  1752. });
  1753. //FUNCTIONS FOR creation of Activities
  1754. function updateActivities(doc, fieldNames, modifier) {
  1755. if (_.contains(fieldNames, 'labelIds') && _.contains(fieldNames, 'boardId')) {
  1756. Activities.find({
  1757. activityType: 'addedLabel',
  1758. cardId: doc._id,
  1759. }).forEach(a => {
  1760. const lidx = doc.labelIds.indexOf(a.labelId);
  1761. if (lidx !== -1 && modifier.$set.labelIds.length > lidx) {
  1762. Activities.update(a._id, {
  1763. $set: {
  1764. labelId: modifier.$set.labelIds[doc.labelIds.indexOf(a.labelId)],
  1765. boardId: modifier.$set.boardId,
  1766. },
  1767. });
  1768. } else {
  1769. Activities.remove(a._id);
  1770. }
  1771. });
  1772. } else if (_.contains(fieldNames, 'boardId')) {
  1773. Activities.remove({
  1774. activityType: 'addedLabel',
  1775. cardId: doc._id,
  1776. });
  1777. }
  1778. }
  1779. function cardMove(
  1780. userId,
  1781. doc,
  1782. fieldNames,
  1783. oldListId,
  1784. oldSwimlaneId,
  1785. oldBoardId,
  1786. ) {
  1787. if (_.contains(fieldNames, 'boardId') && doc.boardId !== oldBoardId) {
  1788. Activities.insert({
  1789. userId,
  1790. activityType: 'moveCardBoard',
  1791. boardName: Boards.findOne(doc.boardId).title,
  1792. boardId: doc.boardId,
  1793. oldBoardId,
  1794. oldBoardName: Boards.findOne(oldBoardId).title,
  1795. cardId: doc._id,
  1796. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1797. swimlaneId: doc.swimlaneId,
  1798. oldSwimlaneId,
  1799. });
  1800. } else if (
  1801. (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) ||
  1802. (_.contains(fieldNames, 'swimlaneId') && doc.swimlaneId !== oldSwimlaneId)
  1803. ) {
  1804. Activities.insert({
  1805. userId,
  1806. oldListId,
  1807. activityType: 'moveCard',
  1808. listName: Lists.findOne(doc.listId).title,
  1809. listId: doc.listId,
  1810. boardId: doc.boardId,
  1811. cardId: doc._id,
  1812. cardTitle: doc.title,
  1813. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1814. swimlaneId: doc.swimlaneId,
  1815. oldSwimlaneId,
  1816. });
  1817. }
  1818. }
  1819. function cardState(userId, doc, fieldNames) {
  1820. if (_.contains(fieldNames, 'archived')) {
  1821. if (doc.archived) {
  1822. Activities.insert({
  1823. userId,
  1824. activityType: 'archivedCard',
  1825. listName: Lists.findOne(doc.listId).title,
  1826. boardId: doc.boardId,
  1827. listId: doc.listId,
  1828. cardId: doc._id,
  1829. swimlaneId: doc.swimlaneId,
  1830. });
  1831. } else {
  1832. Activities.insert({
  1833. userId,
  1834. activityType: 'restoredCard',
  1835. boardId: doc.boardId,
  1836. listName: Lists.findOne(doc.listId).title,
  1837. listId: doc.listId,
  1838. cardId: doc._id,
  1839. swimlaneId: doc.swimlaneId,
  1840. });
  1841. }
  1842. }
  1843. }
  1844. function cardMembers(userId, doc, fieldNames, modifier) {
  1845. if (!_.contains(fieldNames, 'members')) return;
  1846. let memberId;
  1847. // Say hello to the new member
  1848. if (modifier.$addToSet && modifier.$addToSet.members) {
  1849. memberId = modifier.$addToSet.members;
  1850. const username = Users.findOne(memberId).username;
  1851. if (!_.contains(doc.members, memberId)) {
  1852. Activities.insert({
  1853. userId,
  1854. username,
  1855. activityType: 'joinMember',
  1856. boardId: doc.boardId,
  1857. cardId: doc._id,
  1858. memberId,
  1859. listId: doc.listId,
  1860. swimlaneId: doc.swimlaneId,
  1861. });
  1862. }
  1863. }
  1864. // Say goodbye to the former member
  1865. if (modifier.$pull && modifier.$pull.members) {
  1866. memberId = modifier.$pull.members;
  1867. const username = Users.findOne(memberId).username;
  1868. // Check that the former member is member of the card
  1869. if (_.contains(doc.members, memberId)) {
  1870. Activities.insert({
  1871. userId,
  1872. username,
  1873. activityType: 'unjoinMember',
  1874. boardId: doc.boardId,
  1875. cardId: doc._id,
  1876. memberId,
  1877. listId: doc.listId,
  1878. swimlaneId: doc.swimlaneId,
  1879. });
  1880. }
  1881. }
  1882. }
  1883. function cardAssignees(userId, doc, fieldNames, modifier) {
  1884. if (!_.contains(fieldNames, 'assignees')) return;
  1885. let assigneeId;
  1886. // Say hello to the new assignee
  1887. if (modifier.$addToSet && modifier.$addToSet.assignees) {
  1888. assigneeId = modifier.$addToSet.assignees;
  1889. const username = Users.findOne(assigneeId).username;
  1890. if (!_.contains(doc.assignees, assigneeId)) {
  1891. Activities.insert({
  1892. userId,
  1893. username,
  1894. activityType: 'joinAssignee',
  1895. boardId: doc.boardId,
  1896. cardId: doc._id,
  1897. assigneeId,
  1898. listId: doc.listId,
  1899. swimlaneId: doc.swimlaneId,
  1900. });
  1901. }
  1902. }
  1903. // Say goodbye to the former assignee
  1904. if (modifier.$pull && modifier.$pull.assignees) {
  1905. assigneeId = modifier.$pull.assignees;
  1906. const username = Users.findOne(assigneeId).username;
  1907. // Check that the former assignee is assignee of the card
  1908. if (_.contains(doc.assignees, assigneeId)) {
  1909. Activities.insert({
  1910. userId,
  1911. username,
  1912. activityType: 'unjoinAssignee',
  1913. boardId: doc.boardId,
  1914. cardId: doc._id,
  1915. assigneeId,
  1916. listId: doc.listId,
  1917. swimlaneId: doc.swimlaneId,
  1918. });
  1919. }
  1920. }
  1921. }
  1922. function cardLabels(userId, doc, fieldNames, modifier) {
  1923. if (!_.contains(fieldNames, 'labelIds')) return;
  1924. let labelId;
  1925. // Say hello to the new label
  1926. if (modifier.$addToSet && modifier.$addToSet.labelIds) {
  1927. labelId = modifier.$addToSet.labelIds;
  1928. if (!_.contains(doc.labelIds, labelId)) {
  1929. const act = {
  1930. userId,
  1931. labelId,
  1932. activityType: 'addedLabel',
  1933. boardId: doc.boardId,
  1934. cardId: doc._id,
  1935. listId: doc.listId,
  1936. swimlaneId: doc.swimlaneId,
  1937. };
  1938. Activities.insert(act);
  1939. }
  1940. }
  1941. // Say goodbye to the label
  1942. if (modifier.$pull && modifier.$pull.labelIds) {
  1943. labelId = modifier.$pull.labelIds;
  1944. // Check that the former member is member of the card
  1945. if (_.contains(doc.labelIds, labelId)) {
  1946. Activities.insert({
  1947. userId,
  1948. labelId,
  1949. activityType: 'removedLabel',
  1950. boardId: doc.boardId,
  1951. cardId: doc._id,
  1952. listId: doc.listId,
  1953. swimlaneId: doc.swimlaneId,
  1954. });
  1955. }
  1956. }
  1957. }
  1958. function cardCustomFields(userId, doc, fieldNames, modifier) {
  1959. if (!_.contains(fieldNames, 'customFields')) return;
  1960. // Say hello to the new customField value
  1961. if (modifier.$set) {
  1962. _.each(modifier.$set, (value, key) => {
  1963. if (key.startsWith('customFields')) {
  1964. const dotNotation = key.split('.');
  1965. // only individual changes are registered
  1966. if (dotNotation.length > 1) {
  1967. const customFieldId = doc.customFields[dotNotation[1]]._id;
  1968. const act = {
  1969. userId,
  1970. customFieldId,
  1971. value,
  1972. activityType: 'setCustomField',
  1973. boardId: doc.boardId,
  1974. cardId: doc._id,
  1975. listId: doc.listId,
  1976. swimlaneId: doc.swimlaneId,
  1977. };
  1978. Activities.insert(act);
  1979. }
  1980. }
  1981. });
  1982. }
  1983. // Say goodbye to the former customField value
  1984. if (modifier.$unset) {
  1985. _.each(modifier.$unset, (value, key) => {
  1986. if (key.startsWith('customFields')) {
  1987. const dotNotation = key.split('.');
  1988. // only individual changes are registered
  1989. if (dotNotation.length > 1) {
  1990. const customFieldId = doc.customFields[dotNotation[1]]._id;
  1991. const act = {
  1992. userId,
  1993. customFieldId,
  1994. activityType: 'unsetCustomField',
  1995. boardId: doc.boardId,
  1996. cardId: doc._id,
  1997. };
  1998. Activities.insert(act);
  1999. }
  2000. }
  2001. });
  2002. }
  2003. }
  2004. function cardCreation(userId, doc) {
  2005. Activities.insert({
  2006. userId,
  2007. activityType: 'createCard',
  2008. boardId: doc.boardId,
  2009. listName: Lists.findOne(doc.listId).title,
  2010. listId: doc.listId,
  2011. cardId: doc._id,
  2012. cardTitle: doc.title,
  2013. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  2014. swimlaneId: doc.swimlaneId,
  2015. });
  2016. }
  2017. function cardRemover(userId, doc) {
  2018. ChecklistItems.remove({
  2019. cardId: doc._id,
  2020. });
  2021. Checklists.remove({
  2022. cardId: doc._id,
  2023. });
  2024. Cards.remove({
  2025. parentId: doc._id,
  2026. });
  2027. CardComments.remove({
  2028. cardId: doc._id,
  2029. });
  2030. Attachments.remove({
  2031. cardId: doc._id,
  2032. });
  2033. }
  2034. const findDueCards = days => {
  2035. const seekDue = ($from, $to, activityType) => {
  2036. Cards.find({
  2037. archived: false,
  2038. dueAt: { $gte: $from, $lt: $to },
  2039. }).forEach(card => {
  2040. const username = Users.findOne(card.userId).username;
  2041. const activity = {
  2042. userId: card.userId,
  2043. username,
  2044. activityType,
  2045. boardId: card.boardId,
  2046. cardId: card._id,
  2047. cardTitle: card.title,
  2048. listId: card.listId,
  2049. timeValue: card.dueAt,
  2050. swimlaneId: card.swimlaneId,
  2051. };
  2052. Activities.insert(activity);
  2053. });
  2054. };
  2055. const now = new Date(),
  2056. aday = 3600 * 24 * 1e3,
  2057. then = day => new Date(now.setHours(0, 0, 0, 0) + day * aday);
  2058. if (!days) return;
  2059. if (!days.map) days = [days];
  2060. days.map(day => {
  2061. let args = [];
  2062. if (day === 0) {
  2063. args = [then(0), then(1), 'duenow'];
  2064. } else if (day > 0) {
  2065. args = [then(1), then(day), 'almostdue'];
  2066. } else {
  2067. args = [then(day), now, 'pastdue'];
  2068. }
  2069. seekDue(...args);
  2070. });
  2071. };
  2072. const addCronJob = _.debounce(
  2073. Meteor.bindEnvironment(function findDueCardsDebounced() {
  2074. const envValue = process.env.NOTIFY_DUE_DAYS_BEFORE_AND_AFTER;
  2075. if (!envValue) {
  2076. return;
  2077. }
  2078. const notifydays = envValue
  2079. .split(',')
  2080. .map(value => {
  2081. const iValue = parseInt(value, 10);
  2082. if (!(iValue > 0 && iValue < 15)) {
  2083. // notifying due is disabled
  2084. return false;
  2085. } else {
  2086. return iValue;
  2087. }
  2088. })
  2089. .filter(Boolean);
  2090. 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
  2091. const defaultitvl = 8; // default every morning at 8am, if the passed env variable has parsing error use default
  2092. const itvl = parseInt(notifyitvl, 10) || defaultitvl;
  2093. const scheduler = (job => () => {
  2094. const now = new Date();
  2095. const hour = 3600 * 1e3;
  2096. if (now.getHours() === itvl) {
  2097. if (typeof job === 'function') {
  2098. job();
  2099. }
  2100. }
  2101. Meteor.setTimeout(scheduler, hour);
  2102. })(() => {
  2103. findDueCards(notifydays);
  2104. });
  2105. scheduler();
  2106. }),
  2107. 500,
  2108. );
  2109. if (Meteor.isServer) {
  2110. // Cards are often fetched within a board, so we create an index to make these
  2111. // queries more efficient.
  2112. Meteor.startup(() => {
  2113. Cards._collection._ensureIndex({ modifiedAt: -1 });
  2114. Cards._collection._ensureIndex({ boardId: 1, createdAt: -1 });
  2115. // https://github.com/wekan/wekan/issues/1863
  2116. // Swimlane added a new field in the cards collection of mongodb named parentId.
  2117. // When loading a board, mongodb is searching for every cards, the id of the parent (in the swinglanes collection).
  2118. // With a huge database, this result in a very slow app and high CPU on the mongodb side.
  2119. // To correct it, add Index to parentId:
  2120. Cards._collection._ensureIndex({ parentId: 1 });
  2121. // let notifydays = parseInt(process.env.NOTIFY_DUE_DAYS_BEFORE_AND_AFTER) || 2; // default as 2 days b4 and after
  2122. // let notifyitvl = parseInt(process.env.NOTIFY_DUE_AT_HOUR_OF_DAY) || 3600 * 24 * 1e3; // default interval as one day
  2123. // Meteor.call("findDueCards",notifydays,notifyitvl);
  2124. Meteor.defer(() => {
  2125. addCronJob();
  2126. });
  2127. });
  2128. Cards.after.insert((userId, doc) => {
  2129. cardCreation(userId, doc);
  2130. });
  2131. // New activity for card (un)archivage
  2132. Cards.after.update((userId, doc, fieldNames) => {
  2133. cardState(userId, doc, fieldNames);
  2134. });
  2135. //New activity for card moves
  2136. Cards.after.update(function(userId, doc, fieldNames) {
  2137. const oldListId = this.previous.listId;
  2138. const oldSwimlaneId = this.previous.swimlaneId;
  2139. const oldBoardId = this.previous.boardId;
  2140. cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId, oldBoardId);
  2141. });
  2142. // Add a new activity if we add or remove a member to the card
  2143. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2144. cardMembers(userId, doc, fieldNames, modifier);
  2145. updateActivities(doc, fieldNames, modifier);
  2146. });
  2147. // Add a new activity if we add or remove a assignee to the card
  2148. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2149. cardAssignees(userId, doc, fieldNames, modifier);
  2150. updateActivities(doc, fieldNames, modifier);
  2151. });
  2152. // Add a new activity if we add or remove a label to the card
  2153. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2154. cardLabels(userId, doc, fieldNames, modifier);
  2155. });
  2156. // Add a new activity if we edit a custom field
  2157. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2158. cardCustomFields(userId, doc, fieldNames, modifier);
  2159. });
  2160. // Add a new activity if modify time related field like dueAt startAt etc
  2161. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2162. const dla = 'dateLastActivity';
  2163. const fields = fieldNames.filter(name => name !== dla);
  2164. const timingaction = ['receivedAt', 'dueAt', 'startAt', 'endAt'];
  2165. const action = fields[0];
  2166. if (fields.length > 0 && _.contains(timingaction, action)) {
  2167. // add activities for user change these attributes
  2168. const value = modifier.$set[action];
  2169. const oldvalue = doc[action] || '';
  2170. const activityType = `a-${action}`;
  2171. const card = Cards.findOne(doc._id);
  2172. const list = card.list();
  2173. if (list) {
  2174. // 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
  2175. const modifiedAt = new Date(
  2176. new Date(value).getTime() -
  2177. (action === 'endAt' ? 365 * 24 * 3600 * 1e3 : 0),
  2178. ); // set it as 1 year before
  2179. const boardId = list.boardId;
  2180. Lists.direct.update(
  2181. {
  2182. _id: list._id,
  2183. },
  2184. {
  2185. $set: {
  2186. modifiedAt,
  2187. boardId,
  2188. },
  2189. },
  2190. );
  2191. }
  2192. const username = Users.findOne(userId).username;
  2193. const activity = {
  2194. userId,
  2195. username,
  2196. activityType,
  2197. boardId: doc.boardId,
  2198. cardId: doc._id,
  2199. cardTitle: doc.title,
  2200. timeKey: action,
  2201. timeValue: value,
  2202. timeOldValue: oldvalue,
  2203. listId: card.listId,
  2204. swimlaneId: card.swimlaneId,
  2205. };
  2206. Activities.insert(activity);
  2207. }
  2208. });
  2209. // Remove all activities associated with a card if we remove the card
  2210. // Remove also card_comments / checklists / attachments
  2211. Cards.before.remove((userId, doc) => {
  2212. cardRemover(userId, doc);
  2213. });
  2214. }
  2215. //SWIMLANES REST API
  2216. if (Meteor.isServer) {
  2217. /**
  2218. * @operation get_swimlane_cards
  2219. * @summary get all cards attached to a swimlane
  2220. *
  2221. * @param {string} boardId the board ID
  2222. * @param {string} swimlaneId the swimlane ID
  2223. * @return_type [{_id: string,
  2224. * title: string,
  2225. * description: string,
  2226. * listId: string}]
  2227. */
  2228. JsonRoutes.add(
  2229. 'GET',
  2230. '/api/boards/:boardId/swimlanes/:swimlaneId/cards',
  2231. function(req, res) {
  2232. const paramBoardId = req.params.boardId;
  2233. const paramSwimlaneId = req.params.swimlaneId;
  2234. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2235. JsonRoutes.sendResult(res, {
  2236. code: 200,
  2237. data: Cards.find({
  2238. boardId: paramBoardId,
  2239. swimlaneId: paramSwimlaneId,
  2240. archived: false,
  2241. }).map(function(doc) {
  2242. return {
  2243. _id: doc._id,
  2244. title: doc.title,
  2245. description: doc.description,
  2246. listId: doc.listId,
  2247. receivedAt: doc.receivedAt,
  2248. startAt: doc.startAt,
  2249. dueAt: doc.dueAt,
  2250. endAt: doc.endAt,
  2251. assignees: doc.assignees,
  2252. };
  2253. }),
  2254. });
  2255. },
  2256. );
  2257. }
  2258. //LISTS REST API
  2259. if (Meteor.isServer) {
  2260. /**
  2261. * @operation get_all_cards
  2262. * @summary Get all Cards attached to a List
  2263. *
  2264. * @param {string} boardId the board ID
  2265. * @param {string} listId the list ID
  2266. * @return_type [{_id: string,
  2267. * title: string,
  2268. * description: string}]
  2269. */
  2270. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function(
  2271. req,
  2272. res,
  2273. ) {
  2274. const paramBoardId = req.params.boardId;
  2275. const paramListId = req.params.listId;
  2276. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2277. JsonRoutes.sendResult(res, {
  2278. code: 200,
  2279. data: Cards.find({
  2280. boardId: paramBoardId,
  2281. listId: paramListId,
  2282. archived: false,
  2283. }).map(function(doc) {
  2284. return {
  2285. _id: doc._id,
  2286. title: doc.title,
  2287. description: doc.description,
  2288. receivedAt: doc.receivedAt,
  2289. startAt: doc.startAt,
  2290. dueAt: doc.dueAt,
  2291. endAt: doc.endAt,
  2292. assignees: doc.assignees,
  2293. };
  2294. }),
  2295. });
  2296. });
  2297. /**
  2298. * @operation get_card
  2299. * @summary Get a Card
  2300. *
  2301. * @param {string} boardId the board ID
  2302. * @param {string} listId the list ID of the card
  2303. * @param {string} cardId the card ID
  2304. * @return_type Cards
  2305. */
  2306. JsonRoutes.add(
  2307. 'GET',
  2308. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  2309. function(req, res) {
  2310. const paramBoardId = req.params.boardId;
  2311. const paramListId = req.params.listId;
  2312. const paramCardId = req.params.cardId;
  2313. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2314. JsonRoutes.sendResult(res, {
  2315. code: 200,
  2316. data: Cards.findOne({
  2317. _id: paramCardId,
  2318. listId: paramListId,
  2319. boardId: paramBoardId,
  2320. archived: false,
  2321. }),
  2322. });
  2323. },
  2324. );
  2325. /**
  2326. * @operation new_card
  2327. * @summary Create a new Card
  2328. *
  2329. * @param {string} boardId the board ID of the new card
  2330. * @param {string} listId the list ID of the new card
  2331. * @param {string} authorID the user ID of the person owning the card
  2332. * @param {string} parentId the parent ID of the new card
  2333. * @param {string} title the title of the new card
  2334. * @param {string} description the description of the new card
  2335. * @param {string} swimlaneId the swimlane ID of the new card
  2336. * @param {string} [members] the member IDs list of the new card
  2337. * @param {string} [assignees] the array of maximum one ID of assignee of the new card
  2338. * @return_type {_id: string}
  2339. */
  2340. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function(
  2341. req,
  2342. res,
  2343. ) {
  2344. // Check user is logged in
  2345. Authentication.checkLoggedIn(req.userId);
  2346. const paramBoardId = req.params.boardId;
  2347. // Check user has permission to add card to the board
  2348. const board = Boards.findOne({
  2349. _id: paramBoardId,
  2350. });
  2351. const addPermission = allowIsBoardMemberCommentOnly(req.userId, board);
  2352. Authentication.checkAdminOrCondition(req.userId, addPermission);
  2353. const paramListId = req.params.listId;
  2354. const paramParentId = req.params.parentId;
  2355. const currentCards = Cards.find(
  2356. {
  2357. listId: paramListId,
  2358. archived: false,
  2359. },
  2360. { sort: ['sort'] },
  2361. );
  2362. const check = Users.findOne({
  2363. _id: req.body.authorId,
  2364. });
  2365. const members = req.body.members;
  2366. const assignees = req.body.assignees;
  2367. if (typeof check !== 'undefined') {
  2368. const id = Cards.direct.insert({
  2369. title: req.body.title,
  2370. boardId: paramBoardId,
  2371. listId: paramListId,
  2372. parentId: paramParentId,
  2373. description: req.body.description,
  2374. userId: req.body.authorId,
  2375. swimlaneId: req.body.swimlaneId,
  2376. sort: currentCards.count(),
  2377. members,
  2378. assignees,
  2379. });
  2380. JsonRoutes.sendResult(res, {
  2381. code: 200,
  2382. data: {
  2383. _id: id,
  2384. },
  2385. });
  2386. const card = Cards.findOne({
  2387. _id: id,
  2388. });
  2389. cardCreation(req.body.authorId, card);
  2390. } else {
  2391. JsonRoutes.sendResult(res, {
  2392. code: 401,
  2393. });
  2394. }
  2395. });
  2396. /*
  2397. * Note for the JSDoc:
  2398. * 'list' will be interpreted as the path parameter
  2399. * 'listID' will be interpreted as the body parameter
  2400. */
  2401. /**
  2402. * @operation edit_card
  2403. * @summary Edit Fields in a Card
  2404. *
  2405. * @description Edit a card
  2406. *
  2407. * The color has to be chosen between `white`, `green`, `yellow`, `orange`,
  2408. * `red`, `purple`, `blue`, `sky`, `lime`, `pink`, `black`, `silver`,
  2409. * `peachpuff`, `crimson`, `plum`, `darkgreen`, `slateblue`, `magenta`,
  2410. * `gold`, `navy`, `gray`, `saddlebrown`, `paleturquoise`, `mistyrose`,
  2411. * `indigo`:
  2412. *
  2413. * <img src="/card-colors.png" width="40%" alt="Wekan card colors" />
  2414. *
  2415. * Note: setting the color to white has the same effect than removing it.
  2416. *
  2417. * @param {string} boardId the board ID of the card
  2418. * @param {string} list the list ID of the card
  2419. * @param {string} cardId the ID of the card
  2420. * @param {string} [title] the new title of the card
  2421. * @param {string} [sort] the new sort value of the card
  2422. * @param {string} [listId] the new list ID of the card (move operation)
  2423. * @param {string} [description] the new description of the card
  2424. * @param {string} [authorId] change the owner of the card
  2425. * @param {string} [parentId] change the parent of the card
  2426. * @param {string} [labelIds] the new list of label IDs attached to the card
  2427. * @param {string} [swimlaneId] the new swimlane ID of the card
  2428. * @param {string} [members] the new list of member IDs attached to the card
  2429. * @param {string} [assignees] the array of maximum one ID of assignee attached to the card
  2430. * @param {string} [requestedBy] the new requestedBy field of the card
  2431. * @param {string} [assignedBy] the new assignedBy field of the card
  2432. * @param {string} [receivedAt] the new receivedAt field of the card
  2433. * @param {string} [assignBy] the new assignBy field of the card
  2434. * @param {string} [startAt] the new startAt field of the card
  2435. * @param {string} [dueAt] the new dueAt field of the card
  2436. * @param {string} [endAt] the new endAt field of the card
  2437. * @param {string} [spentTime] the new spentTime field of the card
  2438. * @param {boolean} [isOverTime] the new isOverTime field of the card
  2439. * @param {string} [customFields] the new customFields value of the card
  2440. * @param {string} [color] the new color of the card
  2441. * @param {Object} [vote] the vote object
  2442. * @param {string} vote.question the vote question
  2443. * @param {boolean} vote.public show who voted what
  2444. * @param {boolean} vote.allowNonBoardMembers allow all logged in users to vote?
  2445. * @return_type {_id: string}
  2446. */
  2447. JsonRoutes.add(
  2448. 'PUT',
  2449. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  2450. function(req, res) {
  2451. Authentication.checkUserId(req.userId);
  2452. const paramBoardId = req.params.boardId;
  2453. const paramCardId = req.params.cardId;
  2454. const paramListId = req.params.listId;
  2455. if (req.body.hasOwnProperty('title')) {
  2456. const newTitle = req.body.title;
  2457. Cards.direct.update(
  2458. {
  2459. _id: paramCardId,
  2460. listId: paramListId,
  2461. boardId: paramBoardId,
  2462. archived: false,
  2463. },
  2464. {
  2465. $set: {
  2466. title: newTitle,
  2467. },
  2468. },
  2469. );
  2470. }
  2471. if (req.body.hasOwnProperty('sort')) {
  2472. const newSort = req.body.sort;
  2473. Cards.direct.update(
  2474. {
  2475. _id: paramCardId,
  2476. listId: paramListId,
  2477. boardId: paramBoardId,
  2478. archived: false,
  2479. },
  2480. {
  2481. $set: {
  2482. sort: newSort,
  2483. },
  2484. },
  2485. );
  2486. }
  2487. if (req.body.hasOwnProperty('parentId')) {
  2488. const newParentId = req.body.parentId;
  2489. Cards.direct.update(
  2490. {
  2491. _id: paramCardId,
  2492. listId: paramListId,
  2493. boardId: paramBoardId,
  2494. archived: false,
  2495. },
  2496. {
  2497. $set: {
  2498. parentId: newParentId,
  2499. },
  2500. },
  2501. );
  2502. }
  2503. if (req.body.hasOwnProperty('description')) {
  2504. const newDescription = req.body.description;
  2505. Cards.direct.update(
  2506. {
  2507. _id: paramCardId,
  2508. listId: paramListId,
  2509. boardId: paramBoardId,
  2510. archived: false,
  2511. },
  2512. {
  2513. $set: {
  2514. description: newDescription,
  2515. },
  2516. },
  2517. );
  2518. }
  2519. if (req.body.hasOwnProperty('color')) {
  2520. const newColor = req.body.color;
  2521. Cards.direct.update(
  2522. {
  2523. _id: paramCardId,
  2524. listId: paramListId,
  2525. boardId: paramBoardId,
  2526. archived: false,
  2527. },
  2528. { $set: { color: newColor } },
  2529. );
  2530. }
  2531. if (req.body.hasOwnProperty('vote')) {
  2532. const newVote = req.body.vote;
  2533. newVote.positive = [];
  2534. newVote.negative = [];
  2535. if (!newVote.hasOwnProperty('public')) newVote.public = false;
  2536. if (!newVote.hasOwnProperty('allowNonBoardMembers'))
  2537. newVote.allowNonBoardMembers = false;
  2538. Cards.direct.update(
  2539. {
  2540. _id: paramCardId,
  2541. listId: paramListId,
  2542. boardId: paramBoardId,
  2543. archived: false,
  2544. },
  2545. { $set: { vote: newVote } },
  2546. );
  2547. }
  2548. if (req.body.hasOwnProperty('labelIds')) {
  2549. let newlabelIds = req.body.labelIds;
  2550. if (_.isString(newlabelIds)) {
  2551. if (newlabelIds === '') {
  2552. newlabelIds = null;
  2553. } else {
  2554. newlabelIds = [newlabelIds];
  2555. }
  2556. }
  2557. Cards.direct.update(
  2558. {
  2559. _id: paramCardId,
  2560. listId: paramListId,
  2561. boardId: paramBoardId,
  2562. archived: false,
  2563. },
  2564. {
  2565. $set: {
  2566. labelIds: newlabelIds,
  2567. },
  2568. },
  2569. );
  2570. }
  2571. if (req.body.hasOwnProperty('requestedBy')) {
  2572. const newrequestedBy = req.body.requestedBy;
  2573. Cards.direct.update(
  2574. {
  2575. _id: paramCardId,
  2576. listId: paramListId,
  2577. boardId: paramBoardId,
  2578. archived: false,
  2579. },
  2580. { $set: { requestedBy: newrequestedBy } },
  2581. );
  2582. }
  2583. if (req.body.hasOwnProperty('assignedBy')) {
  2584. const newassignedBy = req.body.assignedBy;
  2585. Cards.direct.update(
  2586. {
  2587. _id: paramCardId,
  2588. listId: paramListId,
  2589. boardId: paramBoardId,
  2590. archived: false,
  2591. },
  2592. { $set: { assignedBy: newassignedBy } },
  2593. );
  2594. }
  2595. if (req.body.hasOwnProperty('receivedAt')) {
  2596. const newreceivedAt = req.body.receivedAt;
  2597. Cards.direct.update(
  2598. {
  2599. _id: paramCardId,
  2600. listId: paramListId,
  2601. boardId: paramBoardId,
  2602. archived: false,
  2603. },
  2604. { $set: { receivedAt: newreceivedAt } },
  2605. );
  2606. }
  2607. if (req.body.hasOwnProperty('startAt')) {
  2608. const newstartAt = req.body.startAt;
  2609. Cards.direct.update(
  2610. {
  2611. _id: paramCardId,
  2612. listId: paramListId,
  2613. boardId: paramBoardId,
  2614. archived: false,
  2615. },
  2616. { $set: { startAt: newstartAt } },
  2617. );
  2618. }
  2619. if (req.body.hasOwnProperty('dueAt')) {
  2620. const newdueAt = req.body.dueAt;
  2621. Cards.direct.update(
  2622. {
  2623. _id: paramCardId,
  2624. listId: paramListId,
  2625. boardId: paramBoardId,
  2626. archived: false,
  2627. },
  2628. { $set: { dueAt: newdueAt } },
  2629. );
  2630. }
  2631. if (req.body.hasOwnProperty('endAt')) {
  2632. const newendAt = req.body.endAt;
  2633. Cards.direct.update(
  2634. {
  2635. _id: paramCardId,
  2636. listId: paramListId,
  2637. boardId: paramBoardId,
  2638. archived: false,
  2639. },
  2640. { $set: { endAt: newendAt } },
  2641. );
  2642. }
  2643. if (req.body.hasOwnProperty('spentTime')) {
  2644. const newspentTime = req.body.spentTime;
  2645. Cards.direct.update(
  2646. {
  2647. _id: paramCardId,
  2648. listId: paramListId,
  2649. boardId: paramBoardId,
  2650. archived: false,
  2651. },
  2652. { $set: { spentTime: newspentTime } },
  2653. );
  2654. }
  2655. if (req.body.hasOwnProperty('isOverTime')) {
  2656. const newisOverTime = req.body.isOverTime;
  2657. Cards.direct.update(
  2658. {
  2659. _id: paramCardId,
  2660. listId: paramListId,
  2661. boardId: paramBoardId,
  2662. archived: false,
  2663. },
  2664. { $set: { isOverTime: newisOverTime } },
  2665. );
  2666. }
  2667. if (req.body.hasOwnProperty('customFields')) {
  2668. const newcustomFields = req.body.customFields;
  2669. Cards.direct.update(
  2670. {
  2671. _id: paramCardId,
  2672. listId: paramListId,
  2673. boardId: paramBoardId,
  2674. archived: false,
  2675. },
  2676. { $set: { customFields: newcustomFields } },
  2677. );
  2678. }
  2679. if (req.body.hasOwnProperty('members')) {
  2680. let newmembers = req.body.members;
  2681. if (_.isString(newmembers)) {
  2682. if (newmembers === '') {
  2683. newmembers = null;
  2684. } else {
  2685. newmembers = [newmembers];
  2686. }
  2687. }
  2688. Cards.direct.update(
  2689. {
  2690. _id: paramCardId,
  2691. listId: paramListId,
  2692. boardId: paramBoardId,
  2693. archived: false,
  2694. },
  2695. { $set: { members: newmembers } },
  2696. );
  2697. }
  2698. if (req.body.hasOwnProperty('assignees')) {
  2699. let newassignees = req.body.assignees;
  2700. if (_.isString(newassignees)) {
  2701. if (newassignees === '') {
  2702. newassignees = null;
  2703. } else {
  2704. newassignees = [newassignees];
  2705. }
  2706. }
  2707. Cards.direct.update(
  2708. {
  2709. _id: paramCardId,
  2710. listId: paramListId,
  2711. boardId: paramBoardId,
  2712. archived: false,
  2713. },
  2714. { $set: { assignees: newassignees } },
  2715. );
  2716. }
  2717. if (req.body.hasOwnProperty('swimlaneId')) {
  2718. const newParamSwimlaneId = req.body.swimlaneId;
  2719. Cards.direct.update(
  2720. {
  2721. _id: paramCardId,
  2722. listId: paramListId,
  2723. boardId: paramBoardId,
  2724. archived: false,
  2725. },
  2726. { $set: { swimlaneId: newParamSwimlaneId } },
  2727. );
  2728. }
  2729. if (req.body.hasOwnProperty('listId')) {
  2730. const newParamListId = req.body.listId;
  2731. Cards.direct.update(
  2732. {
  2733. _id: paramCardId,
  2734. listId: paramListId,
  2735. boardId: paramBoardId,
  2736. archived: false,
  2737. },
  2738. {
  2739. $set: {
  2740. listId: newParamListId,
  2741. },
  2742. },
  2743. );
  2744. const card = Cards.findOne({
  2745. _id: paramCardId,
  2746. });
  2747. cardMove(
  2748. req.body.authorId,
  2749. card,
  2750. {
  2751. fieldName: 'listId',
  2752. },
  2753. paramListId,
  2754. );
  2755. }
  2756. JsonRoutes.sendResult(res, {
  2757. code: 200,
  2758. data: {
  2759. _id: paramCardId,
  2760. },
  2761. });
  2762. },
  2763. );
  2764. /**
  2765. * @operation delete_card
  2766. * @summary Delete a card from a board
  2767. *
  2768. * @description This operation **deletes** a card, and therefore the card
  2769. * is not put in the recycle bin.
  2770. *
  2771. * @param {string} boardId the board ID of the card
  2772. * @param {string} list the list ID of the card
  2773. * @param {string} cardId the ID of the card
  2774. * @return_type {_id: string}
  2775. */
  2776. JsonRoutes.add(
  2777. 'DELETE',
  2778. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  2779. function(req, res) {
  2780. Authentication.checkUserId(req.userId);
  2781. const paramBoardId = req.params.boardId;
  2782. const paramListId = req.params.listId;
  2783. const paramCardId = req.params.cardId;
  2784. const card = Cards.findOne({
  2785. _id: paramCardId,
  2786. });
  2787. Cards.direct.remove({
  2788. _id: paramCardId,
  2789. listId: paramListId,
  2790. boardId: paramBoardId,
  2791. });
  2792. cardRemover(req.body.authorId, card);
  2793. JsonRoutes.sendResult(res, {
  2794. code: 200,
  2795. data: {
  2796. _id: paramCardId,
  2797. },
  2798. });
  2799. },
  2800. );
  2801. /**
  2802. * @operation get_cards_by_custom_field
  2803. * @summary Get all Cards that matchs a value of a specific custom field
  2804. *
  2805. * @param {string} boardId the board ID
  2806. * @param {string} customFieldId the list ID
  2807. * @param {string} customFieldValue the value to look for
  2808. * @return_type [{_id: string,
  2809. * title: string,
  2810. * description: string,
  2811. * listId: string,
  2812. * swinlaneId: string}]
  2813. */
  2814. JsonRoutes.add(
  2815. 'GET',
  2816. '/api/boards/:boardId/cardsByCustomField/:customFieldId/:customFieldValue',
  2817. function(req, res) {
  2818. const paramBoardId = req.params.boardId;
  2819. const paramCustomFieldId = req.params.customFieldId;
  2820. const paramCustomFieldValue = req.params.customFieldValue;
  2821. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2822. JsonRoutes.sendResult(res, {
  2823. code: 200,
  2824. data: Cards.find({
  2825. boardId: paramBoardId,
  2826. customFields: {
  2827. $elemMatch: {
  2828. _id: paramCustomFieldId,
  2829. value: paramCustomFieldValue,
  2830. },
  2831. },
  2832. archived: false,
  2833. }).fetch(),
  2834. });
  2835. },
  2836. );
  2837. }
  2838. export default Cards;