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

check if document exists before elasticsearch get #388

Draft
wants to merge 7 commits into
base: dev
Choose a base branch
from
Draft
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
55 changes: 42 additions & 13 deletions services/base-service/models/authorship/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,33 +175,48 @@ class AuthorshipModel extends BaseModel {
}
}


/**
* @method delete
* @description Delete an authorship file
* @method remove
* @description Remove Work/Authorship node from Expert based on jsonld
* @param {object} jsonld: work/authorship object
**/
async remove(jsonld) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

@UcDust this is not completed. Can you show me the jsonld passed here ?

console.log(`AuthorshipModel.remove(${jsonld['@id']})`);
console.log('jsonld',jsonld);
}

/**
* @method remove_node_from_expert
* @description Remove Work/Authorship node from Expert
* @param {String} id of work
* @param {String} expertId : Expert Id
**/
async delete(id, expertId) {
logger.info(`Deleting ${id}`);

// Delete Elasticsearch document
async remove_node_from_expert(id,expertId) {
const expertModel = await this.get_model('expert');
let node;
let expert;
let objectId;
let resp;

try {
expert = await expertModel.client_get(expertId);
let expert = await expertModel.client_get(expertId);
node = this.get_node_by_related_id(expert, id);
objectId = node['@id'].replace("ark:/87287/d7mh2m/publication/","");
} catch(e) {
console.error(e.message);
logger.info(`relatedBy[{@id ${id} not found in expert ${expertId}`);
return 404
};

await expertModel.delete_graph_node(expertId, node);
}

/**
* @method delete
* @description Delete an authorship file
* @param {String} id of work
* @param {String} expertId : Expert Id
**/
async delete(id, expertId) {
logger.info(`Deleting ${id}`);

await this.remove_node_from_expert(id,expertId);

// Delete from FCREPO
let options = {
Expand All @@ -211,11 +226,25 @@ class AuthorshipModel extends BaseModel {

await finApi.delete(options);

const expertModel = await this.get_model('expert');
let expert;
let objectId;

try {
expert = await expertModel.client_get(expertId);
let node = this.get_node_by_related_id(expert, id);
objectId = node['@id'].replace("ark:/87287/d7mh2m/publication/","");
} catch(e) {
console.error(e.message);
logger.info(`relatedBy[{@id ${id} not found in expert ${expertId}`);
return 404
};

if (config.experts.cdl.authorship.propagate) {
let linkId=id.replace("ark:/87287/d7mh2m/relationship/","");
const cdl_user = await expertModel._impersonate_cdl_user(expert,config.experts.cdl.authorship);
logger.info({cdl_request:{linkId:id,objectId:objectId}},`CDL propagate changes ${config.experts.cdl.authorship.propagate}`);
resp = await cdl_user.reject({
let resp = await cdl_user.reject({
linkId: linkId,
categoryId: 1,
objectId: objectId
Expand Down
25 changes: 13 additions & 12 deletions services/base-service/models/base/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,31 +395,32 @@ class BaseModel extends FinEsDataModel {
* @returns {Promise} resolves to elasticsearch result
*/
async get(id, opts={}, index) {
let _source_excludes = true;
let _source_excludes = 'roles';
if( opts.admin ) _source_excludes = false;
else if( opts.compact ) _source_excludes = 'compact';

let identifier = id.replace(/^\//, '').split('/');
identifier.shift();
identifier = identifier.join('/');
//console.log(`FinEsNestedModel.get(${identifier}) on ${this.readIndexAlias}`);

let result= await this.client.get(
let result = await this.client.exists({
index: this.readIndexAlias,
id: identifier
});

if( !result ) return null;

result = await this.client.get(
Copy link
Collaborator

Choose a reason for hiding this comment

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

@UcDust this call is basically replicated in this.client_get I wonder if there is a better way for these two sperate gets to be formulated.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@qjhart I refactored that code a bit in the last commit so that it uses that client_get() function. Added a param for _source_excludes.

{
index: this.readIndexAlias,
id: identifier,
_source: true,
_source_excludes: _source_excludes
_source_excludes: _source_excludes
}
);

if( result ) {
result = result._source;
//if( opts.compact ) this.utils.compactAllTypes(result);
//if( opts.singleNode ) result['@graph'] = this.utils.singleNode(id, result['@graph']);
} else {
return null;
}
result = result._source;
//if( opts.compact ) this.utils.compactAllTypes(result);
//if( opts.singleNode ) result['@graph'] = this.utils.singleNode(id, result['@graph']);

// Add fcrepo and dbsync data if admin, for the dashboard
if( opts.admin === true ) {
Expand Down
34 changes: 26 additions & 8 deletions services/base-service/models/expert/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,16 +243,20 @@ class ExpertModel extends BaseModel {
}

/**
* @method delete
* @description Delete an expert
* @param {String} expertId : Expert Id
* @method remove
* @description Delete an expert based on existing json-ld document.
* @param {Object} jsonld : Expert Document
**/
async delete(expertId) {
logger.info(`expert.delete(${expertId})`);

// Delete Elasticsearch document
let expert;
async remove(jsonld) {
return this.remove_from_elasticsearch(jsonld['@id']);
}

/**
* @method remove_from_elasticsearch
* @description Delete an expert from elasticsearch based on expertId.
* @param {String} expertId : Expert Id
**/
async remove_from_elasticsearch(expertId) {
try {
expert = await this.client_get(expertId);
} catch(e) {
Expand All @@ -265,6 +269,20 @@ class ExpertModel extends BaseModel {
{id:expertId,
index:this.writeIndexAlias
});
}

/**
* @method delete
* @description Delete an expert
* @param {String} expertId : Expert Id
**/
async delete(expertId) {
logger.info(`expert.delete(${expertId})`);

// Delete Elasticsearch document
let expert;

await remove_from_elasticsearch(expertId) {

await finApi.delete(
{
Expand Down
2 changes: 1 addition & 1 deletion services/base-service/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
browse: require('./browse/index.js'),
search: require('./search/index.js'),
expert: require('./expert/index.js'),
work: require('./work/index.js'),
// work: require('./work/index.js'),
authorship: require('./authorship/index.js'),
grant: require('./grant/index.js'),
grant_role: require('./grant_role/index.js'),
Expand Down