diff --git a/jest.config.js b/jest.config.js index 025b5ab..1614e75 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,10 +1,10 @@ const config = { verbose: true, testTimeout: 100000, - testMatch: ["**/test/**/*.test.js"], + testMatch: ["**/test/lib/**/*.test.js"], collectCoverageFrom: ["**/lib/**/*"], coveragePathIgnorePatterns: ["node_modules", "/lib/persistence"], coverageReporters: ["lcov", "text", "text-summary"], }; - -module.exports = config; \ No newline at end of file + +module.exports = config; diff --git a/lib/sdm.js b/lib/sdm.js index 7506212..1abeadf 100644 --- a/lib/sdm.js +++ b/lib/sdm.js @@ -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) { @@ -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; } } } @@ -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 { @@ -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); + } } } diff --git a/test/lib/sdm.test.js b/test/lib/sdm.test.js index 8333eac..c50f4c3 100644 --- a/test/lib/sdm.test.js +++ b/test/lib/sdm.test.js @@ -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 () => { @@ -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 () => { @@ -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"], @@ -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", () => {