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 all 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
27 changes: 12 additions & 15 deletions services/base-service/models/base/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ class BaseModel extends FinEsDataModel {
* @description Get document via _id.
* @param {String} id : _id of document to get
* @param {Object} options : options for get (like _source:false)
* @param {Boolean} sourceExcludes : exclude source fields
*/
async client_get(id,options={}) {
// console.log(`FinEsNestedModel.client_get(${id}) on ${this.readIndexAlias}`);
Expand All @@ -387,13 +388,14 @@ class BaseModel extends FinEsDataModel {
...{
index: this.readIndexAlias,
id: id,
_source: true
_source: true,
_source_excludes: false
},
...options
}
)
return result._source;
}
}

/**
* @method get
Expand All @@ -405,26 +407,21 @@ class BaseModel extends FinEsDataModel {
*/
async get(id, opts={}, index) {
if( id[0] === '/' ) id = id.substring(1);
let _source_excludes = true;
let _source_excludes = 'roles';
if( opts.admin ) _source_excludes = false;
else if( opts.compact ) _source_excludes = 'compact';

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

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;
}
if( !result ) return null;

result = await this.client_get(identifier, null, _source_excludes);
//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 @@ -511,16 +511,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 @@ -532,6 +536,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