cards.js 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688
  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: '',
  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: boardId,
  1185. swimlaneId: swimlaneId,
  1186. listId: 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)
  1201. sort = this.sort;
  1202. return this.move(boardId, swimlaneId, listId, sort);
  1203. },
  1204. move(boardId, swimlaneId, listId, sort) {
  1205. // Copy Custom Fields
  1206. if (this.boardId !== boardId) {
  1207. CustomFields.find({
  1208. _id: {
  1209. $in: this.customFields.map(cf => {
  1210. return cf._id;
  1211. }),
  1212. },
  1213. }).forEach(cf => {
  1214. if (!_.contains(cf.boardIds, boardId)) cf.addBoard(boardId);
  1215. });
  1216. }
  1217. // Get label names
  1218. const oldBoard = Boards.findOne(this.boardId);
  1219. const oldBoardLabels = oldBoard.labels;
  1220. const oldCardLabels = _.pluck(
  1221. _.filter(oldBoardLabels, label => {
  1222. return _.contains(this.labelIds, label._id);
  1223. }),
  1224. 'name',
  1225. );
  1226. const newBoard = Boards.findOne(boardId);
  1227. const newBoardLabels = newBoard.labels;
  1228. const newCardLabelIds = _.pluck(
  1229. _.filter(newBoardLabels, label => {
  1230. return label.name && _.contains(oldCardLabels, label.name);
  1231. }),
  1232. '_id',
  1233. );
  1234. const mutatedFields = {
  1235. boardId,
  1236. swimlaneId,
  1237. listId,
  1238. sort,
  1239. labelIds: newCardLabelIds,
  1240. };
  1241. Cards.update(this._id, {
  1242. $set: mutatedFields,
  1243. });
  1244. },
  1245. addLabel(labelId) {
  1246. return {
  1247. $addToSet: {
  1248. labelIds: labelId,
  1249. },
  1250. };
  1251. },
  1252. removeLabel(labelId) {
  1253. return {
  1254. $pull: {
  1255. labelIds: labelId,
  1256. },
  1257. };
  1258. },
  1259. toggleLabel(labelId) {
  1260. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  1261. return this.removeLabel(labelId);
  1262. } else {
  1263. return this.addLabel(labelId);
  1264. }
  1265. },
  1266. setColor(newColor) {
  1267. if (newColor === 'white') {
  1268. newColor = null;
  1269. }
  1270. return {
  1271. $set: {
  1272. color: newColor,
  1273. },
  1274. };
  1275. },
  1276. assignMember(memberId) {
  1277. return {
  1278. $addToSet: {
  1279. members: memberId,
  1280. },
  1281. };
  1282. },
  1283. assignAssignee(assigneeId) {
  1284. // If there is not any assignee, allow one assignee, not more.
  1285. if (this.getAssignees().length === 0) {
  1286. return {
  1287. $addToSet: {
  1288. assignees: assigneeId,
  1289. },
  1290. };
  1291. } else {
  1292. return false;
  1293. }
  1294. },
  1295. unassignMember(memberId) {
  1296. return {
  1297. $pull: {
  1298. members: memberId,
  1299. },
  1300. };
  1301. },
  1302. unassignAssignee(assigneeId) {
  1303. return {
  1304. $pull: {
  1305. assignees: assigneeId,
  1306. },
  1307. };
  1308. },
  1309. toggleMember(memberId) {
  1310. if (this.members && this.members.indexOf(memberId) > -1) {
  1311. return this.unassignMember(memberId);
  1312. } else {
  1313. return this.assignMember(memberId);
  1314. }
  1315. },
  1316. toggleAssignee(assigneeId) {
  1317. if (this.assignees && this.assignees.indexOf(assigneeId) > -1) {
  1318. return this.unassignAssignee(assigneeId);
  1319. } else {
  1320. return this.assignAssignee(assigneeId);
  1321. }
  1322. },
  1323. assignCustomField(customFieldId) {
  1324. return {
  1325. $addToSet: {
  1326. customFields: {
  1327. _id: customFieldId,
  1328. value: null,
  1329. },
  1330. },
  1331. };
  1332. },
  1333. unassignCustomField(customFieldId) {
  1334. return {
  1335. $pull: {
  1336. customFields: {
  1337. _id: customFieldId,
  1338. },
  1339. },
  1340. };
  1341. },
  1342. toggleCustomField(customFieldId) {
  1343. if (this.customFields && this.customFieldIndex(customFieldId) > -1) {
  1344. return this.unassignCustomField(customFieldId);
  1345. } else {
  1346. return this.assignCustomField(customFieldId);
  1347. }
  1348. },
  1349. setCustomField(customFieldId, value) {
  1350. // todo
  1351. const index = this.customFieldIndex(customFieldId);
  1352. if (index > -1) {
  1353. const update = {
  1354. $set: {},
  1355. };
  1356. update.$set[`customFields.${index}.value`] = value;
  1357. return update;
  1358. }
  1359. // TODO
  1360. // Ignatz 18.05.2018: Return null to silence ESLint. No Idea if that is correct
  1361. return null;
  1362. },
  1363. setCover(coverId) {
  1364. return {
  1365. $set: {
  1366. coverId,
  1367. },
  1368. };
  1369. },
  1370. unsetCover() {
  1371. return {
  1372. $unset: {
  1373. coverId: '',
  1374. },
  1375. };
  1376. },
  1377. setReceived(receivedAt) {
  1378. return {
  1379. $set: {
  1380. receivedAt,
  1381. },
  1382. };
  1383. },
  1384. unsetReceived() {
  1385. return {
  1386. $unset: {
  1387. receivedAt: '',
  1388. },
  1389. };
  1390. },
  1391. setStart(startAt) {
  1392. return {
  1393. $set: {
  1394. startAt,
  1395. },
  1396. };
  1397. },
  1398. unsetStart() {
  1399. return {
  1400. $unset: {
  1401. startAt: '',
  1402. },
  1403. };
  1404. },
  1405. setDue(dueAt) {
  1406. return {
  1407. $set: {
  1408. dueAt,
  1409. },
  1410. };
  1411. },
  1412. unsetDue() {
  1413. return {
  1414. $unset: {
  1415. dueAt: '',
  1416. },
  1417. };
  1418. },
  1419. setEnd(endAt) {
  1420. return {
  1421. $set: {
  1422. endAt,
  1423. },
  1424. };
  1425. },
  1426. unsetEnd() {
  1427. return {
  1428. $unset: {
  1429. endAt: '',
  1430. },
  1431. };
  1432. },
  1433. setOvertime(isOvertime) {
  1434. return {
  1435. $set: {
  1436. isOvertime,
  1437. },
  1438. };
  1439. },
  1440. setSpentTime(spentTime) {
  1441. return {
  1442. $set: {
  1443. spentTime,
  1444. },
  1445. };
  1446. },
  1447. unsetSpentTime() {
  1448. return {
  1449. $unset: {
  1450. spentTime: '',
  1451. isOvertime: false,
  1452. },
  1453. };
  1454. },
  1455. setParentId(parentId) {
  1456. return {
  1457. $set: {
  1458. parentId,
  1459. },
  1460. };
  1461. },
  1462. setVoteQuestion(question, publicVote, allowNonBoardMembers) {
  1463. return {
  1464. $set: {
  1465. vote: {
  1466. question,
  1467. public: publicVote,
  1468. allowNonBoardMembers,
  1469. positive: [],
  1470. negative: [],
  1471. },
  1472. },
  1473. };
  1474. },
  1475. unsetVote() {
  1476. return {
  1477. $unset: {
  1478. vote: '',
  1479. },
  1480. };
  1481. },
  1482. setVoteEnd(end) {
  1483. return {
  1484. $set: { 'vote.end': end },
  1485. };
  1486. },
  1487. unsetVoteEnd() {
  1488. return {
  1489. $unset: { 'vote.end': '' },
  1490. };
  1491. },
  1492. setVote(userId, forIt) {
  1493. switch (forIt) {
  1494. case true:
  1495. // vote for it
  1496. return {
  1497. $pull: {
  1498. 'vote.negative': userId,
  1499. },
  1500. $addToSet: {
  1501. 'vote.positive': userId,
  1502. },
  1503. };
  1504. case false:
  1505. // vote against
  1506. return {
  1507. $pull: {
  1508. 'vote.positive': userId,
  1509. },
  1510. $addToSet: {
  1511. 'vote.negative': userId,
  1512. },
  1513. };
  1514. default:
  1515. // Remove votes
  1516. return {
  1517. $pull: {
  1518. 'vote.positive': userId,
  1519. 'vote.negative': userId,
  1520. },
  1521. };
  1522. }
  1523. },
  1524. });
  1525. //FUNCTIONS FOR creation of Activities
  1526. function updateActivities(doc, fieldNames, modifier) {
  1527. if (_.contains(fieldNames, 'labelIds') && _.contains(fieldNames, 'boardId')) {
  1528. Activities.find({
  1529. activityType: 'addedLabel',
  1530. cardId: doc._id,
  1531. }).forEach(a => {
  1532. const lidx = doc.labelIds.indexOf(a.labelId);
  1533. if (lidx !== -1 && modifier.$set.labelIds.length > lidx) {
  1534. Activities.update(a._id, {
  1535. $set: {
  1536. labelId: modifier.$set.labelIds[doc.labelIds.indexOf(a.labelId)],
  1537. boardId: modifier.$set.boardId,
  1538. },
  1539. });
  1540. } else {
  1541. Activities.remove(a._id);
  1542. }
  1543. });
  1544. } else if (_.contains(fieldNames, 'boardId')) {
  1545. Activities.remove({
  1546. activityType: 'addedLabel',
  1547. cardId: doc._id,
  1548. });
  1549. }
  1550. }
  1551. function cardMove(
  1552. userId,
  1553. doc,
  1554. fieldNames,
  1555. oldListId,
  1556. oldSwimlaneId,
  1557. oldBoardId,
  1558. ) {
  1559. if (_.contains(fieldNames, 'boardId') && doc.boardId !== oldBoardId) {
  1560. Activities.insert({
  1561. userId,
  1562. activityType: 'moveCardBoard',
  1563. boardName: Boards.findOne(doc.boardId).title,
  1564. boardId: doc.boardId,
  1565. oldBoardId,
  1566. oldBoardName: Boards.findOne(oldBoardId).title,
  1567. cardId: doc._id,
  1568. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1569. swimlaneId: doc.swimlaneId,
  1570. oldSwimlaneId,
  1571. });
  1572. } else if (
  1573. (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) ||
  1574. (_.contains(fieldNames, 'swimlaneId') && doc.swimlaneId !== oldSwimlaneId)
  1575. ) {
  1576. Activities.insert({
  1577. userId,
  1578. oldListId,
  1579. activityType: 'moveCard',
  1580. listName: Lists.findOne(doc.listId).title,
  1581. listId: doc.listId,
  1582. boardId: doc.boardId,
  1583. cardId: doc._id,
  1584. cardTitle: doc.title,
  1585. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1586. swimlaneId: doc.swimlaneId,
  1587. oldSwimlaneId,
  1588. });
  1589. }
  1590. }
  1591. function cardState(userId, doc, fieldNames) {
  1592. if (_.contains(fieldNames, 'archived')) {
  1593. if (doc.archived) {
  1594. Activities.insert({
  1595. userId,
  1596. activityType: 'archivedCard',
  1597. listName: Lists.findOne(doc.listId).title,
  1598. boardId: doc.boardId,
  1599. listId: doc.listId,
  1600. cardId: doc._id,
  1601. swimlaneId: doc.swimlaneId,
  1602. });
  1603. } else {
  1604. Activities.insert({
  1605. userId,
  1606. activityType: 'restoredCard',
  1607. boardId: doc.boardId,
  1608. listName: Lists.findOne(doc.listId).title,
  1609. listId: doc.listId,
  1610. cardId: doc._id,
  1611. swimlaneId: doc.swimlaneId,
  1612. });
  1613. }
  1614. }
  1615. }
  1616. function cardMembers(userId, doc, fieldNames, modifier) {
  1617. if (!_.contains(fieldNames, 'members')) return;
  1618. let memberId;
  1619. // Say hello to the new member
  1620. if (modifier.$addToSet && modifier.$addToSet.members) {
  1621. memberId = modifier.$addToSet.members;
  1622. const username = Users.findOne(memberId).username;
  1623. if (!_.contains(doc.members, memberId)) {
  1624. Activities.insert({
  1625. userId,
  1626. username,
  1627. activityType: 'joinMember',
  1628. boardId: doc.boardId,
  1629. cardId: doc._id,
  1630. memberId,
  1631. listId: doc.listId,
  1632. swimlaneId: doc.swimlaneId,
  1633. });
  1634. }
  1635. }
  1636. // Say goodbye to the former member
  1637. if (modifier.$pull && modifier.$pull.members) {
  1638. memberId = modifier.$pull.members;
  1639. const username = Users.findOne(memberId).username;
  1640. // Check that the former member is member of the card
  1641. if (_.contains(doc.members, memberId)) {
  1642. Activities.insert({
  1643. userId,
  1644. username,
  1645. activityType: 'unjoinMember',
  1646. boardId: doc.boardId,
  1647. cardId: doc._id,
  1648. memberId,
  1649. listId: doc.listId,
  1650. swimlaneId: doc.swimlaneId,
  1651. });
  1652. }
  1653. }
  1654. }
  1655. function cardAssignees(userId, doc, fieldNames, modifier) {
  1656. if (!_.contains(fieldNames, 'assignees')) return;
  1657. let assigneeId;
  1658. // Say hello to the new assignee
  1659. if (modifier.$addToSet && modifier.$addToSet.assignees) {
  1660. assigneeId = modifier.$addToSet.assignees;
  1661. const username = Users.findOne(assigneeId).username;
  1662. if (!_.contains(doc.assignees, assigneeId)) {
  1663. Activities.insert({
  1664. userId,
  1665. username,
  1666. activityType: 'joinAssignee',
  1667. boardId: doc.boardId,
  1668. cardId: doc._id,
  1669. assigneeId,
  1670. listId: doc.listId,
  1671. swimlaneId: doc.swimlaneId,
  1672. });
  1673. }
  1674. }
  1675. // Say goodbye to the former assignee
  1676. if (modifier.$pull && modifier.$pull.assignees) {
  1677. assigneeId = modifier.$pull.assignees;
  1678. const username = Users.findOne(assigneeId).username;
  1679. // Check that the former assignee is assignee of the card
  1680. if (_.contains(doc.assignees, assigneeId)) {
  1681. Activities.insert({
  1682. userId,
  1683. username,
  1684. activityType: 'unjoinAssignee',
  1685. boardId: doc.boardId,
  1686. cardId: doc._id,
  1687. assigneeId,
  1688. listId: doc.listId,
  1689. swimlaneId: doc.swimlaneId,
  1690. });
  1691. }
  1692. }
  1693. }
  1694. function cardLabels(userId, doc, fieldNames, modifier) {
  1695. if (!_.contains(fieldNames, 'labelIds')) return;
  1696. let labelId;
  1697. // Say hello to the new label
  1698. if (modifier.$addToSet && modifier.$addToSet.labelIds) {
  1699. labelId = modifier.$addToSet.labelIds;
  1700. if (!_.contains(doc.labelIds, labelId)) {
  1701. const act = {
  1702. userId,
  1703. labelId,
  1704. activityType: 'addedLabel',
  1705. boardId: doc.boardId,
  1706. cardId: doc._id,
  1707. listId: doc.listId,
  1708. swimlaneId: doc.swimlaneId,
  1709. };
  1710. Activities.insert(act);
  1711. }
  1712. }
  1713. // Say goodbye to the label
  1714. if (modifier.$pull && modifier.$pull.labelIds) {
  1715. labelId = modifier.$pull.labelIds;
  1716. // Check that the former member is member of the card
  1717. if (_.contains(doc.labelIds, labelId)) {
  1718. Activities.insert({
  1719. userId,
  1720. labelId,
  1721. activityType: 'removedLabel',
  1722. boardId: doc.boardId,
  1723. cardId: doc._id,
  1724. listId: doc.listId,
  1725. swimlaneId: doc.swimlaneId,
  1726. });
  1727. }
  1728. }
  1729. }
  1730. function cardCustomFields(userId, doc, fieldNames, modifier) {
  1731. if (!_.contains(fieldNames, 'customFields')) return;
  1732. // Say hello to the new customField value
  1733. if (modifier.$set) {
  1734. _.each(modifier.$set, (value, key) => {
  1735. if (key.startsWith('customFields')) {
  1736. const dotNotation = key.split('.');
  1737. // only individual changes are registered
  1738. if (dotNotation.length > 1) {
  1739. const customFieldId = doc.customFields[dotNotation[1]]._id;
  1740. const act = {
  1741. userId,
  1742. customFieldId,
  1743. value,
  1744. activityType: 'setCustomField',
  1745. boardId: doc.boardId,
  1746. cardId: doc._id,
  1747. };
  1748. Activities.insert(act);
  1749. }
  1750. }
  1751. });
  1752. }
  1753. // Say goodbye to the former customField value
  1754. if (modifier.$unset) {
  1755. _.each(modifier.$unset, (value, key) => {
  1756. if (key.startsWith('customFields')) {
  1757. const dotNotation = key.split('.');
  1758. // only individual changes are registered
  1759. if (dotNotation.length > 1) {
  1760. const customFieldId = doc.customFields[dotNotation[1]]._id;
  1761. const act = {
  1762. userId,
  1763. customFieldId,
  1764. activityType: 'unsetCustomField',
  1765. boardId: doc.boardId,
  1766. cardId: doc._id,
  1767. };
  1768. Activities.insert(act);
  1769. }
  1770. }
  1771. });
  1772. }
  1773. }
  1774. function cardCreation(userId, doc) {
  1775. Activities.insert({
  1776. userId,
  1777. activityType: 'createCard',
  1778. boardId: doc.boardId,
  1779. listName: Lists.findOne(doc.listId).title,
  1780. listId: doc.listId,
  1781. cardId: doc._id,
  1782. cardTitle: doc.title,
  1783. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1784. swimlaneId: doc.swimlaneId,
  1785. });
  1786. }
  1787. function cardRemover(userId, doc) {
  1788. ChecklistItems.remove({
  1789. cardId: doc._id,
  1790. });
  1791. Checklists.remove({
  1792. cardId: doc._id,
  1793. });
  1794. Cards.remove({
  1795. parentId: doc._id,
  1796. });
  1797. CardComments.remove({
  1798. cardId: doc._id,
  1799. });
  1800. Attachments.remove({
  1801. cardId: doc._id,
  1802. });
  1803. }
  1804. const findDueCards = days => {
  1805. const seekDue = ($from, $to, activityType) => {
  1806. Cards.find({
  1807. archived: false,
  1808. dueAt: { $gte: $from, $lt: $to },
  1809. }).forEach(card => {
  1810. const username = Users.findOne(card.userId).username;
  1811. const activity = {
  1812. userId: card.userId,
  1813. username,
  1814. activityType,
  1815. boardId: card.boardId,
  1816. cardId: card._id,
  1817. cardTitle: card.title,
  1818. listId: card.listId,
  1819. timeValue: card.dueAt,
  1820. swimlaneId: card.swimlaneId,
  1821. };
  1822. Activities.insert(activity);
  1823. });
  1824. };
  1825. const now = new Date(),
  1826. aday = 3600 * 24 * 1e3,
  1827. then = day => new Date(now.setHours(0, 0, 0, 0) + day * aday);
  1828. if (!days) return;
  1829. if (!days.map) days = [days];
  1830. days.map(day => {
  1831. let args = [];
  1832. if (day === 0) {
  1833. args = [then(0), then(1), 'duenow'];
  1834. } else if (day > 0) {
  1835. args = [then(1), then(day), 'almostdue'];
  1836. } else {
  1837. args = [then(day), now, 'pastdue'];
  1838. }
  1839. seekDue(...args);
  1840. });
  1841. };
  1842. const addCronJob = _.debounce(
  1843. Meteor.bindEnvironment(function findDueCardsDebounced() {
  1844. const envValue = process.env.NOTIFY_DUE_DAYS_BEFORE_AND_AFTER;
  1845. if (!envValue) {
  1846. return;
  1847. }
  1848. const notifydays = envValue
  1849. .split(',')
  1850. .map(value => {
  1851. const iValue = parseInt(value, 10);
  1852. if (!(iValue > 0 && iValue < 15)) {
  1853. // notifying due is disabled
  1854. return false;
  1855. } else {
  1856. return iValue;
  1857. }
  1858. })
  1859. .filter(Boolean);
  1860. 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
  1861. const defaultitvl = 8; // default every morning at 8am, if the passed env variable has parsing error use default
  1862. const itvl = parseInt(notifyitvl, 10) || defaultitvl;
  1863. const scheduler = (job => () => {
  1864. const now = new Date();
  1865. const hour = 3600 * 1e3;
  1866. if (now.getHours() === itvl) {
  1867. if (typeof job === 'function') {
  1868. job();
  1869. }
  1870. }
  1871. Meteor.setTimeout(scheduler, hour);
  1872. })(() => {
  1873. findDueCards(notifydays);
  1874. });
  1875. scheduler();
  1876. }),
  1877. 500,
  1878. );
  1879. if (Meteor.isServer) {
  1880. // Cards are often fetched within a board, so we create an index to make these
  1881. // queries more efficient.
  1882. Meteor.startup(() => {
  1883. Cards._collection._ensureIndex({ modifiedAt: -1 });
  1884. Cards._collection._ensureIndex({ boardId: 1, createdAt: -1 });
  1885. // https://github.com/wekan/wekan/issues/1863
  1886. // Swimlane added a new field in the cards collection of mongodb named parentId.
  1887. // When loading a board, mongodb is searching for every cards, the id of the parent (in the swinglanes collection).
  1888. // With a huge database, this result in a very slow app and high CPU on the mongodb side.
  1889. // To correct it, add Index to parentId:
  1890. Cards._collection._ensureIndex({ parentId: 1 });
  1891. // let notifydays = parseInt(process.env.NOTIFY_DUE_DAYS_BEFORE_AND_AFTER) || 2; // default as 2 days b4 and after
  1892. // let notifyitvl = parseInt(process.env.NOTIFY_DUE_AT_HOUR_OF_DAY) || 3600 * 24 * 1e3; // default interval as one day
  1893. // Meteor.call("findDueCards",notifydays,notifyitvl);
  1894. Meteor.defer(() => {
  1895. addCronJob();
  1896. });
  1897. });
  1898. Cards.after.insert((userId, doc) => {
  1899. cardCreation(userId, doc);
  1900. });
  1901. // New activity for card (un)archivage
  1902. Cards.after.update((userId, doc, fieldNames) => {
  1903. cardState(userId, doc, fieldNames);
  1904. });
  1905. //New activity for card moves
  1906. Cards.after.update(function(userId, doc, fieldNames) {
  1907. const oldListId = this.previous.listId;
  1908. const oldSwimlaneId = this.previous.swimlaneId;
  1909. const oldBoardId = this.previous.boardId;
  1910. cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId, oldBoardId);
  1911. });
  1912. // Add a new activity if we add or remove a member to the card
  1913. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1914. cardMembers(userId, doc, fieldNames, modifier);
  1915. updateActivities(doc, fieldNames, modifier);
  1916. });
  1917. // Add a new activity if we add or remove a assignee to the card
  1918. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1919. cardAssignees(userId, doc, fieldNames, modifier);
  1920. updateActivities(doc, fieldNames, modifier);
  1921. });
  1922. // Add a new activity if we add or remove a label to the card
  1923. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1924. cardLabels(userId, doc, fieldNames, modifier);
  1925. });
  1926. // Add a new activity if we edit a custom field
  1927. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1928. cardCustomFields(userId, doc, fieldNames, modifier);
  1929. });
  1930. // Add a new activity if modify time related field like dueAt startAt etc
  1931. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1932. const dla = 'dateLastActivity';
  1933. const fields = fieldNames.filter(name => name !== dla);
  1934. const timingaction = ['receivedAt', 'dueAt', 'startAt', 'endAt'];
  1935. const action = fields[0];
  1936. if (fields.length > 0 && _.contains(timingaction, action)) {
  1937. // add activities for user change these attributes
  1938. const value = modifier.$set[action];
  1939. const oldvalue = doc[action] || '';
  1940. const activityType = `a-${action}`;
  1941. const card = Cards.findOne(doc._id);
  1942. const list = card.list();
  1943. if (list) {
  1944. // 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
  1945. const modifiedAt = new Date(
  1946. new Date(value).getTime() -
  1947. (action === 'endAt' ? 365 * 24 * 3600 * 1e3 : 0),
  1948. ); // set it as 1 year before
  1949. const boardId = list.boardId;
  1950. Lists.direct.update(
  1951. {
  1952. _id: list._id,
  1953. },
  1954. {
  1955. $set: {
  1956. modifiedAt,
  1957. boardId,
  1958. },
  1959. },
  1960. );
  1961. }
  1962. const username = Users.findOne(userId).username;
  1963. const activity = {
  1964. userId,
  1965. username,
  1966. activityType,
  1967. boardId: doc.boardId,
  1968. cardId: doc._id,
  1969. cardTitle: doc.title,
  1970. timeKey: action,
  1971. timeValue: value,
  1972. timeOldValue: oldvalue,
  1973. listId: card.listId,
  1974. swimlaneId: card.swimlaneId,
  1975. };
  1976. Activities.insert(activity);
  1977. }
  1978. });
  1979. // Remove all activities associated with a card if we remove the card
  1980. // Remove also card_comments / checklists / attachments
  1981. Cards.before.remove((userId, doc) => {
  1982. cardRemover(userId, doc);
  1983. });
  1984. }
  1985. //SWIMLANES REST API
  1986. if (Meteor.isServer) {
  1987. /**
  1988. * @operation get_swimlane_cards
  1989. * @summary get all cards attached to a swimlane
  1990. *
  1991. * @param {string} boardId the board ID
  1992. * @param {string} swimlaneId the swimlane ID
  1993. * @return_type [{_id: string,
  1994. * title: string,
  1995. * description: string,
  1996. * listId: string}]
  1997. */
  1998. JsonRoutes.add(
  1999. 'GET',
  2000. '/api/boards/:boardId/swimlanes/:swimlaneId/cards',
  2001. function(req, res) {
  2002. const paramBoardId = req.params.boardId;
  2003. const paramSwimlaneId = req.params.swimlaneId;
  2004. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2005. JsonRoutes.sendResult(res, {
  2006. code: 200,
  2007. data: Cards.find({
  2008. boardId: paramBoardId,
  2009. swimlaneId: paramSwimlaneId,
  2010. archived: false,
  2011. }).map(function(doc) {
  2012. return {
  2013. _id: doc._id,
  2014. title: doc.title,
  2015. description: doc.description,
  2016. listId: doc.listId,
  2017. };
  2018. }),
  2019. });
  2020. },
  2021. );
  2022. }
  2023. //LISTS REST API
  2024. if (Meteor.isServer) {
  2025. /**
  2026. * @operation get_all_cards
  2027. * @summary Get all Cards attached to a List
  2028. *
  2029. * @param {string} boardId the board ID
  2030. * @param {string} listId the list ID
  2031. * @return_type [{_id: string,
  2032. * title: string,
  2033. * description: string}]
  2034. */
  2035. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function(
  2036. req,
  2037. res,
  2038. ) {
  2039. const paramBoardId = req.params.boardId;
  2040. const paramListId = req.params.listId;
  2041. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2042. JsonRoutes.sendResult(res, {
  2043. code: 200,
  2044. data: Cards.find({
  2045. boardId: paramBoardId,
  2046. listId: paramListId,
  2047. archived: false,
  2048. }).map(function(doc) {
  2049. return {
  2050. _id: doc._id,
  2051. title: doc.title,
  2052. description: doc.description,
  2053. };
  2054. }),
  2055. });
  2056. });
  2057. /**
  2058. * @operation get_card
  2059. * @summary Get a Card
  2060. *
  2061. * @param {string} boardId the board ID
  2062. * @param {string} listId the list ID of the card
  2063. * @param {string} cardId the card ID
  2064. * @return_type Cards
  2065. */
  2066. JsonRoutes.add(
  2067. 'GET',
  2068. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  2069. function(req, res) {
  2070. const paramBoardId = req.params.boardId;
  2071. const paramListId = req.params.listId;
  2072. const paramCardId = req.params.cardId;
  2073. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2074. JsonRoutes.sendResult(res, {
  2075. code: 200,
  2076. data: Cards.findOne({
  2077. _id: paramCardId,
  2078. listId: paramListId,
  2079. boardId: paramBoardId,
  2080. archived: false,
  2081. }),
  2082. });
  2083. },
  2084. );
  2085. /**
  2086. * @operation new_card
  2087. * @summary Create a new Card
  2088. *
  2089. * @param {string} boardId the board ID of the new card
  2090. * @param {string} listId the list ID of the new card
  2091. * @param {string} authorID the user ID of the person owning the card
  2092. * @param {string} parentId the parent ID of the new card
  2093. * @param {string} title the title of the new card
  2094. * @param {string} description the description of the new card
  2095. * @param {string} swimlaneId the swimlane ID of the new card
  2096. * @param {string} [members] the member IDs list of the new card
  2097. * @param {string} [assignees] the array of maximum one ID of assignee of the new card
  2098. * @return_type {_id: string}
  2099. */
  2100. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function(
  2101. req,
  2102. res,
  2103. ) {
  2104. // Check user is logged in
  2105. Authentication.checkLoggedIn(req.userId);
  2106. const paramBoardId = req.params.boardId;
  2107. // Check user has permission to add card to the board
  2108. const board = Boards.findOne({
  2109. _id: paramBoardId,
  2110. });
  2111. const addPermission = allowIsBoardMemberCommentOnly(req.userId, board);
  2112. Authentication.checkAdminOrCondition(req.userId, addPermission);
  2113. const paramListId = req.params.listId;
  2114. const paramParentId = req.params.parentId;
  2115. const currentCards = Cards.find(
  2116. {
  2117. listId: paramListId,
  2118. archived: false,
  2119. },
  2120. { sort: ['sort'] },
  2121. );
  2122. const check = Users.findOne({
  2123. _id: req.body.authorId,
  2124. });
  2125. const members = req.body.members;
  2126. const assignees = req.body.assignees;
  2127. if (typeof check !== 'undefined') {
  2128. const id = Cards.direct.insert({
  2129. title: req.body.title,
  2130. boardId: paramBoardId,
  2131. listId: paramListId,
  2132. parentId: paramParentId,
  2133. description: req.body.description,
  2134. userId: req.body.authorId,
  2135. swimlaneId: req.body.swimlaneId,
  2136. sort: currentCards.count(),
  2137. members,
  2138. assignees,
  2139. });
  2140. JsonRoutes.sendResult(res, {
  2141. code: 200,
  2142. data: {
  2143. _id: id,
  2144. },
  2145. });
  2146. const card = Cards.findOne({
  2147. _id: id,
  2148. });
  2149. cardCreation(req.body.authorId, card);
  2150. } else {
  2151. JsonRoutes.sendResult(res, {
  2152. code: 401,
  2153. });
  2154. }
  2155. });
  2156. /*
  2157. * Note for the JSDoc:
  2158. * 'list' will be interpreted as the path parameter
  2159. * 'listID' will be interpreted as the body parameter
  2160. */
  2161. /**
  2162. * @operation edit_card
  2163. * @summary Edit Fields in a Card
  2164. *
  2165. * @description Edit a card
  2166. *
  2167. * The color has to be chosen between `white`, `green`, `yellow`, `orange`,
  2168. * `red`, `purple`, `blue`, `sky`, `lime`, `pink`, `black`, `silver`,
  2169. * `peachpuff`, `crimson`, `plum`, `darkgreen`, `slateblue`, `magenta`,
  2170. * `gold`, `navy`, `gray`, `saddlebrown`, `paleturquoise`, `mistyrose`,
  2171. * `indigo`:
  2172. *
  2173. * <img src="/card-colors.png" width="40%" alt="Wekan card colors" />
  2174. *
  2175. * Note: setting the color to white has the same effect than removing it.
  2176. *
  2177. * @param {string} boardId the board ID of the card
  2178. * @param {string} list the list ID of the card
  2179. * @param {string} cardId the ID of the card
  2180. * @param {string} [title] the new title of the card
  2181. * @param {string} [listId] the new list ID of the card (move operation)
  2182. * @param {string} [description] the new description of the card
  2183. * @param {string} [authorId] change the owner of the card
  2184. * @param {string} [parentId] change the parent of the card
  2185. * @param {string} [labelIds] the new list of label IDs attached to the card
  2186. * @param {string} [swimlaneId] the new swimlane ID of the card
  2187. * @param {string} [members] the new list of member IDs attached to the card
  2188. * @param {string} [assignees] the array of maximum one ID of assignee attached to the card
  2189. * @param {string} [requestedBy] the new requestedBy field of the card
  2190. * @param {string} [assignedBy] the new assignedBy field of the card
  2191. * @param {string} [receivedAt] the new receivedAt field of the card
  2192. * @param {string} [assignBy] the new assignBy field of the card
  2193. * @param {string} [startAt] the new startAt field of the card
  2194. * @param {string} [dueAt] the new dueAt field of the card
  2195. * @param {string} [endAt] the new endAt field of the card
  2196. * @param {string} [spentTime] the new spentTime field of the card
  2197. * @param {boolean} [isOverTime] the new isOverTime field of the card
  2198. * @param {string} [customFields] the new customFields value of the card
  2199. * @param {string} [color] the new color of the card
  2200. * @return_type {_id: string}
  2201. */
  2202. JsonRoutes.add(
  2203. 'PUT',
  2204. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  2205. function(req, res) {
  2206. Authentication.checkUserId(req.userId);
  2207. const paramBoardId = req.params.boardId;
  2208. const paramCardId = req.params.cardId;
  2209. const paramListId = req.params.listId;
  2210. if (req.body.hasOwnProperty('title')) {
  2211. const newTitle = req.body.title;
  2212. Cards.direct.update(
  2213. {
  2214. _id: paramCardId,
  2215. listId: paramListId,
  2216. boardId: paramBoardId,
  2217. archived: false,
  2218. },
  2219. {
  2220. $set: {
  2221. title: newTitle,
  2222. },
  2223. },
  2224. );
  2225. }
  2226. if (req.body.hasOwnProperty('listId')) {
  2227. const newParamListId = req.body.listId;
  2228. Cards.direct.update(
  2229. {
  2230. _id: paramCardId,
  2231. listId: paramListId,
  2232. boardId: paramBoardId,
  2233. archived: false,
  2234. },
  2235. {
  2236. $set: {
  2237. listId: newParamListId,
  2238. },
  2239. },
  2240. );
  2241. const card = Cards.findOne({
  2242. _id: paramCardId,
  2243. });
  2244. cardMove(
  2245. req.body.authorId,
  2246. card,
  2247. {
  2248. fieldName: 'listId',
  2249. },
  2250. paramListId,
  2251. );
  2252. }
  2253. if (req.body.hasOwnProperty('parentId')) {
  2254. const newParentId = req.body.parentId;
  2255. Cards.direct.update(
  2256. {
  2257. _id: paramCardId,
  2258. listId: paramListId,
  2259. boardId: paramBoardId,
  2260. archived: false,
  2261. },
  2262. {
  2263. $set: {
  2264. parentId: newParentId,
  2265. },
  2266. },
  2267. );
  2268. }
  2269. if (req.body.hasOwnProperty('description')) {
  2270. const newDescription = req.body.description;
  2271. Cards.direct.update(
  2272. {
  2273. _id: paramCardId,
  2274. listId: paramListId,
  2275. boardId: paramBoardId,
  2276. archived: false,
  2277. },
  2278. {
  2279. $set: {
  2280. description: newDescription,
  2281. },
  2282. },
  2283. );
  2284. }
  2285. if (req.body.hasOwnProperty('color')) {
  2286. const newColor = req.body.color;
  2287. Cards.direct.update(
  2288. {
  2289. _id: paramCardId,
  2290. listId: paramListId,
  2291. boardId: paramBoardId,
  2292. archived: false,
  2293. },
  2294. { $set: { color: newColor } },
  2295. );
  2296. }
  2297. if (req.body.hasOwnProperty('labelIds')) {
  2298. let newlabelIds = req.body.labelIds;
  2299. if (_.isString(newlabelIds)) {
  2300. if (newlabelIds === '') {
  2301. newlabelIds = null;
  2302. } else {
  2303. newlabelIds = [newlabelIds];
  2304. }
  2305. }
  2306. Cards.direct.update(
  2307. {
  2308. _id: paramCardId,
  2309. listId: paramListId,
  2310. boardId: paramBoardId,
  2311. archived: false,
  2312. },
  2313. {
  2314. $set: {
  2315. labelIds: newlabelIds,
  2316. },
  2317. },
  2318. );
  2319. }
  2320. if (req.body.hasOwnProperty('requestedBy')) {
  2321. const newrequestedBy = req.body.requestedBy;
  2322. Cards.direct.update(
  2323. {
  2324. _id: paramCardId,
  2325. listId: paramListId,
  2326. boardId: paramBoardId,
  2327. archived: false,
  2328. },
  2329. { $set: { requestedBy: newrequestedBy } },
  2330. );
  2331. }
  2332. if (req.body.hasOwnProperty('assignedBy')) {
  2333. const newassignedBy = req.body.assignedBy;
  2334. Cards.direct.update(
  2335. {
  2336. _id: paramCardId,
  2337. listId: paramListId,
  2338. boardId: paramBoardId,
  2339. archived: false,
  2340. },
  2341. { $set: { assignedBy: newassignedBy } },
  2342. );
  2343. }
  2344. if (req.body.hasOwnProperty('receivedAt')) {
  2345. const newreceivedAt = req.body.receivedAt;
  2346. Cards.direct.update(
  2347. {
  2348. _id: paramCardId,
  2349. listId: paramListId,
  2350. boardId: paramBoardId,
  2351. archived: false,
  2352. },
  2353. { $set: { receivedAt: newreceivedAt } },
  2354. );
  2355. }
  2356. if (req.body.hasOwnProperty('startAt')) {
  2357. const newstartAt = req.body.startAt;
  2358. Cards.direct.update(
  2359. {
  2360. _id: paramCardId,
  2361. listId: paramListId,
  2362. boardId: paramBoardId,
  2363. archived: false,
  2364. },
  2365. { $set: { startAt: newstartAt } },
  2366. );
  2367. }
  2368. if (req.body.hasOwnProperty('dueAt')) {
  2369. const newdueAt = req.body.dueAt;
  2370. Cards.direct.update(
  2371. {
  2372. _id: paramCardId,
  2373. listId: paramListId,
  2374. boardId: paramBoardId,
  2375. archived: false,
  2376. },
  2377. { $set: { dueAt: newdueAt } },
  2378. );
  2379. }
  2380. if (req.body.hasOwnProperty('endAt')) {
  2381. const newendAt = req.body.endAt;
  2382. Cards.direct.update(
  2383. {
  2384. _id: paramCardId,
  2385. listId: paramListId,
  2386. boardId: paramBoardId,
  2387. archived: false,
  2388. },
  2389. { $set: { endAt: newendAt } },
  2390. );
  2391. }
  2392. if (req.body.hasOwnProperty('spentTime')) {
  2393. const newspentTime = req.body.spentTime;
  2394. Cards.direct.update(
  2395. {
  2396. _id: paramCardId,
  2397. listId: paramListId,
  2398. boardId: paramBoardId,
  2399. archived: false,
  2400. },
  2401. { $set: { spentTime: newspentTime } },
  2402. );
  2403. }
  2404. if (req.body.hasOwnProperty('isOverTime')) {
  2405. const newisOverTime = req.body.isOverTime;
  2406. Cards.direct.update(
  2407. {
  2408. _id: paramCardId,
  2409. listId: paramListId,
  2410. boardId: paramBoardId,
  2411. archived: false,
  2412. },
  2413. { $set: { isOverTime: newisOverTime } },
  2414. );
  2415. }
  2416. if (req.body.hasOwnProperty('customFields')) {
  2417. const newcustomFields = req.body.customFields;
  2418. Cards.direct.update(
  2419. {
  2420. _id: paramCardId,
  2421. listId: paramListId,
  2422. boardId: paramBoardId,
  2423. archived: false,
  2424. },
  2425. { $set: { customFields: newcustomFields } },
  2426. );
  2427. }
  2428. if (req.body.hasOwnProperty('members')) {
  2429. let newmembers = req.body.members;
  2430. if (_.isString(newmembers)) {
  2431. if (newmembers === '') {
  2432. newmembers = null;
  2433. } else {
  2434. newmembers = [newmembers];
  2435. }
  2436. }
  2437. Cards.direct.update(
  2438. {
  2439. _id: paramCardId,
  2440. listId: paramListId,
  2441. boardId: paramBoardId,
  2442. archived: false,
  2443. },
  2444. { $set: { members: newmembers } },
  2445. );
  2446. }
  2447. if (req.body.hasOwnProperty('assignees')) {
  2448. let newassignees = req.body.assignees;
  2449. if (_.isString(newassignees)) {
  2450. if (newassignees === '') {
  2451. newassignees = null;
  2452. } else {
  2453. newassignees = [newassignees];
  2454. }
  2455. }
  2456. Cards.direct.update(
  2457. {
  2458. _id: paramCardId,
  2459. listId: paramListId,
  2460. boardId: paramBoardId,
  2461. archived: false,
  2462. },
  2463. { $set: { assignees: newassignees } },
  2464. );
  2465. }
  2466. if (req.body.hasOwnProperty('swimlaneId')) {
  2467. const newParamSwimlaneId = req.body.swimlaneId;
  2468. Cards.direct.update(
  2469. {
  2470. _id: paramCardId,
  2471. listId: paramListId,
  2472. boardId: paramBoardId,
  2473. archived: false,
  2474. },
  2475. { $set: { swimlaneId: newParamSwimlaneId } },
  2476. );
  2477. }
  2478. JsonRoutes.sendResult(res, {
  2479. code: 200,
  2480. data: {
  2481. _id: paramCardId,
  2482. },
  2483. });
  2484. },
  2485. );
  2486. /**
  2487. * @operation delete_card
  2488. * @summary Delete a card from a board
  2489. *
  2490. * @description This operation **deletes** a card, and therefore the card
  2491. * is not put in the recycle bin.
  2492. *
  2493. * @param {string} boardId the board ID of the card
  2494. * @param {string} list the list ID of the card
  2495. * @param {string} cardId the ID of the card
  2496. * @return_type {_id: string}
  2497. */
  2498. JsonRoutes.add(
  2499. 'DELETE',
  2500. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  2501. function(req, res) {
  2502. Authentication.checkUserId(req.userId);
  2503. const paramBoardId = req.params.boardId;
  2504. const paramListId = req.params.listId;
  2505. const paramCardId = req.params.cardId;
  2506. const card = Cards.findOne({
  2507. _id: paramCardId,
  2508. });
  2509. Cards.direct.remove({
  2510. _id: paramCardId,
  2511. listId: paramListId,
  2512. boardId: paramBoardId,
  2513. });
  2514. cardRemover(req.body.authorId, card);
  2515. JsonRoutes.sendResult(res, {
  2516. code: 200,
  2517. data: {
  2518. _id: paramCardId,
  2519. },
  2520. });
  2521. },
  2522. );
  2523. }
  2524. export default Cards;