@@ -81,7 +81,7 @@ export default class OtherCostsPatchRoute extends CampaignRoute<{
8181 private async updateOtherCost (
8282 body : StoplightOperations [ "patch-campaigns-campaign-finance-otherCosts" ] [ "requestBody" ] [ "content" ] [ "application/json" ]
8383 ) : Promise < void > {
84- await this . deleteExistingAttachments ( body . cost_id ) ;
84+ await this . updateAttachments ( ) ;
8585
8686 await tryber . tables . WpAppqCampaignOtherCosts . do ( )
8787 . where ( { id : body . cost_id } )
@@ -91,22 +91,29 @@ export default class OtherCostsPatchRoute extends CampaignRoute<{
9191 type_id : body . type_id ,
9292 supplier_id : body . supplier_id ,
9393 } ) ;
94-
95- if ( body . attachments && body . attachments . length > 0 ) {
96- await this . createAttachments ( body . cost_id , body . attachments ) ;
97- }
9894 }
9995
100- private async deleteExistingAttachments ( costId : number ) : Promise < void > {
101- const attachments =
96+ private async updateAttachments ( ) : Promise < void > {
97+ const { cost_id, attachments } = this . getBody ( ) ;
98+ const existingAttachments =
10299 await tryber . tables . WpAppqCampaignOtherCostsAttachment . do ( )
103- . select ( "url" , "id" )
104- . where ( { cost_id : costId } ) ;
100+ . select ( "id" , "url" , "mime_type" )
101+ . where ( { cost_id : cost_id } ) ;
102+
103+ const existingUrls = existingAttachments . map ( ( a ) => a . url ) ;
104+ const newUrls = attachments . map ( ( a ) => a . url ) ;
105+
106+ const attachmentsToDelete = existingAttachments . filter (
107+ ( existing ) => ! newUrls . includes ( existing . url )
108+ ) ;
105109
106- if ( attachments . length > 0 ) {
107- for ( const attachment of attachments ) {
110+ if ( attachmentsToDelete . length > 0 ) {
111+ for ( const attachment of attachmentsToDelete ) {
108112 try {
109113 await deleteFromS3 ( { url : attachment . url } ) ;
114+ await tryber . tables . WpAppqCampaignOtherCostsAttachment . do ( )
115+ . where ( "id" , attachment . id )
116+ . delete ( ) ;
110117 } catch ( e ) {
111118 console . error (
112119 `Error deleting attachment from S3: ${ attachment . url } ` ,
@@ -115,26 +122,23 @@ export default class OtherCostsPatchRoute extends CampaignRoute<{
115122 throw new Error ( "Error deleting attachment from S3" ) ;
116123 }
117124 }
118-
119- await tryber . tables . WpAppqCampaignOtherCostsAttachment . do ( )
120- . where ( { cost_id : costId } )
121- . delete ( ) ;
122125 }
123- }
124126
125- private async createAttachments (
126- costId : number ,
127- attachments : { url : string ; mime_type : string } [ ]
128- ) : Promise < void > {
129- const attachmentsData = attachments . map ( ( attachment ) => ( {
130- cost_id : costId ,
131- url : attachment . url ,
132- mime_type : attachment . mime_type ,
133- } ) ) ;
134-
135- await tryber . tables . WpAppqCampaignOtherCostsAttachment . do ( ) . insert (
136- attachmentsData
127+ const attachmentsToAdd = attachments . filter (
128+ ( newAttachment ) => ! existingUrls . includes ( newAttachment . url )
137129 ) ;
130+
131+ if ( attachmentsToAdd . length > 0 ) {
132+ const attachmentsData = attachmentsToAdd . map ( ( attachment ) => ( {
133+ cost_id : cost_id ,
134+ url : attachment . url ,
135+ mime_type : attachment . mime_type ,
136+ } ) ) ;
137+
138+ await tryber . tables . WpAppqCampaignOtherCostsAttachment . do ( ) . insert (
139+ attachmentsData
140+ ) ;
141+ }
138142 }
139143
140144 private async getUpdatedCost ( costId : number ) {
0 commit comments