2
0

cards.js 74 KB

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