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