Skip to content

Commit

Permalink
Merge pull request #91 from cap-js/SDMEXT-815/attachmentCreationOnUpload
Browse files Browse the repository at this point in the history
[SDMEXT-815]/attachmentCreationOnUpload
  • Loading branch information
yashmeet29 authored Feb 6, 2025
2 parents 6022048 + 0f21271 commit f5e6d24
Show file tree
Hide file tree
Showing 7 changed files with 704 additions and 520 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ Create a SAP Document Management Integration Option [Service instance and key](h
6. The `Attachments` type has generated an out-of-the-box Attachments table (see highlighted box) at the bottom of the Object page:
<img width="1300" alt="Attachments Table" style="border-radius:0.5rem;" src="etc/facet.png">

7. **Upload a file** by going into Edit mode and either using the **Upload** button on the Attachments table or by drag/drop. Then click the **Save** button to have that file stored in SAP Document Management Integration Option. We demonstrate this by uploading the PDF file from [_xmpl/db/content/Solar Panel Report.pdf_](./xmpl/db/content/Solar%20Panel%20Report.pdf):
7. **Upload a file** by going into Edit mode and either using the **Upload** button on the Attachments table or by drag/drop. The file is then stored in SAP Document Management Integration Option. We demonstrate this by uploading the PDF file from [_xmpl/db/content/Solar Panel Report.pdf_](./xmpl/db/content/Solar%20Panel%20Report.pdf):
<img width="1300" alt="Upload an attachment" style="border-radius:0.5rem;" src="etc/upload.gif">

8. **Open a file** by clicking on the attachment. We demonstrate this by opening the previously uploaded PDF file: `Solar Panel Report.pdf`
Expand Down
36 changes: 31 additions & 5 deletions lib/handler/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { getConfigurations } = require("../util");
const axios = require("axios").default;
const FormData = require("form-data");
const { errorMessage } = require("../util/messageConsts");

async function readAttachment(Key, token, credentials) {
const document = await readDocument(Key, token, credentials.uri);
Expand Down Expand Up @@ -70,7 +71,7 @@ async function getFolderIdByPath(req, credentials, token, attachments) {
const response = await axios.get(getFolderByPathURL, config);
return response.data.properties["cmis:objectId"].value;
} catch (error) {
let statusText = "An Error Occurred";
let statusText = errorMessage;
if (error.response?.statusText) {
statusText = error.response.statusText;
}
Expand All @@ -80,14 +81,13 @@ async function getFolderIdByPath(req, credentials, token, attachments) {
}

async function createFolder(req, credentials, token, attachments) {
const up_ = attachments.keys.up_.keys[0].$generatedFieldName;
const idValue = up_.split("__")[1];
const upID = attachments.keys.up_.keys[0].$generatedFieldName;
const { repositoryId } = getConfigurations();
const folderCreateURL = credentials.uri + "browser/" + repositoryId + "/root";
const formData = new FormData();
formData.append("cmisaction", "createFolder");
formData.append("propertyId[0]", "cmis:name");
formData.append("propertyValue[0]", req.data[idValue]);
formData.append("propertyValue[0]", req.data[upID]);
formData.append("propertyId[1]", "cmis:objectTypeId");
formData.append("propertyValue[1]", "cmis:folder");
formData.append("succinct", "true");
Expand All @@ -104,7 +104,6 @@ async function createAttachment(
data,
credentials,
token,
attachments,
parentId
) {
const { repositoryId } = getConfigurations();
Expand Down Expand Up @@ -171,6 +170,32 @@ async function deleteFolderWithAttachments(credentials, token, parentId) {
return response;
}

async function getAttachment(uri, token, objectId) {
const { repositoryId } = getConfigurations();
const getAttachmentURL =
uri
+ "browser/"
+ repositoryId
+ "/root?"
+ "cmisselector=object&objectId="
+ objectId
+ "&succinct=true";

const config = {
headers: { Authorization: `Bearer ${token}` },
};
try {
return await axios.get(getAttachmentURL, config);
} catch (error) {
let statusText = errorMessage;
if (error.response?.statusText) {
statusText = error.response.statusText;
}
console.log(statusText);
return null;
}
}

async function renameAttachment(
modifiedAttachment,
credentials,
Expand Down Expand Up @@ -215,6 +240,7 @@ module.exports = {
createAttachment,
deleteAttachmentsOfFolder,
deleteFolderWithAttachments,
getAttachment,
readAttachment,
renameAttachment
};
44 changes: 32 additions & 12 deletions lib/persistence/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,74 @@ async function getDraftAttachments(attachments, req, repositoryId) {
.where(conditions)
}

async function getDraftAttachmentsForUpID(attachments, req, repositoryId) {
const up_ = attachments.keys.up_.keys[0].$generatedFieldName;
const conditions = {
[up_]: req.data[up_],
repositoryId: repositoryId
};
return await SELECT("filename", "mimeType", "content", "url", "ID", "HasActiveEntity")
.from(attachments)
.where(conditions)
}

async function getFolderIdForEntity(attachments, req, repositoryId) {
const up_ = attachments.keys.up_.keys[0].$generatedFieldName;
const idValue = up_.split("__")[1];
const conditions = {
[up_]: req.data[idValue],
[up_]: req.data[up_],
repositoryId: repositoryId
};
return await SELECT.from(attachments)
.columns("folderId")
.where(conditions);
}

async function updateAttachmentInDraft(req, data) {
const up_ = req.target.keys.up_.keys[0].$generatedFieldName;
const idValue = up_.split("__")[1];
return await UPDATE(req.target)
.set({ folderId: data.folderId, url: data.url, status: "Clean" })
.where({ [idValue]: req.data[idValue] });
}

async function getURLsToDeleteFromAttachments(deletedAttachments, attachments) {
return await SELECT.from(attachments)
.columns("url")
.where({ ID: { in: [...deletedAttachments] } });
}

async function getExistingAttachments(attachmentIDs, attachments) {
return await SELECT("filename", "url", "ID","folderId")
return await SELECT("filename", "url", "ID", "folderId")
.from(attachments)
.where({ ID: { in: [...attachmentIDs] }});
}

async function setRepositoryId(attachments, repositoryId) {
if(attachments){
if(attachments) {
let nullAttachments = await SELECT()
.from(attachments)
.where({ repositoryId: null });

if (!nullAttachments || nullAttachments.length === 0) {
return;
}
if (!nullAttachments || nullAttachments.length === 0) {
return;
}

for (let attachment of nullAttachments) {
await UPDATE(attachments)
.set({ repositoryId: repositoryId })
.where({ ID: attachment.ID });
}
for (let attachment of nullAttachments) {
await UPDATE(attachments)
.set({ repositoryId: repositoryId })
.where({ ID: attachment.ID });
}
}
}


module.exports = {
getDraftAttachments,
getDraftAttachmentsForUpID,
getURLsToDeleteFromAttachments,
getURLFromAttachments,
getFolderIdForEntity,
updateAttachmentInDraft,
getExistingAttachments,
setRepositoryId
};
Loading

0 comments on commit f5e6d24

Please sign in to comment.