cards.js 80 KB

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