cards.js 73 KB

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