restoreAllArchived.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /**
  2. * Restore All Archived Migration
  3. *
  4. * Restores all archived swimlanes, lists, and cards.
  5. * If any restored items are missing swimlaneId, listId, or cardId,
  6. * creates/assigns proper IDs to make them visible.
  7. */
  8. import { Meteor } from 'meteor/meteor';
  9. import { check } from 'meteor/check';
  10. import { ReactiveCache } from '/imports/reactiveCache';
  11. import { TAPi18n } from '/imports/i18n';
  12. import Boards from '/models/boards';
  13. import Lists from '/models/lists';
  14. import Cards from '/models/cards';
  15. import Swimlanes from '/models/swimlanes';
  16. class RestoreAllArchivedMigration {
  17. constructor() {
  18. this.name = 'restoreAllArchived';
  19. this.version = 1;
  20. }
  21. /**
  22. * Check if migration is needed for a board
  23. */
  24. needsMigration(boardId) {
  25. try {
  26. const archivedSwimlanes = ReactiveCache.getSwimlanes({ boardId, archived: true });
  27. const archivedLists = ReactiveCache.getLists({ boardId, archived: true });
  28. const archivedCards = ReactiveCache.getCards({ boardId, archived: true });
  29. return archivedSwimlanes.length > 0 || archivedLists.length > 0 || archivedCards.length > 0;
  30. } catch (error) {
  31. console.error('Error checking if restoreAllArchived migration is needed:', error);
  32. return false;
  33. }
  34. }
  35. /**
  36. * Execute the migration
  37. */
  38. async executeMigration(boardId) {
  39. try {
  40. const results = {
  41. swimlanesRestored: 0,
  42. listsRestored: 0,
  43. cardsRestored: 0,
  44. itemsFixed: 0,
  45. errors: []
  46. };
  47. const board = ReactiveCache.getBoard(boardId);
  48. if (!board) {
  49. throw new Error('Board not found');
  50. }
  51. // Get archived items
  52. const archivedSwimlanes = ReactiveCache.getSwimlanes({ boardId, archived: true });
  53. const archivedLists = ReactiveCache.getLists({ boardId, archived: true });
  54. const archivedCards = ReactiveCache.getCards({ boardId, archived: true });
  55. // Get active items for reference
  56. const activeSwimlanes = ReactiveCache.getSwimlanes({ boardId, archived: false });
  57. const activeLists = ReactiveCache.getLists({ boardId, archived: false });
  58. // Restore all archived swimlanes
  59. for (const swimlane of archivedSwimlanes) {
  60. Swimlanes.update(swimlane._id, {
  61. $set: {
  62. archived: false,
  63. updatedAt: new Date()
  64. }
  65. });
  66. results.swimlanesRestored++;
  67. if (process.env.DEBUG === 'true') {
  68. console.log(`Restored swimlane: ${swimlane.title}`);
  69. }
  70. }
  71. // Restore all archived lists and fix missing swimlaneId
  72. for (const list of archivedLists) {
  73. const updateFields = {
  74. archived: false,
  75. updatedAt: new Date()
  76. };
  77. // Fix missing swimlaneId
  78. if (!list.swimlaneId) {
  79. // Try to find a suitable swimlane or use default
  80. let targetSwimlane = activeSwimlanes.find(s => !s.archived);
  81. if (!targetSwimlane) {
  82. // No active swimlane found, create default
  83. const swimlaneId = Swimlanes.insert({
  84. title: TAPi18n.__('default'),
  85. boardId: boardId,
  86. sort: 0,
  87. createdAt: new Date(),
  88. updatedAt: new Date(),
  89. archived: false
  90. });
  91. targetSwimlane = ReactiveCache.getSwimlane(swimlaneId);
  92. }
  93. updateFields.swimlaneId = targetSwimlane._id;
  94. results.itemsFixed++;
  95. if (process.env.DEBUG === 'true') {
  96. console.log(`Fixed missing swimlaneId for list: ${list.title}`);
  97. }
  98. }
  99. Lists.update(list._id, {
  100. $set: updateFields
  101. });
  102. results.listsRestored++;
  103. if (process.env.DEBUG === 'true') {
  104. console.log(`Restored list: ${list.title}`);
  105. }
  106. }
  107. // Refresh lists after restoration
  108. const allLists = ReactiveCache.getLists({ boardId, archived: false });
  109. const allSwimlanes = ReactiveCache.getSwimlanes({ boardId, archived: false });
  110. // Restore all archived cards and fix missing IDs
  111. for (const card of archivedCards) {
  112. const updateFields = {
  113. archived: false,
  114. updatedAt: new Date()
  115. };
  116. let needsFix = false;
  117. // Fix missing listId
  118. if (!card.listId) {
  119. // Find or create a default list
  120. let targetList = allLists.find(l => !l.archived);
  121. if (!targetList) {
  122. // No active list found, create one
  123. const defaultSwimlane = allSwimlanes.find(s => !s.archived) || allSwimlanes[0];
  124. const listId = Lists.insert({
  125. title: TAPi18n.__('default'),
  126. boardId: boardId,
  127. swimlaneId: defaultSwimlane._id,
  128. sort: 0,
  129. createdAt: new Date(),
  130. updatedAt: new Date(),
  131. archived: false
  132. });
  133. targetList = ReactiveCache.getList(listId);
  134. }
  135. updateFields.listId = targetList._id;
  136. needsFix = true;
  137. }
  138. // Fix missing swimlaneId
  139. if (!card.swimlaneId) {
  140. // Try to get swimlaneId from the card's list
  141. if (card.listId || updateFields.listId) {
  142. const cardList = allLists.find(l => l._id === (updateFields.listId || card.listId));
  143. if (cardList && cardList.swimlaneId) {
  144. updateFields.swimlaneId = cardList.swimlaneId;
  145. } else {
  146. // Fall back to first available swimlane
  147. const defaultSwimlane = allSwimlanes.find(s => !s.archived) || allSwimlanes[0];
  148. updateFields.swimlaneId = defaultSwimlane._id;
  149. }
  150. } else {
  151. // Fall back to first available swimlane
  152. const defaultSwimlane = allSwimlanes.find(s => !s.archived) || allSwimlanes[0];
  153. updateFields.swimlaneId = defaultSwimlane._id;
  154. }
  155. needsFix = true;
  156. }
  157. if (needsFix) {
  158. results.itemsFixed++;
  159. if (process.env.DEBUG === 'true') {
  160. console.log(`Fixed missing IDs for card: ${card.title}`);
  161. }
  162. }
  163. Cards.update(card._id, {
  164. $set: updateFields
  165. });
  166. results.cardsRestored++;
  167. if (process.env.DEBUG === 'true') {
  168. console.log(`Restored card: ${card.title}`);
  169. }
  170. }
  171. return {
  172. success: true,
  173. changes: [
  174. `Restored ${results.swimlanesRestored} archived swimlanes`,
  175. `Restored ${results.listsRestored} archived lists`,
  176. `Restored ${results.cardsRestored} archived cards`,
  177. `Fixed ${results.itemsFixed} items with missing IDs`
  178. ],
  179. results
  180. };
  181. } catch (error) {
  182. console.error('Error executing restoreAllArchived migration:', error);
  183. return {
  184. success: false,
  185. error: error.message
  186. };
  187. }
  188. }
  189. }
  190. const restoreAllArchivedMigration = new RestoreAllArchivedMigration();
  191. // Register Meteor methods
  192. Meteor.methods({
  193. 'restoreAllArchived.needsMigration'(boardId) {
  194. check(boardId, String);
  195. if (!this.userId) {
  196. throw new Meteor.Error('not-authorized', 'You must be logged in');
  197. }
  198. return restoreAllArchivedMigration.needsMigration(boardId);
  199. },
  200. 'restoreAllArchived.execute'(boardId) {
  201. check(boardId, String);
  202. if (!this.userId) {
  203. throw new Meteor.Error('not-authorized', 'You must be logged in');
  204. }
  205. // Check if user is board admin
  206. const board = ReactiveCache.getBoard(boardId);
  207. if (!board) {
  208. throw new Meteor.Error('board-not-found', 'Board not found');
  209. }
  210. const user = ReactiveCache.getUser(this.userId);
  211. if (!user) {
  212. throw new Meteor.Error('user-not-found', 'User not found');
  213. }
  214. // Only board admins can run migrations
  215. const isBoardAdmin = board.members && board.members.some(
  216. member => member.userId === this.userId && member.isAdmin
  217. );
  218. if (!isBoardAdmin && !user.isAdmin) {
  219. throw new Meteor.Error('not-authorized', 'Only board administrators can run migrations');
  220. }
  221. return restoreAllArchivedMigration.executeMigration(boardId);
  222. }
  223. });
  224. export default restoreAllArchivedMigration;