|
| 1 | +import { getDataSource } from "../../../helpers/db"; |
| 2 | +import { Project } from "../../../model"; |
| 3 | +import { SourceConfig } from "../types"; |
1 | 4 | import { type RlProjectInfo } from "./type"; |
2 | 5 |
|
3 | 6 | export const generateRlUrl = (project: RlProjectInfo) => { |
4 | 7 | return `/project/${project.id}`; |
5 | 8 | }; |
| 9 | + |
| 10 | +export const manageProjectRemovals = async ( |
| 11 | + newList: RlProjectInfo[] | null, |
| 12 | + sourceConfig: SourceConfig, |
| 13 | + round: number // Pass the current round |
| 14 | +) => { |
| 15 | + if (newList === null) { |
| 16 | + console.log( |
| 17 | + `[${new Date().toISOString()}] - ERROR: Failed to manage project removals. New list is null.` |
| 18 | + ); |
| 19 | + return; |
| 20 | + } |
| 21 | + try { |
| 22 | + const dataSource = await getDataSource(); |
| 23 | + if (!dataSource) { |
| 24 | + console.log( |
| 25 | + `[${new Date().toISOString()}] - ERROR: Failed to remove projects. Data source not found.` |
| 26 | + ); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + const shouldKeepProjects = newList |
| 31 | + .filter((project) => project.prelimResult === "Keep") |
| 32 | + .map((project) => `${sourceConfig.source}-${project.id}`); |
| 33 | + console.log("shouldKeepProjects", shouldKeepProjects); |
| 34 | + |
| 35 | + const existingProjectsIds = await dataSource |
| 36 | + .getRepository(Project) |
| 37 | + .createQueryBuilder("project") |
| 38 | + .select("project.id") |
| 39 | + .where("project.source = :source", { source: sourceConfig.source }) |
| 40 | + .andWhere(":round = ANY(project.rfRounds)", { round }) // Only projects in the current round |
| 41 | + .getMany() |
| 42 | + .then((projects) => projects.map((proj) => proj.id)); |
| 43 | + |
| 44 | + console.log("existingProjectsIds", existingProjectsIds); |
| 45 | + |
| 46 | + const projectIdsToManipulate = existingProjectsIds.filter( |
| 47 | + (id) => !shouldKeepProjects.includes(id) |
| 48 | + ); |
| 49 | + |
| 50 | + console.log("projectIdsToManipulate", projectIdsToManipulate); |
| 51 | + |
| 52 | + if (projectIdsToManipulate.length === 0) { |
| 53 | + // No projects to Manipulate |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + // Fetch projects to Remove, including their attests |
| 58 | + const projectsToManipulate = await dataSource |
| 59 | + .getRepository(Project) |
| 60 | + .createQueryBuilder("project") |
| 61 | + .leftJoinAndSelect("project.attests", "attests") |
| 62 | + .where("project.id IN (:...ids)", { ids: projectIdsToManipulate }) |
| 63 | + .getMany(); |
| 64 | + |
| 65 | + const projectsToUpdateRfRounds: Array<{ |
| 66 | + id: string; |
| 67 | + updatedRfRounds: number[]; |
| 68 | + }> = []; |
| 69 | + const projectsToRemove: Project[] = []; |
| 70 | + const projectsToMakeUnImported: Project[] = []; |
| 71 | + |
| 72 | + for (const project of projectsToManipulate) { |
| 73 | + const updatedRfRounds = (project.rfRounds || []).filter( |
| 74 | + (rfr) => rfr !== round |
| 75 | + ); |
| 76 | + if (updatedRfRounds.length > 0) { |
| 77 | + // Collect projects to update |
| 78 | + projectsToUpdateRfRounds.push({ id: project.id, updatedRfRounds }); |
| 79 | + } else { |
| 80 | + if (project.attests.length > 0) { |
| 81 | + projectsToMakeUnImported.push(project); |
| 82 | + } else { |
| 83 | + projectsToRemove.push(project); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + const updateRfRoundsPromises = projectsToUpdateRfRounds.map( |
| 89 | + async ({ id, updatedRfRounds }) => { |
| 90 | + try { |
| 91 | + await dataSource |
| 92 | + .createQueryBuilder() |
| 93 | + .update(Project) |
| 94 | + .set({ rfRounds: updatedRfRounds }) |
| 95 | + .where("id = :id", { id }) |
| 96 | + .execute(); |
| 97 | + console.log( |
| 98 | + `[INFO]: Project updated with removed round. Project ID: ${id}` |
| 99 | + ); |
| 100 | + } catch (updateError: any) { |
| 101 | + console.log( |
| 102 | + `[ERROR]: Failed to update project rfRounds. Project ID: ${id}. Error: ${updateError.message}` |
| 103 | + ); |
| 104 | + throw updateError; // Re-throw to let Promise.all handle it |
| 105 | + } |
| 106 | + } |
| 107 | + ); |
| 108 | + try { |
| 109 | + await Promise.all(updateRfRoundsPromises); |
| 110 | + } catch (error: any) { |
| 111 | + console.log( |
| 112 | + `[ERROR]: One or more updates failed. Error: ${error.message}` |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + // Bulk delete projects without attests |
| 117 | + if (projectsToRemove.length > 0) { |
| 118 | + const projectIdsToDelete = projectsToRemove.map((project) => project.id); |
| 119 | + try { |
| 120 | + await dataSource |
| 121 | + .createQueryBuilder() |
| 122 | + .delete() |
| 123 | + .from(Project) |
| 124 | + .where("id IN (:...ids)", { ids: projectIdsToDelete }) |
| 125 | + .execute(); |
| 126 | + |
| 127 | + console.log( |
| 128 | + `[${new Date().toISOString()}] - INFO: Projects Deleted. Project IDs: ${projectIdsToDelete.join( |
| 129 | + ", " |
| 130 | + )}` |
| 131 | + ); |
| 132 | + } catch (error: any) { |
| 133 | + console.log( |
| 134 | + `[${new Date().toISOString()}] - ERROR: Failed to delete projects. Project IDs: ${projectIdsToDelete.join( |
| 135 | + ", " |
| 136 | + )}. Error: ${error.message}` |
| 137 | + ); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + // Bulk update projects with attests to mark as not imported |
| 142 | + if (projectsToMakeUnImported.length > 0) { |
| 143 | + const projectIdsToMakeUnImported = projectsToMakeUnImported.map( |
| 144 | + (project) => project.id |
| 145 | + ); |
| 146 | + try { |
| 147 | + await dataSource |
| 148 | + .createQueryBuilder() |
| 149 | + .update(Project) |
| 150 | + .set({ imported: false, rfRounds: [round] }) |
| 151 | + .where("id IN (:...ids)", { ids: projectIdsToMakeUnImported }) |
| 152 | + .execute(); |
| 153 | + |
| 154 | + console.log( |
| 155 | + `[${new Date().toISOString()}] - INFO: Projects marked as not imported. Project IDs: ${projectIdsToMakeUnImported.join( |
| 156 | + ", " |
| 157 | + )}` |
| 158 | + ); |
| 159 | + } catch (error: any) { |
| 160 | + console.log( |
| 161 | + `[${new Date().toISOString()}] - ERROR: Failed to mark projects as not imported. Project IDs: ${projectIdsToMakeUnImported.join( |
| 162 | + ", " |
| 163 | + )}. Error: ${error.message}` |
| 164 | + ); |
| 165 | + } |
| 166 | + } |
| 167 | + } catch (error: any) { |
| 168 | + console.log( |
| 169 | + `[${new Date().toISOString()}] - ERROR: Failed to manage project removals. Error: ${error.message}` |
| 170 | + ); |
| 171 | + } |
| 172 | +}; |
0 commit comments