positionHistory.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. /**
  3. * PositionHistory collection to track original positions of swimlanes, lists, and cards
  4. * before the list naming feature was added in commit 719ef87efceacfe91461a8eeca7cf74d11f4cc0a
  5. */
  6. PositionHistory = new Mongo.Collection('positionHistory');
  7. PositionHistory.attachSchema(
  8. new SimpleSchema({
  9. boardId: {
  10. /**
  11. * The board ID this position history belongs to
  12. */
  13. type: String,
  14. },
  15. entityType: {
  16. /**
  17. * Type of entity: 'swimlane', 'list', or 'card'
  18. */
  19. type: String,
  20. allowedValues: ['swimlane', 'list', 'card'],
  21. },
  22. entityId: {
  23. /**
  24. * The ID of the entity (swimlane, list, or card)
  25. */
  26. type: String,
  27. },
  28. originalPosition: {
  29. /**
  30. * The original position data before any changes
  31. */
  32. type: Object,
  33. blackbox: true,
  34. },
  35. originalSwimlaneId: {
  36. /**
  37. * The original swimlane ID (for lists and cards)
  38. */
  39. type: String,
  40. optional: true,
  41. },
  42. originalListId: {
  43. /**
  44. * The original list ID (for cards)
  45. */
  46. type: String,
  47. optional: true,
  48. },
  49. originalTitle: {
  50. /**
  51. * The original title before any changes
  52. */
  53. type: String,
  54. optional: true,
  55. },
  56. createdAt: {
  57. /**
  58. * When this position history was created
  59. */
  60. type: Date,
  61. autoValue() {
  62. if (this.isInsert) {
  63. return new Date();
  64. } else if (this.isUpsert) {
  65. return { $setOnInsert: new Date() };
  66. } else {
  67. this.unset();
  68. }
  69. },
  70. },
  71. updatedAt: {
  72. /**
  73. * When this position history was last updated
  74. */
  75. type: Date,
  76. autoValue() {
  77. if (this.isUpdate || this.isUpsert || this.isInsert) {
  78. return new Date();
  79. } else {
  80. this.unset();
  81. }
  82. },
  83. },
  84. }),
  85. );
  86. PositionHistory.helpers({
  87. /**
  88. * Get the original position data
  89. */
  90. getOriginalPosition() {
  91. return this.originalPosition;
  92. },
  93. /**
  94. * Get the original title
  95. */
  96. getOriginalTitle() {
  97. return this.originalTitle || '';
  98. },
  99. /**
  100. * Get the original swimlane ID
  101. */
  102. getOriginalSwimlaneId() {
  103. return this.originalSwimlaneId;
  104. },
  105. /**
  106. * Get the original list ID
  107. */
  108. getOriginalListId() {
  109. return this.originalListId;
  110. },
  111. /**
  112. * Check if this entity has been moved from its original position
  113. */
  114. hasMoved() {
  115. if (this.entityType === 'swimlane') {
  116. return this.originalPosition.sort !== undefined;
  117. } else if (this.entityType === 'list') {
  118. return this.originalPosition.sort !== undefined ||
  119. this.originalSwimlaneId !== this.entityId;
  120. } else if (this.entityType === 'card') {
  121. return this.originalPosition.sort !== undefined ||
  122. this.originalSwimlaneId !== this.entityId ||
  123. this.originalListId !== this.entityId;
  124. }
  125. return false;
  126. },
  127. /**
  128. * Get a human-readable description of the original position
  129. */
  130. getOriginalPositionDescription() {
  131. const position = this.originalPosition;
  132. if (!position) return 'Unknown position';
  133. if (this.entityType === 'swimlane') {
  134. return `Original position: ${position.sort || 0}`;
  135. } else if (this.entityType === 'list') {
  136. const swimlaneInfo = this.originalSwimlaneId ?
  137. ` in swimlane ${this.originalSwimlaneId}` :
  138. ' in default swimlane';
  139. return `Original position: ${position.sort || 0}${swimlaneInfo}`;
  140. } else if (this.entityType === 'card') {
  141. const swimlaneInfo = this.originalSwimlaneId ?
  142. ` in swimlane ${this.originalSwimlaneId}` :
  143. ' in default swimlane';
  144. const listInfo = this.originalListId ?
  145. ` in list ${this.originalListId}` :
  146. '';
  147. return `Original position: ${position.sort || 0}${swimlaneInfo}${listInfo}`;
  148. }
  149. return 'Unknown position';
  150. }
  151. });
  152. if (Meteor.isServer) {
  153. Meteor.startup(() => {
  154. PositionHistory._collection.createIndex({ boardId: 1, entityType: 1, entityId: 1 });
  155. PositionHistory._collection.createIndex({ boardId: 1, entityType: 1 });
  156. PositionHistory._collection.createIndex({ createdAt: -1 });
  157. });
  158. }
  159. export default PositionHistory;