cards.js 74 KB

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