2
0

fixDuplicateLists.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import { Meteor } from 'meteor/meteor';
  2. import { check } from 'meteor/check';
  3. import Boards from '/models/boards';
  4. import Lists from '/models/lists';
  5. import Swimlanes from '/models/swimlanes';
  6. import Cards from '/models/cards';
  7. /**
  8. * Fix duplicate lists and swimlanes created by WeKan 8.10
  9. * This method identifies and removes duplicate lists while preserving cards
  10. */
  11. Meteor.methods({
  12. 'fixDuplicateLists.fixAllBoards'() {
  13. if (!this.userId) {
  14. throw new Meteor.Error('not-authorized');
  15. }
  16. if (process.env.DEBUG === 'true') {
  17. console.log('Starting duplicate lists fix for all boards...');
  18. }
  19. const allBoards = Boards.find({}).fetch();
  20. let totalFixed = 0;
  21. let totalBoardsProcessed = 0;
  22. for (const board of allBoards) {
  23. try {
  24. const result = this.fixDuplicateListsForBoard(board._id);
  25. totalFixed += result.fixed;
  26. totalBoardsProcessed++;
  27. if (result.fixed > 0 && process.env.DEBUG === 'true') {
  28. console.log(`Fixed ${result.fixed} duplicate lists in board "${board.title}" (${board._id})`);
  29. }
  30. } catch (error) {
  31. console.error(`Error fixing board ${board._id}:`, error);
  32. }
  33. }
  34. if (process.env.DEBUG === 'true') {
  35. console.log(`Duplicate lists fix completed. Processed ${totalBoardsProcessed} boards, fixed ${totalFixed} duplicate lists.`);
  36. }
  37. return {
  38. message: `Fixed ${totalFixed} duplicate lists across ${totalBoardsProcessed} boards`,
  39. totalFixed,
  40. totalBoardsProcessed
  41. };
  42. },
  43. 'fixDuplicateLists.fixBoard'(boardId) {
  44. check(boardId, String);
  45. if (!this.userId) {
  46. throw new Meteor.Error('not-authorized');
  47. }
  48. return this.fixDuplicateListsForBoard(boardId);
  49. },
  50. fixDuplicateListsForBoard(boardId) {
  51. if (process.env.DEBUG === 'true') {
  52. console.log(`Fixing duplicate lists for board ${boardId}...`);
  53. }
  54. // First, fix duplicate swimlanes
  55. const swimlaneResult = this.fixDuplicateSwimlanes(boardId);
  56. // Then, fix duplicate lists
  57. const listResult = this.fixDuplicateLists(boardId);
  58. return {
  59. boardId,
  60. fixedSwimlanes: swimlaneResult.fixed,
  61. fixedLists: listResult.fixed,
  62. fixed: swimlaneResult.fixed + listResult.fixed
  63. };
  64. },
  65. fixDuplicateSwimlanes(boardId) {
  66. const swimlanes = Swimlanes.find({ boardId }).fetch();
  67. const swimlaneGroups = {};
  68. let fixed = 0;
  69. // Group swimlanes by title
  70. swimlanes.forEach(swimlane => {
  71. const key = swimlane.title || 'Default';
  72. if (!swimlaneGroups[key]) {
  73. swimlaneGroups[key] = [];
  74. }
  75. swimlaneGroups[key].push(swimlane);
  76. });
  77. // For each group with duplicates, keep the oldest and remove the rest
  78. Object.keys(swimlaneGroups).forEach(title => {
  79. const group = swimlaneGroups[title];
  80. if (group.length > 1) {
  81. // Sort by creation date, keep the oldest
  82. group.sort((a, b) => new Date(a.createdAt || 0) - new Date(b.createdAt || 0));
  83. const keepSwimlane = group[0];
  84. const removeSwimlanes = group.slice(1);
  85. if (process.env.DEBUG === 'true') {
  86. console.log(`Found ${group.length} duplicate swimlanes with title "${title}", keeping oldest (${keepSwimlane._id})`);
  87. }
  88. // Move all lists from duplicate swimlanes to the kept swimlane
  89. removeSwimlanes.forEach(swimlane => {
  90. const lists = Lists.find({ swimlaneId: swimlane._id }).fetch();
  91. lists.forEach(list => {
  92. // Check if a list with the same title already exists in the kept swimlane
  93. const existingList = Lists.findOne({
  94. boardId,
  95. swimlaneId: keepSwimlane._id,
  96. title: list.title
  97. });
  98. if (existingList) {
  99. // Move cards to existing list
  100. Cards.update(
  101. { listId: list._id },
  102. { $set: { listId: existingList._id } },
  103. { multi: true }
  104. );
  105. // Remove duplicate list
  106. Lists.remove(list._id);
  107. if (process.env.DEBUG === 'true') {
  108. console.log(`Moved cards from duplicate list "${list.title}" to existing list in kept swimlane`);
  109. }
  110. } else {
  111. // Move list to kept swimlane
  112. Lists.update(list._id, { $set: { swimlaneId: keepSwimlane._id } });
  113. if (process.env.DEBUG === 'true') {
  114. console.log(`Moved list "${list.title}" to kept swimlane`);
  115. }
  116. }
  117. });
  118. // Remove duplicate swimlane
  119. Swimlanes.remove(swimlane._id);
  120. fixed++;
  121. });
  122. }
  123. });
  124. return { fixed };
  125. },
  126. fixDuplicateLists(boardId) {
  127. const lists = Lists.find({ boardId }).fetch();
  128. const listGroups = {};
  129. let fixed = 0;
  130. // Group lists by title and swimlaneId
  131. lists.forEach(list => {
  132. const key = `${list.swimlaneId || 'null'}-${list.title}`;
  133. if (!listGroups[key]) {
  134. listGroups[key] = [];
  135. }
  136. listGroups[key].push(list);
  137. });
  138. // For each group with duplicates, keep the oldest and remove the rest
  139. Object.keys(listGroups).forEach(key => {
  140. const group = listGroups[key];
  141. if (group.length > 1) {
  142. // Sort by creation date, keep the oldest
  143. group.sort((a, b) => new Date(a.createdAt || 0) - new Date(b.createdAt || 0));
  144. const keepList = group[0];
  145. const removeLists = group.slice(1);
  146. if (process.env.DEBUG === 'true') {
  147. console.log(`Found ${group.length} duplicate lists with title "${keepList.title}" in swimlane ${keepList.swimlaneId}, keeping oldest (${keepList._id})`);
  148. }
  149. // Move all cards from duplicate lists to the kept list
  150. removeLists.forEach(list => {
  151. Cards.update(
  152. { listId: list._id },
  153. { $set: { listId: keepList._id } },
  154. { multi: true }
  155. );
  156. // Remove duplicate list
  157. Lists.remove(list._id);
  158. fixed++;
  159. if (process.env.DEBUG === 'true') {
  160. console.log(`Moved cards from duplicate list "${list.title}" to kept list`);
  161. }
  162. });
  163. }
  164. });
  165. return { fixed };
  166. },
  167. 'fixDuplicateLists.getReport'() {
  168. if (!this.userId) {
  169. throw new Meteor.Error('not-authorized');
  170. }
  171. const allBoards = Boards.find({}).fetch();
  172. const report = [];
  173. for (const board of allBoards) {
  174. const swimlanes = Swimlanes.find({ boardId: board._id }).fetch();
  175. const lists = Lists.find({ boardId: board._id }).fetch();
  176. // Check for duplicate swimlanes
  177. const swimlaneGroups = {};
  178. swimlanes.forEach(swimlane => {
  179. const key = swimlane.title || 'Default';
  180. if (!swimlaneGroups[key]) {
  181. swimlaneGroups[key] = [];
  182. }
  183. swimlaneGroups[key].push(swimlane);
  184. });
  185. // Check for duplicate lists
  186. const listGroups = {};
  187. lists.forEach(list => {
  188. const key = `${list.swimlaneId || 'null'}-${list.title}`;
  189. if (!listGroups[key]) {
  190. listGroups[key] = [];
  191. }
  192. listGroups[key].push(list);
  193. });
  194. const duplicateSwimlanes = Object.values(swimlaneGroups).filter(group => group.length > 1);
  195. const duplicateLists = Object.values(listGroups).filter(group => group.length > 1);
  196. if (duplicateSwimlanes.length > 0 || duplicateLists.length > 0) {
  197. report.push({
  198. boardId: board._id,
  199. boardTitle: board.title,
  200. duplicateSwimlanes: duplicateSwimlanes.length,
  201. duplicateLists: duplicateLists.length,
  202. totalSwimlanes: swimlanes.length,
  203. totalLists: lists.length
  204. });
  205. }
  206. }
  207. return {
  208. totalBoards: allBoards.length,
  209. boardsWithDuplicates: report.length,
  210. report
  211. };
  212. }
  213. });