Skip to content

Commit

Permalink
Fix folder deletion when entity without having any attachments is del…
Browse files Browse the repository at this point in the history
…eted
  • Loading branch information
yashmeet29 committed May 29, 2024
1 parent 0796789 commit 7352c2d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
6 changes: 3 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const config = {
verbose: true,
testTimeout: 100000,
testMatch: ["**/test/**/*.test.js"],
testMatch: ["**/test/lib/**/*.test.js"],
collectCoverageFrom: ["**/lib/**/*"],
coveragePathIgnorePatterns: ["node_modules", "<rootDir>/lib/persistence"],
coverageReporters: ["lcov", "text", "text-summary"],
};
module.exports = config;

module.exports = config;
24 changes: 16 additions & 8 deletions lib/sdm.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ module.exports = class SDMAttachmentsService extends (
req.reject(409, duplicateDraftFileErr(duplicateDraftFilesErrMsg));
}
const token = await fetchAccessToken(this.creds);
console.log("Token: ", token);
const folderIds = await getFolderIdForEntity(attachments, req);
let parentId = "";
if (folderIds?.length == 0) {
Expand Down Expand Up @@ -128,12 +127,17 @@ module.exports = class SDMAttachmentsService extends (
if (attachmentsToDelete.length > 0) {
req.attachmentsToDelete = attachmentsToDelete;
}
if (req.event == "DELETE") {
const folderIds = await getFolderIdForEntity(attachments, req);
if (folderIds?.length > 0) {
const parentId = folderIds[0].folderId;
req.parentId = parentId;
}
}
if (req.event == "DELETE") {
const token = await fetchAccessToken(this.creds);
const folderId = await getFolderIdByPath(
req,
this.creds,
token,
attachments
);
if (folderId) {
req.parentId = folderId;
}
}
}
Expand All @@ -145,8 +149,8 @@ module.exports = class SDMAttachmentsService extends (
const attachments =
cds.model.definitions[req.query.target.name + ".attachments"];

const token = await fetchAccessToken(this.creds);
if (req?.attachmentsToDelete?.length > 0) {
const token = await fetchAccessToken(this.creds);
if (req?.parentId) {
await deleteFolderWithAttachments(this.creds, token, req.parentId);
} else {
Expand Down Expand Up @@ -179,6 +183,10 @@ module.exports = class SDMAttachmentsService extends (
});
if (errorResponse != "") req.info(200, errorResponse);
}
} else {
if (req?.parentId) {
await deleteFolderWithAttachments(this.creds, token, req.parentId);
}
}
}

Expand Down
37 changes: 29 additions & 8 deletions test/lib/sdm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,10 @@ describe("SDMAttachmentsService", () => {
event: "DELETE",
};
getURLsToDeleteFromAttachments.mockResolvedValueOnce(["url"]);
getFolderIdForEntity.mockResolvedValueOnce([{ folderId: "folder" }]);
getFolderIdByPath.mockResolvedValueOnce("folder");
await service.attachDeletionData(mockReq);
expect(mockReq.parentId).toEqual("folder");
expect(getFolderIdForEntity).toHaveBeenCalledTimes(1);
expect(getFolderIdByPath).toHaveBeenCalledTimes(1);
});

it("attachDeletionData() should not set req.parentId if event is DELETE and getFolderIdForEntity() returns empty array", async () => {
Expand All @@ -358,10 +358,10 @@ describe("SDMAttachmentsService", () => {
event: "DELETE",
};
getURLsToDeleteFromAttachments.mockResolvedValueOnce(["url"]);
getFolderIdForEntity.mockResolvedValueOnce([]);
getFolderIdByPath.mockResolvedValueOnce(null);
await service.attachDeletionData(mockReq);
expect(mockReq.parentId).toBeUndefined();
expect(getFolderIdForEntity).toHaveBeenCalledTimes(1);
expect(getFolderIdByPath).toHaveBeenCalledTimes(1);
});

it("attachDeletionData() should not call getFolderIdForEntity() if event is not DELETE", async () => {
Expand Down Expand Up @@ -442,22 +442,21 @@ describe("SDMAttachmentsService", () => {
expect(req.info).toHaveBeenCalledWith(200, "\n" + expectedErrorResponse);
});

it("should not call fetchAccessToken, deleteAttachmentsOfFolder, and handleRequest methods if req.attachmentsToDelete is empty", async () => {
it("should not call deleteAttachmentsOfFolder, and handleRequest methods if req.attachmentsToDelete is empty", async () => {
const records = [];
jest.spyOn(service, "handleRequest");
const req = {
query: { target: { name: "testTarget" } },
attachmentsToDelete: [],
};
fetchAccessToken.mockResolvedValue("test_token");

await service.deleteAttachmentsWithKeys(records, req);

expect(fetchAccessToken).not.toHaveBeenCalled();
expect(deleteAttachmentsOfFolder).not.toHaveBeenCalled();
expect(service.handleRequest).not.toHaveBeenCalled();
});

test("deleteAttachmentsWithKeys() should delete entire folder when parentId is available", async () => {
it("deleteAttachmentsWithKeys() should delete entire folder when parentId is available", async () => {
const mockReq = {
query: { target: { name: "testName" } },
attachmentsToDelete: ["file1", "file2"],
Expand All @@ -477,6 +476,28 @@ describe("SDMAttachmentsService", () => {
);
expect(deleteAttachmentsOfFolder).not.toHaveBeenCalled();
});
it("should call deleteFolderWithAttachments when there is parentId and attachmentsToDelete is empty", async () => {
const service = new SDMAttachmentsService();
const records = [];
const req = {
query: { target: { name: "testTarget" } },
parentId: "1234",
attachmentsToDelete: [],
};

fetchAccessToken.mockResolvedValueOnce("GeneratedToken");
deleteFolderWithAttachments.mockResolvedValueOnce({});

await service.deleteAttachmentsWithKeys(records, req);

expect(fetchAccessToken).toHaveBeenCalledTimes(1);
expect(deleteFolderWithAttachments).toHaveBeenCalledTimes(1);
expect(deleteFolderWithAttachments).toHaveBeenCalledWith(
service.creds,
"GeneratedToken",
req.parentId
);
});
});

describe("onCreate", () => {
Expand Down

0 comments on commit 7352c2d

Please sign in to comment.