cards.js 79 KB

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