cards.js 74 KB

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