cards.js 70 KB

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