cards.js 68 KB

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