{"version":3,"file":"uploadTransactionAttachments.mjs","names":["apiMutate","ConfigService","uploadTransactionAttachments","transactionId","attachments","filesByTypeId","Map","forEach","file","attachmentType","typeId","transactionAttachmentTypeId","has","set","get","push","groups","results","Promise","allSettled","map","attachmentTypeId","files","formData","FormData","append","productV1ApiUrl","method","headers","data","uploaded","failed","error","result","index","status","reason"],"sources":["../../../../../../src/udp/pages/UdpTransactionsPage/components/transactionAttachment/uploadTransactionAttachments.js"],"sourcesContent":["import { apiMutate } from '../../../../../utilities';\nimport { ConfigService } from '../../../../../configService';\n\n/**\n * Uploads staged attachments (`{ file, attachmentType }`) for a transaction.\n * The upload endpoint takes one attachment type per request, so files are\n * grouped by type and sent as one request per group, all in parallel.\n *\n * Never rejects when a group fails. It resolves with the files that did\n * upload and the ones that did not, so a caller can drop only what landed\n * and leave the rest staged; rejecting as a whole would make a retry send an\n * already-uploaded group a second time and duplicate those attachments.\n *\n * @returns {Promise<{uploaded: File[], failed: File[], error: unknown}>}\n */\nexport const uploadTransactionAttachments = async (\n  transactionId,\n  attachments\n) => {\n  const filesByTypeId = new Map();\n  attachments.forEach(({ file, attachmentType }) => {\n    const typeId = attachmentType?.transactionAttachmentTypeId;\n    if (!filesByTypeId.has(typeId)) filesByTypeId.set(typeId, []);\n    filesByTypeId.get(typeId).push(file);\n  });\n\n  const groups = [...filesByTypeId];\n  const results = await Promise.allSettled(\n    groups.map(([attachmentTypeId, files]) => {\n      const formData = new FormData();\n      files.forEach((file) => formData.append('files', file));\n      formData.append('attachmentTypeId', attachmentTypeId);\n      return apiMutate(\n        ConfigService.productV1ApiUrl,\n        `UdpTransactionAttachment/transaction/${transactionId}/upload`,\n        {\n          method: 'post',\n          headers: { transactionId, attachmentTypeId },\n          data: formData\n        }\n      );\n    })\n  );\n\n  const uploaded = [];\n  const failed = [];\n  let error = null;\n  results.forEach((result, index) => {\n    const [, files] = groups[index];\n    if (result.status === 'fulfilled') {\n      uploaded.push(...files);\n    } else {\n      failed.push(...files);\n      error = error ?? result.reason;\n    }\n  });\n\n  return { uploaded, failed, error };\n};\n"],"mappings":";;;;;;;;;;;;;;;AAeA,MAAaE,+BAA+B,OAC1CC,eACAC,gBACG;CACH,MAAMC,gCAAgB,IAAIC,KAAK;AAC/BF,aAAYG,SAAS,EAAEC,MAAMC,qBAAqB;EAChD,MAAMC,SAASD,gBAAgBE;AAC/B,MAAI,CAACN,cAAcO,IAAIF,OAAO,CAAEL,eAAcQ,IAAIH,QAAQ,EAAE,CAAC;AAC7DL,gBAAcS,IAAIJ,OAAO,CAACK,KAAKP,KAAK;GACpC;CAEF,MAAMQ,SAAS,CAAC,GAAGX,cAAc;CACjC,MAAMY,UAAU,MAAMC,QAAQC,WAC5BH,OAAOI,KAAK,CAACC,kBAAkBC,WAAW;EACxC,MAAMC,WAAW,IAAIC,UAAU;AAC/BF,QAAMf,SAASC,SAASe,SAASE,OAAO,SAASjB,KAAK,CAAC;AACvDe,WAASE,OAAO,oBAAoBJ,iBAAiB;AACrD,SAAOrB,UACLC,cAAcyB,iBACd,wCAAwCvB,cAAa,UACrD;GACEwB,QAAQ;GACRC,SAAS;IAAEzB;IAAekB;IAAkB;GAC5CQ,MAAMN;GAEV,CAAC;GAEL,CAAC;CAED,MAAMO,WAAW,EAAE;CACnB,MAAMC,SAAS,EAAE;CACjB,IAAIC,QAAQ;AACZf,SAAQV,SAAS0B,QAAQC,UAAU;EACjC,MAAM,GAAGZ,SAASN,OAAOkB;AACzB,MAAID,OAAOE,WAAW,YACpBL,UAASf,KAAK,GAAGO,MAAM;OAClB;AACLS,UAAOhB,KAAK,GAAGO,MAAM;AACrBU,WAAQA,SAASC,OAAOG;;GAE1B;AAEF,QAAO;EAAEN;EAAUC;EAAQC;EAAO"}