11import { desc , eq , sql } from "drizzle-orm" ;
22
33import { db } from "db/config.server" ;
4- import { gataReport , reportFile } from "db/schema" ;
4+ import { gataReport , oldReportFiles , cloudinaryImage , reportFiles } from "db/schema" ;
55import { ReportType } from "~/types/GataReport.type" ;
66import type { ReportSchema } from "~/utils/formSchema" ;
77
88import type { User } from "./user" ;
9- import { deleteImage } from "../services/cloudinaryService" ;
109
1110export const getReportsSimple = async ( reportType : ReportType ) => {
1211 return await db
@@ -90,14 +89,13 @@ export const updateReport = async (reportId: string, values: ReportSchema, logge
9089} ;
9190
9291export const deleteReport = async ( reportId : string ) => {
93- const reportFiles = await db . select ( ) . from ( reportFile ) . where ( eq ( reportFile . reportId , reportId ) ) ;
92+ const reportFiles = await db . select ( ) . from ( oldReportFiles ) . where ( eq ( oldReportFiles . reportId , reportId ) ) ;
9493 await Promise . all (
9594 reportFiles . map ( async ( file ) => {
9695 if ( ! file . cloudId ) {
9796 throw new Error ( "No cloud id!" ) ;
9897 }
99- await deleteImage ( file . cloudId ) ;
100- await db . delete ( reportFile ) . where ( eq ( reportFile . id , file . id ) ) ;
98+ await db . delete ( oldReportFiles ) . where ( eq ( oldReportFiles . id , file . id ) ) ;
10199 } )
102100 ) ;
103101 await db . delete ( gataReport ) . where ( eq ( gataReport . id , reportId ) ) ;
@@ -113,3 +111,35 @@ export const updateReportContent = async (reportId: string, content: string, log
113111 } )
114112 . where ( eq ( gataReport . id , reportId ) ) ;
115113} ;
114+
115+ export const getAllReportsWithOldFiles = async ( ) => {
116+ const reports = await db
117+ . select ( {
118+ id : gataReport . id ,
119+ content : gataReport . content ,
120+ } )
121+ . from ( gataReport ) ;
122+
123+ const reportsWithFiles = await Promise . all (
124+ reports . map ( async ( report ) => {
125+ const files = await db . select ( ) . from ( oldReportFiles ) . where ( eq ( oldReportFiles . reportId , report . id ) ) ;
126+ return { ...report , oldFiles : files } ;
127+ } )
128+ ) ;
129+
130+ return reportsWithFiles . filter ( ( report ) => report . oldFiles . length > 0 ) ;
131+ } ;
132+
133+ export const batchInsertCloudImages = async ( images : Array < typeof cloudinaryImage . $inferInsert > ) => {
134+ if ( images . length === 0 ) return ;
135+ await db . insert ( cloudinaryImage ) . values ( images ) ;
136+ } ;
137+
138+ export const batchInsertReportFiles = async ( reportFileRecords : Array < { reportId : string ; fileId : string } > ) => {
139+ if ( reportFileRecords . length === 0 ) return ;
140+ await db . insert ( reportFiles ) . values ( reportFileRecords ) ;
141+ } ;
142+
143+ export const updateReportContentOnly = async ( reportId : string , content : string ) => {
144+ await db . update ( gataReport ) . set ( { content } ) . where ( eq ( gataReport . id , reportId ) ) ;
145+ } ;
0 commit comments