Skip to content

Commit

Permalink
Review comments & UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmeet29 committed May 22, 2024
1 parent 6c7709a commit 24ed394
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 177 deletions.
9 changes: 7 additions & 2 deletions lib/handler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ 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";
if (error.response && error.response.statusText) {
statusText = error.response.statusText;
}
console.log(statusText);
return null;
}
}
Expand Down Expand Up @@ -120,7 +125,7 @@ async function createAttachment(
return response;
}

async function deleteAttachmentOrFolder(credentials, token, objectId) {
async function deleteAttachmentsOfFolder(credentials, token, objectId) {
const { repositoryId } = getConfigurations();
const documentDeleteURL =
credentials.uri + "browser/" + repositoryId + "/root";
Expand Down Expand Up @@ -175,7 +180,7 @@ module.exports = {
getFolderIdByPath,
createFolder,
createAttachment,
deleteAttachmentOrFolder,
deleteAttachmentsOfFolder,
deleteFolderWithAttachments,
readAttachment,
};
10 changes: 0 additions & 10 deletions lib/persistence/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,13 @@ async function getFolderIdForEntity(attachments, req) {
.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({ [up_]: req.data[idValue] })
.and({ filename: { in: fileNames } });
}

async function getURLsToDeleteFromAttachments(deletedAttachments, attachments) {
return await SELECT.from(attachments)
.columns("url")
.where({ ID: { in: [...deletedAttachments] } });
}
module.exports = {
getDraftAttachments,
getDuplicateAttachments,
getURLsToDeleteFromAttachments,
getURLFromAttachments,
getFolderIdForEntity,
Expand Down
41 changes: 5 additions & 36 deletions lib/sdm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ const {
getFolderIdByPath,
createFolder,
createAttachment,
deleteAttachmentOrFolder,
deleteAttachmentsOfFolder,
deleteFolderWithAttachments,
readAttachment,
} = require("../lib/handler");
const { fetchAccessToken } = require("./util/index");
const {
getDraftAttachments,
getDuplicateAttachments,
getURLsToDeleteFromAttachments,
getURLFromAttachments,
getFolderIdForEntity,
Expand Down Expand Up @@ -55,20 +54,11 @@ module.exports = class SDMAttachmentsService extends (
if (duplicateDraftFilesErrMsg != "") {
req.reject(409, duplicateDraftFileErr(duplicateDraftFilesErrMsg));
}
//verify if duplicates exist for the drafts
const duplicateFilesErrMsg = await this.isFileNameDuplicate(
attachment_val,
attachments,
req
);
if (duplicateFilesErrMsg != "") {
req.reject(409, duplicateFileErr(duplicateFilesErrMsg));
}
const token = await fetchAccessToken(this.creds);
console.log("Token: ", token);
const folderIds = await getFolderIdForEntity(attachments, req);
let parentId = "";
if (folderIds.length == 0) {
if (folderIds?.length == 0) {
const folderId = await getFolderIdByPath(
req,
this.creds,
Expand All @@ -87,7 +77,7 @@ module.exports = class SDMAttachmentsService extends (
parentId = response.data.succinctProperties["cmis:objectId"];
}
} else {
parentId = folderIds[0].folderId;
parentId = folderIds ? folderIds[0].folderId : "";
}
const failedReq = await this.onCreate(
attachment_val,
Expand Down Expand Up @@ -121,27 +111,6 @@ module.exports = class SDMAttachmentsService extends (
return duplicates.join(", ");
}

async isFileNameDuplicate(attachment_val, attachments, req) {
let fileNames = [];
for (let index in attachment_val) {
fileNames.push(attachment_val[index].filename);
}
const are_duplicates = await getDuplicateAttachments(
fileNames,
attachments,
req
);
let duplicateFilesErrMsg = "";
are_duplicates.forEach((file) => {
duplicateFilesErrMsg += "," + file.filename;
});
// Remove leading comma if any
if (duplicateFilesErrMsg.charAt(0) === ",") {
duplicateFilesErrMsg = duplicateFilesErrMsg.slice(1);
}
return duplicateFilesErrMsg;
}

async attachDeletionData(req) {
const attachments =
cds.model.definitions[req.query.target.name + ".attachments"];
Expand All @@ -165,7 +134,7 @@ module.exports = class SDMAttachmentsService extends (
}
if (req.event == "DELETE") {
const folderIds = await getFolderIdForEntity(attachments, req);
if (folderIds.length > 0) {
if (folderIds?.length > 0) {
const parentId = folderIds[0].folderId;
req.parentId = parentId;
}
Expand All @@ -187,7 +156,7 @@ module.exports = class SDMAttachmentsService extends (
} else {
const deletePromises = req.attachmentsToDelete.map(
async (attachment) => {
const deleteAttachmentResponse = await deleteAttachmentOrFolder(
const deleteAttachmentResponse = await deleteAttachmentsOfFolder(
this.creds,
token,
attachment.url
Expand Down
2 changes: 0 additions & 2 deletions lib/util/messageConsts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module.exports.duplicateFileErr = (duplicateFiles) =>
`The files ${duplicateFiles} are already present in the repository. Please remove them from drafts, rename and try again.`;
module.exports.duplicateDraftFileErr = (duplicateDraftFiles) =>
`The files ${duplicateDraftFiles} are added multiple times. Please remove them from drafts, rename and try again.`;
module.exports.emptyFileErr = (fileName) =>
Expand Down
Loading

0 comments on commit 24ed394

Please sign in to comment.