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