Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDMEXT354/Capability to attach same document to different entities #12

Merged
merged 5 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions index.cds
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using { Attachments} from '@cap-js/attachments';
extend aspect Attachments with {
folderId : String @title: 'Folder ID';
};
96 changes: 85 additions & 11 deletions lib/handler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,33 @@ const FormData = require("form-data");

async function readAttachment(Key, token, credentials) {
try {
const document = await readDocument(Key, token, credentials.uri)
return document
const document = await readDocument(Key, token, credentials.uri);
return document;
} catch (error) {
throw new Error(error);
}
}

async function readDocument(Key, token, uri){
async function readDocument(Key, token, uri) {
const { repositoryId } = getConfigurations();
const documentReadURL = uri+ "browser/" + repositoryId + "/root?objectID=" + Key + "&cmisselector=content";
const documentReadURL =
uri +
"browser/" +
repositoryId +
"/root?objectID=" +
Key +
"&cmisselector=content";
const config = {
headers: {Authorization: `Bearer ${token}`},
responseType: 'arraybuffer'
headers: { Authorization: `Bearer ${token}` },
responseType: "arraybuffer",
};

try {
const response = await axios.get(documentReadURL, config);
const responseBuffer = Buffer.from(response.data, 'binary');
const responseBuffer = Buffer.from(response.data, "binary");
return responseBuffer;
} catch (error) {
let statusText = "An Error Occurred";
let statusText = "An Error Occurred";
if (error.response && error.response.statusText) {
statusText = error.response.statusText;
}
Expand All @@ -33,12 +39,62 @@ async function readDocument(Key, token, uri){
}
}

async function createAttachment(data, credentials, token, attachments) {
async function getFolderIdByPath(req, credentials, token, attachments) {
const up_ = attachments.keys.up_.keys[0].$generatedFieldName;
const idValue = up_.split("__")[1];
const { repositoryId } = getConfigurations();
const getFolderByPathURL =
credentials.uri +
"browser/" +
repositoryId +
"/root/" +
req.data[idValue] +
"?cmisselector=object";
const config = {
headers: { Authorization: `Bearer ${token}` },
};
try {
const response = await axios.get(getFolderByPathURL, config);
return response.data.properties["cmis:objectId"].value;
} catch (error) {
return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add console.log(error) here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
}

async function createFolder(req, credentials, token, attachments) {
const up_ = attachments.keys.up_.keys[0].$generatedFieldName;
const idValue = up_.split("__")[1];
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("propertyId[1]", "cmis:objectTypeId");
formData.append("propertyValue[1]", "cmis:folder");
formData.append("succinct", "true");

let headers = formData.getHeaders();
headers["Authorization"] = "Bearer " + token;
const config = {
headers: headers,
};
return await updateServerRequest(folderCreateURL, formData, config);
}

async function createAttachment(
data,
credentials,
token,
attachments,
parentId
) {
const { repositoryId } = getConfigurations();
const documentCreateURL =
credentials.uri + "browser/" + repositoryId + "/root";
const formData = new FormData();
formData.append("cmisaction", "createDocument");
formData.append("objectId", parentId);
formData.append("propertyId[0]", "cmis:name");
formData.append("propertyValue[0]", data.filename);
formData.append("propertyId[1]", "cmis:objectTypeId");
Expand All @@ -64,7 +120,7 @@ async function createAttachment(data, credentials, token, attachments) {
return response;
}

async function deleteAttachment(credentials, token, objectId, attachments) {
async function deleteAttachmentOrFolder(credentials, token, objectId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls rename it to deleteAttachmentsOfFolder

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

const { repositoryId } = getConfigurations();
const documentDeleteURL =
credentials.uri + "browser/" + repositoryId + "/root";
Expand All @@ -84,6 +140,21 @@ async function deleteAttachment(credentials, token, objectId, attachments) {
return response;
}

async function deleteFolderWithAttachments(credentials, token, parentId) {
const { repositoryId } = getConfigurations();
const folderDeleteURL = credentials.uri + "browser/" + repositoryId + "/root";
const formData = new FormData();
formData.append("cmisaction", "deleteTree");
formData.append("objectId", parentId);
let headers = formData.getHeaders();
headers["Authorization"] = "Bearer " + token;
const config = {
headers: headers,
};
const response = await updateServerRequest(folderDeleteURL, formData, config);
return response;
}

const updateServerRequest = async (
url,
formData,
Expand All @@ -101,7 +172,10 @@ const updateServerRequest = async (
};

module.exports = {
getFolderIdByPath,
createFolder,
createAttachment,
deleteAttachment,
deleteAttachmentOrFolder,
deleteFolderWithAttachments,
readAttachment,
};
18 changes: 15 additions & 3 deletions lib/persistence/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,21 @@ async function getDraftAttachments(attachments, req) {
.and({ HasActiveEntity: { "<>": 1 } });
}

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

async function getDuplicateAttachments(fileNames, attachments, req) {
const up_ = attachments.keys.up_.keys[0].$generatedFieldName;
const idValue = up_.split("__")[1];
return await SELECT.distinct(["filename"])
.from(attachments)
.where({ filename: { in: fileNames } });
.where({ [up_]: req.data[idValue] })
.and({ filename: { in: fileNames } });
}

async function getURLsToDeleteFromAttachments(deletedAttachments, attachments) {
Expand All @@ -29,5 +40,6 @@ module.exports = {
getDraftAttachments,
getDuplicateAttachments,
getURLsToDeleteFromAttachments,
getURLFromAttachments
getURLFromAttachments,
getFolderIdForEntity,
};
Loading
Loading