Skip to content

[SDMEXT-590] Display attachments specific to repository #86

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

Merged
merged 11 commits into from
Jan 24, 2025
Merged
2 changes: 1 addition & 1 deletion index.cds
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using { Attachments} from '@cap-js/attachments';
extend aspect Attachments with {
folderId : String @title: 'Folder ID' @readonly;
repositoryId : String @title: 'Repository ID' @readonly default null;
Copy link
Contributor

Choose a reason for hiding this comment

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

make this as Hidden

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

};

annotate Attachments with @UI:{
HeaderInfo: {
$Type : 'UI.HeaderInfoType',
Expand Down
40 changes: 34 additions & 6 deletions lib/persistence/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
const cds = require("@sap/cds/lib");
const { SELECT } = cds.ql;
const { SELECT, UPDATE } = cds.ql;

async function getURLFromAttachments(keys, attachments) {
return await SELECT.from(attachments, keys).columns("url");
}

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

async function getFolderIdForEntity(attachments, req) {
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],
repositoryId: repositoryId
};
return await SELECT.from(attachments)
.columns("folderId")
.where({ [up_]: req.data[idValue] });
.where(conditions);
}

async function getURLsToDeleteFromAttachments(deletedAttachments, attachments) {
Expand All @@ -33,10 +41,30 @@ async function getExistingAttachments(attachmentIDs, attachments) {
.where({ ID: { in: [...attachmentIDs] }});
}

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

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

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


module.exports = {
getDraftAttachments,
getURLsToDeleteFromAttachments,
getURLFromAttachments,
getFolderIdForEntity,
getExistingAttachments
getExistingAttachments,
setRepositoryId
};
45 changes: 38 additions & 7 deletions lib/sdm.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {
deleteFolderWithAttachments,
readAttachment,
renameAttachment
} = require("../lib/handler");
} = require("./handler/index");
const {
fetchAccessToken,
checkAttachmentsToRename,
Expand All @@ -22,7 +22,8 @@ const {
getDraftAttachments,
getURLsToDeleteFromAttachments,
getURLFromAttachments,
getFolderIdForEntity
getFolderIdForEntity,
setRepositoryId
} = require("../lib/persistence");
const { duplicateDraftFileErr, emptyFileErr, renameFileErr, virusFileErr, duplicateFileErr, versionedRepositoryErr, otherFileErr } = require("./util/messageConsts");

Expand Down Expand Up @@ -72,9 +73,10 @@ module.exports = class SDMAttachmentsService extends (
}

async draftSaveHandler(req) {
const { repositoryId } = getConfigurations();
await this.checkRepositoryType(req);
const attachments = cds.model.definitions[req.query.target.name + ".attachments"];
const attachment_val = await getDraftAttachments(attachments, req);
const attachment_val = await getDraftAttachments(attachments, req, repositoryId);

if (attachment_val.length > 0) {
await this.isFileNameDuplicateInDrafts(attachment_val,req);
Expand Down Expand Up @@ -161,8 +163,9 @@ module.exports = class SDMAttachmentsService extends (
}


async getParentId(attachments,req,token){
const folderIds = await getFolderIdForEntity(attachments, req);
async getParentId(attachments,req,token){
const { repositoryId } = getConfigurations();
const folderIds = await getFolderIdForEntity(attachments, req, repositoryId);
let parentId = "";
if (folderIds?.length == 0) {
const folderId = await getFolderIdByPath(
Expand All @@ -188,7 +191,7 @@ module.exports = class SDMAttachmentsService extends (
return parentId;
}

async isFileNameDuplicateInDrafts(data,req) {
async isFileNameDuplicateInDrafts(data,req) {
let fileNames = [];
for (let index in data) {
fileNames.push(data[index].filename);
Expand All @@ -205,6 +208,30 @@ async isFileNameDuplicateInDrafts(data,req) {
}
}

async filterAttachments(req) {
const { repositoryId } = getConfigurations();
if (!req.query.SELECT.where) {
req.query.SELECT.where = [];
}

if (req.query.SELECT.where.length > 0) {
req.query.SELECT.where.push('and');
}

req.query.SELECT.where.push(
{ ref: ['repositoryId'] },
'=',
{ val: repositoryId }
);
}

async setRepository(req) {
const attachments =
cds.model.definitions[req.query.target.name];
const { repositoryId } = getConfigurations(); // Fetch repositoryId from configurations
await setRepositoryId(attachments, repositoryId)
}

async attachDeletionData(req) {
await this.checkRepositoryType(req);
const attachments =
Expand Down Expand Up @@ -300,6 +327,7 @@ async isFileNameDuplicateInDrafts(data,req) {
Ids = [],
success = [],
success_ids = [];
const { repositoryId } = getConfigurations();
await Promise.all(
data.map(async (d) => {
// Check if d.content is null
Expand All @@ -317,6 +345,7 @@ async isFileNameDuplicateInDrafts(data,req) {
if (response.status == 201) {
d.folderId = parentId;
d.url = response.data.succinctProperties["cmis:objectId"];
d.repositoryId = repositoryId;
d.content = null;
success_ids.push(d.ID);
success.push(d);
Expand Down Expand Up @@ -407,12 +436,14 @@ async isFileNameDuplicateInDrafts(data,req) {
return "Clean";
}

registerUpdateHandlers(srv, entity) {
registerUpdateHandlers(srv, entity, target) {
srv.before(
["DELETE", "UPDATE"],
entity,
this.attachDeletionData.bind(this)
);
srv.before("READ", [target,target.drafts], this.setRepository.bind(this))
srv.before("READ", [target,target.drafts], this.filterAttachments.bind(this))
srv.before("SAVE", entity, this.draftSaveHandler.bind(this));
srv.after(
["DELETE", "UPDATE"],
Expand Down
Loading
Loading